最近lead了一個Spring Boot的項目,偶然從CSDN推送中發現了Spring Boot Admin,很感興趣,就試用了一下。秉著“無筆記,無收獲”的原則,寫一篇入門筆記。如果能幫助到一些朋友,那就更美了。主要參考了官方文檔Spring Boot Admin Reference Guide。
1. 什么是Spring Boot Admin?
正如文檔中所說
Codecentric’s Spring Boot Admin is a community project to manage and monitor your Spring Boot ? applications. The applications register with our Spring Boot Admin Client (via HTTP) or are discovered using Spring Cloud ? (e.g. Eureka, Consul). The UI is just an AngularJs application on top of the Spring Boot Actuator endpoints.
簡而言之,Spring Boot Admin是用來管理和監視您的Spring Boot應用程序的。應用程序需要注冊為客戶端,UI由AngularJs開發。
2. 怎么使用?
需要分別配置好服務端和客戶端。
2.1 設置Server端
Server端簡單的運行為一個Servlet應用就好。
2.1.1 新建一個SpringBoot項目
使用Spring Initializr快速創建:
- Group:com.example
-
Artifact: SpringBootAdmindemo
1.JPG - 點擊Generate Project下載zip包用IDEA打開
2.1.2 添加相關依賴
在pom.xml中添加以下依賴
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-server</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.1.3 修改服務器端口
因為我本機8080端口被占用,修改application.properties中服務器端口為9998
server.port=9998
2.1.4 在啟動類上添加@EnableAdminServer注釋
@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@EnableAdminServer
public class SpringBootAdmindemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootAdmindemoApplication.class, args);
}
}
啟動應用,在瀏覽器中輸入http://localhost:9998即可訪問。當前沒有應用注冊。
2.2 設置客戶端
客戶端就是我們想要被監控的Spring Boot應用。
2.2.1 新建一個SpringBoot項目
新建一個SpringBoot項目作為客戶端程序供測試使用。
同樣使用Spring Initializr快速創建:
- Group:com.example.client
-
Artifact: SpringBootAdminClientdemo
3.JPG
2.2.2 添加依賴
在pom.xml中添加以下依賴
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>2.0.2</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-web</artifactId>
</dependency>
2.2.3 注冊應用到服務端
在application.properties中添加相關參數
- 服務端URL參數: http://localhost:9998。
- 默認情況下,大多數端點不是通過http公開的,這里我們公開所有端點。
- 同樣修改一下應用端口為9997以免沖突
spring.boot.admin.client.url=http://localhost:9998
management.endpoints.web.exposure.include=*
server.port=9997
2.2.4 添加配置類SecurityPermitAllConfig使執行器端點可訪問
package com.example.client.SpringBootAdminClientdemo.filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SecurityPermitAllConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().anyRequest().permitAll()
.and().csrf().disable();
}
}
2.2.5 展示客戶端版本信息
現在啟動應用就可以在管理頁面看到該客戶端實例信息,為了展示客戶端版本信息(其它信息請查閱Spring Boot Reference Guide),要在build的時候列出build-info,修改pom.xml*。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
用maven package生成jar包,在命令行中啟動客戶端應用
java -jar SpringBootAdminClientdemo-0.0.1-SNAPSHOT.jar
啟動成功后,會收到瀏覽器消息顯示新應用上線。
進入管理頁面,可以看到當前有一個應用。
點擊Wallboard可以看到該應用詳細信息。
3.結語
如有不清楚的地方,歡迎留言咨詢,共同探討。