Spring Boot Admin用于管理和监控一个或多个Spring Boot服务,其分为Server端和Client端,Server端相当于一个注册中心,Client端通过Http请求向Server端进行注册,也可以结合Eureka、Nacos等注册中心实现服务注册。
<dependency>
<groupId>de.codecentricgroupId>
<artifactId>spring-boot-admin-starter-serverartifactId>
<version>2.3.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
server:
port: 8138
servlet:
context-path: /admin
spring:
application:
name: springboot-admin-server
security:
user:
name: admin
password: 123456
@Configuration
public 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();
}
}
@EnableAdminServer
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
<dependency>
<groupId>de.codecentricgroupId>
<artifactId>spring-boot-admin-starter-clientartifactId>
<version>2.3.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
server:
port: 8238
spring:
application:
name: springboot-admin-client
boot:
admin:
client:
url: http://localhost:8138/admin
instance:
prefer-ip: true
username: admin
password: 123456
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
shutdown:
enabled: true
logfile:
external-file: ./logs/log_file.log
logging:
config: classpath:logback-spring.xml
level:
com.xlhj.boot.admin: debug
info:
author: caijliu
projectName: springboot整合springboot admin
version: 1.0
<configuration>
<property name="log.path" value="./logs"/>
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<conversionRule conversionWord="wEx" converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter" />
<property name="CONSOLE_LOG_PATTERN" value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${CONSOLE_LOG_PATTERN}pattern>
<charset>UTF-8charset>
encoder>
appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.path}/log_file.logfile>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%npattern>
<charset>UTF-8charset>
encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${log.path}/log_file-%d{yyyy-MM-dd}-%i.logFileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MBmaxFileSize>
timeBasedFileNamingAndTriggeringPolicy>
<maxHistory>15maxHistory>
rollingPolicy>
appender>
<logger name="com.xlhj.boot.admin" level="info"/>
<root level="info">
<appender-ref ref="console"/>
<appender-ref ref="file"/>
root>
configuration>
@SpringBootApplication
public 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());
}
}
分别启动Server和Client服务,在浏览器输入地址:http://localhost:8138/admin
输入用户名和密码:admin/123456,登陆成功后,可以看到如下页面
点击该服务可以进入具体的监控页面,注意需要点击下图箭头所指位置
点击进入服务后,可以看到如下信息
将滚动条往下拉,可以看到内存、线程等信息
选择日志—>日志文件可以查看到服务的日志信息
点击日志配置可以动态调节每个包/类的日志级别
选择应用墙选项,可以查看到应用有哪些服务被监控
Spring Boot Admin还可以对其监控的服务提供告警功能,当出现重大故障,如服务宕机时,可以及时以邮件方式通知运维人员
在Server服务中引入邮件依赖
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
在yml配置文件中加入如下配置信息
spring:
mail:
host: smtp.qq.com
username: [email protected]
password: 授权码
boot:
admin:
notify:
mail:
to: 收件人邮箱
from: 发件人邮箱
@Configuration
public class NotificationConfig {
private InstanceRepository instanceRepository;
private ObjectProvider<List<Notifier>> provider;
public NotificationConfig(InstanceRepository instanceRepository, ObjectProvider<List<Notifier>> 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;
}
}