Spring Boot Admin 是一个开源的社区项目,用于管理和监控 Spring Boot 应用程序。应用程序可以通过 http 的方式,或 Spring Cloud 服务发现机制注册到 SBA 中,然后就可以实现对 Spring Boot 项目的可视化管理和查看了 Spring Boot Admin 可以监控 Spring Boot 单机或集群项目,它提供详细的健康 (Health)信息、内存信息、JVM 系统和环境属性、垃圾回收信息、日志设置和查看、定时任务查看、Spring Boot 缓存查看和管理等功能。接下来我们一起来搭建和使用吧搭建Admin服务[SpringBoot2.7.0]
- pom.xml 配置文件
org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-testde.codecentric spring-boot-admin-starter-server2.7.0 org.springframework.boot spring-boot-starter-actuatororg.springframework.boot spring-boot-starter-securityorg.springframework.boot spring-boot-starter-mailorg.springframework.boot spring-boot-maven-plugin2.7.0
- application.yml 配置文件
spring:
profiles:
active: dev
- application-dev.yml 配置文件
server:
address: 127.0.0.1
port: 9999
servlet:
context-path: /springBootAdmin
spring:
application:
name: springBootAdmin
security:
user:
name: admin
password: admin
mail:
protocol: smtp
host: smtp.163.com
username: lixing_xcx@163.com
password: JFURNCZSERNXYCVE
default-encoding: UTF-8
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
- SpringBoot应用启动类配置
@SpringBootApplication
@EnableAdminServer // 开启SpringBootAdmin服务
@EnableWebSecurity // 开启SpringSecurity安全认证
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- SpringBoot2.7.0+ 配置文件
package com.example.demo.config;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import javax.servlet.Filter;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class SecurityConfig {
// @Bean
// UserDetailsService userDetailsService() {
// InMemoryUserDetailsManager users = new InMemoryUserDetailsManager();
// // TODO 创建默认用户
// users.createUser(User.withUsername("admin").password("admin").roles("admin").build());
// users.createUser(User.withUsername("lixing").password("lixing").roles("admin").build());
// return users;
// }
// @Bean
// WebSecurityCustomizer webSecurityCustomizer() {
// return new WebSecurityCustomizer() {
// @Override
// public void customize(WebSecurity web) {
// // TODO 设置 /demo/noAuth 匿名登录,不经过 Spring Security 过滤器链
// web.ignoring().antMatchers("/demo/noAuth");
// }
// };
// }
// @Bean
// SecurityFilterChain securityFilterChain() {
// List filters = new ArrayList<>();
// return new DefaultSecurityFilterChain(new AntPathRequestMatcher("/**"), filters);
// }
private final String adminContextPath;
public SecurityConfig(AdminServerProperties adminServerProperties) {
this.adminContextPath = adminServerProperties.getContextPath();
}
@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
// 静态资源和登录页面可以不用认证
http.authorizeRequests().antMatchers(adminContextPath + "/assets/**").permitAll()
.antMatchers(adminContextPath + "/login").permitAll()
// 其他请求必须认证
.anyRequest().authenticated()
// 自定义登录和退出
.and().formLogin().loginPage(adminContextPath + "/login")
.and().logout().logoutUrl(adminContextPath + "/logout")
// 启用HTTP-Basic, 用于Spring Boot Admin Client注册
.and().httpBasic().and().csrf().disable();
return http.build();
}
}
- SpringBoot2.7.0- 配置文件
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
搭建Client服务[SpringBoot2.7.0]
- pom.xml 配置文件
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test de.codecentric spring-boot-admin-starter-client 2.7.0 org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-maven-plugin 2.7.0
- application.yml 配置文件
spring:
profiles:
active: dev
- application-dev.yml 配置文件
server:
address: 127.0.0.1
port: 9998
servlet:
context-path: /springBootClient
# spring Boot Admin 服务端地址
spring:
application:
name: springBootClient
boot:
admin:
client:
url: http://127.0.0.1:9999/springBootAdmin
username: admin
password: admin
instance:
service-url: http://127.0.0.1:9998/springBootClient
metadata:
user.name: admin
user.password: admin
# TODO SpringBootAdmin资源监控配置
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
logfile:
enabled: on
登录SpringBootAdmin控制台
- dev 日志配置
custom:
log:
level: debug
path: D:logs
debug: on
logging:
config: classpath:logback-spring.xml
level:
root: debug
#com.example.demo.demo1: warn
#com.example.demo.demo2: error
file:
name: D:logsinfo.log
- fat 日志配置
custom:
log:
level: info
path: D:logs
debug: off
logging:
config: classpath:logback-spring.xml
level:
root: info
#com.example.demo.demo1: warn
#com.example.demo.demo2: error
file:
name: D:logsinfo.log
- logback-spring.xml 配置
${CONTEXT_NAME} ${CONSOLE_LOG_PATTERN} UTF-8 ${logPath}/info.log ${FILE_LOG_PATTERN} UTF-8 ${logPath}/info-%d{yyyy-MM-dd}.%i.log ${MAX_FILE_SIZE} ${MAX_HISTORY} ${logPath}/warn.log WARN ACCEPT DENY ${FILE_LOG_PATTERN} UTF-8 ${logPath}/warn-%d{yyyy-MM-dd}.%i.log ${MAX_FILE_SIZE} ${MAX_HISTORY} ${logPath}/error.log ERROR ACCEPT DENY ${FILE_LOG_PATTERN} UTF-8 ${logPath}/error-%d{yyyy-MM-dd}.%i.log ${MAX_FILE_SIZE} ${MAX_HISTORY} 0 1024 0 1024 0 1024
- 控制台配置info日志
- 控制台配置warn日志
- 控制台配置error日志



