最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

引言

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

要開始使用 Spring Boot Admin,首先需要創(chuàng)建一個服務(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 端點:

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

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

通過以上配置,客戶端應(yīng)用會自動注冊到 Admin Server,并開始上報監(jiān)控數(shù)據(jù)。

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

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

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

健康狀態(tài)是最基本的監(jiān)控指標,展示應(yīng)用及其依賴組件(數(shù)據(jù)庫、緩存、消息隊列等)的狀態(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() {
        // 實現(xiàn)具體的服務(wù)檢查邏輯
        return 0; // 0 表示正常
    }
}

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

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

# 配置 Micrometer 收集更詳細的性能指標
management.metrics.export.simple.enabled=true
management.metrics.enable.all=true

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

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 端點
management.endpoint.logfile.enabled=true
management.endpoint.logfile.external-file=${logging.file.name}

三、告警配置

Spring Boot Admin 支持多種告警方式,當(dāng)應(yīng)用狀態(tài)變化時(例如從 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è)微信告警

對于企業(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 實例 %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 實現(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(() -> {
            // 實現(xiàn)告警邏輯,如調(diào)用外部告警系統(tǒng)API
        });
    }
}

四、安全配置

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

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

配置基本認證:

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());
    }
}

客戶端注冊到安全保護的 Admin Server 需要提供憑證:

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

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

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

在微服務(wù)環(huán)境中,可以集成 Eureka、Consul 等服務(wù)發(fā)現(xiàn)機制自動注冊應(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ù)問題嚴重程度選擇不同的通知方式:

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

5.3 自定義儀表盤

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

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

總結(jié)

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

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

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

相關(guān)文章

  • Java實現(xiàn)數(shù)組與String相互轉(zhuǎn)換的常見方法詳解

    Java實現(xiàn)數(shù)組與String相互轉(zhuǎn)換的常見方法詳解

    在Java開發(fā)中,數(shù)組與字符串之間的相互轉(zhuǎn)換是處理數(shù)據(jù)時的常見需求,本文將詳細介紹Java中數(shù)組與字符串相互轉(zhuǎn)換的常見方法,大家可以根據(jù)需要進行選擇
    2025-07-07
  • MyBatis mapping類基本用法

    MyBatis mapping類基本用法

    這篇文章主要為大家介紹了MyBatis mapping類基本用法示例詳解,
    有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • 2020 IDEA安裝教程與激活(idea2020激活碼)

    2020 IDEA安裝教程與激活(idea2020激活碼)

    這篇文章主要介紹了2020 IDEA安裝教程與激活(idea2020激活碼),本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • 詳細分析JAVA加解密算法

    詳細分析JAVA加解密算法

    這篇文章主要介紹了JAVA加解密算法的的相關(guān)資料,文中講解非常詳細,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 解決springboot接入springfox-swagger2遇到的一些問題

    解決springboot接入springfox-swagger2遇到的一些問題

    這篇文章主要介紹了解決springboot接入springfox-swagger2遇到的一些問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • HashMap和Hashtable的詳細比較

    HashMap和Hashtable的詳細比較

    這篇文章主要介紹了HashMap和Hashtable的詳細比較的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • SpringBoot和MyBatis環(huán)境下實現(xiàn)動態(tài)數(shù)據(jù)源切換過程

    SpringBoot和MyBatis環(huán)境下實現(xiàn)動態(tài)數(shù)據(jù)源切換過程

    dynamic-datasource-spring-boot-starter?是一個用于在SpringBoot和MyBatis環(huán)境下實現(xiàn)動態(tài)數(shù)據(jù)源切換的工具,它簡化了配置和切換邏輯,通過引入依賴,配置數(shù)據(jù)源和使用@DS注解,可以輕松實現(xiàn)數(shù)據(jù)源的動態(tài)切換
    2025-10-10
  • java將m3u8格式轉(zhuǎn)成視頻文件的方法

    java將m3u8格式轉(zhuǎn)成視頻文件的方法

    這篇文章主要介紹了如何java將m3u8格式轉(zhuǎn)成視頻文件,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • java?啟動參數(shù)?springboot?idea詳解

    java?啟動參數(shù)?springboot?idea詳解

    這篇文章主要介紹了java?啟動參數(shù)?springboot?idea的相關(guān)知識,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • springboot集成mybatis-plus全過程

    springboot集成mybatis-plus全過程

    本文詳細介紹了如何在SpringBoot環(huán)境下集成MyBatis-Plus,包括配置maven依賴、application.yaml文件、創(chuàng)建數(shù)據(jù)庫和Java實體類、Mapper層、Service層和Controller層的設(shè)置,同時,還涵蓋了時間自動填充、分頁查詢、多對一和一對多的數(shù)據(jù)庫映射關(guān)系設(shè)置
    2024-09-09

最新評論

台北县| 石景山区| 武川县| 柘城县| 商水县| 修水县| 大安市| 紫阳县| 林芝县| 菏泽市| 沧州市| 资中县| 绍兴市| 巴彦淖尔市| 湘潭县| 永清县| 县级市| 荣成市| 景谷| 会东县| 万载县| 汾阳市| 阳朔县| 多伦县| 龙川县| 南涧| 民权县| 阳城县| 永城市| 丰台区| 淮北市| 故城县| 南部县| 西宁市| 胶州市| 康保县| 武穴市| 宜川县| 伊宁县| 岳西县| 太湖县|