這套組合是當前Java生態中最流行的監控解決方案之一,特別適合云原生環境下的微服務應用監控。下面我將從技術實現到最佳實踐進行全面解析。
一、技術棧組成與協作
1. 組件分工
組件 | 角色 | 關鍵能力 |
---|---|---|
Micrometer | 應用指標門面(Facade) | 統一指標采集API,對接多種監控系統 |
Prometheus | 時序數據庫+采集器 | 指標存儲、查詢、告警規則處理 |
Grafana | 可視化平臺 | 儀表盤展示、數據可視化分析 |
2. 數據流動
二、Micrometer 集成實踐
1. Spring Boot 配置
Maven依賴:
<dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
application.yml配置:
management:endpoints:web:exposure:include: health,info,prometheusmetrics:export:prometheus:enabled: truetags:application: ${spring.application.name} # 統一添加應用標簽
2. 自定義指標示例
業務指標采集:
@Service
public class OrderService {private final Counter orderCounter;private final Timer orderProcessingTimer;public OrderService(MeterRegistry registry) {// 創建計數器orderCounter = Counter.builder("orders.total").description("Total number of orders").tag("type", "online").register(registry);// 創建計時器orderProcessingTimer = Timer.builder("orders.processing.time").description("Order processing time").publishPercentiles(0.5, 0.95) // 50%和95%分位.register(registry);}public void processOrder(Order order) {// 方法1: 手動計時long start = System.currentTimeMillis();try {// 業務邏輯...orderCounter.increment();} finally {long duration = System.currentTimeMillis() - start;orderProcessingTimer.record(duration, TimeUnit.MILLISECONDS);}// 方法2: 使用Lambda自動計時orderProcessingTimer.record(() -> {// 業務邏輯...orderCounter.increment();});}
}
三、Prometheus 配置優化
1. 抓取配置示例
# prometheus.yml
global:scrape_interval: 15sevaluation_interval: 30sscrape_configs:- job_name: 'spring-apps'metrics_path: '/actuator/prometheus'scrape_interval: 10s # 對應用更頻繁采集static_configs:- targets: ['app1:8080', 'app2:8080']relabel_configs:- source_labels: [__address__]target_label: instance- source_labels: [__meta_service_name]target_label: service
2. 關鍵優化參數
存儲配置:
# 控制塊存儲行為
storage:tsdb:retention: 15d # 數據保留時間out_of_order_time_window: 1h # 允許亂序數據窗口# 限制內存使用
query:lookback-delta: 5mmax-concurrency: 20
四、Grafana 儀表盤設計
1. 核心監控儀表盤
JVM監控面板配置:
Panel 1: Heap Memory Usage
Query: sum(jvm_memory_used_bytes{area="heap"}) by (instance) / sum(jvm_memory_max_bytes{area="heap"}) by (instance)
Visualization: Time series with % unitPanel 2: GC Pause Time
Query: rate(jvm_gc_pause_seconds_sum[1m])
Visualization: HeatmapPanel 3: Thread States
Query: jvm_threads_states_threads{instance=~"$instance"}
Visualization: Stacked bar chart
2. 業務指標可視化
訂單業務看板:
{"panels": [{"title": "Orders per Minute","targets": [{"expr": "rate(orders_total[1m])","legendFormat": "{{instance}}"}],"type": "graph","yaxes": [{"format": "ops"}]},{"title": "Processing Time (95%)","targets": [{"expr": "histogram_quantile(0.95, rate(orders_processing_time_seconds_bucket[1m]))","legendFormat": "P95"}],"type": "stat","unit": "s"}]
}
五、生產環境最佳實踐
1. 指標命名規范
類型 | 前綴 | 示例 |
---|---|---|
計數器 | _total | http_requests_total |
計量器 | _current | queue_size_current |
計時器 | _seconds | api_latency_seconds |
分布概要 | _summary | response_size_summary |
2. 標簽使用原則
- 避免高基數標簽:如用戶ID、訂單號等
- 統一標簽命名:團隊內保持一致(如
env
vsenvironment
) - 重要維度標記:
region
、az
、service_version
等
3. 資源優化技巧
Micrometer配置:
@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {return registry -> registry.config().meterFilter(MeterFilter.deny(id -> {// 過濾不需要的指標return id.getName().startsWith("jvm_classes");})).commonTags("region", System.getenv("AWS_REGION"));
}
Prometheus資源限制:
# 容器部署時設置資源限制
resources:limits:memory: 8Girequests:cpu: 2memory: 4Gi
六、高級功能實現
1. 自定義Collector
public class CustomMetricsCollector extends Collector {@Overridepublic List<MetricFamilySamples> collect() {List<MetricFamilySamples> samples = new ArrayList<>();// 添加自定義指標samples.add(new MetricFamilySamples("custom_metric",Type.GAUGE,"Custom metric description",Collections.singletonList(new MetricFamilySamples.Sample("custom_metric",List.of("label1"),List.of("value1"),getCurrentValue()))));return samples;}
}// 注冊Collector
new CustomMetricsCollector().register();
2. 告警規則示例
groups:
- name: application-alertsrules:- alert: HighErrorRateexpr: rate(http_server_requests_errors_total[5m]) / rate(http_server_requests_total[5m]) > 0.05for: 10mlabels:severity: criticalannotations:summary: "High error rate on {{ $labels.instance }}"description: "Error rate is {{ $value }}"- alert: GCTooLongexpr: rate(jvm_gc_pause_seconds_sum[1h]) > 0.1labels:severity: warning
這套監控組合的優勢在于:
- 云原生友好:完美契合Kubernetes環境
- 低侵入性:Micrometer作為抽象層減少代碼耦合
- 高效存儲:Prometheus的TSDB壓縮比高
- 豐富可視化:Grafana社區提供大量現成儀表盤
建議實施路徑:
- 先搭建基礎監控(JVM/HTTP指標)
- 逐步添加業務指標
- 最后實現自定義告警和自動化處理