1. 程式人生 > >SpringBoot | 第八章:搭建Spring Boot Admin分散式微服務監控中心

SpringBoot | 第八章:搭建Spring Boot Admin分散式微服務監控中心

(一)、什麼是Spring Boot Admin

      Spring Boot Admin 是一個針對spring-boot的actuator介面進行UI美化封裝的監控工具。他可以返回在列表中瀏覽所有被監控spring-boot專案的基本資訊比如:Spring容器管理的所有的bean、詳細的Health資訊、記憶體資訊、JVM資訊、垃圾回收資訊、各種配置資訊(比如資料來源、快取列表和命中率)等,Threads 執行緒管理,Environment 管理等。

(二)、Spring Boot Admin 是由Client端和Server端組成

  在 Spring Boot 專案中,Spring Boot Admin 作為 Server 端,其他的要被監控的應用作為 Client 端, 基於這種的配置如下步驟:

 2.1 搭建Server端

  2.1.1 新建一個Maven專案,目錄結構如下

   

 2.1.2 引入Maven依賴

    <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-server</artifactId>
			<version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-webflux</artifactId>
		</dependency>
		<!-- Spring Boot Actuator對外暴露應用的監控資訊,Jolokia提供使用HTTP介面獲取JSON格式 的資料 -->
		<dependency>
			<groupId>org.jolokia</groupId>
			<artifactId>jolokia-core</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>com.googlecode.json-simple</groupId>
			<artifactId>json-simple</artifactId>
			<version>1.1</version>
		</dependency>
	</dependencies>

2.1.3 編輯yml檔案

   application.yml

spring:
  application:
    name: spring-boot-admin-server

    
## 自定義info資訊
info:
   thinkingcao: 
     name: PHP是最好的語言

2.1.4 Server啟動類添加註解,開啟監控

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import de.codecentric.boot.admin.server.config.EnableAdminServer;

/**
 * <pre>
 * @author cao_wencao
 * @date 2018年11月22日 上午10:36:11
 * </pre>
 */
@SpringBootApplication
@EnableAdminServer
@EnableAutoConfiguration
public class AdminServerApplication {

	/**
	 * <pre>  
	 * @author cao_wencao
	 * @param args
	 * </pre>  
	 */
	public static void main(String[] args) {
		SpringApplication.run(AdminServerApplication.class, args);
	}

}

2.1.5 啟動檢視console 控制檯

 只有3個介面的監控許可權

2.1.6 訪問Server端檢視,0個例項

2.2  搭建Client端

 2.2.1 新建一個Maven專案,目錄結構如下

2.2.2 引入Maven依賴

 <parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.0.RELEASE</version>
	</parent>
	<dependencies>
		<dependency>
			<groupId>de.codecentric</groupId>
			<artifactId>spring-boot-admin-starter-client</artifactId>
			<version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jolokia</groupId>
			<artifactId>jolokia-core</artifactId>
		</dependency>
		<dependency>
			<groupId>com.googlecode.json-simple</groupId>
			<artifactId>json-simple</artifactId>
			<version>1.1</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

2.2.3 編輯yml檔案

   application.yml

## 將Client作為服務註冊到Server,通過Server來監聽專案的執行情況
spring:
  boot:
    admin:
      client:
        url: http://localhost:8080
 ## application例項名       
  application:
    name : spring-boot-admin-client
 
server:
  port: 8081

## 啟用所有端點的監控許可權
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

2.1.4 Client啟動類添加註解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * <pre>
 * @author cao_wencao
 * @date 2018年11月22日 上午10:44:36
 * </pre>
 */
@SpringBootApplication
public class AdminClientApplication {

	/**
	 * <pre>  
	 * @author cao_wencao
	 * @param args
	 * </pre>  
	 */
	public static void main(String[] args) {
		SpringApplication.run(AdminClientApplication.class, args);
	}


}

2.2.5 啟動檢視console 控制檯,擁有19個監控許可權

2.2.6 先啟動完Server端發現沒有例項註冊時,在啟動完Client後,再次訪問Server端,檢視監控介面情況

 

訪問/actuator/info介面,檢視自定義配置檔案Info資訊

 

上圖清晰的展示了Client端的服務註冊到被Server監控的監控中心去了,並且可以檢視詳細的堆疊、執行緒等資訊,類似Jconsole

專案原始碼:https://github.com/Thinkingcao/SpringBoot-AdminUI