Spring Boot Admin 用于监控基于 Spring Boot 的应用,它是在 Spring Boot Actuator 的基础上提供简洁的可视化 WEB UI。
(一)简介
Spring Boot Admin 提供了很多功能,如显示 name、id 和 version,显示在线状态,Loggers 的日志级别管理,Threads 线程管理,Environment 管理等。
(二)Spring Boot Admin 是由服务端和客户端组成
在 Spring Boot 项目中,Spring Boot Admin 作为 Server 端,其他的要被监控的应用作为 Client 端,基于这种的配置如下步骤:
2.1 Server
2.1.1 添加相关依赖
-
<dependency>
-
<groupId>de.codecentric
</groupId>
-
<artifactId>spring-boot-admin-starter-server
</artifactId>
-
<version>2.0.1-SNAPSHOT
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-web
</artifactId>
-
</dependency>
2.1.2 启动类添加注解,开启监控
-
@Configuration
-
@EnableAutoConfiguration
-
@EnableAdminServer
-
public
class SpringBootAdminApplication {
-
public static void main(String[] args) {
-
SpringApplication.run(SpringBootAdminApplication.class, args);
-
}
-
}
2.1.3 配置文件
-
server:
-
port:
8788
2.2 Client
2.2.1 添加相关依赖
-
<dependency>
-
<groupId>de.codecentric
</groupId>
-
<artifactId>spring-boot-admin-starter-client
</artifactId>
-
<version>2.0.0
</version>
-
</dependency>
2.2.2 配置文件
-
spring.boot.admin.client.url:
"http://localhost:8788"
-
management.endpoints.web.exposure.
include:
"*"
以上的配置,就可以实现 Spring Boot 项目中 Spring Boot Admin 监控其他应用了,但是这不是我们的重点,详细信息请参考官网文档:http://codecentric.github.io/spring-boot-admin/2.0.0/,我们的重点是在 Spring Cloud 中使用 Spring Boot Admin 监控 Spring Cloud 的服务,下面我们就详细的讲解在 Spring Cloud 中搭建 Spring Boot Admin
(三)在 Spring Cloud 中基于 Eureka 的 Spring Boot Admin 的搭建
3.1 启动之前的项目 eureka server,端口8761
3.2 新建 module(springboot-admin),创建步骤参考上篇
3.2.1 添加相关依赖,pom文件:
-
<dependency>
-
<groupId>de.codecentric
</groupId>
-
<artifactId>spring-boot-admin-starter-server
</artifactId>
-
<version>2.0.1-SNAPSHOT
</version>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-web
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot
</groupId>
-
<artifactId>spring-boot-starter-security
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.cloud
</groupId>
-
<artifactId>spring-cloud-starter-netflix-eureka-client
</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.jolokia
</groupId>
-
<artifactId>jolokia-core
</artifactId>
-
</dependency>
由于项目中使用的是 spring boot 2.0 版本,所以这里要使用 spring boot admin 的最新版本(还未正式发布),本人测试使用 spring boot admin 2.0.0 版本会有问题。
3.2.2 启动类添加注解
-
@Configuration
-
@EnableAutoConfiguration
-
@EnableAdminServer
-
@EnableEurekaClient
-
public
class SpringBootAdminApplication {
-
-
public static void main(String[] args) {
-
SpringApplication.run(SpringBootAdminApplication.class, args);
-
}
-
-
@Profile(
"insecure")
-
@Configuration
-
public
static
class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
-
@Override
-
protected void configure(HttpSecurity http) throws Exception {
-
http.authorizeRequests().anyRequest().permitAll()
//
-
.and().csrf().disable();
-
}
-
}
-
-
@Profile(
"secure")
-
@Configuration
-
public
static
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
-
}
-
}
-
}
SecurityPermitAllConfig和SecuritySecureConfig的配置是 Spring Boot Admin 官方给的配置,是对 url 进行安全认证等配置,照着配置即可。@EnableEurekaClient 注解是把 Spring Boot Admin 注册到 Eureka 里,这样 Spring Boot Admin 就可以发现注册到 Eureka 里的其他服务实例,@EnableAdminServer 注解是开启监控功能。
3.2.3 配置文件
官方有给出示例,主要是配置 eureka 的地址,这里要强调说明的一点,由于 Spring Boot 2.0 的 Actuator 只暴露了 /health、/info 两个端口(为了安全考虑),所以要配置 management.endpoints.web.exposure.include 的属性,下面的配置文件中暴力了一点,配置暴露了所有的端点,由于 Spring Boot Admin 有 web UI 管理界面,配置了登录的用户名密码如下,使用了 security.user 的属性,其他的详细配置,可以查看官方文档。
-
spring:
-
application:
-
name: spring-boot-admin
-
profiles:
-
active:
-
- secure
-
server:
-
port:
8788
-
-
# tag::configuration-eureka[]
-
eureka:
#<1>
-
instance:
-
leaseRenewalIntervalInSeconds:
10
-
health-check-url-path: /actuator/health
-
client:
-
registryFetchIntervalSeconds:
5
-
serviceUrl:
-
defaultZone: ${EUREKA_SERVICE_URL:http:
//localhost:8761}/eureka/
-
-
management:
-
endpoints:
-
web:
-
exposure:
-
include:
"*"
#<2>
-
endpoint:
-
health:
-
show-details: ALWAYS
-
# end::configuration-eureka[]
-
-
---
-
spring:
-
profiles: insecure
-
-
---
-
spring:
-
profiles: secure
-
security:
-
user:
-
name:
"user"
-
password:
"password"
-
eureka:
-
instance:
-
metadata-map:
-
user.name:
"user"
#These two are needed so that the server
-
user.password:
"password"
#can access the protected client endpoints
3.2.4 启动 spring boot admin 服务,界面如下:
用户名密码即上面的配置,user/password登录成功后
此时由于 Eureka 里只有 Spring Boot Admin 自身已注册,所以其监控列表里只有自己,下面我们启动其他的服务,让其注册到 Eureka 里。
3.2.5 启动 spring-demo-service 服务
(之前文章里现有的服务,可以查找此前的文章,或者查看文末的源码下载),在启动前,还有一处要配置,就是在 3.2.3 里说的,Actuator 在 spring boot 2.0 版本后,只暴露了两个端点,所以此时启动,监控不到所需的信息,下面修改配置文件如下:
-
server:
-
port: 8281
-
-
eureka:
-
client:
-
serviceUrl:
-
# 向每个注册中心注册
-
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
-
spring:
-
application:
-
name: spring-demo-service
-
-
management:
-
endpoints:
-
web:
-
exposure:
-
include: '*'
-
endpoint:
-
health:
-
show-details:
ALWAYS
此时启动 spring-demo-service,发现监控列表里 spring-demo-service 已经有了
点击 SPRING-DEMO-SERVICE 进入,页面如下,可以看到有我们之前介绍的一些功能。
3.2.6 其他的配置
如果想要显示版本信息,配置文件中加入 info.version=1.0.0 可以配置版本信息