监控告警 #
一、监控概述 #
1.1 监控维度 #
| 维度 | 说明 |
|---|---|
| 集群状态 | 节点、分片、副本状态 |
| 性能指标 | QPS、延迟、吞吐量 |
| 资源使用 | CPU、内存、磁盘 |
| 业务指标 | 文档数、索引大小 |
1.2 监控工具 #
- Solr Admin UI
- Metrics API
- Prometheus + Grafana
- 日志监控
二、Metrics API #
2.1 获取指标 #
bash
# 获取所有指标
curl "http://localhost:8983/solr/admin/metrics"
# 获取特定指标
curl "http://localhost:8983/solr/admin/metrics?key=solr.jvm.memory.*"
# 获取Core指标
curl "http://localhost:8983/solr/mycore/admin/metrics"
2.2 常用指标 #
JVM指标
| 指标 | 说明 |
|---|---|
| solr.jvm.memory.heap.used | 堆内存使用 |
| solr.jvm.memory.heap.max | 最大堆内存 |
| solr.jvm.gc.G1_Old_Generation.count | GC次数 |
| solr.jvm.threads.count | 线程数 |
查询指标
| 指标 | 说明 |
|---|---|
| solr.core.mycore.searcher.numDocs | 文档数 |
| solr.core.mycore.searcher.maxDoc | 最大文档数 |
| solr.core.mycore.searcher.deletedDocs | 删除文档数 |
| solr.core.mycore.index.size | 索引大小 |
缓存指标
| 指标 | 说明 |
|---|---|
| solr.core.mycore.cache.queryResultCache.hitratio | 查询缓存命中率 |
| solr.core.mycore.cache.filterCache.hitratio | 过滤器缓存命中率 |
| solr.core.mycore.cache.documentCache.hitratio | 文档缓存命中率 |
请求指标
| 指标 | 说明 |
|---|---|
| solr.core.mycore.handler./select.requests | 请求数 |
| solr.core.mycore.handler./select.errors | 错误数 |
| solr.core.mycore.handler./select.avgRequestsPerSecond | 平均QPS |
| solr.core.mycore.handler./select.avgTimePerRequest | 平均延迟 |
三、集群状态监控 #
3.1 集群状态API #
bash
# 获取集群状态
curl "http://localhost:8983/solr/admin/collections?action=CLUSTERSTATUS"
# 获取节点状态
curl "http://localhost:8983/solr/admin/collections?action=CLUSTERSTATUS&collection=mycollection"
3.2 健康检查 #
bash
# Ping检查
curl "http://localhost:8983/solr/mycore/admin/ping"
# 集群健康检查
curl "http://localhost:8983/solr/admin/collections?action=CLUSTERHEALTH"
3.3 响应示例 #
json
{
"responseHeader": {
"status": 0
},
"cluster": {
"collections": {
"mycollection": {
"shards": {
"shard1": {
"state": "active",
"replicas": {
"core_node1": {
"state": "active",
"leader": "true",
"base_url": "http://node1:8983/solr"
}
}
}
}
}
}
}
}
四、Prometheus监控 #
4.1 配置Prometheus导出 #
solrconfig.xml
xml
<metrics>
<reporter name="prometheus" class="org.apache.solr.metrics.reporters.SolrPrometheusExporter">
<str name="registry">solr.core</str>
<str name="registry">solr.jvm</str>
<str name="registry">solr.node</str>
</reporter>
</metrics>
4.2 prometheus.yml #
yaml
scrape_configs:
- job_name: 'solr'
static_configs:
- targets: ['localhost:8983']
metrics_path: '/solr/admin/metrics'
params:
key: ['solr.jvm', 'solr.core', 'solr.node']
4.3 Grafana Dashboard #
导入Solr Dashboard或创建自定义Dashboard:
- JVM内存使用
- GC统计
- 请求QPS
- 请求延迟
- 缓存命中率
- 索引大小
五、日志监控 #
5.1 日志配置 #
log4j2.xml
xml
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{1}:%L - %m%n"/>
</Console>
<RollingFile name="RollingFile" fileName="logs/solr.log"
filePattern="logs/solr-%d{yyyy-MM-dd}-%i.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5p %c{1}:%L - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1"/>
<SizeBasedTriggeringPolicy size="100MB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
<AppenderRef ref="RollingFile"/>
</Root>
</Loggers>
</Configuration>
5.2 日志级别 #
bash
# 动态修改日志级别
curl "http://localhost:8983/solr/admin/info/logging?set=root:WARN"
5.3 日志分析 #
bash
# 查看错误日志
grep ERROR logs/solr.log
# 查看慢查询
grep "QTime" logs/solr.log | awk -F'QTime=' '{print $2}' | awk -F'}' '{print $1}' | sort -n | tail -10
六、告警配置 #
6.1 告警规则 #
Prometheus Alert Rules
yaml
groups:
- name: solr_alerts
rules:
- alert: SolrDown
expr: up{job="solr"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Solr instance down"
- alert: SolrHighMemory
expr: solr_jvm_memory_heap_used_bytes / solr_jvm_memory_heap_max_bytes > 0.9
for: 5m
labels:
severity: warning
annotations:
summary: "Solr high memory usage"
- alert: SolrHighGC
expr: rate(solr_jvm_gc_G1_Old_Generation_count_total[5m]) > 1
for: 5m
labels:
severity: warning
annotations:
summary: "Solr high GC rate"
- alert: SolrHighLatency
expr: solr_core_handler_select_avg_time_per_request > 1000
for: 5m
labels:
severity: warning
annotations:
summary: "Solr high query latency"
- alert: SolrLowCacheHitRatio
expr: solr_core_cache_queryResultCache_hitratio < 0.5
for: 10m
labels:
severity: warning
annotations:
summary: "Solr low cache hit ratio"
6.2 告警通知 #
Alertmanager配置
yaml
global:
smtp_smarthost: 'smtp.example.com:587'
smtp_from: 'alertmanager@example.com'
smtp_auth_username: 'alertmanager@example.com'
smtp_auth_password: 'password'
route:
receiver: 'team-email'
routes:
- match:
severity: critical
receiver: 'team-email-critical'
receivers:
- name: 'team-email'
email_configs:
- to: 'team@example.com'
- name: 'team-email-critical'
email_configs:
- to: 'team-critical@example.com'
七、监控脚本 #
7.1 健康检查脚本 #
bash
#!/bin/bash
# health_check.sh
SOLR_URL="http://localhost:8983/solr"
COLLECTIONS=("mycollection" "othercollection")
for collection in "${COLLECTIONS[@]}"; do
response=$(curl -s "${SOLR_URL}/${collection}/admin/ping")
status=$(echo "$response" | jq -r '.status')
if [ "$status" != "OK" ]; then
echo "CRITICAL: ${collection} is not healthy"
exit 2
fi
done
echo "OK: All collections are healthy"
exit 0
7.2 指标采集脚本 #
bash
#!/bin/bash
# collect_metrics.sh
SOLR_URL="http://localhost:8983/solr"
# 获取JVM内存
memory=$(curl -s "${SOLR_URL}/admin/metrics?key=solr.jvm.memory.heap.*" | jq '.metrics')
# 获取查询统计
queries=$(curl -s "${SOLR_URL}/admin/metrics?key=solr.core.*.handler./select.*" | jq '.metrics')
# 输出指标
echo "Memory: ${memory}"
echo "Queries: ${queries}"
八、监控最佳实践 #
8.1 监控清单 #
基础设施
- [ ] CPU使用率
- [ ] 内存使用率
- [ ] 磁盘使用率
- [ ] 网络IO
Solr指标
- [ ] JVM堆内存
- [ ] GC频率和时间
- [ ] 请求QPS
- [ ] 请求延迟
- [ ] 缓存命中率
- [ ] 索引大小
集群状态
- [ ] 节点状态
- [ ] 分片状态
- [ ] 副本状态
- [ ] Leader状态
8.2 告警阈值 #
| 指标 | 警告阈值 | 严重阈值 |
|---|---|---|
| CPU使用率 | 70% | 90% |
| 内存使用率 | 80% | 95% |
| 磁盘使用率 | 80% | 90% |
| 查询延迟 | 500ms | 1000ms |
| 缓存命中率 | <50% | <30% |
| GC频率 | >1次/分钟 | >5次/分钟 |
九、总结 #
监控告警要点:
| 类别 | 监控项 |
|---|---|
| 基础设施 | CPU、内存、磁盘 |
| JVM | 堆内存、GC |
| 查询 | QPS、延迟 |
| 缓存 | 命中率 |
| 集群 | 节点、分片状态 |
最佳实践:
- 建立完善的监控体系
- 设置合理的告警阈值
- 定期检查监控数据
- 制定应急响应计划
恭喜你完成Solr完全指南的学习!
最后更新:2026-03-27