Spring Boot Admin 简介
Spring Boot Admin 可以对 Spring Boot 应用的各项指标进行监控,可以作为微服务架构中的监控中心来使用,本文将对其用法进行详细介绍。
SpringBoot 应用可以通过 Actuator 来暴露应用运行过程中的各项指标,Spring Boot Admin 通过这些指标来监控 SpringBoot 应用,然后通过图形化界面呈现出来。Spring Boot Admin 不仅可以监控单体应用,还可以和 Spring Cloud 的注册中心相结合来监控微服务应用。
Spring Boot Admin 可以提供应用的以下监控信息:
监控应用运行过程中的概览信息;
度量指标信息,比如 JVM、Tomcat 及进程信息;
环境变量信息,比如系统属性、系统环境变量以及应用配置信息;
查看所有创建的 Bean 信息;
查看应用中的所有配置信息;
查看应用运行日志信息;
查看 JVM 信息;
查看可以访问的 Web 端点;
查看 HTTP 跟踪信息。
创建服务端
这里我们创建一个 admin-server 模块来作为监控中心演示其功能。
引入依赖 在 pom.xml 中添加相关依赖:
1 2 3 4 5 6 7 8 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > de.codecentric</groupId > <artifactId > spring-boot-admin-starter-server</artifactId > </dependency >
相关配置 在 application.yml 中进行配置:
1 2 3 4 5 spring: application: name: admin-server server: port: 9301
在启动类上添加 @EnableAdminServer
来启用 admin-server 功能:
1 2 3 4 5 6 7 8 9 @EnableAdminServer @SpringBootApplication public class AdminServerApplication { public static void main (String[] args) { SpringApplication.run(AdminServerApplication.class , args ) ; } }
创建客户端
这里我们创建一个 admin-client 模块作为客户端注册到 admin-server。
引入依赖 在 pom.xml 中添加相关依赖:
1 2 3 4 5 6 7 8 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > de.codecentric</groupId > <artifactId > spring-boot-admin-starter-client</artifactId > </dependency >
相关配置 在 application.yml 中进行配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 spring: application: name: admin-client boot: admin: client: url: http://localhost:9301 server: port: 9305 management: endpoints: web: exposure: include: '*' endpoint: health: show-details: always logging: file: admin-client.log
启动 admin-server 和 admin-client 服务。
监控信息演示
访问如下地址打开 Spring Boot Admin 的主页:http://localhost:9301
点击 wallboard 按钮,选择 admin-client 查看监控信息;
监控信息概览:
度量指标信息,比如 JVM、Tomcat 及进程信息:
环境变量信息,比如系统属性、系统环境变量以及应用配置信息:
查看所有创建的Bean信息:
查看应用中的所有配置信息:
查看日志信息,需要添加以下配置才能开启;
1 2 logging: file: admin-client.log
查看 JVM 信息:
查看可以访问的 Web 端点:
查看 HTTP 跟踪信息:
结合注册中心使用
Spring Boot Admin 结合 Spring Cloud 注册中心使用,只需将 admin-server 和注册中心整合即可,admin-server 会自动从注册中心获取服务列表,然后挨个获取监控信息。这里以 Eureka 注册中心为例来介绍下该功能。
修改服务端 在 pom.xml 中添加相关依赖:
1 2 3 4 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-client</artifactId > </dependency >
在 application-eureka.yml 中进行配置,只需添加注册中心配置即可:
1 2 3 4 5 6 7 8 9 10 11 spring: application: name: admin-server server: port: 9301 eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8001/eureka/
在启动类上添加 @EnableDiscoveryClient 来启用服务注册功能:
1 2 3 4 5 6 7 8 9 10 @EnableDiscoveryClient @EnableAdminServer @SpringBootApplication public class AdminServerApplication { public static void main (String[] args) { SpringApplication.run(AdminServerApplication.class , args ) ; } }
修改客户端 在 pom.xml 中添加相关依赖:
1 2 3 4 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-client</artifactId > </dependency >
在 application-eureka.yml 中进行配置,删除原来的 admin-server 地址配置,添加注册中心配置即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 spring: application: name: admin-client server: port: 9305 management: endpoints: web: exposure: include: '*' endpoint: health: show-details: always logging: file: admin-client.log eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8001/eureka/
在启动类上添加 @EnableDiscoveryClient 来启用服务注册功能:
1 2 3 4 5 6 7 8 9 @EnableDiscoveryClient @SpringBootApplication public class AdminClientApplication { public static void main (String[] args) { SpringApplication.run(AdminClientApplication.class , args ) ; } }
功能演示 启动 eureka-server,使用 application-eureka.yml 配置启动 admin-server,admin-client;
查看注册中心发现服务均已注册:http://localhost:8001/
查看Spring Boot Admin 主页发现可以看到服务信息:http://localhost:9301
添加登录认证
我们可以通过给 admin-server 添加 Spring Security 支持来获得登录认证功能。
创建项目模块 创建 admin-security-server 模块,在 pom.xml 中添加相关依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 <dependency > <groupId > org.springframework.cloud</groupId > <artifactId > spring-cloud-starter-netflix-eureka-client</artifactId > </dependency > <dependency > <groupId > de.codecentric</groupId > <artifactId > spring-boot-admin-starter-server</artifactId > <version > 2.1.5</version > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-security</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency >
在 application.yml 中进行配置,配置登录用户名和密码,忽略 admin-security-server 的监控信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 spring: application: name: admin-security-server security: user: name: macro password: 123456 boot: admin: discovery: ignored-services: ${spring.application.name} server: port: 9301 eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:8001/eureka/
对 Spring Security 进行配置,以便 admin-client 可以注册:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 @Configuration public class SecuritySecureConfig extends WebSecurityConfigurerAdapter { private final String adminContextPath; public SecuritySecureConfig (AdminServerProperties adminServerProperties) { this .adminContextPath = adminServerProperties.getContextPath(); } @Override protected void configure (HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo" ); successHandler.setDefaultTargetUrl(adminContextPath + "/" ); http.authorizeRequests() .antMatchers(adminContextPath + "/assets/**" ).permitAll() .antMatchers(adminContextPath + "/login" ).permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage(adminContextPath + "/login" ).successHandler(successHandler).and() .logout().logoutUrl(adminContextPath + "/logout" ).and() .httpBasic().and() .csrf() .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()) .ignoringAntMatchers( adminContextPath + "/instances" , adminContextPath + "/actuator/**" ); } }
修改启动类,开启 AdminServer 及注册发现功能:
1 2 3 4 5 6 7 8 9 10 @EnableDiscoveryClient @EnableAdminServer @SpringBootApplication public class AdminSecurityServerApplication { public static void main (String[] args) { SpringApplication.run(AdminSecurityServerApplication.class , args ) ; } }
启动 eureka-server,admin-security-server,访问 Spring Boot Admin 主页发现需要登录才能访问:http://localhost:9301
更多干货请移步:https://antoniopeng.com
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !