微服務(wù)監(jiān)控健康檢查之SpringBootAdmin的簡單應(yīng)用

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ù)兩個實例


image.png

細(xì)節(jié)信息任柜,包括磁盤卒废、cpu、內(nèi)存使用情況


image.png

關(guān)閉一個服務(wù)時將會收到郵件提醒宙地,郵件提醒模板可以自定義摔认,具體請參考官方文檔


image.png

服務(wù)上線時會有更具體的信息


image.png

6. 總結(jié)

SpringBootAdmin提供一個界面優(yōu)美的監(jiān)控界面,展示微服務(wù)運行的基本信息宅粥,通過提供郵件等提醒服務(wù)可以方便提醒管理者微服務(wù)運行情況参袱,能與SpringCloud相關(guān)組件完美兼容,侵入性低秽梅,適合對監(jiān)控要求不高的應(yīng)用場景

官方文檔更齊全抹蚀,歡迎大佬評論打賞指出問題

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市企垦,隨后出現(xiàn)的幾起案子环壤,更是在濱河造成了極大的恐慌,老刑警劉巖钞诡,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件郑现,死亡現(xiàn)場離奇詭異,居然都是意外死亡荧降,警方通過查閱死者的電腦和手機接箫,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來朵诫,“玉大人辛友,你說我怎么就攤上這事〖舴担” “怎么了废累?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵邓梅,是天一觀的道長。 經(jīng)常有香客問我九默,道長,這世上最難降的妖魔是什么宾毒? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任驼修,我火速辦了婚禮,結(jié)果婚禮上诈铛,老公的妹妹穿的比我還像新娘乙各。我一直安慰自己,他們只是感情好幢竹,可當(dāng)我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般谆焊。 火紅的嫁衣襯著肌膚如雪航揉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天邑飒,我揣著相機與錄音循签,去河邊找鬼。 笑死疙咸,一個胖子當(dāng)著我的面吹牛县匠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播撒轮,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼乞旦,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了题山?” 一聲冷哼從身側(cè)響起兰粉,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎顶瞳,沒想到半個月后亲桦,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡浊仆,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年客峭,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片抡柿。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡舔琅,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出洲劣,到底是詐尸還是另有隱情备蚓,我是刑警寧澤课蔬,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站郊尝,受9級特大地震影響二跋,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜流昏,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一扎即、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧况凉,春花似錦谚鄙、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至知市,卻和暖如春傻盟,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背嫂丙。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工莫杈, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人奢入。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓筝闹,卻偏偏與公主長得像,于是被迫代替她去往敵國和親腥光。 傳聞我的和親對象是個殘疾皇子关顷,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容