我正在尝试使用SolrJ API使用以下代码连接SolrCloud:
String zkHostString = "localhost:9983";
String USER = "solr";
String PASSWORD = "SolrRocks";
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER, PASSWORD));
CloseableHttpClient httpClient = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
CloudSolrClient solr = new CloudSolrClient.Builder().withZkHost(zkHostString).withHttpClient(httpClient).build();
((CloudSolrClient)solr).setDefaultCollection("gettingstarted");
但是将错误视为:
线程中的异常" main" org.apache.solr.client.solrj.impl.CloudSolrClient $ RouteException:在与http://192.168.0.104:8983/solr/gettingstarted_shard2_replica1服务器通信时发生IOException at org.apache.solr.client.solrj.impl.CloudSolrClient.directUpdate(CloudSolrClient.java:767) 在org.apache.solr.client.solrj.impl.CloudSolrClient.sendRequest(CloudSolrClient.java:1173) at org.apache.solr.client.solrj.impl.CloudSolrClient.requestWithRetryOnStaleState(CloudSolrClient.java:1062) at org.apache.solr.client.solrj.impl.CloudSolrClient.request(CloudSolrClient.java:1004) 在org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:149) 在org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:173) 在org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:190) 在com.app.graphiti.TextParser.main(TextParser.java:92) 引起:org.apache.solr.client.solrj.SolrServerException:在与http://192.168.0.104:8983/solr/gettingstarted_shard2_replica1服务器通信时发生IOException at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:607) 在org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:262) 在org.apache.solr.client.solrj.impl.HttpSolrClient.request(HttpSolrClient.java:251) at org.apache.solr.client.solrj.impl.LBHttpSolrClient.doRequest(LBHttpSolrClient.java:435) at org.apache.solr.client.solrj.impl.LBHttpSolrClient.request(LBHttpSolrClient.java:387) at org.apache.solr.client.solrj.impl.CloudSolrClient.lambda $ directUpdate $ 0(CloudSolrClient.java:742) at java.util.concurrent.FutureTask.run(Unknown Source) at org.apache.solr.common.util.ExecutorUtil $ MDCAwareThreadPoolExecutor.lambda $ execute $ 0(ExecutorUtil.java:229) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source) 在java.lang.Thread.run(未知来源) 引起:org.apache.http.client.ClientProtocolException 在org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186) 在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82) 在org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55) at org.apache.solr.client.solrj.impl.HttpSolrClient.executeMethod(HttpSolrClient.java:498) ......还有10个 引起:org.apache.http.client.NonRepeatableRequestException:无法使用不可重复的请求实体重试请求。 在org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:225) 在org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) 在org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) 在org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110) 在org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184) ......还有13个 16:55:40.289 [main-SendThread(0:0:0:0:0:0:0:1:9983)] DEBUG org.apache.zookeeper.ClientCnxn - 获得sessionid的ping响应:1ms后为0x15a3bc76e1f000e 16:55:43.624 [main-SendThread(0:0:0:0:0:0:0:1:9983)] DEBUG org.apache.zookeeper.ClientCnxn - 获得sessionid的ping响应:1ms后为0x15a3bc76e1f000e 16:55:46.958 [main-SendThread(0:0:0:0:0:0:0:1:9983)] DEBUG org.apache.zookeeper.ClientCnxn - 获得sessionid的ping响应:1ms后为0x15a3bc76e1f000e \
请帮忙。 Vrinda Davda
答案 0 :(得分:1)
我相信这里发生的事情是SolrJ正在使用POST来添加您的文档,而不是在进行抢占式身份验证。以前这样比较容易,但现在似乎要求您使用HttpClientBuilder
配置CredentialsProvider
和HttpRequestInterceptor
,在必要时更新{新请求{1}}和TARGET_AUTH_STATE
的请求上下文的{1}}。
我一天大部分时间都在和自己斗争,直到我发现 Brian Teggart 2015年3月12日发布在此主题中:
Preemptive Basic authentication with Apache HttpClient 4
这对我使用Solr 6.4.1服务器和SolrJ 5.3.1客户端。
答案 1 :(得分:0)
我找到了一种更简单的方法。
您可以添加这样的请求拦截器,因此您不必担心自己创建正确配置的HttpClient实例。这只会将拦截器添加到Solrj创建的默认HttpClient中。
org.apache.solr.client.solrj.impl.HttpClientUtil.addRequestInterceptor(new SolrPreemptiveAuthInterceptor());
RequestInterceptor看起来像这样:
public class SolrPreemptiveAuthInterceptor implements HttpRequestInterceptor {
final static Logger log = LoggerFactory.getLogger(SolrPreemptiveAuthInterceptor.class);
@Override
public void process(HttpRequest request, HttpContext context) throws HttpException, IOException {
AuthState authState = (AuthState) context.getAttribute(HttpClientContext.TARGET_AUTH_STATE);
// If no auth scheme available yet, try to initialize it preemptively
if (authState.getAuthScheme() == null) {
log.info("No AuthState: set Basic Auth");
HttpHost targetHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST);
AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(HttpClientContext.CREDS_PROVIDER);
Credentials creds = credsProvider.getCredentials(authScope);
if(creds == null){
log.info("No Basic Auth credentials: add them");
creds = getCredentials(authScope);
}
authState.update(new BasicScheme(), creds);
}
}
private Credentials getCredentials(AuthScope authScope) {
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, password);
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(authScope, creds);
log.info("Creating Basic Auth credentials for user {}", user);
return credsProvider.getCredentials(authScope);
}
}
答案 2 :(得分:0)
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user, password);
provider.setCredentials(AuthScope.ANY, credentials);
HttpClient client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider).build();
solrClient = new HttpSolrClient.Builder().withBaseSolrUrl(endpoint).withHttpClient(client).build();