引入SolrJ依赖后生产环境Solr连接失败及健康检查报错排查
问题描述
我通过SolrJ连接Solr,POM依赖配置如下:
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> </dependency>
本地环境连接localhost时正常,业务代码如下:
public class SolrJSearcher { public String solrSearch() throws SolrServerException, IOException { String collection = "Users"; SolrClient client = new HttpSolrClient.Builder("http://localhost:8983/solr/" + collection).build(); SolrQuery query = new SolrQuery(); query.setQuery("*:*"); query.setRows(1); QueryResponse response = client.query(query); SolrDocumentList results = response.getResults(); String returnValue = JSONUtil.toJSON(results); return returnValue; } }
但将主机名改为生产环境地址后无法连接,更奇怪的是,即使删除上述业务方法只保留POM依赖,部署时仍出现如下报错:
2022-11-21 17:09:31.013+0100 WARN [org.springframework.boot.actuate.solr.SolrHealthIndicator] (http-nio-8080-exec-1) [anonymousUser/NONE/d145ccb7-4dfe-4acd-8b6a-b924900d621d] Solr health check failed org.apache.solr.client.solrj.SolrServerException: Server refused connection at: http://127.0.0.1:8983/solr at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:672) at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:265) at org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:248) at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:211) at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:228) at org.springframework.boot.actuate.solr.SolrHealthIndicator$RootStatusCheck.getStatus(SolrHealthIndicator.java:115) at org.springframework.boot.actuate.solr.SolrHealthIndicator.initializeStatusCheck(SolrHealthIndicator.java:78) at org.springframework.boot.actuate.solr.SolrHealthIndicator.initializeStatusCheck(SolrHealthIndicator.java:66) at org.springframework.boot.actuate.solr.SolrHealthIndicator.doHealthCheck(SolrHealthIndicator.java:53) at org.springframework.boot.actuate.health.AbstractHealthIndicator.health(AbstractHealthIndicator.java:82) at org.springframework.boot.actuate.health.HealthIndicator.getHealth(HealthIndicator.java:37) at org.springframework.boot.actuate.health.HealthEndpointWebExtension.getHealth(HealthEndpointWebExtension.java:85) at org.springframework.boot.actuate.health.HealthEndpointWebExtension.getHealth(HealthEndpointWebExtension.java:44) at org.springframework.boot.actuate.health.HealthEndpointSupport.getContribution(HealthEndpointSupport.java:99) at org.springframework.boot.actuate.health.HealthEndpointSupport.getAggregateHealth(HealthEndpointSupport.java:110) at org.springframework.boot.actuate.health.HealthEndpointSupport.getContribution(HealthEndpointSupport.java:96) at org.springframework.boot.actuate.health.HealthEndpointSupport.getHealth(HealthEndpointSupport.java:74) at org.springframework.boot.actuate.health.HealthEndpointSupport.getHealth(HealthEndpointSupport.java:61) at org.springframework.boot.actuate.health.HealthEndpointWebExtension.health(HealthEndpointWebExtension.java:71) at org.springframework.boot.actuate.health.HealthEndpointWebExtension.health(HealthEndpointWebExtension.java:60) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:282) at org.springframework.boot.actuate.endpoint.invoke.reflect.ReflectiveOperationInvoker.invoke(ReflectiveOperationInvoker.java:77) at org.springframework.boot.actuate.endpoint.annotation.AbstractDiscoveredOperation.invoke(AbstractDiscoveredOperation.java:60) at
请问该问题的原因是什么?是否需要配置以阻止自动连接localhost?
问题原因与解决方案
原因分析
报错信息里的SolrHealthIndicator是Spring Boot Actuator的健康检查组件。当项目引入solr-solrj依赖且启用了Actuator(默认引入Actuator starter即启用),Spring Boot会自动配置该组件,它默认尝试连接http://127.0.0.1:8983/solr做健康检查——这个逻辑和你自己业务代码里的SolrClient实例无关,所以即使删除业务代码,依然会触发报错。
解决方案
有两种处理方式:
1. 配置正确的Solr地址供健康检查使用
在配置文件(如application.properties或application.yml)中指定生产环境的Solr地址,覆盖默认值:
# application.properties spring.data.solr.host=http://你的生产环境Solr地址:端口/solr
YAML格式:
# application.yml spring: data: solr: host: http://你的生产环境Solr地址:端口/solr
配置后,Actuator的健康检查会使用这个地址,同时你也可以在业务代码里读取该配置,避免硬编码:
@Value("${spring.data.solr.host}") private String solrHost; public String solrSearch() throws SolrServerException, IOException { String collection = "Users"; SolrClient client = new HttpSolrClient.Builder(solrHost + "/" + collection).build(); // 后续业务逻辑不变 }
2. 禁用Solr健康检查
如果项目不需要Solr的健康检查,可以直接关闭该功能:
# application.properties management.health.solr.enabled=false
YAML格式:
# application.yml management: health: solr: enabled: false
内容的提问来源于stack exchange,提问作者QueryQuasar
相关产品推荐
相关产品推荐

