SpringBoot admin 2.0 詳解

文章首發(fā)于微信公眾號(hào)《程序員果果》
地址:https://mp.weixin.qq.com/s/JSJH32N_bCzaWjZTubUDTA
本篇源碼:https://github.com/gf-huanchupk/SpringBootLearning

一肉盹、什么是Spring Boot Admin 屈张?

Spring Boot Admin是一個(gè)開源社區(qū)項(xiàng)目,用于管理和監(jiān)控SpringBoot應(yīng)用程序历等。 應(yīng)用程序作為Spring Boot Admin Client向?yàn)镾pring Boot Admin Server注冊(cè)(通過HTTP)或使用SpringCloud注冊(cè)中心(例如Eureka,Consul)發(fā)現(xiàn)缸逃。 UI是的Vue.js應(yīng)用程序休讳,展示Spring Boot Admin Client的Actuator端點(diǎn)上的一些監(jiān)控。服務(wù)端采用Spring WebFlux + Netty的方式另锋。Spring Boot Admin為注冊(cè)的應(yīng)用程序提供以下功能:

  • 顯示健康狀況
  • 顯示詳細(xì)信息,例如
  • JVM和內(nèi)存指標(biāo)
  • micrometer.io指標(biāo)
  • 數(shù)據(jù)源指標(biāo)
  • 緩存指標(biāo)
  • 顯示構(gòu)建信息編號(hào)
  • 關(guān)注并下載日志文件
  • 查看jvm system-和environment-properties
  • 查看Spring Boot配置屬性
  • 支持Spring Cloud的postable / env-和/ refresh-endpoint
  • 輕松的日志級(jí)管理
  • 與JMX-beans交互
  • 查看線程轉(zhuǎn)儲(chǔ)
  • 查看http-traces
  • 查看auditevents
  • 查看http-endpoints
  • 查看計(jì)劃任務(wù)
  • 查看和刪除活動(dòng)會(huì)話(使用spring-session)
  • 查看Flyway / Liquibase數(shù)據(jù)庫(kù)遷移
  • 下載heapdump
  • 狀態(tài)變更通知(通過電子郵件狭归,Slack夭坪,Hipchat,......)
  • 狀態(tài)更改的事件日志(非持久性)

二唉铜、入門

1. 創(chuàng)建 Spring Boot Admin Server

pom.xml

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gf</groupId>
    <artifactId>admin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>admin-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.1.0</spring-boot-admin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <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>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</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>

application.yml

spring:
  application:
    name: admin-server
server:
  port: 8769

啟動(dòng)類 AdminServerApplication

啟動(dòng)類加上@EnableAdminServer注解台舱,開啟AdminServer的功能:

@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run( AdminServerApplication.class, args );
    }

}

2. 創(chuàng)建 Spring Boot Admin Client

pom.xml

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gf</groupId>
    <artifactId>admin-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>admin-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.1.0</spring-boot-admin.version>
    </properties>

    <dependencies>
        <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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</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>

application.yml

  • spring.boot.admin.client.url:要注冊(cè)的Spring Boot Admin Server的URL。
  • management.endpoints.web.exposure.include:與Spring Boot 2一樣潭流,默認(rèn)情況下竞惋,大多數(shù)actuator的端口都不會(huì)通過http公開,* 代表公開所有這些端點(diǎn)灰嫉。對(duì)于生產(chǎn)環(huán)境拆宛,應(yīng)該仔細(xì)選擇要公開的端點(diǎn)。
spring:
  application:
    name: admin-client
  boot:
    admin:
      client:
        url: http://localhost:8769
server:
  port: 8768

management:
  endpoints:
    web:
      exposure:
        include: '*'
  endpoint:
    health:
      show-details: ALWAYS

啟動(dòng)類 AdminClientApplication

@SpringBootApplication
public class AdminClientApplication {

    public static void main(String[] args) {
        SpringApplication.run( AdminClientApplication.class, args );
    }

}

啟動(dòng)兩個(gè)工程讼撒,在瀏覽器上輸入localhost:8769 浑厚,瀏覽器顯示的界面如下:

查看wallboard:

點(diǎn)擊wallboard股耽,可以查看admin-client具體的信息,比如內(nèi)存狀態(tài)信息:

查看spring bean的情況:

查看應(yīng)用程序運(yùn)行狀況钳幅,信息和詳細(xì):

還有很多監(jiān)控信息物蝙,多點(diǎn)一點(diǎn)就知道。

三敢艰、集成 Eureka

1. 創(chuàng)建 sc-eureka-server

這是一個(gè) eureka-server 注冊(cè)中心诬乞。

pom.xml

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gf</groupId>
    <artifactId>sc-admin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sc-admin-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.1.0</spring-boot-admin.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.1.0</version>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</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>

application.yml

spring:
  application:
    name: sc-eureka-server
server:
  port: 8761
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
    register-with-eureka: false
    fetch-registry: false
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

啟動(dòng)類 ScEurekaServerApplication

@SpringBootApplication
@EnableEurekaServer
public class ScEurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run( ScEurekaServerApplication.class, args );
    }

}

2. 創(chuàng)建 sc-admin-server

這是一個(gè) Spring Boot Admin Server端。

pom.xml

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gf</groupId>
    <artifactId>sc-admin-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sc-admin-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.1.0</spring-boot-admin.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-server</artifactId>
            <version>2.1.0</version>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jolokia</groupId>
            <artifactId>jolokia-core</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</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>

application.yml

spring:
  application:
    name: admin-server
server:
  port: 8769
eureka:
  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health

management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

啟動(dòng)類 ScAdminServerApplication

@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class ScAdminServerApplication {

    public static void main(String[] args) {
        SpringApplication.run( ScAdminServerApplication.class, args );
    }

}

3. 創(chuàng)建 sc-admin-client

這是一個(gè) Spring Boot Admin client 端钠导。

pom.xml

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.gf</groupId>
    <artifactId>sc-admin-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sc-admin-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.1.0</spring-boot-admin.version>
        <spring-cloud.version>Finchley.SR2</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>
        <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-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</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>

application.yml

spring:
  application:
    name: sc-admin-client
eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health

  client:
    registryFetchIntervalSeconds: 5
    service-url:
      defaultZone: ${EUREKA_SERVICE_URL:http://localhost:8761}/eureka/
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS
server:
  port: 8762

啟動(dòng)類 ScAdminClientApplication

@SpringBootApplication
@EnableDiscoveryClient
public class ScAdminClientApplication {

    public static void main(String[] args) {
        SpringApplication.run( ScAdminClientApplication.class, args );
    }

}

啟動(dòng)三個(gè)工程震嫉,訪問localhost:8769,出現(xiàn)如下界面:

admin 會(huì)自己拉取 Eureka 上注冊(cè)的 app 信息牡属,主動(dòng)去注冊(cè)票堵。這也是唯一區(qū)別之前入門中手動(dòng)注冊(cè)的地方,就是 client 端不需要 admin-client 的依賴逮栅,也不需要配置 admin 地址了悴势,一切全部由 admin-server 自己實(shí)現(xiàn)。這樣的設(shè)計(jì)對(duì)環(huán)境變化很友好措伐,不用改了admin-server后去改所有app 的配置了瞳浦。

四、集成 Spring Security

Web應(yīng)用程序中的身份驗(yàn)證和授權(quán)有多種方法废士,因此Spring Boot Admin不提供默認(rèn)方法。默認(rèn)情況下蝇完,spring-boot-admin-server-ui提供登錄頁(yè)面和注銷按鈕官硝。我們結(jié)合 Spring Security 實(shí)現(xiàn)需要用戶名和密碼登錄的安全認(rèn)證。

sc-admin-server工程的pom文件需要增加以下的依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

在 sc-admin-server工的配置文件 application.yml 中配置 spring security 的用戶名和密碼短蜕,這時(shí)需要在服務(wù)注冊(cè)時(shí)帶上 metadata-map 的信息氢架,如下:

spring:
  security:
    user:
      name: "admin"
      password: "admin"
      
eureka:
  instance:
    metadata-map:
      user.name: ${spring.security.user.name}
      user.password: ${spring.security.user.password}

@EnableWebSecurity注解以及WebSecurityConfigurerAdapter一起配合提供基于web的security。繼承了WebSecurityConfigurerAdapter之后朋魔,再加上幾行代碼岖研,我們就能實(shí)現(xiàn)要求用戶在進(jìn)入應(yīng)用的任何URL之前都進(jìn)行驗(yàn)證的功能,寫一個(gè)配置類SecuritySecureConfig繼承WebSecurityConfigurerAdapter警检,配置如下:

@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

    private final String adminContextPath;

    public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
        this.adminContextPath = adminServerProperties.getContextPath();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // @formatter:off
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl(adminContextPath + "/");

        http.authorizeRequests()
                //授予對(duì)所有靜態(tài)資產(chǎn)和登錄頁(yè)面的公共訪問權(quán)限孙援。
                .antMatchers(adminContextPath + "/assets/**").permitAll()
                .antMatchers(adminContextPath + "/login").permitAll()
                //必須對(duì)每個(gè)其他請(qǐng)求進(jìn)行身份驗(yàn)證
                .anyRequest().authenticated()
                .and()
                //配置登錄和注銷
                .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and()
                .logout().logoutUrl(adminContextPath + "/logout").and()
                //啟用HTTP-Basic支持。這是Spring Boot Admin Client注冊(cè)所必需的
                .httpBasic().and();
        // @formatter:on
    }
}

重新訪問 http://localhost:8769/ 會(huì)出現(xiàn)登錄界面扇雕,密碼是 配置文件中配置好的拓售,賬號(hào) admin 密碼 admin,界面如下:

五镶奉、通知

1. 郵件通知

在 Spring Boot Admin 中 當(dāng)注冊(cè)的應(yīng)用程序狀態(tài)更改為DOWN础淤、UNKNOWN崭放、OFFLINE 都可以指定觸發(fā)通知,下面講解配置郵件通知鸽凶。

在sc-admin-server工程pom文件币砂,加上mail的依賴,如下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

在配置文件application.yml文件中玻侥,配置收發(fā)郵件的配置:

spring:
  mail:
    host: smtp.163.com
    username: xxxx@163.com
    password: xxxx
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
  boot:
    admin:
      notify:
        mail:
          from: xxxx@163.com
          to: xxxx@qq.com

配置后决摧,重啟sc-admin-server工程,之后若出現(xiàn)注冊(cè)的客戶端的狀態(tài)從 UP 變?yōu)?OFFLINE 或其他狀態(tài)使碾,服務(wù)端就會(huì)自動(dòng)將電子郵件發(fā)送到上面配置的收件地址蜜徽。

注意 : 配置了郵件通知后,會(huì)出現(xiàn) 反復(fù)通知 service offline / up票摇。這個(gè)問題的原因在于 查詢應(yīng)用程序的狀態(tài)和信息超時(shí)拘鞋,下面給出兩種解決方案:

#方法一:增加超時(shí)時(shí)間(單位:ms)

spring.boot.admin.monitor.read-timeout=20000

#方法二:關(guān)閉閉未使用或不重要的檢查點(diǎn)

management.health.db.enabled=false
management.health.mail.enabled=false
management.health.redis.enabled=false
management.health.mongo.enabled=false

2. 自定義通知

可以通過添加實(shí)現(xiàn)Notifier接口的Spring Beans來(lái)添加您自己的通知程序,最好通過擴(kuò)展 AbstractEventNotifier或AbstractStatusChangeNotifier矢门。在sc-admin-server工程中編寫一個(gè)自定義的通知器:

@Component
public class CustomNotifier  extends AbstractStatusChangeNotifier {
    private static final Logger LOGGER = LoggerFactory.getLogger( LoggingNotifier.class);

    public CustomNotifier(InstanceRepository repository) {
        super(repository);
    }

    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() -> {
            if (event instanceof InstanceStatusChangedEvent) {
                LOGGER.info("Instance {} ({}) is {}", instance.getRegistration().getName(), event.getInstance(),
                        ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus());

                String status = ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus();

                switch (status) {
                    // 健康檢查沒通過
                    case "DOWN":
                        System.out.println("發(fā)送 健康檢查沒通過 的通知盆色!");
                        break;
                    // 服務(wù)離線
                    case "OFFLINE":
                        System.out.println("發(fā)送 服務(wù)離線 的通知!");
                        break;
                    //服務(wù)上線
                    case "UP":
                        System.out.println("發(fā)送 服務(wù)上線 的通知祟剔!");
                        break;
                    // 服務(wù)未知異常
                    case "UNKNOWN":
                        System.out.println("發(fā)送 服務(wù)未知異常 的通知隔躲!");
                        break;
                    default:
                        break;
                }

            } else {
                LOGGER.info("Instance {} ({}) {}", instance.getRegistration().getName(), event.getInstance(),
                        event.getType());
            }
        });
    }
}

源碼下載:https://github.com/gf-huanchupk/SpringBootLearning

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市物延,隨后出現(xiàn)的幾起案子宣旱,更是在濱河造成了極大的恐慌,老刑警劉巖叛薯,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浑吟,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡耗溜,警方通過查閱死者的電腦和手機(jī)组力,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)抖拴,“玉大人燎字,你說(shuō)我怎么就攤上這事“⒄” “怎么了候衍?”我有些...
    開封第一講書人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)家夺。 經(jīng)常有香客問我脱柱,道長(zhǎng),這世上最難降的妖魔是什么拉馋? 我笑而不...
    開封第一講書人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任榨为,我火速辦了婚禮惨好,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘随闺。我一直安慰自己日川,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開白布矩乐。 她就那樣靜靜地躺著龄句,像睡著了一般。 火紅的嫁衣襯著肌膚如雪散罕。 梳的紋絲不亂的頭發(fā)上分歇,一...
    開封第一講書人閱讀 51,718評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音欧漱,去河邊找鬼职抡。 笑死,一個(gè)胖子當(dāng)著我的面吹牛误甚,可吹牛的內(nèi)容都是我干的缚甩。 我是一名探鬼主播,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼窑邦,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼擅威!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起冈钦,我...
    開封第一講書人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤郊丛,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后瞧筛,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體宾袜,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年驾窟,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片认轨。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡绅络,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出嘁字,到底是詐尸還是另有隱情恩急,我是刑警寧澤,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布纪蜒,位于F島的核電站衷恭,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏纯续。R本人自食惡果不足惜随珠,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一灭袁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧窗看,春花似錦茸歧、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至拉讯,卻和暖如春涤浇,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背魔慷。 一陣腳步聲響...
    開封第一講書人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工只锭, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人盖彭。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓纹烹,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親召边。 傳聞我的和親對(duì)象是個(gè)殘疾皇子铺呵,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

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