简体   繁体   English

如何从 Java 应用程序连接到 Heroku 上的 Bonsai ElasticSearch

[英]How to connect to Bonsai ElasticSearch on Heroku from a Java app

I have a Java application that is using ElasticSearch API to connect to Bonsai on Heroku.我有一个使用 ElasticSearch API 连接到 Heroku 上的 Bonsai 的 Java 应用程序。 After deploying my app to Heroku I've found that it will be impossible to connect to a traditional ElasticSearch address 127.0.0.1:9300 via TransportClient.将我的应用程序部署到 Heroku 后,我发现无法通过 TransportClient 连接到传统的 ElasticSearch 地址127.0.0.1:9300

So I've changed TransportAddress to the one below after reading one of the answers here .因此,在阅读此处的其中一个答案后,我将 TransportAddress 更改为以下内容。

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

However now I'm getting a java.net.UnknownHostException: http://juniper-325345373.eu-west-1.bonsaisearch.net/: Name or service not known但是现在我得到一个java.net.UnknownHostException: http://juniper-325345373.eu-west-1.bonsaisearch.net/: Name or service not known

How should I correctly define the address in order to connect to Bonsai ElasticSearch?我应该如何正确定义地址才能连接到 Bonsai ElasticSearch?

I found very userful link on bonsai's web site.我在盆景的网站上找到了非常有用的链接。 https://docs.bonsai.io/article/278-java https://docs.bonsai.io/article/278-java

1. Extract your account data from URI 1. 从 URI 中提取您的帐户数据

eg https://a1b2c3d4e:5f6g7h8i9@somehost-1234567.region-xy.bonsaisearch.net例如https://a1b2c3d4e:5f6g7h8i9@somehost-1234567.region-xy.bonsaisearch.net

String username = a1b2c3d4e;
String password = 5f6g7h8i9;

2. Extract host address 2.提取主机地址

String host = somehost-1234567.region-x-y.bonsaisearch.net;
String port = 443;

more information could be found here: https://docs.bonsai.io/article/94-connecting-to-bonsai更多信息可以在这里找到: https ://docs.bonsai.io/article/94-connecting-to-bonsai

3. Implement credentials provider 3.实施凭证提供者

  CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));

4. Implement RestHighLevelClient to connect to bonsai 4.实现RestHighLevelClient连接盆景

RestHighLevelClient restClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost(host,port,"https"))
           .setHttpClientConfigCallback(httpAsyncClientBuilder -> 
    httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
                       .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy())));`

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

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