我一直在尝试使用Java客户端连接到弹性云上的ES示例,但没有成功。在主机名中,我输入了https://myinstance-xx.europe-west1.gcp.cloud.es.io
private String hostName = "https://myinstance-xx.es.europe-west1.gcp.cloud.es.io";
private String username = "username";
private String password = "pass";
@Bean
public ElasticsearchClient client() {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(username, password));
RestClientBuilder builder = RestClient.builder(new HttpHost(hostName, 9200))
.setHttpClientConfigCallback(httpClientBuilder ->
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
// Create the low-level client
RestClient restClient = builder.build();
// Create the transport with a Jackson mapper
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
// And create the API client
return new ElasticsearchClient(transport);
}
但是,我收到“连接被拒绝”异常
java.net.ConnectException: Connection refused
通过Java客户端连接到Elastic Cloud上的ES的最佳方式是什么?
1条答案
按热度按时间pzfprimi1#
1.登录后转到cloud.elastic.go/home,找到“管理此部署”链接。它应该类似于
https://cloud.elastic.co/deployments/xxxxxxxx
1.在“应用程序”下,您应该有一个“ElasticSearch”端点(单击“复制端点”)
1.在API控制台上创建API密钥:
这会给予你一些类似的东西:
1.现在在Java代码中:
应该可以了。还有其他的连接方式,但这是我的首选方式。