Spring Boot 有一个非常好用的监控和管理的源软件,这个软件就是 Spring Boot Admin。该软件能够将 Actuator 中的信息进行界面化的展示,也可以监控所有 Spring Boot 应用的健康状况,提供实时警报功能。
主要的功能点有:
搭建服务流程说明
创建Spring Boot Admin项目
版本说明:版本建议: Spring Boot 2.x=Spring Boot Admin 2.x (比如Spring Boot 2.3.x 可以用Spring Boot Admin 2.3.x)
创建一个 Spring Boot 项目,用于展示各个服务中的监控信息,加上 Spring Boot Admin 的依赖,具体代码如下所示:
pom 依赖
org.springframework.boot
spring-boot-starter-web
de.codecentric
spring-boot-admin-starter-server
2.5.6
de.codecentric
spring-boot-admin-server-ui
2.5.6
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-test
test
添加 @EnableAdminServer
@EnableAdminServer
@SpringBootApplication
public class SpringbootAdminApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootAdminApplication.class, args);
}
}
在属性文件中增加端口配置信息:
server:
port: 8082
servlet:
context-path:
spring:
application:
name: springboot-admin-test
security:
user:
name: "admin"
password: "admin"
management:
endpoint:
health:
show-details: always
启动程序,访问 Web 地址 http://127.0.0.1:8082/ 就可以看到主页面了,这个时候是没有数据的,如图 所示
流程
创建 amdin-order 服务,将服务注册到 admin-server 中
de.codecentric
spring-boot-admin-starter-client
2.4.0
org.springframework.boot
spring-boot-starter-actuator
ch.qos.logback
logback-classic
org.apache.logging.log4j
log4j-to-slf4j
spring:
application:
## 注册服务名
name: spring-boot-admin-test
## spring boot admin
boot:
admin:
client:
api-path:
url: http://127.0.0.1:8082
instance:
prefer-ip: true # 使用ip注册进来
#endpoints config
management:
endpoint:
health:
show-details: always
endpoints:
enabled-by-default: true
web:
base-path: /actuator
exposure:
include: '*'
@SpringBootApplication
public class AdminOrderApp {
public static void main(String[] args) {
SpringApplication.run(AdminOrderApp.class,args);
}
}
重新刷新 admin 平台,admin-order 服务就可以监控
自定义的 Info 信息、健康状态、元数据,如图
Endpoint 端点接口信息
JVM 信息
客户端需要把日志同步ADMI服务中,通过JMX,客户端配置如下
${CONSOLE_LOG_PATTERN}
${log.path}/debug.log
${log.path}/%d{yyyy-MM, aux}/debug.%d{yyyy-MM-dd}.%i.log.gz
50MB
30
%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n
${log.path}/error.log
${log.path}/%d{yyyy-MM}/error.%d{yyyy-MM-dd}.%i.log.gz
50MB
30
%date [%thread] %-5level [%logger{50}] %file:%line - %msg%n
ERROR
yml 配置
management:
endpoints:
web:
exposure:
include: '*'
enabled-by-default: true
endpoint:
health:
show-details: ALWAYS
# 日志记录
logfile:
external-file: D:/project/springcould-alibaba-example/logs/admin-order/debug.log
重新打 Admin 监控平台,点击 admin-order 服务查看日志,如下
日志文件等级配置
org.springframework.boot
spring-boot-starter-security
@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();
}
}
spring:
security:
user:
name: "admin"
password: "admin"
从新启动 admin-server 服务
客户端注册 admin 服务需要配置 username 和 password
spring:
application:
## 注册服务名
name: spring-boot-admin-test
## spring boot admin
boot:
admin:
client:
api-path:
url: http://127.0.0.1:8082
instance:
prefer-ip: true # 使用ip注册进来
username: admin
password: admin