SpringBoot快速集成SpringBootAdmin管控台監控服務


SpringBootAdmin是一個針對 Spring Boot 的 Actuator 接口進行 UI 美化封裝的監控工具,它可以在列表中瀏覽所有被監控 spring-boot 項目的基本信息、詳細的 Health 信息、內存信息、JVM 信息、垃圾回收信息、各種配置信息(比如數據源、緩存列表和命中率)等。可分為服務端( spring-boot-admin-server)和客戶端( spring-boot-admin-client),服務端和客戶端之間采用http通訊方式實現數據交互。服務端server需要單獨啟動一個服務,而客戶端client只需要集成到各個微服務中。

1、初識SpringBootAdmin

首先我們需要了解到Spring Boot Admin應用程序是能夠提供以下功能供我們使用:
  1. 顯示健康狀況
  2. 顯示詳細信息
  3. JVM和內存指標
  4. micrometer.io指標
  5. 數據源指標
  6. 緩存指標
  7. 顯示內部編號
  8. 關注並下載日志文件
  9. 查看JVM系統和環境屬性
  10. 查看Spring Boot配置屬性
  11. 支持Spring Cloud的可發布/ env-&/ refresh-endpoint
  12. 輕松的日志級別管理
  13. 與JMX-beans交互
  14. 查看線程轉儲
  15. 查看http-traces
  16. 查看審核事件
  17. 查看http端點
  18. 查看預定的任務
  19. 查看和刪除活動會話(使用spring-session)
  20. 查看Flyway / Liquibase數據庫遷移
  21. 下載heapdump
  22. 狀態更改通知(通過電子郵件,Slack,Hipchat等)
  23. 狀態更改的事件日志(非持久性)

2、搭建服務端--POM文件中添加相關依賴

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</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-server</artifactId>
            <version>2.5.1</version>
        </dependency>

3、修改服務端application啟動類

在咱們啟動類上面新增@EnableAdminServer注解,進行啟用SpringBootAdminServer服務端

@SpringBootApplication
@EnableAdminServer
public class BootAdminServerApplication {

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

4、配置security安全信息

在application.properties文件中新增以下配置信息。


# 應用程序端口
server.port=8085
# 配置一個賬號和密碼
spring.security.user.name=admin
spring.security.user.password=admin

初始化SecuritySecureConfig配置(如未初始化是看不到帶SpringBootAdmin Logo登錄頁面的)

    @Configuration
    public static class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
        private final String adminContextPath;

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

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            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();
        }
    }

5、啟動server服務端

服務啟動后,在瀏覽器中輸入以下地址。我們是可以看見對應登錄頁面,對應賬號密碼就是咱們在properties文件中配置的。

http://127.0.0.1:8085/login

 登錄后可以看到應用列表數量是空的,此時咱們需要開始搭建咱們的Client客戶端了。

 

 6、搭建client客戶端

在pom文件中新增以下依賴信息。(注意版本要與server端保持一致)

<!-- SpringBootAdmin管控台 -->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
            <version>2.5.1</version>
        </dependency>

修改properties文件

spring.boot.admin.client.url=http://127.0.0.1:8085
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin
spring.application.name=spring-boot-application
management.endpoints.web.exposure.include=*

spring.boot.admin.client.url 指向我們上面服務端的項目接口路徑。management.endpoints.web.exposure.include 表示將所有端口都暴露出來,可以被監控到。spring.application.name 表示改項目在spring-boot-admin 上的的顯示名稱。spring.boot.admin.client.username 和password 就是設置的用戶名和密碼了,這里需要注意的是,如果admin-server 中沒有集成 security 的話,不用配置用戶名和密碼也可以注冊進去,在服務端可以監控到,但如果admin-server 集成了security,就需要保證client 中配置的用戶名和server 中配置的用戶名密碼保持一致。

 

 

 

 

 

 

 把client客戶端啟動后,會自動注冊到咱們server服務端,咱們可以通過server服務端應用牆找到對應服務查看詳細指標信息。(題外話:期間博主是有遇到客戶端啟動后,服務端無法采集到對應指標信息。原因是由於client客戶端有配置security,沒有給對應探針接口放行。如大家客戶端有用到security的話,需要在security配置中放行以下兩個接口信息。)

 

 

 

 

 

// 對應匿名+已授權均可訪問
                .antMatchers("/actuator/**","/instances").permitAll()

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM