亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

SpringBoot Admin之應(yīng)用監(jiān)控與告警配置方式

 更新時(shí)間:2025年04月15日 09:45:48   作者:程序媛學(xué)姐  
這篇文章主要介紹了SpringBoot Admin之應(yīng)用監(jiān)控與告警配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

引言

在微服務(wù)架構(gòu)和分布式系統(tǒng)的廣泛應(yīng)用背景下,有效監(jiān)控應(yīng)用運(yùn)行狀態(tài)并及時(shí)發(fā)現(xiàn)潛在問題變得尤為重要。Spring Boot Admin 是一個(gè)開源的監(jiān)控解決方案,專為 Spring Boot 應(yīng)用設(shè)計(jì),它提供了直觀的 Web UI 界面,使開發(fā)者和運(yùn)維人員能夠?qū)崟r(shí)監(jiān)控應(yīng)用指標(biāo)、查看日志、管理 Bean、檢查環(huán)境配置等。本文將深入探討 Spring Boot Admin 的核心功能、配置方法以及如何實(shí)現(xiàn)有效的應(yīng)用監(jiān)控和告警系統(tǒng),幫助團(tuán)隊(duì)構(gòu)建更加健壯和可靠的應(yīng)用生態(tài)。

一、Spring Boot Admin 基礎(chǔ)架構(gòu)

Spring Boot Admin 由兩部分組成:服務(wù)端和客戶端。服務(wù)端提供 Web UI 界面,收集和展示客戶端信息;客戶端則將應(yīng)用信息推送到服務(wù)端或允許服務(wù)端拉取信息。這種架構(gòu)使得 Spring Boot Admin 能夠集中監(jiān)控多個(gè) Spring Boot 應(yīng)用。

要開始使用 Spring Boot Admin,首先需要?jiǎng)?chuàng)建一個(gè)服務(wù)端應(yīng)用:

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

在應(yīng)用程序入口類上添加 @EnableAdminServer 注解:

import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

接下來,配置需要監(jiān)控的客戶端應(yīng)用:

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

在客戶端的 application.properties 文件中配置 Admin Server 地址和 Actuator 端點(diǎn):

# Admin Server 地址
spring.boot.admin.client.url=http://localhost:8080
spring.boot.admin.client.instance.name=${spring.application.name}

# 暴露 Actuator 端點(diǎn)
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

通過以上配置,客戶端應(yīng)用會(huì)自動(dòng)注冊(cè)到 Admin Server,并開始上報(bào)監(jiān)控?cái)?shù)據(jù)。

二、監(jiān)控指標(biāo)與可視化

Spring Boot Admin 基于 Spring Boot Actuator 提供的端點(diǎn),展示了豐富的應(yīng)用監(jiān)控指標(biāo),包括:

2.1 健康狀態(tài)監(jiān)控

健康狀態(tài)是最基本的監(jiān)控指標(biāo),展示應(yīng)用及其依賴組件(數(shù)據(jù)庫(kù)、緩存、消息隊(duì)列等)的狀態(tài):

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator implements HealthIndicator {
    
    @Override
    public Health health() {
        int errorCode = checkService(); // 自定義健康檢查邏輯
        if (errorCode != 0) {
            return Health.down()
                    .withDetail("Error Code", errorCode)
                    .build();
        }
        return Health.up().build();
    }
    
    private int checkService() {
        // 實(shí)現(xiàn)具體的服務(wù)檢查邏輯
        return 0; // 0 表示正常
    }
}

2.2 性能指標(biāo)監(jiān)控

Spring Boot Admin 展示了 JVM 內(nèi)存使用、線程狀態(tài)、GC 活動(dòng)等關(guān)鍵性能指標(biāo):

# 配置 Micrometer 收集更詳細(xì)的性能指標(biāo)
management.metrics.export.simple.enabled=true
management.metrics.enable.all=true

自定義業(yè)務(wù)指標(biāo)可以通過 Micrometer 注冊(cè):

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Service;

@Service
public class OrderService {
    
    private final Counter orderCounter;
    
    public OrderService(MeterRegistry registry) {
        this.orderCounter = Counter.builder("app.orders.created")
                .description("Number of orders created")
                .register(registry);
    }
    
    public void createOrder() {
        // 業(yè)務(wù)邏輯
        orderCounter.increment();
    }
}

2.3 日志查看

Spring Boot Admin 允許直接在 Web 界面查看應(yīng)用日志,配置方式如下:

# 配置日志文件路徑
logging.file.name=logs/application.log
# 啟用 logfile 端點(diǎn)
management.endpoint.logfile.enabled=true
management.endpoint.logfile.external-file=${logging.file.name}

三、告警配置

Spring Boot Admin 支持多種告警方式,當(dāng)應(yīng)用狀態(tài)變化時(shí)(例如從 UP 變?yōu)?DOWN)觸發(fā)通知:

3.1 郵件告警

配置郵件告警需要添加依賴:

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

在 Admin Server 的配置文件中添加郵件配置:

# 郵件服務(wù)器配置
spring.mail.host=smtp.example.com
spring.mail.port=25
spring.mail.username=admin
spring.mail.password=secret
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

# 告警配置
spring.boot.admin.notify.mail.to=admin@example.com
spring.boot.admin.notify.mail.from=spring-boot-admin@example.com
spring.boot.admin.notify.mail.template=classpath:/email-templates/status-changed.html
spring.boot.admin.notify.mail.ignore-changes=UNKNOWN:UP,DOWN:UNKNOWN

3.2 釘釘/企業(yè)微信告警

對(duì)于企業(yè)環(huán)境,可以配置釘釘或企業(yè)微信告警:

import de.codecentric.boot.admin.server.config.AdminServerNotifierAutoConfiguration;
import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import reactor.core.publisher.Mono;

@Component
public class DingTalkNotifier extends AbstractStatusChangeNotifier {
    
    private final String webhookUrl = "https://oapi.dingtalk.com/robot/send?access_token=xxx";
    private final RestTemplate restTemplate = new RestTemplate();
    
    public DingTalkNotifier(InstanceRepository repository) {
        super(repository);
    }
    
    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() -> {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            
            String message = String.format("""
                    {
                        "msgtype": "text",
                        "text": {
                            "content": "應(yīng)用狀態(tài)變更: %s 實(shí)例 %s 狀態(tài)從 %s 變?yōu)?%s"
                        }
                    }
                    """,
                    instance.getRegistration().getName(),
                    instance.getId(),
                    getLastStatus(event.getInstance()),
                    instance.getStatusInfo().getStatus());
            
            HttpEntity<String> request = new HttpEntity<>(message, headers);
            restTemplate.postForEntity(webhookUrl, request, String.class);
        });
    }
}

3.3 自定義告警規(guī)則

Spring Boot Admin 允許通過自定義 Notifier 實(shí)現(xiàn)復(fù)雜的告警規(guī)則:

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

@Component
public class CustomNotifier extends AbstractStatusChangeNotifier {
    
    public CustomNotifier(InstanceRepository repository) {
        super(repository);
    }
    
    @Override
    protected boolean shouldNotify(InstanceEvent event, Instance instance) {
        // 自定義告警觸發(fā)條件
        if (instance.getStatusInfo().isDown()) {
            // 檢查是否是生產(chǎn)環(huán)境
            String env = instance.getRegistration().getMetadata().get("environment");
            return "production".equals(env);
        }
        return false;
    }
    
    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() -> {
            // 實(shí)現(xiàn)告警邏輯,如調(diào)用外部告警系統(tǒng)API
        });
    }
}

四、安全配置

在生產(chǎn)環(huán)境中,應(yīng)該為 Spring Boot Admin 添加安全保護(hù)。最常用的方式是集成 Spring Security:

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

配置基本認(rèn)證:

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.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.security.web.csrf.CookieCsrfTokenRepository;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        successHandler.setTargetUrlParameter("redirectTo");
        successHandler.setDefaultTargetUrl("/");
        
        http.authorizeRequests()
                .antMatchers("/assets/**").permitAll()
                .antMatchers("/login").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage("/login").successHandler(successHandler)
                .and()
                .logout().logoutUrl("/logout")
                .and()
                .httpBasic()
                .and()
                .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
    }
}

客戶端注冊(cè)到安全保護(hù)的 Admin Server 需要提供憑證:

spring.boot.admin.client.username=admin
spring.boot.admin.client.password=admin

五、實(shí)戰(zhàn)應(yīng)用與最佳實(shí)踐

5.1 集成服務(wù)發(fā)現(xiàn)

在微服務(wù)環(huán)境中,可以集成 Eureka、Consul 等服務(wù)發(fā)現(xiàn)機(jī)制自動(dòng)注冊(cè)應(yīng)用:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
@Configuration
public class DiscoveryConfig {
    
    @Bean
    public EurekaDiscoveryClient discoveryClient(EurekaClient eurekaClient) {
        return new EurekaDiscoveryClient(eurekaClient);
    }
}

5.2 分層告警策略

建立分層告警策略,根據(jù)問題嚴(yán)重程度選擇不同的通知方式:

  • 輕微問題:僅記錄日志或發(fā)送郵件
  • 中等問題:發(fā)送企業(yè)即時(shí)通訊通知(釘釘/企業(yè)微信)
  • 嚴(yán)重問題:短信和電話告警,確保立即處理

5.3 自定義儀表盤

通過 Grafana 與 Prometheus 集成,創(chuàng)建更強(qiáng)大的監(jiān)控儀表盤:

# Prometheus 配置
management.metrics.export.prometheus.enabled=true
management.endpoint.prometheus.enabled=true

總結(jié)

Spring Boot Admin 為 Spring Boot 應(yīng)用提供了強(qiáng)大而簡(jiǎn)潔的監(jiān)控解決方案,通過直觀的界面展示應(yīng)用的健康狀態(tài)、性能指標(biāo)和配置信息。合理配置告警機(jī)制,可以及時(shí)發(fā)現(xiàn)并響應(yīng)潛在問題,提高系統(tǒng)的可靠性和可用性。在實(shí)際應(yīng)用中,應(yīng)結(jié)合安全配置、服務(wù)發(fā)現(xiàn)、分層告警策略等最佳實(shí)踐,構(gòu)建完善的應(yīng)用監(jiān)控體系。

隨著微服務(wù)和分布式系統(tǒng)的復(fù)雜度不斷提高,Spring Boot Admin 作為輕量級(jí)監(jiān)控工具,能夠幫助開發(fā)者和運(yùn)維人員更好地理解應(yīng)用行為、優(yōu)化性能,以及快速定位和解決問題,是現(xiàn)代 Spring Boot 應(yīng)用不可或缺的監(jiān)控利器。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java對(duì)象轉(zhuǎn)化成String類型的四種方法小結(jié)

    java對(duì)象轉(zhuǎn)化成String類型的四種方法小結(jié)

    在java項(xiàng)目的實(shí)際開發(fā)和應(yīng)用中,常常需要用到將對(duì)象轉(zhuǎn)為String這一基本功能。本文就詳細(xì)的介紹幾種方法,感興趣的可以了解一下
    2021-08-08
  • 詳解Java MD5二次加密的應(yīng)用

    詳解Java MD5二次加密的應(yīng)用

    MD5的全稱是message-digest algorithm 5 信息-摘要算法。這篇文章主要為大家詳細(xì)介紹了Java中MD5二次加密的應(yīng)用,感興趣的小伙伴可以了解一下
    2023-02-02
  • Spring?Boot實(shí)現(xiàn)WebSocket實(shí)時(shí)通信

    Spring?Boot實(shí)現(xiàn)WebSocket實(shí)時(shí)通信

    本文主要介紹了Spring?Boot實(shí)現(xiàn)WebSocket實(shí)時(shí)通信,包含實(shí)現(xiàn)實(shí)時(shí)消息傳遞和群發(fā)消息等功能,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-05-05
  • 詳解Java中綴表達(dá)式的實(shí)現(xiàn)

    詳解Java中綴表達(dá)式的實(shí)現(xiàn)

    中綴表達(dá)式是一個(gè)通用的算術(shù)或邏輯公式表示方法。,中綴表達(dá)式不容易被計(jì)算機(jī)解析,但仍被許多程序語(yǔ)言使用,因?yàn)樗先藗兊钠毡橛梅?。本文介紹了實(shí)現(xiàn)中綴表達(dá)式的方法,需要的可以參考一下
    2022-07-07
  • 詳解springmvc常用5種注解

    詳解springmvc常用5種注解

    在本篇里我們給大家總結(jié)了關(guān)于springmvc常用5種注解相關(guān)知識(shí)點(diǎn)以及實(shí)例代碼,需要的朋友們參考下。
    2019-07-07
  • SpringBoot使用Redis對(duì)用戶IP進(jìn)行接口限流的項(xiàng)目實(shí)踐

    SpringBoot使用Redis對(duì)用戶IP進(jìn)行接口限流的項(xiàng)目實(shí)踐

    本文主要介紹了SpringBoot使用Redis對(duì)用戶IP進(jìn)行接口限流,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • MyBatis中獲取Mysql數(shù)據(jù)庫(kù)插入記錄的主鍵值的實(shí)現(xiàn)

    MyBatis中獲取Mysql數(shù)據(jù)庫(kù)插入記錄的主鍵值的實(shí)現(xiàn)

    本文主要介紹了MyBatis中獲取Mysql數(shù)據(jù)庫(kù)插入記錄的主鍵值的實(shí)現(xiàn),包含了三種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • 一文詳解Java中的類加載機(jī)制

    一文詳解Java中的類加載機(jī)制

    Java虛擬機(jī)把描述類的數(shù)據(jù)從Class文件加載到內(nèi)存,并對(duì)數(shù)據(jù)進(jìn)行校驗(yàn)、轉(zhuǎn)換解析和初始化,最終形成可以被虛擬機(jī)直接使用的Java類型,這個(gè)過程被稱作虛擬機(jī)的類加載機(jī)制。本文將詳解Java的類加載機(jī)制,需要的可以參考一下
    2022-05-05
  • SpringBoot項(xiàng)目jar和war打包部署方式詳解

    SpringBoot項(xiàng)目jar和war打包部署方式詳解

    這篇文章主要為大家介紹了SpringBoot項(xiàng)目jar和war打包部署方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Spring + Spring Boot + MyBatis + MongoDB的整合教程

    Spring + Spring Boot + MyBatis + MongoDB的整合教程

    這篇文章主要給大家介紹了關(guān)于Spring + Spring Boot + MyBatis + MongoDB的整合教程,文中通過圖文以及示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-12-12

最新評(píng)論