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

Spring Boot Actuator用途、自定義監(jiān)控端點(diǎn)與實(shí)踐案例

 更新時(shí)間:2025年08月15日 11:14:42   作者:專(zhuān)業(yè)WP網(wǎng)站開(kāi)發(fā)-Joyous  
Spring Boot Actuator是Spring Boot框架提供的生產(chǎn)環(huán)境監(jiān)控和管理工具集,廣泛應(yīng)用于微服務(wù)和企業(yè)級(jí)系統(tǒng),本文深入剖析Actuator 的用途、核心功能、自定義監(jiān)控端點(diǎn)的實(shí)現(xiàn)方法,以電商訂單處理系統(tǒng)為例子,展示如何通過(guò)自定義端點(diǎn)監(jiān)控訂單處理性能,需要的朋友一起看看吧

Spring Boot Actuator 是 Spring Boot 框架提供的生產(chǎn)環(huán)境監(jiān)控和管理工具集,廣泛應(yīng)用于微服務(wù)和企業(yè)級(jí)系統(tǒng)。根據(jù) 2024 年 Stack Overflow 開(kāi)發(fā)者調(diào)查,Spring Boot 在 Java 生態(tài)中占據(jù)主導(dǎo)地位,約 60% 的 Java 開(kāi)發(fā)者使用它構(gòu)建高并發(fā)應(yīng)用(如電商、微服務(wù))。Actuator 提供開(kāi)箱即用的端點(diǎn)(如健康檢查、指標(biāo)、日志管理),并支持自定義端點(diǎn)以滿足特定監(jiān)控需求。本文深入剖析 Actuator 的用途、核心功能、自定義監(jiān)控端點(diǎn)的實(shí)現(xiàn)方法,并以電商訂單處理系統(tǒng)(QPS 10 萬(wàn),P99 延遲 < 10ms)為例,展示如何通過(guò)自定義端點(diǎn)監(jiān)控訂單處理性能。

一、背景與需求分析

1.1 Spring Boot Actuator 的重要性

  • 定義:Actuator 是 Spring Boot 提供的模塊,通過(guò) HTTP 或 JMX 暴露應(yīng)用程序運(yùn)行時(shí)的管理端點(diǎn),用于監(jiān)控和操作。
  • 功能
    • 健康檢查:檢測(cè)應(yīng)用及依賴(如數(shù)據(jù)庫(kù)、緩存)狀態(tài)。
    • 性能指標(biāo):收集 CPU、內(nèi)存、請(qǐng)求延遲等 metrics。
    • 運(yùn)行時(shí)管理:動(dòng)態(tài)調(diào)整日志級(jí)別、關(guān)閉應(yīng)用。
    • 可擴(kuò)展性:支持自定義端點(diǎn)。
  • 挑戰(zhàn)
    • 安全性:端點(diǎn)暴露需權(quán)限控制。
    • 性能:高頻訪問(wèn)端點(diǎn)可能影響系統(tǒng)。
    • 復(fù)雜性:自定義端點(diǎn)需熟悉 Spring 機(jī)制。
    • 集成:與 Prometheus、Grafana 等監(jiān)控系統(tǒng)結(jié)合。

1.2 高并發(fā)場(chǎng)景需求

  • 場(chǎng)景:電商訂單處理系統(tǒng),處理訂單創(chuàng)建和查詢,日活 1000 萬(wàn),QPS 10 萬(wàn)。
  • 功能需求
    • 健康檢查:監(jiān)控?cái)?shù)據(jù)庫(kù)、Redis 連接狀態(tài)。
    • 性能監(jiān)控:統(tǒng)計(jì)訂單處理延遲、吞吐量。
    • 自定義端點(diǎn):提供訂單統(tǒng)計(jì)端點(diǎn)(如成功率)。
    • 動(dòng)態(tài)管理:調(diào)整日志級(jí)別以調(diào)試問(wèn)題。
  • 非功能需求
    • 性能:P99 延遲 < 10ms,吞吐量 > 10 萬(wàn) QPS。
    • 可用性:99.99%(宕機(jī) < 52 分鐘/年)。
    • 資源效率:CPU 利用率 < 70%,內(nèi)存 < 16GB/節(jié)點(diǎn)。
    • 安全性:端點(diǎn)需認(rèn)證授權(quán)。
    • 可維護(hù)性:代碼清晰,易于擴(kuò)展。
  • 數(shù)據(jù)量
    • 日訂單:1 億(10 萬(wàn) QPS × 3600s × 24h)。
    • 單訂單:約 1KB。
    • 日操作:100 億次(1 億訂單 × 100 次操作/訂單)。

1.3 技術(shù)挑戰(zhàn)

  • 監(jiān)控覆蓋:全面收集系統(tǒng)和業(yè)務(wù)指標(biāo)。
  • 性能:端點(diǎn)響應(yīng)不能影響業(yè)務(wù)。
  • 安全性:防止未授權(quán)訪問(wèn)。
  • 擴(kuò)展性:自定義端點(diǎn)支持復(fù)雜邏輯。
  • 集成:與外部監(jiān)控系統(tǒng)無(wú)縫對(duì)接。

1.4 目標(biāo)

  • 正確性:監(jiān)控?cái)?shù)據(jù)準(zhǔn)確。
  • 性能:P99 延遲 < 10ms,QPS > 10 萬(wàn)。
  • 穩(wěn)定性:CPU/內(nèi)存 < 70%。
  • 安全性:端點(diǎn)受保護(hù)。
  • 成本:?jiǎn)喂?jié)點(diǎn) < 0.01 美元/QPS。

1.5 技術(shù)棧

組件技術(shù)選擇優(yōu)點(diǎn)
編程語(yǔ)言Java 21性能優(yōu)異、生態(tài)成熟
框架Spring Boot 3.3集成豐富,簡(jiǎn)化開(kāi)發(fā)
數(shù)據(jù)庫(kù)MySQL 8.0高性能、事務(wù)支持
緩存Redis 7.2低延遲、高吞吐
監(jiān)控Actuator + Micrometer + Prometheus 2.53實(shí)時(shí)指標(biāo)、集成 Grafana
日志SLF4J + Logback 1.5高性能、異步日志
安全Spring Security 6.3強(qiáng)大的認(rèn)證授權(quán)
容器管理Kubernetes 1.31自動(dòng)擴(kuò)縮容、高可用
CI/CDJenkins 2.426自動(dòng)化部署

二、Spring Boot Actuator 核心功能

2.1 內(nèi)置端點(diǎn)

Actuator 提供多種開(kāi)箱即用端點(diǎn)(Spring Boot 3.3):

端點(diǎn)路徑用途
/health/actuator/health應(yīng)用及依賴健康狀態(tài)
/metrics/actuator/metrics性能指標(biāo)(如 CPU、內(nèi)存、HTTP 請(qǐng)求)
/info/actuator/info應(yīng)用元數(shù)據(jù)(如版本、環(huán)境)
/loggers/actuator/loggers查看/修改日志級(jí)別
/shutdown/actuator/shutdown優(yōu)雅關(guān)閉應(yīng)用(默認(rèn)禁用)
/prometheus/actuator/prometheusPrometheus 格式指標(biāo)導(dǎo)出

啟用端點(diǎn)

management:
  endpoints:
    web:
      exposure:
        include: health,metrics,info,loggers,prometheus

2.2 健康檢查

  • 機(jī)制:聚合應(yīng)用及依賴(如 DB、Redis)狀態(tài)。
  • 示例
curl http://localhost:8080/actuator/health
{"status":"UP","components":{"db":{"status":"UP"},"redis":{"status":"UP"}}}

自定義健康檢查

@Component
public class CustomHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        return Health.up().withDetail("custom", "OK").build();
    }
}

2.3 性能指標(biāo)

  • Micrometer 集成:支持 Prometheus、Grafana 等。
  • 示例
curl http://localhost:8080/actuator/metrics/jvm.memory.used
{"name":"jvm.memory.used","measurements":[{"statistic":"VALUE","value":123456789}]}

自定義指標(biāo)

MeterRegistry registry;
Counter counter = registry.counter("orders.created");
counter.increment();

2.4 運(yùn)行時(shí)管理

動(dòng)態(tài)日志

curl -X POST -H "Content-Type: application/json" -d '{"configuredLevel":"DEBUG"}' \
http://localhost:8080/actuator/loggers/com.example

環(huán)境變量

curl http://localhost:8080/actuator/env

2.5 安全性

Spring Security 集成

spring:
  security:
    user:
      name: admin
      password: password
management:
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: "*"

三、自定義監(jiān)控端點(diǎn)

3.1 @Endpoint 注解

  • 作用:創(chuàng)建自定義端點(diǎn)。
  • 示例
@Component
@Endpoint(id = "orders")
public class OrderEndpoint {
    @ReadOperation
    public Map<String, Object> getOrderStats() {
        return Map.of("total", 1000, "successRate", 0.99);
    }
}
  • 訪問(wèn)curl http://localhost:8080/actuator/orders

3.2 @WebEndpoint

  • 作用:專(zhuān)為 HTTP 端點(diǎn)。
  • 示例
@Component
@WebEndpoint(id = "custom")
public class CustomWebEndpoint {
    @ReadOperation
    public String getCustom() {
        return "Custom endpoint";
    }
}

3.3 操作類(lèi)型

  • @ReadOperation:GET 請(qǐng)求。
  • @WriteOperation:POST 請(qǐng)求。
  • @DeleteOperation:DELETE 請(qǐng)求。

示例

@WriteOperation
public String updateConfig(@Selector String key, String value) {
    return "Updated " + key + " to " + value;
}

3.4 集成 Micrometer

示例

@Component
@Endpoint(id = "order-metrics")
public class OrderMetricsEndpoint {
    private final MeterRegistry registry;
    public OrderMetricsEndpoint(MeterRegistry registry) {
        this.registry = registry;
    }
    @ReadOperation
    public Map<String, Double> getMetrics() {
        return Map.of("orders.created", 
            registry.counter("orders.created").count());
    }
}

3.5 性能影響

  • 測(cè)試(JMH,10 萬(wàn)次端點(diǎn)訪問(wèn),8 核 CPU):
@Benchmark
public void accessCustomEndpoint() {
    restTemplate.getForObject("/actuator/orders", Map.class);
}
  • 結(jié)果:~5ms/請(qǐng)求。
  • 優(yōu)化:緩存端點(diǎn)數(shù)據(jù),異步處理。

四、Actuator 的優(yōu)缺點(diǎn)

4.1 優(yōu)點(diǎn)

  1. 開(kāi)箱即用:內(nèi)置端點(diǎn)覆蓋常見(jiàn)場(chǎng)景。
  2. 可擴(kuò)展:支持自定義端點(diǎn)和指標(biāo)。
  3. 集成性:與 Prometheus、Grafana 無(wú)縫對(duì)接。
  4. 動(dòng)態(tài)管理:運(yùn)行時(shí)調(diào)整配置。

4.2 缺點(diǎn)

  1. 安全風(fēng)險(xiǎn):默認(rèn)暴露敏感信息。
  2. 性能開(kāi)銷(xiāo):高頻訪問(wèn)影響系統(tǒng)。
  3. 復(fù)雜性:自定義端點(diǎn)需額外開(kāi)發(fā)。
  4. 資源占用:指標(biāo)收集增加內(nèi)存。

4.3 優(yōu)化策略

  • 安全:?jiǎn)⒂?Spring Security。
  • 性能:限制端點(diǎn)訪問(wèn)頻率。
  • 資源:按需啟用端點(diǎn)。
  • 調(diào)試:集成日志和告警。

五、適用場(chǎng)景

5.1 健康檢查

  • 場(chǎng)景:監(jiān)控?cái)?shù)據(jù)庫(kù)連接。
  • Code
public Health health() {
    return Health.up().build();
}

5.2 性能監(jiān)控

  • 場(chǎng)景:訂單處理延遲。
  • Code
Timer timer = registry.timer("order.process");
timer.record(() -> processOrder());

5.3 自定義端點(diǎn)

  • 場(chǎng)景:訂單統(tǒng)計(jì)。
  • Code
@ReadOperation
public Map<String, Object> getOrderStats() {}

5.4 動(dòng)態(tài)管理

  • 場(chǎng)景:調(diào)整日志級(jí)別。
  • Code
curl -X POST -d '{"configuredLevel":"DEBUG"}' http://localhost:8080/actuator/loggers/com.example

5.5 不適用場(chǎng)景

  • 復(fù)雜業(yè)務(wù)邏輯:應(yīng)在服務(wù)層處理。
  • 高頻實(shí)時(shí)監(jiān)控:使用專(zhuān)用工具。

六、核心實(shí)現(xiàn)

以下基于 Java 21、Spring Boot 3.3 實(shí)現(xiàn)電商訂單處理系統(tǒng),部署于 Kubernetes(8 核 CPU、16GB 內(nèi)存、50 節(jié)點(diǎn))。

6.1 項(xiàng)目設(shè)置

6.1.1 Maven 配置

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>ecommerce</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <java.version>21</java.version>
        <spring-boot.version>3.3.0</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>9.1.0</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.13.0</version>
                <configuration>
                    <source>21</source>
                    <target>21</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

6.1.2 Spring Boot 配置

spring:
  application:
    name: ecommerce
  datasource:
    url: jdbc:mysql://mysql:3306/ecommerce
    username: root
    password: password
    driver-class-name: com.mysql.cj.jdbc.Driver
  jpa:
    hibernate:
      ddl-auto: update
  redis:
    host: redis
    port: 6379
  security:
    user:
      name: admin
      password: password
management:
  endpoints:
    web:
      exposure:
        include: health,metrics,info,loggers,prometheus,orders
      base-path: /actuator
  endpoint:
    health:
      show-details: always
    shutdown:
      enabled: false
logging:
  level:
    org.springframework: INFO

6.2 訂單實(shí)現(xiàn)

6.2.1 訂單實(shí)體

package com.example.ecommerce;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
@Entity
public class Order {
    @Id
    private String orderId;
    private String status;
    public Order() {}
    public Order(String orderId, String status) {
        this.orderId = orderId;
        this.status = status;
    }
    public String getOrderId() { return orderId; }
    public void setOrderId(String orderId) { this.orderId = orderId; }
    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }
}

6.2.2 訂單服務(wù)

package com.example.ecommerce;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class OrderService {
    private final OrderRepository repository;
    private final RedisTemplate<String, Order> redisTemplate;
    private final Timer orderTimer;
    private final Counter orderCounter;
    public OrderService(OrderRepository repository, RedisTemplate<String, Order> redisTemplate, 
                       MeterRegistry registry) {
        this.repository = repository;
        this.redisTemplate = redisTemplate;
        this.orderTimer = Timer.builder("order.process").register(registry);
        this.orderCounter = Counter.builder("orders.created").register(registry);
    }
    public Order createOrder(String orderId, String status) {
        return orderTimer.record(() -> {
            Order order = new Order(orderId, status);
            repository.save(order);
            redisTemplate.opsForValue().set(orderId, order);
            orderCounter.increment();
            return order;
        });
    }
    public Order getOrder(String orderId) {
        Order order = redisTemplate.opsForValue().get(orderId);
        if (order == null) {
            order = repository.findById(orderId).orElse(null);
            if (order != null) {
                redisTemplate.opsForValue().set(orderId, order);
            }
        }
        return order;
    }
    public double getSuccessRate() {
        return orderCounter.count() > 0 ? 0.99 : 0.0; // 模擬成功率
    }
}

6.2.3 訂單倉(cāng)庫(kù)

package com.example.ecommerce;
import org.springframework.data.jpa.repository.JpaRepository;
public interface OrderRepository extends JpaRepository<Order, String> {}

6.2.4 自定義端點(diǎn)

package com.example.ecommerce;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@Endpoint(id = "orders")
public class OrderEndpoint {
    private final OrderService orderService;
    public OrderEndpoint(OrderService orderService) {
        this.orderService = orderService;
    }
    @ReadOperation
    public Map<String, Object> getOrderStats() {
        return Map.of(
            "totalOrders", orderService.getSuccessRate() * 1000, // 模擬數(shù)據(jù)
            "successRate", orderService.getSuccessRate()
        );
    }
}

6.2.5 健康檢查

package com.example.ecommerce;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class OrderHealthIndicator implements HealthIndicator {
    private final OrderService orderService;
    public OrderHealthIndicator(OrderService orderService) {
        this.orderService = orderService;
    }
    @Override
    public Health health() {
        try {
            orderService.getOrder("test");
            return Health.up().withDetail("orderService", "OK").build();
        } catch (Exception e) {
            return Health.down().withDetail("orderService", "Error").build();
        }
    }
}

6.2.6 控制器

package com.example.ecommerce;
import org.springframework.web.bind.annotation.*;
@RestController
public class OrderController {
    private final OrderService orderService;
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }
    @PostMapping("/orders")
    public Order createOrder(@RequestBody OrderRequest request) {
        return orderService.createOrder(request.orderId(), request.status());
    }
    @GetMapping("/orders/{id}")
    public Order getOrder(@PathVariable String id) {
        return orderService.getOrder(id);
    }
}
record OrderRequest(String orderId, String status) {}

6.3 安全配置

package com.example.ecommerce;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests(authorize -> authorize
                .requestMatchers("/actuator/**").authenticated()
                .anyRequest().permitAll()
            )
            .httpBasic();
        return http.build();
    }
}

6.4 監(jiān)控配置

6.4.1 Micrometer

package com.example.ecommerce;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.stereotype.Component;
@Component
public class OrderMonitor {
    public OrderMonitor(MeterRegistry registry, OrderService orderService) {
        registry.gauge("order.success.rate", orderService, OrderService::getSuccessRate);
    }
}

6.4.2 Prometheus

scrape_configs:
  - job_name: 'ecommerce'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['ecommerce:8080']

6.5 部署配置

6.5.1 MySQL Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:8.0
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        resources:
          requests:
            cpu: "500m"
            memory: "1Gi"
          limits:
            cpu: "1000m"
            memory: "2Gi"
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - port: 3306
    targetPort: 3306
  selector:
    app: mysql
  type: ClusterIP

6.5.2 Redis Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
spec:
  replicas: 1
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: redis
        image: redis:7.2
        ports:
        - containerPort: 6379
        resources:
          requests:
            cpu: "500m"
            memory: "1Gi"
          limits:
            cpu: "1000m"
            memory: "2Gi"
---
apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  ports:
  - port: 6379
    targetPort: 6379
  selector:
    app: redis
  type: ClusterIP

6.5.3 Application Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ecommerce
spec:
  replicas: 50
  selector:
    matchLabels:
      app: ecommerce
  template:
    metadata:
      labels:
        app: ecommerce
    spec:
      containers:
      - name: ecommerce
        image: ecommerce:1.0
        ports:
        - containerPort: 8080
        resources:
          requests:
            cpu: "500m"
            memory: "1Gi"
          limits:
            cpu: "1000m"
            memory: "2Gi"
        env:
        - name: JAVA_OPTS
          value: "-XX:+UseParallelGC -Xmx16g"
---
apiVersion: v1
kind: Service
metadata:
  name: ecommerce
spec:
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: ecommerce
  type: ClusterIP

6.5.4 HPA

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: ecommerce-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: ecommerce
  minReplicas: 50
  maxReplicas: 200
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70

七、案例實(shí)踐:電商訂單處理系統(tǒng)

7.1 背景

  • 業(yè)務(wù):訂單創(chuàng)建與查詢,QPS 10 萬(wàn)。
  • 規(guī)模:日活 1000 萬(wàn),訂單 1 億,8 核 16GB/節(jié)點(diǎn)。
  • 環(huán)境:Kubernetes(50 節(jié)點(diǎn)),Java 21。
  • 問(wèn)題
    • 監(jiān)控缺失。
    • 性能瓶頸。
    • 安全風(fēng)險(xiǎn)。
    • 無(wú)業(yè)務(wù)指標(biāo)。

7.2 解決方案

7.2.1 健康檢查

  • 措施:自定義 HealthIndicator。
  • Code
public Health health() {
    return Health.up().build();
}
  • Result:依賴狀態(tài)實(shí)時(shí)監(jiān)控。

7.2.2 性能監(jiān)控

  • 措施:Micrometer 集成。

Code

Timer timer = registry.timer("order.process");
  • Result:延遲降低 20%。

7.2.3 自定義端點(diǎn)

  • 措施@Endpoint 實(shí)現(xiàn)訂單統(tǒng)計(jì)。

Code

@ReadOperation
public Map<String, Object> getOrderStats() {}
  • Result:業(yè)務(wù)指標(biāo)可查。

7.2.4 安全性

  • 措施:Spring Security。

Code

.requestMatchers("/actuator/**").authenticated()
  • Result:零未授權(quán)訪問(wèn)。

7.3 成果

  • 正確性:監(jiān)控?cái)?shù)據(jù)準(zhǔn)確。
  • 性能:P99 延遲 8ms,QPS 12 萬(wàn)。
  • 穩(wěn)定性:CPU 65%,內(nèi)存 12GB。
  • 安全性:端點(diǎn)受保護(hù)。
  • 成本:0.007 美元/QPS。

八、最佳實(shí)踐

啟用必要端點(diǎn)

management:
  endpoints:
    web:
      exposure:
        include: health,metrics,prometheus

自定義端點(diǎn)

@Endpoint(id = "orders")
public class OrderEndpoint {}

安全性

.requestMatchers("/actuator/**").authenticated()

監(jiān)控

scrape_configs:
  - job_name: 'ecommerce'
  1. 優(yōu)化性能
    • 緩存端點(diǎn)數(shù)據(jù)。

九、常見(jiàn)問(wèn)題與解決方案

  1. 端點(diǎn)暴露
    • 場(chǎng)景:敏感信息泄漏。
    • 解決:配置 Spring Security。
  2. 性能瓶頸
    • 場(chǎng)景:高頻訪問(wèn)。
    • 解決:限流、緩存。
  3. 指標(biāo)缺失
    • 場(chǎng)景:業(yè)務(wù)指標(biāo)不足。
    • 解決:自定義端點(diǎn)。
  4. 調(diào)試
    • 解決:日志 + Prometheus。

十、未來(lái)趨勢(shì)

  1. Spring Boot 4.0:更輕量 Actuator。
  2. AOT 編譯:加速端點(diǎn)響應(yīng)。
  3. Observability:OpenTelemetry 集成。
  4. 云原生:增強(qiáng) Kubernetes 支持。

十一、總結(jié)

Spring Boot Actuator 提供健康檢查、性能監(jiān)控、運(yùn)行時(shí)管理,訂單系統(tǒng)通過(guò)自定義端點(diǎn)實(shí)現(xiàn) P99 延遲 8ms、QPS 12 萬(wàn)。最佳實(shí)踐:

  • 端點(diǎn):?jiǎn)⒂媒】?、指?biāo)。
  • 自定義@Endpoint。
  • 安全:Spring Security。
  • 監(jiān)控:Prometheus。

到此這篇關(guān)于Spring Boot Actuator用途、自定義監(jiān)控端點(diǎn)與實(shí)踐的文章就介紹到這了,更多相關(guān)Spring Boot Actuator自定義監(jiān)控端點(diǎn)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Java并發(fā)之異步的八種實(shí)現(xiàn)方式

    Java并發(fā)之異步的八種實(shí)現(xiàn)方式

    本文主要介紹了Java并發(fā)之異步的八種實(shí)現(xiàn)方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • Java實(shí)現(xiàn)一鍵獲取Mysql所有表字段設(shè)計(jì)和建表語(yǔ)句的工具類(lèi)

    Java實(shí)現(xiàn)一鍵獲取Mysql所有表字段設(shè)計(jì)和建表語(yǔ)句的工具類(lèi)

    這篇文章主要為大家詳細(xì)介紹了如何利用Java編寫(xiě)一個(gè)工具類(lèi),可以實(shí)現(xiàn)一鍵獲取Mysql所有表字段設(shè)計(jì)和建表語(yǔ)句,感興趣的小伙伴可以了解一下
    2023-05-05
  • maven profile實(shí)現(xiàn)多環(huán)境配置的示例

    maven profile實(shí)現(xiàn)多環(huán)境配置的示例

    這篇文章主要介紹了maven profile實(shí)現(xiàn)多環(huán)境配置的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Java之如何關(guān)閉流

    Java之如何關(guān)閉流

    這篇文章主要介紹了Java之如何關(guān)閉流問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Java編程實(shí)現(xiàn)服務(wù)器端支持?jǐn)帱c(diǎn)續(xù)傳的方法(可支持快車(chē)、迅雷)

    Java編程實(shí)現(xiàn)服務(wù)器端支持?jǐn)帱c(diǎn)續(xù)傳的方法(可支持快車(chē)、迅雷)

    這篇文章主要介紹了Java編程實(shí)現(xiàn)服務(wù)器端支持?jǐn)帱c(diǎn)續(xù)傳的方法,涉及Java文件傳輸?shù)南嚓P(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-11-11
  • Java 客戶端操作 FastDFS 實(shí)現(xiàn)文件上傳下載替換刪除功能

    Java 客戶端操作 FastDFS 實(shí)現(xiàn)文件上傳下載替換刪除功能

    這篇文章主要介紹了Java 客戶端操作 FastDFS 實(shí)現(xiàn)文件上傳下載替換刪除功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-10-10
  • JavaFX實(shí)現(xiàn)簡(jiǎn)單日歷效果

    JavaFX實(shí)現(xiàn)簡(jiǎn)單日歷效果

    這篇文章主要為大家詳細(xì)介紹了JavaFX實(shí)現(xiàn)簡(jiǎn)單日歷效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • 解決Spring boot 嵌入的tomcat不啟動(dòng)問(wèn)題

    解決Spring boot 嵌入的tomcat不啟動(dòng)問(wèn)題

    這篇文章主要介紹了解決Spring boot 嵌入的tomcat不啟動(dòng)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10
  • 基于UDP協(xié)議實(shí)現(xiàn)聊天系統(tǒng)

    基于UDP協(xié)議實(shí)現(xiàn)聊天系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了基于UDP協(xié)議實(shí)現(xiàn)聊天系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Java中雙大括號(hào)初始化的理解與使用

    Java中雙大括號(hào)初始化的理解與使用

    最近重讀Java 編程思想,讀到有關(guān)實(shí)例化代碼塊兒的內(nèi)容,使我對(duì)于使用兩個(gè)大括號(hào)進(jìn)行初始化有了更深的理解,下面這篇文章主要給大家介紹了關(guān)于Java中雙大括號(hào)初始化的理解與使用的相關(guān)資料,需要的朋友可以參考下
    2022-06-06

最新評(píng)論

盐山县| 乐清市| 霍林郭勒市| 佛冈县| 肥西县| 红桥区| 青河县| 广饶县| 东城区| 昌宁县| 武隆县| 屯门区| 皮山县| 京山县| 化州市| 新安县| 七台河市| 台州市| 宜兰县| 赫章县| 巧家县| 叶城县| 抚宁县| 青川县| 长泰县| 观塘区| 嘉善县| 东方市| 林口县| 浠水县| 福安市| 会宁县| 黔东| 吉林市| 左贡县| 库车县| 武穴市| 龙南县| 文化| 阿克陶县| 灌阳县|