Spring Boot Admin用于管理和监控一个或多个Spring Boot服务,其分为Server端和Client端,Server端相当于一个注册中心,Client端通过Http请求向Server端进行注册,也可以结合Eureka、Nacos等注册中心实现服务注册。
1. Server端服务开发 1.1. 引入核心依赖
de.codecentric spring-boot-admin-starter-server 2.3.1 org.springframework.boot spring-boot-starter-security org.springframework.boot spring-boot-starter-web
1.2. application.yml配置文件
server: port: 8138 servlet: context-path: /adminspring: application: name: springboot-admin-server security: user: name: admin password: 123456
1.3. Security配置文件
@Configurationpublic class SecurityConfig extends WebSecurityConfigurerAdapter { private String adminSecurityContextPath; public SecurityConfig(AdminServerProperties adminServerProperties) { this.adminSecurityContextPath = adminServerProperties.getContextPath(); } @Override protected void configure(HttpSecurity http) throws Exception { SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler(); successHandler.setTargetUrlParameter("redirectTo"); http.authorizeRequests() .antMatchers(adminSecurityContextPath + "/assets/**").permitAll() .antMatchers(adminSecurityContextPath + "/login").permitAll() .anyRequest().authenticated() .and() .formLogin().loginPage(adminSecurityContextPath + "/login").successHandler(successHandler) .and() .logout().logoutUrl(adminSecurityContextPath + "/logout") .and() .httpBasic() .and() .csrf() .ignoringAntMatchers(adminSecurityContextPath + "/instances", adminSecurityContextPath + "/actuator/**") .disable(); }}
1.4. 主启动类
@EnableAdminServer@SpringBootApplicationpublic class AdminServerApplication { public static void main(String[] args) { SpringApplication.run(AdminServerApplication.class, args); }}
2. Client端服务开发 2.1. 引入核心依赖
de.codecentric spring-boot-admin-starter-client 2.3.1 org.springframework.boot spring-boot-starter-web
2.2. application.yml配置文件
server: port: 8238spring: application: name: springboot-admin-client boot: admin: client: url: http://localhost:8138/admin instance: prefer-ip: true username: admin password: 123456management: endpoints: web: exposure: include: '*' endpoint: health: show-details: always shutdown: enabled: true logfile: external-file: ./logs/log_file.loglogging: config: classpath:logback-spring.xml level: com.xlhj.boot.admin: debuginfo: author: caijliu projectName: springboot整合springboot admin version: 1.0
2.3. logback-spring.xml文件
${CONSOLE_LOG_PATTERN} UTF-8 ${log.path}/log_file.log %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n UTF-8 ${log.path}/log_file-%d{yyyy-MM-dd}-%i.log 10MB 15
2.4. 主启动类
@SpringBootApplicationpublic class AdminClientApplication { private static Logger logger = LoggerFactory.getLogger(AdminClientApplication.class); public static void main(String[] args) { SpringApplication.run(AdminClientApplication.class, args); logger.info("服务启动成功,当前时间为:{}", LocalDateTime.now()); logger.error("服务启动成功,当前时间为:{}", LocalDateTime.now()); }}
3. 验证
分别启动Server和Client服务,在浏览器输入地址:http://localhost:8138/admin
输入用户名和密码:admin/123456,登陆成功后,可以看到如下页面
点击该服务可以进入具体的监控页面,注意需要点击下图箭头所指位置
点击进入服务后,可以看到如下信息
将滚动条往下拉,可以看到内存、线程等信息
选择日志—>日志文件可以查看到服务的日志信息
点击日志配置可以动态调节每个包/类的日志级别
选择应用墙选项,可以查看到应用有哪些服务被监控
4. 配置邮件告警
Spring Boot Admin还可以对其监控的服务提供告警功能,当出现重大故障,如服务宕机时,可以及时以邮件方式通知运维人员
4.1. 引入核心依赖
在Server服务中引入邮件依赖
org.springframework.boot spring-boot-starter-mail
4.2. application.yml
在yml配置文件中加入如下配置信息
spring: mail: host: smtp.qq.com username: [email protected] password: 授权码 boot: admin: notify: mail: to: 收件人邮箱 from: 发件人邮箱
4.3. 通知配置文件
@Configurationpublic class NotificationConfig { private InstanceRepository instanceRepository; private ObjectProvider> provider; public NotificationConfig(InstanceRepository instanceRepository, ObjectProvider> provider) { this.instanceRepository = instanceRepository; this.provider = provider; } @Bean public FilteringNotifier filteringNotifier() { CompositeNotifier compositeNotifier = new CompositeNotifier(this.provider.getIfAvailable(Collections::emptyList)); return new FilteringNotifier(compositeNotifier, this.instanceRepository); } @Bean @Primary public RemindingNotifier remindingNotifier() { RemindingNotifier remindingNotifier = new RemindingNotifier(filteringNotifier(), this.instanceRepository); //配置每隔多久提示 remindingNotifier.setReminderPeriod(Duration.ofMinutes(1)); //配置每隔多久检查 remindingNotifier.setCheckReminderInverval(Duration.ofSeconds(10)); return remindingNotifier; }}
4.4. 验证