SpringBootAdmin簡介
官方文檔:What is 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 a Vue.js application on top of the Spring Boot Actuator endpoints.
谷歌翻譯如下:
codecentric的Spring Boot Admin是一個社區(qū)項目尊浓,用于管理和監(jiān)控SpringBoot?應(yīng)用程序。
應(yīng)用程序向我們的Spring Boot Admin Client注冊(通過HTTP)或使用SpringCloud?(例如Eureka,Consul)發(fā)現(xiàn)梗掰。
UI只是Spring Boot Actuator端點上的Vue.js應(yīng)用程序。
普通話版
SpringBootAdmin是一個用于管理和監(jiān)控SpringBoot微服務(wù)的社區(qū)項目啡莉,可以使用客戶端注冊或者Eureka服務(wù)發(fā)現(xiàn)向服務(wù)端提供監(jiān)控信息局骤。
注意,服務(wù)端相當(dāng)于提供UI界面搓彻,實際的監(jiān)控信息由客戶端Actuator提供
通過SpringBootAdmin,你可以通過華麗大氣的界面訪問到整個微服務(wù)需要的監(jiān)控信息嘱朽,例如服務(wù)健康檢查信息旭贬、CPU、內(nèi)存搪泳、操作系統(tǒng)信息等等
本文檔通過Eureka服務(wù)發(fā)現(xiàn)的配置方式稀轨,向大家展示SpringBootAdmin的簡單使用
Talk is cheap, show me the code!
1. 父工程搭建
本示例工程父工程下共四個模塊,分別為
- eureka-server 服務(wù)注冊中心
- admin-server SpringBootAdmin服務(wù)端
- cloud-service1 微服務(wù)客戶端1
- cloud-service2 微服務(wù)客戶端2(實際為cloud-service1 的copy岸军,測試一個服務(wù)多個實例)
使用了目前最新的版本奋刽,父pom文件主要做了版本控制瓦侮,內(nèi)容如下:
<?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.kichun</groupId>
<artifactId>cloud-demo</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>eureka-server</module>
<module>cloud-service1</module>
<module>cloud-service2</module>
<module>admin-server</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<!-- Spring Cloud 依賴管理 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
<!-- SpringBootAdmin 依賴管理 -->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-dependencies</artifactId>
<version>2.1.3</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>
2. Eureka注冊中心工程搭建
這個不細(xì)說,不懂出門右轉(zhuǎn)佣谐,百度大把配置
模塊pom文件, 注意最新版本依賴了hystrix肚吏,一定要加,否則跑不起來
<?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">
<parent>
<artifactId>cloud-demo</artifactId>
<groupId>com.kichun</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>eureka-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
程序入口
@Slf4j
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
YML配置文件
spring:
application:
name: eureka-server
main:
allow-bean-definition-overriding: true
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3. admin-server 服務(wù)端搭建
模塊pom文件
<?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">
<parent>
<artifactId>cloud-demo</artifactId>
<groupId>com.kichun</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>admin-server</artifactId>
<dependencies>
<!-- eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- web服務(wù) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 服務(wù)端作為一個客戶端,也需要加actuator依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- springbootadmin 啟動器 -->
<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>
<!--發(fā)郵件用的-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
</project>
應(yīng)用入口, 關(guān)鍵注解@EnableAdminServer
@Slf4j
@SpringBootApplication
@EnableAdminServer
@EnableEurekaClient
public class AdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(AdminServerApplication.class, args);
}
}
YML配置文件狭魂,均有做說明
spring:
application:
name: admin-server
boot:
# 此處為SpringBootAdmin配置
admin:
ui:
# web界面的title
title: 服務(wù)健康檢查
# 郵件提醒
notify:
mail:
# 發(fā)給誰
to: kichunwang@foxmail.com
# 誰發(fā)的
from: 184375760@qq.com
# spring mail郵件配置
mail:
# smtp主機
host: smtp.qq.com
# 發(fā)件人賬號
username: 184375760@qq.com
# 發(fā)件人密碼罚攀,如qq郵箱等使用的是授權(quán)碼
password: bogffhsssi這個請大佬改成自己密碼ihb
server:
port: 8602
eureka:
instance:
hostname: localhost
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
4. 客戶端搭建
模塊pom文件
<?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">
<parent>
<artifactId>cloud-demo</artifactId>
<groupId>com.kichun</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-service1</artifactId>
<dependencies>
<!--eureka 客戶端-->
<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-web</artifactId>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
應(yīng)用入口
@Slf4j
@SpringBootApplication
@EnableEurekaClient
public class Service1Application {
public static void main(String[] args) {
SpringApplication.run(Service1Application.class, args);
}
}
關(guān)鍵為YML配置
spring:
application:
name: service1
main:
allow-bean-definition-overriding: true
server:
port: 8601
eureka:
instance:
hostname: localhost
leaseRenewalIntervalInSeconds: 10
# 健康檢查路徑
health-check-url-path: /actuator/health
client:
registryFetchIntervalSeconds: 5
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# 此處為SpringAdminClient配置,需要暴露Actuator節(jié)點
management:
# 暴露所有的endpoint 注意生產(chǎn)環(huán)境安全問題
endpoints:
web:
exposure:
include: "*"
# 展示endpoint細(xì)節(jié)信息
endpoint:
health:
show-details: ALWAYS
5 . 效果展示
按啟動Eureka雌澄,SpringBootAdmin服務(wù)端斋泄、各服務(wù)客戶端順序啟動,訪問服務(wù)端端口
如圖镐牺,共兩個應(yīng)用啟動是己,一個為SpringBootAdmin服務(wù)端本身共一個實例,一個Service1服務(wù)兩個實例
細(xì)節(jié)信息任柜,包括磁盤卒废、cpu、內(nèi)存使用情況
關(guān)閉一個服務(wù)時將會收到郵件提醒宙地,郵件提醒模板可以自定義摔认,具體請參考官方文檔
服務(wù)上線時會有更具體的信息
6. 總結(jié)
SpringBootAdmin提供一個界面優(yōu)美的監(jiān)控界面,展示微服務(wù)運行的基本信息宅粥,通過提供郵件等提醒服務(wù)可以方便提醒管理者微服務(wù)運行情況参袱,能與SpringCloud相關(guān)組件完美兼容,侵入性低秽梅,適合對監(jiān)控要求不高的應(yīng)用場景
官方文檔更齊全抹蚀,歡迎大佬評論打賞指出問題