1. 程式人生 > >Spring Boot Admin 2.1.0 全攻略

Spring Boot Admin 2.1.0 全攻略

Spring Boot Admin簡介

Spring Boot Admin是一個開源社群專案,用於管理和監控SpringBoot應用程式。 應用程式作為Spring Boot Admin Client向為Spring Boot Admin Server註冊(通過HTTP)或使用SpringCloud註冊中心(例如Eureka,Consul)發現。 UI是的AngularJs應用程式,展示Spring Boot Admin Client的Actuator端點上的一些監控。常見的功能或者監控如下:

  • 顯示健康狀況
  • 顯示詳細資訊,例如
    • JVM和記憶體指標
    • micrometer.io指標
    • 資料來源指標
    • 快取指標
  • 顯示構建資訊編號
  • 關注並下載日誌檔案
  • 檢視jvm系統和環境屬性
  • 檢視Spring Boot配置屬性
  • 支援Spring Cloud的postable / env-和/ refresh-endpoint
  • 輕鬆的日誌級管理
  • 與JMX-beans互動
  • 檢視執行緒轉儲
  • 檢視http跟蹤
  • 檢視auditevents
  • 檢視http-endpoints
  • 檢視計劃任務
  • 檢視和刪除活動會話(使用spring-session)
  • 檢視Flyway / Liquibase資料庫遷移
  • 下載heapdump
  • 狀態變更通知(通過電子郵件,Slack,Hipchat,......)
  • 狀態更改的事件日誌(非永續性)

快速開始

建立Spring Boot Admin Server

本文的所有工程的Spring Boot版本為2.1.0 、Spring Cloud版本為Finchley.SR2。案例採用Maven多module形式,父pom檔案引入以下的依賴(完整的依賴見原始碼):


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId
>
<version>2.1.0.RELEASE</version> <relativePath/> </parent> <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> </dependencies> </dependencyManagement> <spring-cloud.version>Finchley.SR2</spring-cloud.version> 複製程式碼

在工程admin-server引入admin-server的起來依賴和web的起步依賴,程式碼如下:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.1.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
複製程式碼

然後在工程的啟動類AdminServerApplication加上@EnableAdminServer註解,開啟AdminServer的功能,程式碼如下:


@SpringBootApplication
@EnableAdminServer
public class AdminServerApplication {

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

}

複製程式碼

在工程的配置檔案application.yml中配置程式名和程式的埠,程式碼如下:

spring:
  application:
    name: admin-server
server:
  port: 8769
複製程式碼

這樣Admin Server就建立好了。

建立Spring Boot Admin Client

在admin-client工程的pom檔案引入admin-client的起步依賴和web的起步依賴,程式碼如下:


        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.1.0</version>
        </dependency>
       
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


複製程式碼

在工程的配置檔案application.yml中配置應用名和埠資訊,以及向admin-server註冊的地址為http://localhost:8769,最後暴露自己的actuator的所有埠資訊,具體配置如下:

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

複製程式碼

在工程的啟動檔案如下:


@SpringBootApplication
public class AdminClientApplication {

    public static void main(String[] args) {
        SpringApplication.run( AdminClientApplication.class, args );
    }
複製程式碼

一次啟動兩個工程,在瀏覽器上輸入localhost:8769 ,瀏覽器顯示的介面如下:

21.png

檢視wallboard:

22.png

點選wallboard,可以檢視admin-client具體的資訊,比如記憶體狀態資訊:

23.png

也可以檢視spring bean的情況:

24.png

更多監控資訊,自己體驗。

Spring boot Admin結合SC註冊中心使用

同上一個案例一樣,本案例也是使用的是Spring Boot版本為2.1.0 、Spring Cloud版本為Finchley.SR2。案例採用Maven多module形式,父pom檔案引入以下的依賴(完整的依賴見原始碼),此處省略。

搭建註冊中心

註冊中心使用Eureka、使用Consul也是可以的,在eureka-server工程中的pom檔案中引入:

 <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

複製程式碼

配置eureka-server的埠資訊,以及defaultZone和防止自注冊。最後系統暴露eureka-server的actuator的所有埠。

spring:
  application:
    name: 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
複製程式碼

在工程的啟動檔案EurekaServerApplication加上@EnableEurekaServer註解開啟Eureka Server.


@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

複製程式碼

eureka-server搭建完畢。

搭建admin-server

在admin-server工程的pom檔案引入admin-server的起步依賴、web的起步依賴、eureka-client的起步依賴,如下:

<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-starter-server</artifactId>
    <version>2.1.0</version>
</dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
        
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
複製程式碼

然後配置admin-server,應用名、埠資訊。並向註冊中心註冊,註冊地址為http://localhost:8761,最後將actuator的所有埠暴露出來,配置如下:


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
複製程式碼

在工程的啟動類AdminServerApplication加上@EnableAdminServer註解,開啟admin server的功能,加上@EnableDiscoveryClient註解開啟eurke client的功能。


@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient
public class AdminServerApplication {

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

}

複製程式碼

搭建admin-client

在admin-client的pom檔案引入以下的依賴,由於2.1.0採用webflux,引入webflux的起步依賴,引入eureka-client的起步依賴,並引用actuator的起步依賴如下:


 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</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-actuator</artifactId>
        </dependency>
複製程式碼

在工程的配置檔案配置應用名、埠、向註冊中心註冊的地址,以及暴露actuator的所有埠。


spring:
  application:
    name: 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

複製程式碼

在啟動類加上@EnableDiscoveryClie註解,開啟DiscoveryClient的功能。

@SpringBootApplication
@EnableDiscoveryClient
public class AdminClientApplication {

    public static void main(String[] args) {
        SpringApplication.run( AdminClientApplication.class, args );
    }
}
複製程式碼

一次啟動三個工程,在瀏覽器上訪問localhost:8769,瀏覽器會顯示和上一小節一樣的介面。

31.png

整合spring security

在2.1.0版本中去掉了hystrix dashboard,登入介面預設整合到了spring security模組,只要加上spring security就集成了登入模組。

只需要改變下admin-server工程,需要在admin-server工程的pom檔案引入以下的依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
複製程式碼

在admin-server工的配置檔案application.yml中配置spring security的使用者名稱和密碼,這時需要在服務註冊時帶上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}

複製程式碼

寫一個配置類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" );

        http.authorizeRequests()
                .antMatchers( adminContextPath + "/assets/**" ).permitAll()
                .antMatchers( adminContextPath + "/login" ).permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage( adminContextPath + "/login" ).successHandler( successHandler ).and()
                .logout().logoutUrl( adminContextPath + "/logout" ).and()
                .httpBasic().and()
                .csrf().disable();
        // @formatter:on
    }
}

複製程式碼

重啟啟動工程,在瀏覽器上訪問:http://localhost:8769/,會被重定向到登入介面,登入的使用者名稱和密碼為配置檔案中配置的,分別為admin和admin,介面顯示如下:

32.png

整合郵箱報警功能

在spring boot admin中,也可以整合郵箱報警功能,比如服務不健康了、下線了,都可以給指定郵箱傳送郵件。整合非常簡單,只需要改造下admin-server即可:

在admin-server工程Pom檔案,加上mail的起步依賴,程式碼如下:

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

複製程式碼

在配置檔案application.yml檔案中,需要配置郵件相關的配置,如下:

spring.mail.host: smtp.163.com
spring.mail.username: miles02
spring.mail.password:
spring.boot.admin.notify.mail.to: [email protected]

複製程式碼

做完以上配置後,當我們已註冊的客戶端的狀態從 UP 變為 OFFLINE 或其他狀態,服務端就會自動將電子郵件傳送到上面配置的地址。

原始碼下載

快速開始: github.com/forezp/Spri…

和spring cloud結合:github.com/forezp/Spri…

參考資料

codecentric.github.io/spring-boot…

github.com/codecentric…


掃一掃,支援下作者吧

(轉載本站文章請註明作者和出處 方誌朋的部落格