![『Bug记录』 SpringBoot 如何连接腾讯 Es 服务器 [IOException],第1张 『Bug记录』 SpringBoot 如何连接腾讯 Es 服务器 [IOException],第1张](/aiimages/%E3%80%8EBug%E8%AE%B0%E5%BD%95%E3%80%8F+SpringBoot+%E5%A6%82%E4%BD%95%E8%BF%9E%E6%8E%A5%E8%85%BE%E8%AE%AF+Es+%E6%9C%8D%E5%8A%A1%E5%99%A8+%5BIOException%5D.png)
最近需要使用 ES 服务器,就买了一台。根据开发文档描述,发现咋都连接失败。
package com.sugar.es;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
public class indexCreate {
private final static String host = "https://es-1eq9og57.public.tencentelasticsearch.com";
private final static int port = 9200;
private final static String scheme = "https";
private final static String UserName = "elastic";
private final static String PassWord = "密码@";
public static void main(String[] args) throws Exception{
// 认证
BasicCredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(UserName,PassWord));
// 创建客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost(host,port,scheme)
).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
httpAsyncClientBuilder.disableAuthCaching();
return httpAsyncClientBuilder.setDefaultCredentialsProvider(provider);
}
})
);
// 创建索引
CreateIndexRequest request = new CreateIndexRequest("role2");
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println("索引创建状态" + response.isAcknowledged());
client.close();
}
}
问题排查
原来已经配置了 scheme 为 https,host 就无需带上,改成如下即可。
private final static String host = "hes-1eq9og57.public.tencentelasticsearch.com";
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)