我有一个使用ElasticSearch API连接到Heroku上的Bonsai的Java应用程序。将我的应用程序部署到Heroku之后,我发现不可能通过TransportClient连接到传统的ElasticSearch地址127.0.0.1:9300

因此,在阅读答案here之一后,我将TransportAddress更改为以下地址。

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("http://juniper-325345373.eu-west-1.bonsaisearch.net/"), 80));

但是现在我得到了java.net.UnknownHostException: http://juniper-325345373.eu-west-1.bonsaisearch.net/: Name or service not known
如何正确定义地址才能连接到Bonsai ElasticSearch?

最佳答案

我在盆景的网站上找到了非常有用的链接。 https://docs.bonsai.io/article/278-java

1.从URI 中提取您的帐户数据

例如https://a1b2c3d4e:[email protected]

String username = a1b2c3d4e;
String password = 5f6g7h8i9;

2.提取主机地址
String host = somehost-1234567.region-x-y.bonsaisearch.net;
String port = 443;

在这里可以找到更多信息:https://docs.bonsai.io/article/94-connecting-to-bonsai

3.实现凭据提供程序
  CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

4.实现 RestHighLevelClient 以连接到盆景
RestHighLevelClient restClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost(host,port,"https"))
           .setHttpClientConfigCallback(httpAsyncClientBuilder ->
    httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
                       .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())));`

07-24 09:33