SpringBootAdmin使用


SpringBootAdmin用來管理和監控SpringBoot應用程序,它利用spring-boot-starter-actuator提供的功能,將各個微服務的狀態整合到一起,並提供良好的界面查看支持,並且能夠動態的修改實例日志級別。SpringBootAdmin分為server端和client端,server端可查看各個微服務的狀態,client端將微服務注冊到server端。github源碼地址:https://github.com/codecentric/spring-boot-admin

1、服務端基本配置

  1)pom.xml依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
    <version>2.2.1</version>
</dependency>

  2)application.properties配置

server.port=8001
spring.application.name=admin-server

  3)SpringBoot啟動類添加@EnableAdminServer注解

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

2、客戶端基本配置

  1)pom.xml依賴

<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>
    <version>2.2.1</version>
</dependency>

  2)application.properties配置

server.port=8002
spring.application.name=admin-client
spring.boot.admin.client.url=http://127.0.0.1:8001
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=ALWAYS

  3)客戶端不需要做任何代碼修改

3、查看SpringBootAdmin效果

  1)啟動服務端和客戶端后,訪問http://127.0.0.1:8001,就可以查看所有注冊的實例

  2)點擊具體實例就可以查看到當前實例的具體運行狀態,在該界面可查看應用實例的健康狀態、線程、內存使用、GC時間

  3)修改日志級別。點擊日志配置,根據包過濾需要修改的類,將level由info調整到debug后,該實例對應的代碼就可以輸出debug級別的日志了。

 

 

4、啟用認證

  SpringBootAdmin的認證系統由spring-security管理。

  1)服務端pom.xml文件增加spring-security依賴,改為:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>de.codecentric</groupId>
    <artifactId>spring-boot-admin-server-ui</artifactId>
    <version>2.2.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

  2)服務端application.properties增加security用戶和密碼配置信息,修改為:

server.port=8001
spring.application.name=admin-server
spring.security.user.name=admin
spring.security.user.password=abc123

  3)服務端增加Java代碼,配置spring-security

package com.zhi.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

import de.codecentric.boot.admin.server.config.AdminServerProperties;

/**
 * 配置security驗證頁面指向SpringBootAdmin提供的UI界面
 * 
 * @author zhi.leaf
 * @since 2020年1月20日17:44:25
 *
 */
@Configuration
public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {

    private final String contextPath;

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 跨域設置,SpringBootAdmin客戶端通過instances注冊,見InstancesController
        http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
                .ignoringAntMatchers(contextPath + "/instances");

        http.authorizeRequests().antMatchers(contextPath + "/assets/**").permitAll(); // 靜態資源
        http.authorizeRequests().anyRequest().authenticated(); // 所有請求必須通過認證

        // 整合spring-boot-admin-server-ui
        http.formLogin().loginPage("/login").permitAll();
        http.logout().logoutUrl("/logout").logoutSuccessUrl("/login");

        // 啟用basic認證,SpringBootAdmin客戶端使用的是basic認證
        http.httpBasic();
    }
}

  4)客戶端application.properties也增加用戶和密碼信息,和服務端保持一致,改為

server.port=8002
spring.application.name=admin-client
spring.boot.admin.client.url=http://127.0.0.1:8001
spring.boot.admin.client.username=admin
spring.boot.admin.client.password=abc123

  5)重啟服務端和客戶端后,http://127.0.0.1:8001/,將會出現登錄界面

5、通知提醒。SpringBootAdmin提供了多種通知功能,也可以自定義通知提醒。這里我們驗證一下郵件通知功能

  1)服務端poxm.xml增加配置

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

  2)增加配置

spring.mail.host=smtp.qq.com
spring.mail.username=xxxx@foxmail.com
spring.mail.password=xxxx

spring.boot.admin.notify.mail.from=xxxx@foxmail.com
spring.boot.admin.notify.mail.to=yyyy@foxmail.com

  3)當客戶端實例停掉后,我們會受到如下郵件提醒,郵件模板是可以配置的。


免責聲明!

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



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