Prometheus 监控 Solr 全栈实战:从查询延迟到索引吞吐的分布式可观测性

📅 2026/7/13 15:36:53
Prometheus 监控 Solr 全栈实战:从查询延迟到索引吞吐的分布式可观测性
Prometheus 监控 Solr 全栈实战从查询延迟到索引吞吐的分布式可观测性Apache Solr 是企业级搜索平台的核心其查询 QPS、索引吞吐量、JVM 堆内存、垃圾回收、段合并以及副本状态等指标直接影响电商搜索、日志分析等业务的性能与稳定性。Prometheus 通过JMX Exporter或专为 Solr 打造的solr-exporter将 Solr 暴露的 JMX MBean 转化为标准化指标让开发与运维团队能够在一个统一的仪表盘上透视 Solr 集群的全貌。本文将带你从 JMX 配置到可视化、告警一步步落地 Solr 的可观测性。1. 方案选型JMX Exporter 与 solr-exporter方案优点缺点适用场景JMX ExporterPrometheus 官方 Java 代理成熟稳定随 Solr 进程启动无额外进程可抓取任意 JMX MBean配置稍复杂需编写 YAML 规则指标名称可能需要自定义规则优化通用 Java 监控已在 Solr 集群运行 JMX 的场景solr-exporter(Lucidworks)专为 Solr 设计指标标签更友好core、collection内置 Prometheus 端点支持 SolrCloud需独立运行占用额外资源版本需与 Solr 对应需要开箱即用、面向搜索业务级指标的用户JMX jolokia可将 JMX 转为 HTTP JSON再由 Prometheus 抓取引入额外组件增加延迟不推荐不如直接用 JMX Exporter推荐首选JMX Exporter因为它零额外守护进程且能捕获 Solr 所有的 MBean查询处理器、缓存、复制等。如果需要 collection 级别的聚合指标可再并行部署solr-exporter。本文以 JMX Exporter 为主线。2. 使用 JMX Exporter 监控 Solr2.1 下载并配置 JMX Exporter 代理在每台 Solr 节点上下载jmx_prometheus_javaagentjar 包wgethttps://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.20.0/jmx_prometheus_javaagent-0.20.0.jarsudomkdir-p/opt/solr/prometheussudomvjmx_prometheus_javaagent-0.20.0.jar /opt/solr/prometheus/创建配置文件/opt/solr/prometheus/solr_jmx.yml内容需覆盖 Solr 的核心指标。以下为精简但实用的模板startDelaySeconds:0hostPort:127.0.0.1:18983# Solr JMX 端口默认 18983SolrCloud或 8983单机ssl:falserules:# 查询处理器统计-pattern:solr/standardtypeselect, handler(.)(requests|errors|timeouts|totalTime|requestTimes|avgRequestsPerSecond|5minRateRequestsPerSecond|15minRateRequestsPerSecond|p99_ms|p95_ms|p75_ms|median_ms)name:solr_select_$2labels:handler:$1type:COUNTER-pattern:solr/standardtypeselect, handler(.)(requests|errors|timeouts|totalTime|requestTimes|avgRequestsPerSecond|5minRateRequestsPerSecond|15minRateRequestsPerSecond|p99_ms|p95_ms|p75_ms|median_ms)name:solr_select_$2labels:handler:$1type:GAUGE# 更新处理器统计-pattern:solr/updatetypeupdate, handler(.)(requests|errors|totalTime|requestTimes|avgRequestsPerSecond|5minRateRequestsPerSecond|15minRateRequestsPerSecond)name:solr_update_$2labels:handler:$1type:COUNTER-pattern:solr/updatetypeupdate, handler(.)(requests|errors|totalTime|requestTimes|avgRequestsPerSecond|5minRateRequestsPerSecond|15minRateRequestsPerSecond)name:solr_update_$2labels:handler:$1type:GAUGE# 缓存统计-pattern:solr/corecore(.), typefilterCache(size|hitratio|insanitycount|lookups|hits)name:solr_filtercache_$2labels:core:$1-pattern:solr/corecore(.), typequeryResultCache(size|hitratio|insanitycount|lookups|hits)name:solr_queryresultcache_$2labels:core:$1# 索引统计-pattern:solr/corecore(.), typeindex(numDocs|maxDoc|deletedDocs|indexHeapUsageBytes|segmentCount|sizeInBytes)name:solr_index_$2labels:core:$1# JVM 内存与 GC-pattern:java.langtypeMemoryHeapMemoryUsage(used|max)name:jvm_memory_heap_$1-pattern:java.langtypeGarbageCollector, name(.)(CollectionCount|CollectionTime)name:jvm_gc_$1_$2# 副本状态SolrCloud-pattern:solr/corecore(.), typereplication(isLeader|isActive|isReplicating|indexReplicatedAt)name:solr_replication_$2labels:core:$1可根据需要增加更多 MBean参考 Solr Metrics 文档。2.2 修改 Solr 启动参数编辑 Solr 的solr.in.sh或solr.in.cmd在SOLR_OPTS中添加 JMX Exporter 代理SOLR_OPTS$SOLR_OPTS-javaagent:/opt/solr/prometheus/jmx_prometheus_javaagent-0.20.0.jar9098:/opt/solr/prometheus/solr_jmx.yml这里让代理监听在9098端口避免与 Solr 自身端口冲突。重启 Solr 后curl http://localhost:9098/metrics即可看到指标。若 Solr 已配置 JMX 认证需在 YAML 中填写username和password。3. 配置 Prometheus 抓取scrape_configs:-job_name:solrscrape_interval:30sstatic_configs:-targets:-solr-node1:9098-solr-node2:9098-solr-node3:9098labels:cluster:solrcloud-prodenv:production4. 核心监控指标与 PromQL分类关键指标依上述规则含义PromQL 示例查询速率solr_select_requestsCounter查询请求总数rate(solr_select_requests[1m])查询延迟 P99solr_select_p99_msGauge99分位查询延迟ms直接查看或求平均值查询错误率solr_select_errors查询出错次数rate(solr_select_errors[5m])更新速率solr_update_requests索引更新请求速率rate(solr_update_requests[1m])缓存命中率solr_filtercache_hitratio0-1过滤器缓存命中率理想 0.95索引文档数solr_index_numDocsCore 内的文档数监控增长趋势索引删除文档solr_index_deletedDocs已被标记删除的文档过多会占用资源堆内存使用率jvm_memory_heap_used / jvm_memory_heap_maxJVM 堆使用百分比 85% 需告警GC 计数与时间jvm_gc_*_CollectionCount/jvm_gc_*_CollectionTimeGC 压力rate(jvm_gc_G1 Young Generation_CollectionTime[5m])副本状态solr_replication_isLeader1主节点角色监测 leader 分布复制状态solr_replication_isReplicating是否正在从主节点复制长时间为1可能有问题注意指标名称可能因配置的pattern而有差异。使用 Grafana Explore 查询/metrics来确认实际名称。5. Grafana 仪表盘推荐Solr Cluster DashboardDashboard ID12456基于 JMX Exporter 的 Solr 仪表盘涵盖查询延迟、QPS、缓存命中率、JVM 内存、索引大小等。Apache Solr (Prometheus)ID10447备选。自建面板可重点创建查询延迟热力分布、各 Core 的 QPS、JVM 堆趋势等面板。导入后选择数据源将cluster变量绑定到你的 Solr 集群标签。6. 告警规则实战groups:-name:solr_alertsrules:-alert:SolrNodeDownexpr:up{jobsolr} 0for:1mlabels:severity:criticalannotations:summary:Solr 节点 {{ $labels.instance }} 下线-alert:SolrHighQueryLatencyP99expr:solr_select_p99_ms500for:5mlabels:severity:warningannotations:summary:Solr 查询 P99 延迟超过 500mshandler: {{ $labels.handler }}-alert:SolrHighQueryErrorRateexpr:rate(solr_select_errors[5m])0.1for:5mlabels:severity:criticalannotations:summary:Solr 查询错误率 0.1/秒-alert:SolrLowFilterCacheHitRateexpr:solr_filtercache_hitratio 0.85for:10mlabels:severity:warningannotations:summary:过滤器缓存命中率低于 85% (core: {{ $labels.core }})-alert:SolrHighJvmHeapUsageexpr:(jvm_memory_heap_used / jvm_memory_heap_max)0.85for:10mlabels:severity:warningannotations:summary:Solr 节点 {{ $labels.instance }} JVM 堆使用超过 85%-alert:SolrReplicationLagexpr:solr_replication_isReplicating{core.*} 1for:30mlabels:severity:warningannotations:summary:Solr Core {{ $labels.core }} 复制状态持续时间过长可能延迟-alert:SolrManyDeletedDocsexpr:solr_index_deletedDocs1000000for:1hlabels:severity:infoannotations:summary:Solr Core {{ $labels.core }} 有大量已删除文档建议优化根据业务特点调整阈值例如查询延迟敏感的应用可将 P99 阈值降至 200ms。7. 进阶SolrCloud、安全与多集群7.1 监控 SolrCloud上述 JMX 配置已包含副本状态isLeader、isActive等。你可以在 Grafana 中创建表格列出每个 collection 的 leader 分布或绘制每个节点的 leader 数量。注意在 SolrCloud 模式下JMX 端口通常为 18983且需要确保 Prometheus 能访问每个节点的该端口或我们配置的代理端口 9098。7.2 启用 Solr 认证若 Solr 启用了 Basic Auth 或 KerberosJMX Exporter 本身不需要认证因为它直接从本地 JVM 读取 MBean。但如果你需要通过 Prometheus 抓取代理的 HTTP 端点时可以为代理添加认证如使用 Nginx 反代。更简单的方法是确保 9098 端口仅被 Prometheus 服务器访问防火墙。7.3 使用 solr-exporter 作为补充lucidworks/solr-exporter能提供 collection、shard、replica 级别的聚合指标并可暴露 Prometheus 端点。部署示例dockerrun-d\--namesolr-exporter\-eZK_HOSTzookeeper1:2181\-eSOLR_URLhttp://solr:8983\-eLISTEN_PORT9983\lucidworks/solr-exporter:latest然后 Prometheus 抓取9983端口。该导出器会暴露solr_collection_*、solr_shard_*等高级指标方便进行容量规划。8. 性能与安全考量JMX Exporter 开销代理仅在被 scrape 时采集 JMX对 Solr 性能影响微乎其微。抓取间隔设为 30s 足够。防火墙在每台 Solr 服务器上仅允许 Prometheus 服务器 IP 访问 9098 端口。日志Solr 的 GC 日志和慢查询日志可配合mtail或 ELK 转化为指标进一步丰富监控。部署这套 Prometheus 监控体系后Solr 的查询性能、索引吞吐、JVM 健康、缓存效率以及集群稳定性都将一目了然。任何性能劣化或节点故障都能在第一时间通过告警送达使搜索服务从“黑盒”变为全透明的可观测资产最终保障用户体验与业务连续。