参考:
官方文档:
https://codecentric.github.io/spring-boot-admin/current/#_what_is_spring_boot_admin

配置解读

Client端配置

参数 默认值 说明
spring.boot.admin.client.enabled true 是否启用springbootAdmin客户端
spring.boot.admin.client.url 要注册的server端的url地址。如果要同时在多个server端口注册,则用逗号分隔各个server端的url地址
spring.boot.admin.client.api-path instances server端获取client信息的路径,默认情况下server通过访问/instances请求来获取到client端的信息。(client端向server端注册,注册成功后server端会给该client创建一个唯一的clientID值。当server端需要获取client的信息,比如health信息时,server端会发送http://IP:PORT/instances/clientID/actuator/health
即可,这里的http://IP:PORT
是client所在服务器的IP地址,instances就是该属性的值)
spring.boot.admin.client.username 如果server端需要进行认证时,该属性用于配置用户名
spring.boot.admin.client.password 如果server端需要进行认证时,该属性用于配置密码
spring.boot.admin.client.period 10000 注册时间间隔,单位是毫秒(client通过持续不断地向server端进行注册来保持client端与server端的连接)
spring.boot.admin.client.connect-timeout 5000 注册连接超时时间,单位是毫秒.当client向server进行注册时,如果5秒钟没有注册完成则认为本次注册失败;
spring.boot.admin.client.read-timeout 5000 注册读取超时,单位是毫秒
spring.boot.admin.client.auto-registration true 是否开启自动注册
spring.boot.admin.client.auto-deregistration null 是否开启自动注销,如果服务端运行在云平台,默认值是true
spring.boot.admin.client.register-once true 如果值为true的话,client只会在一个server端进行注册(按照spring.boot.admin.client.url中设置的server的顺序)。如果该server端宕机,会自动在下一个server端进行注册。如果该属性值为false,则会在所有的server端进行注册
spring.boot.admin.client.instance.management-url 默认该属性值与management-base-url 和 management.context-path两个属性值有关 注册的management-url,如果可用的url不同的话可以重写该值
spring.boot.admin.client.instance.management-base-url 默认该属性值与management.port, service-url 以及server.servlet-path有关 用于计算management-url 的基本URL。该路径值在运行时进行获取并赋值给 base url
spring.boot.admin.client.instance.health-url 注册的health-url地址,如果可用的url不同可以重写该值
spring.boot.admin.client.instance.service-base-url 用于计算service-url 的基本URL。该路径值在运行时进行获取并赋值给 base url。
spring.boot.admin.client.instance.service-url 注册的service-url值
spring.boot.admin.client.instance.name 默认值是配置的spring.application.name的值 客户端工程的名字
spring.boot.admin.client.instance.prefer-ip false 是否使用注册的ip地址来取代上述各个url中hostname的值

Server端配置

参数 默认值 说明
spring.boot.admin.context-path / server端的访问路径
spring.boot.admin.monitor.period 10000 更新client端状态的时间间隔,单位是毫秒
spring.boot.admin.monitor.status-lifetime 100000 client端状态的生命周期,该生命周期内不会更新client状态。单位是毫秒
spring.boot.admin.monitor.connect-timeout 2000 查询client状态信息时的连接超时时间,单位是毫秒(如果2秒内没有获取到client的状态信息,则认为连接已经断开)
spring.boot.admin.monitor.read-timeout 2000 查询client状态信息时的读取超时时间,单位是毫秒(如果2秒内没有获取到client的状态信息,则认为读取失败)
spring.boot.admin.metadata-keys-to-sanitize 默认值是”.password”, “._secret”,”.∗secret”, “._key”, “.”,”.token”, “.credentials.”, “.*vcap_services”,”.credentials.”,”.∗vcap services” 要被过滤掉的元数据(当与正则表达式相匹配时,这些数据会在输出的json数据中过滤掉)
spring.boot.admin.probed-endpoints 默认是”health”, “env”, “metrics”, “httptrace:trace”, “threaddump:dump”, “jolokia”, “info”, “logfile”, “refresh”, “flyway”, “liquibase”, “heapdump”, “loggers”, “auditevents” 要获取的client的端点信息
spring.boot.admin.instance-proxy.ignored-headers 默认值是”Cookie”, “Set-Cookie”, “Authorization” 向client发起请求时不会被转发的headers信息
spring.boot.admin.ui.brand 在导航栏中显示的brand值
spring.boot.admin.ui.title 默认是”Spring Boot Admin” 显示的页面标题

一、SprinBoot项目

Ⅰ服务端配置

1.新建项目,启动类加上@EnableAdminServer

  1. @SpringBootApplication
  2. @EnableAdminServer
  3. public class SpringbootadminApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SpringbootadminApplication.class, args);
  6. }
  7. }

2.引入依赖

  1. <!--springbootadmin-->
  2. <dependency>
  3. <groupId>de.codecentric</groupId>
  4. <artifactId>spring-boot-admin-starter-server</artifactId>
  5. <version>2.6.7</version>
  6. </dependency>
  7. <!--springbootsecurity-->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-security</artifactId>
  11. </dependency>

3.springbootsecurity登录配置类

  1. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  6. @Configuration(proxyBeanMethods = false)
  7. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  8. private final String adminContextPath;
  9. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  10. this.adminContextPath = adminServerProperties.getContextPath();
  11. }
  12. @Override
  13. protected void configure(HttpSecurity http) throws Exception {
  14. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  15. successHandler.setTargetUrlParameter( "redirectTo" );
  16. http.authorizeRequests()
  17. .antMatchers( adminContextPath + "/assets/**" ).permitAll()
  18. .antMatchers( adminContextPath + "/login" ).permitAll()
  19. .anyRequest().authenticated()
  20. .and()
  21. .formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and()
  22. .logout().logoutUrl( adminContextPath + "/logout" ).and()
  23. .httpBasic().and()
  24. .csrf().disable();
  25. }
  26. }

4.application.yml

  1. server:
  2. port: 19998
  3. spring:
  4. application:
  5. name: 服务监控中心
  6. boot:
  7. admin:
  8. ui:
  9. title: 服务监控中心
  10. brand: 服务监控中心
  11. security:
  12. user:
  13. name: 'xqny'
  14. password: 'xqny'
  15. logging:
  16. file:
  17. name: ./logs/adminsever.log

Ⅱ客户端配置

1.引入依赖

  1. <!--spring-boot-admin客户端-->
  2. <dependency>
  3. <groupId>de.codecentric</groupId>
  4. <artifactId>spring-boot-admin-starter-client</artifactId>
  5. <version>2.6.7</version>
  6. </dependency>
  7. <!--spring-boot-admin资源监控-->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-actuator</artifactId>
  11. <version>2.7.0</version>
  12. </dependency>

2.application.yml

  1. server:
  2. port: 18008
  3. spring:
  4. profiles:
  5. active: prod
  6. application:
  7. name: 数字孪生
  8. boot:
  9. admin:
  10. client:
  11. url: http://192.168.1.8:19998
  12. username: 'xqny'
  13. password: 'xqny'
  14. instance:
  15. prefer-ip: true #true 注册时 admin 中显示IP地址不显示主机名
  16. management:
  17. endpoints:
  18. web:
  19. exposure:
  20. include: '*'
  21. enabled-by-default: true
  22. endpoint:
  23. health:
  24. show-details: ALWAYS
  25. health:
  26. elasticsearch:
  27. enabled: false
  28. logfile:
  29. external-file: './logs/szls.log'
  30. logging:
  31. file:
  32. name: ./logs/szls.log

3.logback-spring.xml
logback-spring.xml
(trace等级可以打印actuator的日志)

  1. <logger name="org.springframework.boot.actuate.endpoint.web.servlet"/>

二、SpringCloud项目

原理:将springbootadmin-sever注册到nacos

Ⅰ服务端配置

1.新建项目,启动类加上@EnableAdminServer

  1. @SpringBootApplication
  2. @EnableAdminServer
  3. public class SpringbootadminApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SpringbootadminApplication.class, args);
  6. }
  7. }

2.引入依赖

  1. <!--springbootadmin-->
  2. <dependency>
  3. <groupId>de.codecentric</groupId>
  4. <artifactId>spring-boot-admin-starter-server</artifactId>
  5. <version>2.5.5</version>
  6. </dependency>
  7. <!--springbootsecurity-->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-security</artifactId>
  11. </dependency>
  12. <!--注册中心客户端-->
  13. <dependency>
  14. <groupId>com.alibaba.cloud</groupId>
  15. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  16. </dependency>
  17. <!--配置中心客户端-->
  18. <dependency>
  19. <groupId>com.alibaba.cloud</groupId>
  20. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  21. </dependency>

3.springbootsecurity登录配置类

  1. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  6. @Configuration(proxyBeanMethods = false)
  7. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  8. private final String adminContextPath;
  9. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  10. this.adminContextPath = adminServerProperties.getContextPath();
  11. }
  12. @Override
  13. protected void configure(HttpSecurity http) throws Exception {
  14. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  15. successHandler.setTargetUrlParameter( "redirectTo" );
  16. http.authorizeRequests()
  17. .antMatchers( adminContextPath + "/assets/**" ).permitAll()
  18. .antMatchers( adminContextPath + "/login" ).permitAll()
  19. .anyRequest().authenticated()
  20. .and()
  21. .formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and()
  22. .logout().logoutUrl( adminContextPath + "/logout" ).and()
  23. .httpBasic().and()
  24. .csrf().disable();
  25. }
  26. }

4.application.yml

  1. server:
  2. port: 8090
  3. spring:
  4. profiles:
  5. active: prod
  6. application:
  7. name: admin-server
  8. cloud:
  9. nacos:
  10. discovery:
  11. server-addr: 127.0.0.1:8848
  12. config:
  13. server-addr: 127.0.0.1:8848
  14. security:
  15. user:
  16. name: 'admin'
  17. password: 'admin'

Ⅱ客户端配置

1.引入依赖

  1. <!--spring-boot-admin客户端-->
  2. <dependency>
  3. <groupId>de.codecentric</groupId>
  4. <artifactId>spring-boot-admin-starter-client</artifactId>
  5. <version>2.5.5</version>
  6. </dependency>
  7. <!--spring-boot-admin资源监控-->
  8. <dependency>
  9. <groupId>org.springframework.boot</groupId>
  10. <artifactId>spring-boot-starter-actuator</artifactId>
  11. <version>2.5.5</version>
  12. </dependency>
  13. <!--注册中心客户端-->
  14. <dependency>
  15. <groupId>com.alibaba.cloud</groupId>
  16. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
  17. </dependency>
  18. <!--配置中心客户端-->
  19. <dependency>
  20. <groupId>com.alibaba.cloud</groupId>
  21. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
  22. </dependency>

2.application.yml

  1. server:
  2. port: 18008
  3. logging:
  4. file:
  5. name: ./logs/admin-client-logtest.log
  6. pattern:
  7. file: '%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n'
  8. level:
  9. org:
  10. springframework:
  11. web: info
  12. spring:
  13. profiles:
  14. active: prod
  15. application:
  16. name: admin-client-logtest
  17. cloud:
  18. nacos:
  19. discovery:
  20. server-addr: 127.0.0.1:8848
  21. config:
  22. server-addr: 127.0.0.1:8848
  23. boot:
  24. admin:
  25. client:
  26. url: http://127.0.0.1:8090
  27. username: 'admin'
  28. password: 'admin'
  29. management:
  30. endpoints:
  31. web:
  32. exposure:
  33. include: '*'
  34. enabled-by-default: true
  35. endpoint:
  36. health:
  37. show-details: ALWAYS

三、自定义健康检查

https://blog.csdn.net/youanyyou/article/details/120375840

  1. @Component
  2. public class CustomHealthIndicator extends AbstractHealthIndicator {
  3. @Override
  4. protected void doHealthCheck(Health.Builder builder) throws Exception {
  5. // 使用 builder 来创建健康状态信息
  6. // 如果你throw 了一个 exception,那么status 就会被置为DOWN,异常信息会被记录下来
  7. builder.up().withDetail("app", "这个项目很健康").withDetail("error", "Nothing, I'm very good");
  8. }
  9. }