简介
使用Actuator来监控Spring Boot应用,其提供了许多REST接口来查看应用的信息。但其返回的是大量的JSON格式数据,信息看上去不直观也不易于理解。而Spring Boot Admin(SBA)是一款基于Actuator开发的开源软件:https://github.com/codecentric/spring-boot-admin,以图形化界面的方式展示Spring Boot应用的配置信息、Beans信息、环境属性、线程信息、JVM状况等。
SBA搭建方式有两种,一种是通过Admin Client来主动完成服务的发现与注册,另一种是借助Spring Cloud 的Eureka服务治理,所有注册到拥有Admin Server服务的Eureka服务中心的服务都将会被监控。
一、 Admin Client方式
SBA 服务端
- 引入依赖
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 如果需要密码登陆,需配置security,若没有,需要在application.properties中将安全关闭-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 配置登陆信息
server:
port: 8087
spring:
application:
name: spring-boot-admin
profiles:
active:
- secure
spring:
profiles: secure
security:
user:
name: "user"
password: "password"
- 开启服务并配置security
@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class SbatestApplication {
public static void main(String[] args) {
SpringApplication.run(SbatestApplication.class, args);
}
@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
}
}
}
SBA客户端
- 引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>${spring-boot-admin.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 配置信息application.properties
server.port=8086
spring.application.name=Client
spring.boot.admin.client.url=http://localhost:8087
management.endpoints.web.exposure.include=*
spring.boot.admin.client.username=user
spring.boot.admin.client.password=password
二、借助Eureka
- 创建服务中心Eureka
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
配置注册中心application.yaml
server:
port: 8089
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
application:
name: eureka-server
@EnableEurekaServer 开启服务发现
- 将Server注册到Eureka
添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
配置信息
server:
port: 8087
spring:
application:
name: spring-boot-admin
profiles:
active:
- secure
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8089}/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
---
spring:
profiles: insecure
---
spring:
profiles: secure
security:
user:
name: "user"
password: "password"
eureka:
instance:
metadata-map:
user.name: "user"
user.password: "password"
- 将应用注册到服务中心
server:
port: 8281
eureka:
client:
serviceUrl:
# 向每个注册中心注册
defaultZone: http://localhost:8089/eureka/
spring:
application:
name: spring-boot-admin-client
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
添加邮件预警
SBA服务端也可以配置邮件预警服务,默认情况下对于被检测的应用启动或者停止的时候会触发预警。
首先添加邮件依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
然后在SBA服务端的yml中配置邮件预警:
spring:
mail:
host: smtp.163.com
username: xxx@163.com
password: xxx
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
boot:
admin:
notify:
mail:
from: xxx@163.com
to: xxx@qq.com