Spring Boot Admin 2.1.0 全攻略

轉載請標明出處:
https://www.fangzhipeng.com
本文出自方誌朋的博客

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 或其他狀態,服務端就會自動將電子郵件發送到上面配置的地址。

源碼下載

快速開始: https://github.com/forezp/Spr...

和spring cloud結合:https://github.com/forezp/Spr...

參考資料

http://codecentric.github.io/...

https://github.com/codecentri...

更多閱讀

史上最簡單的 SpringCloud 教程彙總

SpringBoot教程彙總

Java面試題系列彙總
<div>

<p align="center">
    <img src="https://www.fangzhipeng.com/img/avatar.jpg" width="258" height="258"/>
    <br>
    掃一掃,支持下作者吧
</p>
<p align="center" style="margin-top: 15px; font-size: 11px;color: #cc0000;">
    <strong>(轉載本站文章請註明作者和出處 <a href="https://www.fangzhipeng.com">方誌朋的博客</a>)</strong>
</p>

</div>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章