一、什麼是Spring Boot Admin ?
Spring Boot Admin是一個開源社群專案,用於管理和監控SpringBoot應用程式。 應用程式作為Spring Boot Admin Client向為Spring Boot Admin Server註冊(通過HTTP)或使用SpringCloud註冊中心(例如Eureka,Consul)發現。 UI是的Vue.js應用程式,展示Spring Boot Admin Client的Actuator端點上的一些監控。服務端採用Spring WebFlux + Netty的方式。Spring Boot Admin為註冊的應用程式提供以下功能:
- 顯示健康狀況
- 顯示詳細資訊,例如
- JVM和記憶體指標
- micrometer.io指標
- 資料來源指標
- 快取指標
- 顯示構建資訊編號
- 關注並下載日誌檔案
- 檢視jvm system-和environment-properties
- 檢視Spring Boot配置屬性
- 支援Spring Cloud的postable / env-和/ refresh-endpoint
- 輕鬆的日誌級管理
- 與JMX-beans互動
- 檢視執行緒轉儲
- 檢視http-traces
- 檢視auditevents
- 檢視http-endpoints
- 檢視計劃任務
- 檢視和刪除活動會話(使用spring-session)
- 檢視Flyway / Liquibase資料庫遷移
- 下載heapdump
- 狀態變更通知(通過電子郵件,Slack,Hipchat,......)
- 狀態更改的事件日誌(非永續性)
二、入門
1. 建立 Spring Boot Admin Server
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製程式碼
application.yml
spring:
application:
name: admin-server
server:
port: 8769
複製程式碼
啟動類 AdminServerApplication
啟動類加上@EnableAdminServer註解,開啟AdminServer的功能:
@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run( AdminServerApplication.class, args );
}
}
複製程式碼
2. 建立 Spring Boot Admin Client
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>admin-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
</properties>
<dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製程式碼
application.yml
- spring.boot.admin.client.url:要註冊的Spring Boot Admin Server的URL。
- management.endpoints.web.exposure.include:與Spring Boot 2一樣,預設情況下,大多數actuator的埠都不會通過http公開,* 代表公開所有這些端點。對於生產環境,應該仔細選擇要公開的端點。
spring:
application:
name: admin-client
boot:
admin:
client:
url: http://localhost:8769
server:
port: 8768
management:
endpoints:
web:
exposure:
include: '*'
endpoint:
health:
show-details: ALWAYS
複製程式碼
啟動類 AdminClientApplication
@SpringBootApplication
public class AdminClientApplication {
public static void main(String[] args) {
SpringApplication.run( AdminClientApplication.class, args );
}
}
複製程式碼
啟動兩個工程,在瀏覽器上輸入localhost:8769 ,瀏覽器顯示的介面如下:
檢視wallboard:
點選wallboard,可以檢視admin-client具體的資訊,比如記憶體狀態資訊:
檢視spring bean的情況:
檢視應用程式執行狀況,資訊和詳細:
還有很多監控資訊,多點一點就知道。
三、整合 Eureka
1. 建立 sc-eureka-server
這是一個 eureka-server 註冊中心。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>sc-admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sc-admin-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製程式碼
application.yml
spring:
application:
name: sc-eureka-server
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
register-with-eureka: false
fetch-registry: false
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
複製程式碼
啟動類 ScEurekaServerApplication
@SpringBootApplication
@EnableEurekaServer
public class ScEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run( ScEurekaServerApplication.class, args );
}
}
複製程式碼
2. 建立 sc-admin-server
這是一個 Spring Boot Admin Server端。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>sc-admin-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sc-admin-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製程式碼
application.yml
spring:
application:
name: admin-server
server:
port: 8769
eureka:
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
複製程式碼
啟動類 ScAdminServerApplication
@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class ScAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run( ScAdminServerApplication.class, args );
}
}
複製程式碼
3. 建立 sc-admin-client
這是一個 Spring Boot Admin client 端。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gf</groupId>
<artifactId>sc-admin-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sc-admin-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-boot-admin.version>2.1.0</spring-boot-admin.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
複製程式碼
application.yml
spring:
application:
name: sc-admin-client
eureka:
instance:
leaseRenewalIntervalInSeconds: 10
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
service-url:
defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: ALWAYS
server:
port: 8762
複製程式碼
啟動類 ScAdminClientApplication
@SpringBootApplication
@EnableDiscoveryClient
public class ScAdminClientApplication {
public static void main(String[] args) {
SpringApplication.run( ScAdminClientApplication.class, args );
}
}
複製程式碼
啟動三個工程,訪問localhost:8769,出現如下介面:
admin 會自己拉取 Eureka 上註冊的 app 資訊,主動去註冊。這也是唯一區別之前入門中手動註冊的地方,就是 client 端不需要 admin-client 的依賴,也不需要配置 admin 地址了,一切全部由 admin-server 自己實現。這樣的設計對環境變化很友好,不用改了admin-server後去改所有app 的配置了。
四、整合 Spring Security
Web應用程式中的身份驗證和授權有多種方法,因此Spring Boot Admin不提供預設方法。預設情況下,spring-boot-admin-server-ui提供登入頁面和登出按鈕。我們結合 Spring Security 實現需要使用者名稱和密碼登入的安全認證。
sc-admin-server工程的pom檔案需要增加以下的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
複製程式碼
在 sc-admin-server工的配置檔案 application.yml 中配置 spring security 的使用者名稱和密碼,這時需要在服務註冊時帶上 metadata-map 的資訊,如下:
spring:
security:
user:
name: "admin"
password: "admin"
eureka:
instance:
metadata-map:
user.name: ${spring.security.user.name}
user.password: ${spring.security.user.password}
複製程式碼
@EnableWebSecurity註解以及WebSecurityConfigurerAdapter一起配合提供基於web的security。繼承了WebSecurityConfigurerAdapter之後,再加上幾行程式碼,我們就能實現要求使用者在進入應用的任何URL之前都進行驗證的功能,寫一個配置類SecuritySecureConfig繼承WebSecurityConfigurerAdapter,配置如下:
@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");
successHandler.setDefaultTargetUrl(adminContextPath + "/");
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()
//啟用HTTP-Basic支援。這是Spring Boot Admin Client註冊所必需的
.httpBasic().and();
// @formatter:on
}
}
複製程式碼
重新訪問 http://localhost:8769/ 會出現登入介面,密碼是 配置檔案中配置好的,賬號 admin 密碼 admin,介面如下:
五、通知
1. 郵件通知
在 Spring Boot Admin 中 當註冊的應用程式狀態更改為DOWN、UNKNOWN、OFFLINE 都可以指定觸發通知,下面講解配置郵件通知。
在sc-admin-server工程pom檔案,加上mail的依賴,如下:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
複製程式碼
在配置檔案application.yml檔案中,配置收發郵件的配置:
spring:
mail:
host: smtp.163.com
username: xxxx@163.com
password: xxxx
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
boot:
admin:
notify:
mail:
from: xxxx@163.com
to: xxxx@qq.com
複製程式碼
配置後,重啟sc-admin-server工程,之後若出現註冊的客戶端的狀態從 UP 變為 OFFLINE 或其他狀態,服務端就會自動將電子郵件傳送到上面配置的收件地址。
注意 : 配置了郵件通知後,會出現 反覆通知 service offline / up。這個問題的原因在於 查詢應用程式的狀態和資訊超時,下面給出兩種解決方案:
#方法一:增加超時時間(單位:ms)
spring.boot.admin.monitor.read-timeout=20000
#方法二:關閉閉未使用或不重要的檢查點
management.health.db.enabled=false
management.health.mail.enabled=false
management.health.redis.enabled=false
management.health.mongo.enabled=false
複製程式碼
2. 自定義通知
可以通過新增實現Notifier介面的Spring Beans來新增您自己的通知程式,最好通過擴充套件 AbstractEventNotifier或AbstractStatusChangeNotifier。在sc-admin-server工程中編寫一個自定義的通知器:
@Component
public class CustomNotifier extends AbstractStatusChangeNotifier {
private static final Logger LOGGER = LoggerFactory.getLogger( LoggingNotifier.class);
public CustomNotifier(InstanceRepository repository) {
super(repository);
}
@Override
protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
return Mono.fromRunnable(() -> {
if (event instanceof InstanceStatusChangedEvent) {
LOGGER.info("Instance {} ({}) is {}", instance.getRegistration().getName(), event.getInstance(),
((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());
String status = ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus();
switch (status) {
// 健康檢查沒通過
case "DOWN":
System.out.println("傳送 健康檢查沒通過 的通知!");
break;
// 服務離線
case "OFFLINE":
System.out.println("傳送 服務離線 的通知!");
break;
//服務上線
case "UP":
System.out.println("傳送 服務上線 的通知!");
break;
// 服務未知異常
case "UNKNOWN":
System.out.println("傳送 服務未知異常 的通知!");
break;
default:
break;
}
} else {
LOGGER.info("Instance {} ({}) {}", instance.getRegistration().getName(), event.getInstance(),
event.getType());
}
});
}
}
複製程式碼
原始碼下載:github.com/gf-huanchup…