前面我們講了actuator,但是Spring Boot Actuator隻是提供了一個個的接口,需要我們自行內建到監控程式中。Admin-UI基于actuator實作,能夠傳回界面展示監控資訊
SpringBootAdmin用來管理和監控SpringBoot應用程式,它利用spring-boot-starter-actuator提供的功能,将各個微服務的狀态整合到一起,并提供良好的界面檢視支援,并且能夠動态的修改執行個體日志級别。SpringBootAdmin分為server端和client端,server端可檢視各個微服務的狀态,client端将微服務注冊到server端。github源碼位址:https://github.com/codecentric/spring-boot-admin
Admin-UI-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>admin-ui-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin-ui-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8081
spring.application.name=admin-server
#監控中心賬号密碼
spring.security.user.name=admin
spring.security.user.password=admin
配置security驗證
package com.example.adminuiserver;
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.csrf.CookieCsrfTokenRepository;
import de.codecentric.boot.admin.server.config.AdminServerProperties;
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
private final String contextPath;
public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
this.contextPath = adminServerProperties.getContextPath();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// 跨域設定,SpringBootAdmin用戶端通過instances注冊,見InstancesController
http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
.ignoringAntMatchers(contextPath + "/instances");
http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 靜态資源
http.authorizeRequests().anyRequest().authenticated(); // 所有請求必須通過認證
// 整合spring-boot-admin-server-ui
http.formLogin().loginPage("/login").permitAll();
http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");
// 啟用basic認證,SpringBootAdmin用戶端使用的是basic認證
http.httpBasic();
}
}
啟動類
package com.example.adminuiserver;
import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
@EnableAdminServer
@SpringBootApplication
public class AdminUiServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminUiServerApplication.class, args);
}
}
Admin-UI-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>admin-ui-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>admin-ui-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
<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>
<version>2.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties
server.port=8080
spring.application.name=admin-client
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
spring.boot.admin.client.url=http://127.0.0.1:8081
#spring.boot.admin.client.instance.metadata.user.name=admin
#spring.boot.admin.client.instance.metadata.user.password=admin
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
啟動類
package com.example.adminuiclient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AdminUiClientApplication {
public static void main(String[] args) {
SpringApplication.run(AdminUiClientApplication.class, args);
}
}
運作
兩個項目都進行啟動後
通路server服務 http://localhost:8081/login進行登入
上面我們配置的賬号和密碼都是 admin
登入後
展示了服務的運作資訊
點選應用牆進去後 可以看到各類運作參數資訊
通知提醒
SpringBootAdmin提供了多種通知功能,也可以自定義通知提醒。這裡我們驗證一下郵件通知功能
1)服務端poxm.xml增加配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2)增加配置
spring.mail.host=smtp.qq.com
[email protected]
spring.mail.password=xxxx
spring.boot.admin.notify.mail.from=xxx[email protected]
[email protected]
3)效果
當用戶端執行個體停掉後,我們會收到郵件提醒。