使用 Solr 6.3.0
,云模式,3 个外部 zookeeper
作为集群,并使用 solrJ
作为客户端。
A:无需身份验证
在启用身份验证
之前,我使用以下代码添加/更新文档:
CloudSolrClient client = cloudClientBuilder.build();
UpdateResponse resp = client.add(doc, 5000);
client.commit();
client.close();
return resp;
效果很好,新文档立即出现在搜索结果中。
B:启用身份验证
然后我启用了基本身份验证插件
和基于规则的授权插件
(以及SSL
,如果有的话)。
为了设置凭据信息,代码重构如下:
// create request,
UpdateRequest req = new UpdateRequest();
// add doc to request,
req.add(doc, 5000);
// set credential,
req.setBasicAuthCredentials(user, password);
// create client,
CloudSolrClient client = cloudClientBuilder.build();
client.setDefaultCollection(ConfigUtil.getProp(ConfigUtil.KEY_SOLR_CORE));
// do request & get response,
UpdateResponse resp = req.process(client);
client.commit();
client.close();
然后它会得到类似这样的错误:
Error 401 require authentication, require authentication.
调试时,错误发生在client.commit();
行。
尝试使用curl
我使用curl
进行更新:
curl -k --user solr:password "https://localhost:8983/solr/corexxx/update?wt=json&indent=true&commit=true" -d '[{"id":"20041", "name":"xxx 41", "location":"xxx", "description":"xxx"}]'
提交成功!并且,更新会立即在搜索结果中可见。
我的猜测
由于 curl
运行良好,我猜 solr cloud 本身也运行良好。
因此,该问题是由于 B 中基于 SolrJ 的代码引起的。
问题:
- 为什么代码 B 出现
HTTP 401
错误?我该如何修复它? - 我可以使用 A 中的代码,并且仍然能够提供凭据信息吗?如果可以,那么如何提供?
谢谢。
最佳答案
您应该将 client.commit()
更改为 req.commit(client, ConfigUtil.getProp(ConfigUtil.KEY_SOLR_CORE))
,凭据在 UpdateRequest 中设置。
关于SolrJ - 启用身份验证插件时无法提交更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42055709/