简体   繁体   English

从 Java 客户端连接到 Elastic Cloud

[英]Connect to Elastic Cloud from the Java Client

I've been trying to connect to my ES instance on the Elastic cloud with no success using the Java client and following the Documentation In the hostname I put https://myinstance-xx.europe-west1.gcp.cloud.es.io我一直在尝试使用Java客户连接到我在弹性云上的 ES 实例,但没有成功

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);
}

However, I get an Connection Refused exception但是,我收到拒绝连接异常

java.net.ConnectException: Connection refused

What's the best way to connect to ES on Elastic Cloud through the Java Client?通过Java Client 连接ES on Elastic Cloud 的最佳方式是什么?

  1. Go to cloud.elastic.go/home after your are logged in, find the "Manage this deployment" link.登录后,Go 到 cloud.elastic.go/home,找到“管理此部署”链接。 It should be something like https://cloud.elastic.co/deployments/xxxxxxxx它应该类似于https://cloud.elastic.co/deployments/xxxxxxxx
  2. Under "Applications" you should have an "Elasticsearch" enpoint (click on "Copy endpoint")在“应用程序”下你应该有一个“Elasticsearch”enpoint(点击“复制端点”)
  3. On the API console create an API Key:在 API 控制台创建一个 API Key:
POST /_security/api_key 
{
  "name": "my_key_name",
  "expiration": "15d"
}

That's gonna give you something like:那会给你类似的东西:

{
  "id": "API_KEY_ID",
  "name": "my_key_name",
  "expiration": 1670471984009,
  "api_key": "API_KEY",
  "encoded": "XxxxxxxxXXXXXxxxxxxXXXXxxxxxXXXX=="
}
  1. Now in your Java code:现在在您的 Java 代码中:

     // endpoint you copied String hostname = "youdepname.es.us-west1.gcp.cloud.es.io"; String apiKeyId = "API_KEY_ID"; String apiKeySecret = "API_KEY"; String apiKeyAuth = Base64.getEncoder().encodeToString( (apiKeyId + ":" + apiKeySecret).getBytes(StandardCharsets.UTF_8)); RestClientBuilder builder = RestClient.builder( new HttpHost(hostname, 9243, "https")).setRequestConfigCallback( new RestClientBuilder.RequestConfigCallback() { @Override public RequestConfig.Builder customizeRequestConfig( RequestConfig.Builder requestConfigBuilder) { return requestConfigBuilder.setConnectTimeout(5000).setSocketTimeout(60000); } }); Header[] defaultHeaders = new Header[]{new BasicHeader("Authorization", "ApiKey " + apiKeyAuth)}; builder.setDefaultHeaders(defaultHeaders); RestClient restClient = builder.build(); // Create the transport with a Jackson mapper ElasticsearchTransport transport = new RestClientTransport( restClient, new JacksonJsonpMapper()); // And create the API client ElasticsearchClient client = new ElasticsearchClient(transport);

That should do it.应该这样做。 There are other ways to connect, but this is my preferred way.还有其他连接方式,但这是我的首选方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM