我正在使用Java HighLevelRestClient连接到我在AWS上托管的elasticsearch实例。我可以对邮递员和我的浏览器上的URL提出请求就好了,但是当我使用客户端库时,我会收到
java.net.ConnectException:拒绝连接。
(我目前不需要任何身份验证,因为这是一个小型公共测试实例)。这是我的代码:
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); GetRequest getRequest = new GetRequest("some_index", "some_type","some_id"); final String[] elasticGetResponse = new String[1]; restHighLevelClient.getAsync(getRequest, new ActionListener() { @Override public void onResponse(GetResponse documentFields) { try { elasticGetResponse[0] = restHighLevelClient.get(getRequest).toString(); } catch (IOException e) { e.printStackTrace(); } } @Override public void onFailure(Exception e) { e.printStackTrace(); } });
请让我知道如何解决这个问题...谢谢!
更新:这是我的restClientBuilder的代码:
MySSLHelper sslHelper = new MySSLHelper(SSLConfig.builder() .withKeyStoreProvider(myKeyStoreProvider) .withTrustStoreProvider(InternalTrustStoreProvider.INSTANCE) .build()); RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("MY_ELASTICSEARCH_ENDPOINT")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { @Override public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) { return httpAsyncClientBuilder.setSSLContext(sslHelper.getContext()); } });
答案 0 :(得分:0)
我遇到了同样的问题,并解决了端口和协议的放置问题,如本页所示: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.0/java-rest-high-getting-started-initialization.html
我的代码保持这样:
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(elasticsearchHost, 9200, "http")));
请尝试执行以下操作:
RestClientBuilder restClientBuilder = RestClient.builder(new HttpHost("MY_ELASTICSEARCH_ENDPOINT", "MY_ELASTICSEARCH_PORT", "MY_ELASTICSEARCH_PROTOCOL"))...
希望这会有所帮助。
再见