Solr部署:Prometheus和Grafana监控系统完整搭建指南 如果您使用Prometheus 和Grafana 进行指标存储和数据可视化,Solr提供了两种解决方案来收集指标和其他数据:
Prometheus Exporter :功能丰富的导出器,支持自定义配置
带Prometheus格式的Metrics API :原生支持,更高效但配置固化
本文将详细介绍如何配置和使用这两种方案来构建完整的Solr监控系统。
监控方案概述 Prometheus Exporter特性 Prometheus导出器(solr-exporter
)不仅可以监控来自Metrics API的Solr指标,还可以监控:
来自分面查询的分面计数
Collections API命令响应
Ping请求响应
自定义查询结果
Metrics API Prometheus格式 Metrics API提供了Prometheus响应写入器来原生输出Solr指标进行抓取。它更高效,无需运行Prometheus Exporter,但输出格式固定,配置灵活性较低。
Prometheus Exporter详细配置 架构概述 以下图表提供了更详细的架构视图:
1 2 3 4 5 6 7 8 9 10 11 12 ┌─────────────────┐ HTTP请求 ┌─────────────────┐ 指标查询 ┌─────────────────┐ │ Prometheus │ ──────────────► │ solr-exporter │ ──────────────► │ Solr │ │ Server │ │ │ │ Cluster │ └─────────────────┘ └─────────────────┘ └─────────────────┘ │ │ │ 指标数据 │ XML配置 ▼ ▼ ┌─────────────────┐ ┌─────────────────┐ │ Grafana │ │ 配置文件 │ │ Dashboard │ │solr-exporter- │ └─────────────────┘ │ config.xml │ └─────────────────┘
运行Prometheus Exporter的三个关键方面
**修改solr-exporter-config.xml
**定义要收集的数据
从Solr内部启动导出器
修改Prometheus配置 监听正确的端口
启动Exporter 基本启动命令 您可以通过在prometheus-exporter/
目录中运行./bin/solr-exporter
(Linux)或.\bin\solr-exporter.cmd
(Windows)来启动solr-exporter
。
不同操作系统和模式的启动命令 Linux系统 用户管理/单节点模式:
1 2 3 cd prometheus-exporter./bin/solr-exporter -p 9854 -b http://localhost:8983/solr \ --config-file ./conf/solr-exporter-config.xml --num-threads 8
SolrCloud模式:
1 2 3 cd prometheus-exporter./bin/solr-exporter -p 9854 -z localhost:2181/solr \ --config-file ./conf/solr-exporter-config.xml --num-threads 16
Windows系统 用户管理/单节点模式:
1 2 3 cd prometheus-exporter.\bin\solr-exporter.cmd -p 9854 -b http://localhost:8983 /solr ^ --config-file .\conf\solr-exporter-config.xml --num-threads 8
SolrCloud模式:
1 2 3 cd prometheus-exporter.\bin\solr-exporter.cmd -p 9854 -z localhost:2181 /solr ^ --config-file .\conf\solr-exporter-config.xml --num-threads 16
命令行参数详解 基本参数
参数
说明
默认值
-h
, --help
显示命令行帮助和用法
-
-p
, --port
Prometheus监听端口
8989
--scrape-interval
从Solr收集指标的间隔(秒)
60
--num-threads
线程池大小
1
连接参数 -b
, --baseurl
用途 :用户管理集群或单节点安装的Solr基础URL
示例 :http://localhost:8983/solr
注意 :不要与-z
参数同时使用
-z
, --zk-host
用途 :SolrCloud的ZooKeeper连接字符串
示例 :localhost:9983
或 localhost:2181/solr
注意 :不要与-b
参数同时使用
配置参数 --config-file
默认值 :prometheus-exporter/conf/solr-exporter-config.xml
作用 :定义要读取的Solr指标的配置文件路径
--cluster-id
作用 :监控集群的唯一ID,作为所有指标的cluster_id
标签
默认 :使用baseUrl
或zkHost
的哈希值
安全参数 -u
, --credentials
格式 :username:password
示例 :--credentials solr:SolrRocks
--ssl-enabled
作用 :启用mTLS连接到Solr
环境变量要求 :
SOLR_SSL_KEY_STORE
SOLR_SSL_KEY_STORE_PASSWORD
SOLR_SSL_TRUST_STORE
SOLR_SSL_TRUST_STORE_PASSWORD
环境变量选项 JVM配置
环境变量
说明
默认值
JAVA_HEAP
设置初始和最大Java堆大小
512m
JAVA_MEM
自定义Java内存设置
-
GC_TUNE
自定义垃圾收集设置
-XX:+UseG1GC
JAVA_OPTS
额外的JVM选项
-
特殊配置 ZK_CREDS_AND_ACLS
作用 :连接受ACL保护的ZooKeeper主机的凭据
参考 :ZooKeeper访问控制文档
CLASSPATH_PREFIX
作用 :启动solr-exporter
时加载额外库的位置
安全Solr集群的指标获取 安全配置注入 您的SolrCloud安全配置可以使用环境变量注入到solr-exporter
中,类似于使用SolrJ的其他客户端。
安全集群配置示例 假设您有一个由基础认证、SSL和ZooKeeper访问控制保护的SolrCloud实例。
基础认证文件 创建basicauth.properties
文件:
1 2 httpBasicAuthUser =myUser httpBasicAuthPassword =myPassword
启动安全导出器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 cd prometheus-exporterexport JAVA_OPTS="-Djavax.net.ssl.trustStore=truststore.p12 \ -Djavax.net.ssl.trustStorePassword=truststorePassword \ -Dsolr.httpclient.builder.factory=org.apache.solr.client.solrj.impl.PreemptiveBasicAuthClientBuilderFactory \ -Dsolr.httpclient.config=basicauth.properties" export ZK_CREDS_AND_ACLS="-DzkCredentialsProvider=org.apache.solr.common.cloud.VMParamsSingleSetCredentialsDigestZkCredentialsProvider \ -DzkDigestUsername=readonly-user \ -DzkDigestPassword=zkUserPassword" export CLASSPATH_PREFIX="../server/solr-webapp/webapp/WEB-INF/lib/commons-codec-1.11.jar" ./bin/solr-exporter -p 9854 -z zk1:2181,zk2:2181,zk3:2181 \ --config-file ./conf/solr-exporter-config.xml --num-threads 16
注意 :导出器需要commons-codec
库用于SSL/BasicAuth,但没有自带。示例从Solr Web应用程序重用它。
Exporter配置详解 配置文件结构 solr-exporter
的配置定义了从Solr获取的数据。这包括指标,也可以包括对PingRequestHandler、Collections API的查询,以及对任何查询请求处理程序的查询。
默认配置示例 默认示例配置位于prometheus-exporter/conf/solr-exporter-config.xml
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 <config > <rules > <ping > <lst name ="request" > <lst name ="query" > <str name ="path" > /admin/ping</str > </lst > <arr name ="jsonQueries" > <str > . as $object | $object | (if $object.status == "OK" then 1.0 else 0.0 end) as $value | { name : "solr_ping", type : "GAUGE", help : "See following URL: https://solr.apache.org/guide/solr/latest/deployment-guide/ping.html", label_names : [], label_values : [], value : $value } </str > </arr > </lst > </ping > <metrics > <lst name ="request" > <lst name ="query" > <str name ="path" > /admin/metrics</str > <lst name ="params" > <str name ="group" > all</str > <str name ="type" > all</str > <str name ="prefix" > </str > <str name ="property" > </str > </lst > </lst > <arr name ="jsonQueries" > <str > .metrics["solr.jetty"] | to_entries | .[] | select(.key | startswith("org.eclipse.jetty.server.handler.DefaultHandler")) | select(.key | endswith("xx-responses")) as $object | $object.key | split(".") | last | split("-") | first as $status | $object.value.count as $value | { name : "solr_metrics_jetty_response_total", type : "COUNTER", help : "See following URL: https://solr.apache.org/guide/solr/latest/deployment-guide/metrics-reporting.html", label_names : ["status"], label_values : [$status], value : $value } </str > </arr > </lst > </metrics > <collections > <lst name ="request" > <lst name ="query" > <str name ="path" > /admin/collections</str > <lst name ="params" > <str name ="action" > CLUSTERSTATUS</str > </lst > </lst > <arr name ="jsonQueries" > <str > .cluster.live_nodes | length as $value | { name : "solr_collections_live_nodes", type : "GAUGE", help : "See following URL: https://solr.apache.org/guide/solr/latest/deployment-guide/cluster-node-management.html#clusterstatus", label_names : [], label_values : [], value : $value } </str > </arr > </lst > </collections > <search > <lst name ="request" > <lst name ="query" > <str name ="collection" > collection1</str > <str name ="path" > /select</str > <lst name ="params" > <str name ="q" > *:*</str > <str name ="start" > 0</str > <str name ="rows" > 0</str > <str name ="json.facet" > { category: { type: terms, field: cat } } </str > </lst > </lst > <arr name ="jsonQueries" > <str > .facets.category.buckets[] as $object | $object.val as $term | $object.count as $value | { name : "solr_facets_category", type : "GAUGE", help : "Category facets", label_names : ["term"], label_values : [$term], value : $value } </str > </arr > </lst > </search > </rules > </config >
配置标签和元素 基本结构 1 2 3 4 5 <config > <rules > </rules > </config >
请求类型
请求类型
说明
<ping>
抓取Ping请求的响应
<metrics>
抓取Metrics API请求的响应
<collections>
抓取Collections API请求的响应
<search>
抓取查询请求的响应
查询配置属性 collection
可选 :仅用于SolrCloud集群
作用 :发出查询的集合
core
可选 :仅用于用户管理集群或单节点安装
作用 :发出查询的核心
path
作用 :请求发送到的查询端点路径
示例 :admin/metrics
、/select
、admin/collections
params
作用 :额外的查询参数
示例 :Metrics端点的group和prefix参数
JSON查询配置 <jsonQueries>
是一个数组,定义了一个或多个jq语法的JSON查询。有关如何构建这些查询的详细信息,请参见jq用户手册 。
jq查询输出格式:
1 2 3 4 5 6 7 8 { "name" : "solr_ping" , "type" : "GAUGE" , "help" : "See following URL: https://solr.apache.org/guide/solr/latest/deployment-guide/ping.html" , "label_names" : [ "base_url" , "core" ] , "label_values" : [ "http://localhost:8983/solr" , "collection1" ] , "value" : 1.0 }
输出格式 Prometheus格式转换 solr-exporter
将JSON转换为以下输出格式:
1 2 3 # TYPE <name> <type> # HELP <name> <help> <name>{<label_names[0]>=<label_values[0]>,<label_names[1]>=<label_values[1]>,...} <value>
参数说明
参数
说明
name
指标名称,遵循Prometheus命名最佳实践
type
指标类型:COUNTER
、GAUGE
、SUMMARY
、HISTOGRAM
或UNTYPED
help
指标帮助文本
label_names
指标标签名称
label_values
指标标签值
value
指标值(必须是Double类型)
转换示例 JSON输入:
1 2 3 4 5 6 7 8 { "name" : "solr_ping" , "type" : "GAUGE" , "help" : "See following URL: https://solr.apache.org/guide/solr/latest/deployment-guide/ping.html" , "label_names" : [ "base_url" , "core" ] , "label_values" : [ "http://localhost:8983/solr" , "collection1" ] , "value" : 1.0 }
Prometheus输出:
1 2 3 # TYPE solr_ping gauge # HELP solr_ping See following URL: https://solr.apache.org/guide/solr/latest/deployment-guide/ping.html solr_ping{base_url="http://localhost:8983/solr",core="collection1"} 1.0
Prometheus配置 Prometheus服务器设置 Prometheus是一个需要单独下载和部署的独立服务器。更多信息可以在Prometheus入门指南 页面找到。
配置Prometheus抓取Solr指标 为了让Prometheus知道solr-exporter
的存在,必须将监听地址添加到Prometheus服务器的prometheus.yml
配置文件中:
1 2 3 4 scrape_configs: - job_name: 'solr' static_configs: - targets: ['localhost:9854' ]
高级Prometheus配置 多个Exporter实例 1 2 3 4 5 6 7 8 9 10 scrape_configs: - job_name: 'solr-cluster-1' static_configs: - targets: ['solr1:9854' , 'solr2:9854' , 'solr3:9854' ] scrape_interval: 30s - job_name: 'solr-cluster-2' static_configs: - targets: ['solr4:9854' , 'solr5:9854' , 'solr6:9854' ] scrape_interval: 30s
带标签的配置 1 2 3 4 5 6 7 scrape_configs: - job_name: 'solr' static_configs: - targets: ['localhost:9854' ] labels: environment: 'production' cluster: 'main'
验证配置 应用设置到Prometheus后,它将开始从solr-exporter
拉取Solr的指标。您可以通过浏览http://localhost:9090
并在Prometheus GUI中查询solr_ping
指标来测试Prometheus服务器、solr-exporter
和Solr是否协同工作。
Grafana仪表板配置 Grafana部署 要使用Grafana进行可视化,必须单独下载和部署。更多信息可以在Grafana文档 网站找到。Grafana从许多来源消费数据,包括您之前设置的Prometheus服务器。
示例仪表板 导入预构建仪表板 Grafana示例仪表板在以下JSON文件中提供:prometheus-exporter/conf/grafana-solr-dashboard.json
。您可以将此文件与其他Grafana仪表板配置一起放置,并根据对solr-exporter
配置所做的任何自定义进行必要修改。
**快速导入:**您可以通过使用仪表板ID 12456
的导入功能,直接通过grafana.com 导入Solr仪表板。
自定义仪表板示例 核心指标面板配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 { "title" : "Solr Query Rate" , "type" : "graph" , "targets" : [ { "expr" : "rate(solr_metrics_core_requests_total[5m])" , "legendFormat" : "{{core}} - {{handler}}" } ] , "yAxes" : [ { "label" : "Queries per second" , "min" : 0 } ] }
内存使用面板:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 { "title" : "JVM Memory Usage" , "type" : "graph" , "targets" : [ { "expr" : "solr_metrics_jvm_memory_heap_used" , "legendFormat" : "{{instance}} - Heap Used" } , { "expr" : "solr_metrics_jvm_memory_heap_max" , "legendFormat" : "{{instance}} - Heap Max" } ] }
使用Metrics API的Prometheus格式 原生Prometheus指标 Prometheus指标可直接从Solr通过Metrics API获得,通过设置wt
参数为prometheus
:
1 http://localhost:8983/solr/admin/metrics?wt=prometheus
API特性
效率更高 :无需运行独立的exporter
固定输出 :不提供配置能力
原生集成 :直接从Solr获取指标
Prometheus配置(Metrics API) 直接抓取配置 与Prometheus Exporter类似,需要配置prometheus.yml
让Prometheus服务器摄取指标。区别是必须直接从运行Solr的每个主机/端口抓取带有wt=prometheus
参数的Metrics API:
1 2 3 4 5 6 7 scrape_configs: - job_name: 'solr' metrics_path: "/solr/admin/metrics" static_configs: - targets: ['localhost:8983' , 'localhost:7574' ] params: wt: ['prometheus' ]
高级抓取配置 1 2 3 4 5 6 7 8 9 10 11 scrape_configs: - job_name: 'solr-metrics' metrics_path: "/solr/admin/metrics" static_configs: - targets: ['solr1:8983' , 'solr2:8983' , 'solr3:8983' ] params: wt: ['prometheus' ] group: ['jvm' , 'jetty' , 'core' ] type: ['counter' , 'gauge' ] scrape_interval: 15s scrape_timeout: 10s
生产环境最佳实践 1. 性能优化 Exporter性能调优 1 2 3 4 5 6 7 8 9 --num-threads 16 --scrape-interval 30 export JAVA_HEAP="1g" export GC_TUNE="-XX:+UseG1GC -XX:MaxGCPauseMillis=200"
Prometheus配置优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 global: scrape_interval: 15s evaluation_interval: 15s external_labels: monitor: 'solr-monitor' rule_files: - "solr_rules.yml" scrape_configs: - job_name: 'solr' scrape_interval: 30s scrape_timeout: 10s static_configs: - targets: ['localhost:9854' ]
2. 监控告警规则 Prometheus告警规则 创建solr_rules.yml
文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 groups: - name: solr rules: - alert: SolrDown expr: solr_ping == 0 for: 1m labels: severity: critical annotations: summary: "Solr instance is down" description: "Solr instance {{ $labels.instance }} has been down for more than 1 minute." - alert: SolrHighMemoryUsage expr: (solr_metrics_jvm_memory_heap_used / solr_metrics_jvm_memory_heap_max) > 0.85 for: 5m labels: severity: warning annotations: summary: "Solr high memory usage" description: "Solr instance {{ $labels.instance }} memory usage is above 85% for more than 5 minutes." - alert: SolrSlowQueries expr: rate(solr_metrics_core_requests_time_total[5m]) / rate(solr_metrics_core_requests_total[5m]) > 1000 for: 2m labels: severity: warning annotations: summary: "Solr slow queries detected" description: "Average query time on {{ $labels.instance }} /{{ $labels.core }} is above 1000ms."
3. 高可用配置 多Exporter部署 1 2 3 4 5 6 7 ./bin/solr-exporter -p 9854 -z zk1:2181,zk2:2181,zk3:2181 \ --cluster-id main-cluster ./bin/solr-exporter -p 9855 -z zk1:2181,zk2:2181,zk3:2181 \ --cluster-id main-cluster-backup
Prometheus高可用 1 2 3 4 5 6 7 8 9 global: external_labels: replica: prometheus-1 scrape_configs: - job_name: 'solr' static_configs: - targets: ['exporter1:9854' , 'exporter2:9855' ]
4. 安全配置 HTTPS和认证 1 2 3 4 5 6 export JAVA_OPTS="-Djavax.net.ssl.trustStore=/path/to/truststore.p12 \ -Djavax.net.ssl.trustStorePassword=password" ./bin/solr-exporter -p 9854 --ssl-enabled \ -u admin:password -z zk1:2181,zk2:2181,zk3:2181
网络访问控制 1 2 3 4 5 6 7 8 scrape_configs: - job_name: 'solr' static_configs: - targets: ['localhost:9854' ] basic_auth: username: monitor password: monitor_password
5. 存储和保留策略 Prometheus存储配置 1 2 3 4 5 6 ./prometheus \ --config.file=prometheus.yml \ --storage.tsdb.path=./data \ --storage.tsdb.retention.time=30d \ --storage.tsdb.retention.size=10GB
数据备份 1 2 3 4 #!/bin/bash DATE=$(date +%Y%m%d_%H%M%S) tar -czf prometheus-data-${DATE} .tar.gz /path/to/prometheus/data/
故障排除指南 常见问题及解决方案 1. Exporter连接失败 问题症状:
1 ERROR: Failed to connect to Solr at http://localhost:8983/solr
解决步骤:
1 2 3 4 5 6 7 8 curl http://localhost:8983/solr/admin/info/system sudo iptables -L | grep 8983telnet localhost 8983
2. 指标缺失 **问题症状:**Prometheus中看不到某些Solr指标
排查步骤:
1 2 3 4 5 6 7 8 curl http://localhost:9854/metrics xmllint --format ./conf/solr-exporter-config.xml ./bin/solr-exporter --help
3. 性能问题 **问题症状:**导出器响应慢或资源占用高
优化措施:
1 2 3 4 5 6 7 8 9 --num-threads 32 --scrape-interval 60 export JAVA_HEAP="2g" export JAVA_OPTS="-XX:+UseG1GC -XX:MaxGCPauseMillis=200"
监控诊断工具 健康检查脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #!/bin/bash echo "=== Solr Monitoring Health Check ===" echo "1. Checking Solr..." curl -f http://localhost:8983/solr/admin/ping || echo "❌ Solr is down" echo "2. Checking Solr Exporter..." curl -f http://localhost:9854/metrics > /dev/null || echo "❌ Exporter is down" echo "3. Checking Prometheus..." curl -f http://localhost:9090/-/healthy || echo "❌ Prometheus is down" echo "4. Checking Grafana..." curl -f http://localhost:3000/api/health || echo "❌ Grafana is down" echo "=== Health Check Complete ==="
总结 通过本指南,您应该能够成功搭建基于Prometheus和Grafana的完整Solr监控系统。关键要点包括:
选择合适的方案 :Prometheus Exporter提供更多灵活性,Metrics API更简单高效
正确配置安全 :在生产环境中启用SSL和认证
优化性能 :调整线程数、抓取间隔和JVM参数
设置告警 :配置关键指标的监控告警
定期维护 :监控系统本身也需要监控和维护
建议在测试环境中充分验证配置后再部署到生产环境,并建立定期的监控系统健康检查流程。