一、監控管理
首先我們來了解一下監控中心是什么?
針對微服務的服務狀態包括http請求資源、服務器內存變化(堆、內存、線程、日志管理等)、檢測服務配置連接地址是否可用(模擬訪問,懶加載情況下)、統計現在有多少個bean(是spring容器中的bean)、統計Spring MVC的@ResultMapping(統計http接口)。
Spring Boot提供了兩種監控中心:
-
Actuator監控應用:沒有界面,返回json格式;
-
AdminUI:底層使用的就是Actuator監控應用,實現可視化的界面;
1、Actuator監控應用
Actuator是Spring Boot的一個附加功能,可幫助你在應用程序生產環境時監視和管理應用程序。可以使用http的各種請求來監管、審計、收集應用的運行情況,特別對於微服務管理十分有意義。
2、新建Actuator項目
新建一個項目springboot-actuator,添加依賴:
<!-- spring-boot-starter-parent 整合第三方常用框架依賴信息(各種引來信息)--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <dependencies> <!-- spring-boot-starter-web 是Spring Boot整合Spring MVC Web --> <!-- 相當於把第三方常用Maven依賴信息,在parent項目中封裝好了,使用Spring Boot提供依賴信息關聯整合的Jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- actuator監控中心 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
新建包com.goldwind.controller,在包下創建IndexController.java:
package com.goldwind.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * @Author: zy * @Description: 控制器 * @Date: 2020-2-7 */ @RestController public class IndexController { @RequestMapping("/index") public String index(){ return "index"; } }
在src/java/resources下新建application.yml文件:
#通過下面的配置啟用所有的監控端點,默認情況下,這些端點是禁用的;
management:
endpoints:
web:
exposure:
include: "*"
運行程序,訪問http://localhost:8080/actuator/mappings可以查看當前支持哪些API請求:
根據端點的作用,可以將spring-boot-starter-actuator模塊中原生端點分為三大類:
- 應用配置類:獲取應用程序中加載的應用配置、環境變量、自動化配置報告等與Spring Boot應用密切相關的配置類信息;
- 度量指標類:獲取應用程序運行過程中用於監控的度量指標,比如內存信息、線程池信息、HTTP請求統計等;
- 操作控制類:提供了對應用的關閉等操作類功能;
我們介紹幾個常見的原生端點,通過actuator/+端點名就可以獲取相應的信息:
路徑 |
作用 |
/actuator/beans |
顯示應用程序中所有Spring bean的完整列表。 |
/actuator/configprops |
顯示所有配置信息。 |
/actuator/env |
陳列所有的環境變量。 |
/actuator/mappings |
顯示所有@RequestMapping的url整理列表。 |
/actuator/health |
顯示應用程序運行狀況信息 up表示成功 down失敗 |
/actuator/info |
查看自定義應用信息 |
更多相關信息可以參考博客:SpringBoot actuator 應用監控。
3、新建admin-ui-server項目
新建一個項目springboot-admin-ui-server,添加依賴:
<?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> <groupId>com.goldwind.com</groupId> <artifactId>springboot-admin-ui-server</artifactId> <version>1.0-SNAPSHOT</version> <!-- spring-boot-starter-parent 整合第三方常用框架依賴信息(各種引來信息)--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <dependencies> <!-- spring-boot-starter-web 是Spring Boot整合Spring MVC Web --> <!-- 相當於把第三方常用Maven依賴信息,在parent項目中封裝好了,使用Spring Boot提供依賴信息關聯整合的Jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 支持admin-ui的關鍵配置 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-server</artifactId> <version>2.2.2</version> </dependency> </dependencies> </project>
新建application.yml文件,配置端口號和服務名稱:
server:
port: 8090
spring:
application:
name: admin-ui-server
新增啟動類,在包com.goldwind下新建App.java文件,必須添加@EnableAdminServer
注解:
package com.goldwind; import de.codecentric.boot.admin.server.config.EnableAdminServer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @Author: zy * @Description: 啟動類 * @Date: 2020-2-8 */ @SpringBootApplication @EnableAdminServer public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
啟動springboot-admin-ui-server,瀏覽器中打開監控中心的UI管理頁面:http://localhost:8090/applications:
4、新建admin-ui-client項目
新建一個項目springboot-admin-ui-client,添加依賴:
<?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> <groupId>com.goldwind.com</groupId> <artifactId>springboot-admin-ui-client</artifactId> <version>1.0-SNAPSHOT</version> <!-- spring-boot-starter-parent 整合第三方常用框架依賴信息(各種引來信息)--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> </parent> <dependencies> <!-- spring-boot-starter-web 是Spring Boot整合Spring MVC Web --> <!-- 相當於把第三方常用Maven依賴信息,在parent項目中封裝好了,使用Spring Boot提供依賴信息關聯整合的Jar包 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 支持admin-ui的關鍵配置 --> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>2.2.2</version> </dependency> </dependencies> </project>
新建application.yml文件,配置服務名稱,端口號默認是8080,並注冊當前服務到admin-ui-server服務中:
management:
endpoints:
web:
exposure:
include: "*"
spring:
application:
name: admin-ui-client
#注冊當前服務到admin-ui-server服務中
boot:
admin:
client:
url: http://localhost:8090
新增啟動類,在包com.goldwind下新建App.java文件:
package com.goldwind; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * @Author: zy * @Description: 啟動類 * @Date: 2020-2-8 */ @SpringBootApplication public class App { public static void main(String[] args){ SpringApplication.run(App.class,args); } }
啟動服務,再次打開UI管理頁面:
客戶端服務已經注冊到監控中心,可以點擊項目查看監控信息:
接下來就可以分析客戶端服務的運行時狀態了。
二、源碼下載
源碼地址:
https://github.com/Zhengyang550/springboot-admin-ui-client
https://github.com/Zhengyang550/springboot-admin-ui-server
參考文章:
[1] SpringBoot中的監控中心