参考:
官方文档:
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
@SpringBootApplication
@EnableAdminServer
public class SpringbootadminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootadminApplication.class, args);
}
}
2.引入依赖
<!--springbootadmin-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.6.7</version>
</dependency>
<!--springbootsecurity-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
3.springbootsecurity登录配置类
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
@Configuration(proxyBeanMethods = false)
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" );
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().disable();
}
}
4.application.yml
server:
port: 19998
spring:
application:
name: 服务监控中心
boot:
admin:
ui:
title: 服务监控中心
brand: 服务监控中心
security:
user:
name: 'xqny'
password: 'xqny'
logging:
file:
name: ./logs/adminsever.log
Ⅱ客户端配置
1.引入依赖
<!--spring-boot-admin客户端-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.6.7</version>
</dependency>
<!--spring-boot-admin资源监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.7.0</version>
</dependency>
2.application.yml
server:
port: 18008
spring:
profiles:
active: prod
application:
name: 数字孪生
boot:
admin:
client:
url: http://192.168.1.8:19998
username: 'xqny'
password: 'xqny'
instance:
prefer-ip: true #true 注册时 admin 中显示IP地址不显示主机名
management:
endpoints:
web:
exposure:
include: '*'
enabled-by-default: true
endpoint:
health:
show-details: ALWAYS
health:
elasticsearch:
enabled: false
logfile:
external-file: './logs/szls.log'
logging:
file:
name: ./logs/szls.log
3.logback-spring.xml
logback-spring.xml
(trace等级可以打印actuator的日志)
<logger name="org.springframework.boot.actuate.endpoint.web.servlet"/>
二、SpringCloud项目
原理:将springbootadmin-sever注册到nacos
Ⅰ服务端配置
1.新建项目,启动类加上@EnableAdminServer
@SpringBootApplication
@EnableAdminServer
public class SpringbootadminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootadminApplication.class, args);
}
}
2.引入依赖
<!--springbootadmin-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.5.5</version>
</dependency>
<!--springbootsecurity-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--注册中心客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--配置中心客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
3.springbootsecurity登录配置类
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
@Configuration(proxyBeanMethods = false)
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" );
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().disable();
}
}
4.application.yml
server:
port: 8090
spring:
profiles:
active: prod
application:
name: admin-server
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
security:
user:
name: 'admin'
password: 'admin'
Ⅱ客户端配置
1.引入依赖
<!--spring-boot-admin客户端-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.5.5</version>
</dependency>
<!--spring-boot-admin资源监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.5.5</version>
</dependency>
<!--注册中心客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--配置中心客户端-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2.application.yml
server:
port: 18008
logging:
file:
name: ./logs/admin-client-logtest.log
pattern:
file: '%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n'
level:
org:
springframework:
web: info
spring:
profiles:
active: prod
application:
name: admin-client-logtest
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
config:
server-addr: 127.0.0.1:8848
boot:
admin:
client:
url: http://127.0.0.1:8090
username: 'admin'
password: 'admin'
management:
endpoints:
web:
exposure:
include: '*'
enabled-by-default: true
endpoint:
health:
show-details: ALWAYS
三、自定义健康检查
https://blog.csdn.net/youanyyou/article/details/120375840
@Component
public class CustomHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
// 使用 builder 来创建健康状态信息
// 如果你throw 了一个 exception,那么status 就会被置为DOWN,异常信息会被记录下来
builder.up().withDetail("app", "这个项目很健康").withDetail("error", "Nothing, I'm very good");
}
}