简单来说,Spring Boot Admin是一个管理和监控Spring Boot应用程序的开源软件.每个应用都认为是一个客户端,通过HTTP或者服务注册发现Spring Cloud(Eureka、Consul等等)注册到admin server中进行展示,Spring Boot Admin UI部分使用AngularJs将数据展示在前端.
Spring Boot Admin是一个针对spring-boot的actuator接口进行UI美化封装的监控工具.它可以在列表中浏览所有被监控spring-boot项目的基本信息,详细的Health信息、内存信息、JVM信息、垃圾回收信息、各种配置信息(比如数据源、缓存列表和命中率)等,还可以直接修改logger的level.
主要提供如下功能:
- 显示 name/id 和版本号;
- 显示在线状态;
- Logging 日志级别管理;
- JMX beans 管理;
- Threads 会话和线程管理;
- Trace 应用请求跟踪;
- 应用运行参数信息,如:
- Java 系统属性
- Java 环境变量属性
- 内存信息
- Spring 环境属性
整个项目的目录结构为:
首先创建一个服务注册中心Eureka Server,使得Spring Boot Admin服务向它注册.
1.1 eureka server的pom.xml文件
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.boot
spring-boot-starter-security
org.springframework.boot
spring-boot-starter-actuator
1.2 application.yml文件
server:
port: 8761
spring:
application:
name: eurka-server
eureka:
instance:
health-check-url-path: /actuator/health
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
1.3 入口类
package com.syc.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
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.configuration.WebSecurityConfigurerAdapter;
/**
* 跟一一哥学SpringCloud微服务
*/
@EnableEurekaServer
@SpringBootApplication
public class AdminEurekaApplication {
public static void main(String[] args){
SpringApplication.run(AdminEurekaApplication.class,args);
}
//直接把安全拦截给取消掉.
@Configuration
static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
}
}
1.4 eureka server目录结构

在Maven主工程目录下新建Moeule工程admin-server,pom文件引入相关依赖.其中SBA(SpringBootAdmin)的依赖必需在dependency中指定version的版本号(包括server和client端).
2.1 创建pom.xml文件
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
de.codecentric
spring-boot-admin-starter-server
2.0.1
org.springframework.boot
spring-boot-starter-security
2.2 application.yml文件
配置文件配置端口号、程序名、服务注册中心地址.
server:
port: 8763
spring:
application:
name: admin-server
security:
user:
name: admin
password: admin
eureka:
instance:
health-check-url-path: /actuator/health
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
client:
service-url:
defaultZone: http://localhost:8761/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
2.3 安全配置类
进行安全配置,在Spring Boot Admin中添加安全登陆界面后, SBA 服务端可以访问客户端的敏感端点,官方文档建议我们应该为服务端和客户端添加一些安全配置.
package com.syc.cloud;
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
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 {
// @formatter:off
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();
// @formatter:on
}
}
2.4 入口类
启动类添加@EnableAdminServer注解.
package com.syc.cloud;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableAdminServer
@EnableEurekaClient
@SpringBootApplication
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
2.5 项目结构

在maven主工程目录下,创建一个admin client工程.
3.1 pom.xml文件
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-actuator
3.2 application.yml文件
server:
port: 8762
eureka:
instance:
leaseRenewalIntervalInSeconds: 10 #表示eureka client发送心跳给server端的频率,默认为30秒
health-check-url-path: /actuator/health #健康检查的地址(依赖spring-boot-starter-actuator)
client:
registryFetchIntervalSeconds: 10 #表示eureka client间隔多久去拉取服务注册信息,默认为30秒
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: admin-client
#actuator监控暴露全部端口,显示所有信息
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: always
3.3 入口类
package com.syc.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminClientApplication.class, args);
}
}
3.4 项目结构




Eureka 中的 metadataMap 专门用来存放一些自定义的数据,当注册中心或者其他服务需要此服务的某些配置时可以在 metadataMap 里取.实际上,每个 instance 都有各自的 metadataMap. map 中存放着需要用到的属性,例如上面配置中的 eureka.instance.metadata-map.user.name.当这个服务成功注册到 Eureka 上,Spring Boot Admin 就会取拿到这个 instance,进而拿到 metadataMap 里的属性,然后放入请求头,向此服务发送请求,访问此服务的 Actuator 开放的端点.