SpringBoot+actuator和admin-UI实现监控中心方式

 更新时间:2024年05月21日 10:57:39   作者:蚂蚁舞  
这篇文章主要介绍了SpringBoot+actuator和admin-UI实现监控中心方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

使用SpringBoot很久了,但是很少使用到SpringBoot的查看和监控,将来八成也不会用到,万一有机会用到呢?

所以记录一下以前学习SpringBoot+actuator和adminUI实现监控中心的方式

Springboot的版本2.0.x

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.0.5.RELEASE</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

导入对应的包

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
	<version>2.0.5.RELEASE</version>
</dependency>

<!-- security 一旦导入就会生效 -->
<!--
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>
 
<dependency>
	<groupId>org.springframework.security</groupId>
	<artifactId>spring-security-test</artifactId>
	<scope>test</scope>
</dependency>
 -->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

application.properties


server.port=7080

# 配置用户名和密码
#spring.security.user.name=admin
#spring.security.user.password=123456

# 端点信息配置
management.server.port=8081
management.server.servlet.context-path=/sys
# 默认 never always可以显示硬盘使用情况和线程情况
management.endpoint.health.show-details=always
# 端点暴露的内容,默认["health","info"],设置"*"代表暴露所有可访问的端点
management.endpoints.web.exposure.include=*

# actuator 信息
info.actuator.name=test

启动之后

myw

访问

http://localhost:8081/sys/actuator

myw

在这里使用的Actuator是spring boot的一个附加功能,可帮助你在应用程序生产环境时监视和管理应用程序。

可以使用HTTP的各种请求来监管,审计,收集应用的运行情况.特别对于微服务管理十分有意义.

缺点:没有可视化界面

使用场景,针对微服务的服务状态监控,服务器的内存变化(堆内存,线程,日志管理等),检测服务配置连接地址是否可用(模拟访问,懒加载),统计现在有多少个bean(Spring容器中的bean) 统计接口数量,

应用场景:生产环境

  • /actuator/beans 显示应用程序中所有Spring bean的完整列表
  • /actuator/configprops 显示所有配置信息
  • /actuator/env 陈列所有的环境变量
  • /actuator/mappings 显示所有@RequestMapping的url整理列表
  • /actuator/health 显示应用程序运行状况信息 up表示成功 down失败,对于懒加载没报错的可以看到控制台报错了
  • /actuator/info 查看自定义应用信息

懒加载有个缺点,例如mysql的配置,启动的时候不会发现错误,只有运行的时候才报错

当访问

http://localhost:8081/sys/actuator/health

myw

使用adminUI的方式 client客户端导包和配置

pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
	<version>2.0.5.RELEASE</version>
</dependency>
<dependency>
	<groupId>de.codecentric</groupId>
	<artifactId>spring-boot-admin-starter-client</artifactId>
	<version>2.0.5</version>
</dependency>

application.properties

server.port=8071

spring.application.name=boot-example-admin-client

spring.boot.admin.client.url=http://127.0.0.1:8050/myw-admin
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456
spring.boot.admin.client.instance.service-url=http://127.0.0.1:8071

#management.server.port=8081
#management.server.servlet.context-path=/sys
# 默认 never always可以显示硬盘使用情况和线程情况
management.endpoint.health.show-details=always
# 端点暴露的内容,默认["health","info"],设置"*"代表暴露所有可访问的端点
management.endpoints.web.exposure.include=*

使用admin-ui的方式 server服务端导包和配置

pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
	<groupId>de.codecentric</groupId>
	<artifactId>spring-boot-admin-starter-server</artifactId>
	<version>2.0.5</version>
</dependency>

application.properties


server.port=8050
server.servlet.context-path=/myw-admin
spring.application.name=boot-example-admin-server

spring.security.user.name=admin
spring.security.user.password=123456


WebSecurityConfig.java

package boot.example.admin.server;

import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

/**
 * SpringBootAdmin 登录鉴权使用
 *
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private final String contextPath;

    public WebSecurityConfig(AdminServerProperties adminServerProperties) {
        this.contextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 跨域设置,SpringBootAdmin客户端通过instances注册,见InstancesController
        http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(contextPath + "/instances");

        http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 静态资源
        http.authorizeRequests().antMatchers(contextPath + "/actuator/**").permitAll(); // 自身监控
        http.authorizeRequests().anyRequest().authenticated(); // 所有请求必须通过认证

        // 整合spring-boot-admin-server-ui
        http.formLogin().loginPage("/login").permitAll();
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");

        // 启用basic认证,SpringBootAdmin客户端使用的是basic认证
        http.httpBasic();
    }
}

启动客户端和服务端的监控中心

http://localhost:8050/myw-admin/login#/applications

myw

myw

myw

记录一下在SpringBoot2.6.6版本使用

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.6.6</version>
	<relativePath/> <!-- lookup parent from repository -->
</parent>

client的pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
	<version>2.6.6</version>
</dependency>
<dependency>
	<groupId>de.codecentric</groupId>
	<artifactId>spring-boot-admin-starter-client</artifactId>
	<version>2.5.6</version>
</dependency>

application.properties 1

server.port=8071

spring.application.name=boot-example-admin-client1

spring.boot.admin.client.url=http://localhost:8050
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456

management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*


application.properties 2


server.port=8072

spring.application.name=boot-example-admin-client2

spring.boot.admin.client.url=http://localhost:8050
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456

management.server.port=8082
management.server.base-path = /sys
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*

application.properties 3


server.port=8073

spring.application.name=boot-example-admin-client3

spring.boot.admin.client.url=http://localhost:8050
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=123456

#spring.security.user.name=admin
#spring.security.user.password=123456

management.server.port=8083
management.server.base-path = /sys
management.endpoint.health.show-details=always
management.endpoints.web.exposure.include=*

服务端的配置

pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
	<groupId>de.codecentric</groupId>
	<artifactId>spring-boot-admin-starter-server</artifactId>
	<version>2.5.6</version>
</dependency>

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

application.properties


server.port=8050

spring.application.name=boot-example-admin-server

spring.security.user.name=admin
spring.security.user.password=123456


总结

备注记录到这里 将来会不会用到再说了。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Java Web监听器如何实现定时发送邮件

    Java Web监听器如何实现定时发送邮件

    这篇文章主要介绍了Java Web监听器如何实现定时发送邮件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-12-12
  • 基于java高并发处理方案

    基于java高并发处理方案

    这篇文章主要介绍了基于java高并发处理方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08
  • Java综合整理堆排序 快速排序 归并排序

    Java综合整理堆排序 快速排序 归并排序

    堆排序是利用堆这种数据结构而设计的一种排序算法,堆排序是一种选择排序,它的最坏,最好,平均时间复杂度均为O(nlogn),它也是不稳定排序。首先简单了解下堆结构
    2022-01-01
  • Spring Boot环境属性占位符解析及类型转换详解

    Spring Boot环境属性占位符解析及类型转换详解

    这篇文章主要给大家介绍了关于Spring Boot环境属性占位符解析及类型转换的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-08-08
  • Java多线程用法的实例详解

    Java多线程用法的实例详解

    这篇文章主要介绍了Java多线程用法的实例详解的相关资料,希望通过本文大家能够理解掌握这部分内容,需要的朋友可以参考下
    2017-09-09
  • 普通对象使用spring容器中的对象的实现方法

    普通对象使用spring容器中的对象的实现方法

    这篇文章主要介绍了普通对象使用spring容器中的对象的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-06-06
  • Java中的线程安全集合CopyOnWriteArrayList解析

    Java中的线程安全集合CopyOnWriteArrayList解析

    这篇文章主要介绍了Java中的线程安全CopyOnWriteArrayList解析,CopyOnWriteArrayList是ArrayList的线程安全版本,从他的名字可以推测,CopyOnWriteArrayList是在有写操作的时候会copy一份数据,然后写完再设置成新的数据,需要的朋友可以参考下
    2023-12-12
  • Java中使用jaxp进行sax解析_动力节点Java学院整理

    Java中使用jaxp进行sax解析_动力节点Java学院整理

    使用SAX的优势在于其解析速度较快,相对于DOM而言占用内存较少。这篇文章主要介绍了Java中使用jaxp进行sax解析,需要的朋友可以参考下
    2017-08-08
  • Java 常见排序算法代码分享

    Java 常见排序算法代码分享

    这篇文章主要介绍了Java 常见排序算法代码分享,文章通过分享详细的代码总结文章内容,具有一定的参考价值,需要的小伙伴可以参考一下,希望对你有所帮助
    2022-03-03
  • Java拆装箱深度剖析

    Java拆装箱深度剖析

    这篇文章主要为大家深度剖析了Java拆箱装箱的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-12-12

最新评论