『Bug记录』 SpringBoot 如何连接腾讯 Es 服务器 [IOException]

『Bug记录』 SpringBoot 如何连接腾讯 Es 服务器 [IOException],第1张

『Bug记录』 SpringBoot 如何连接腾讯 Es 服务器 [IOException]

文章目录

问题溯源问题排查

问题溯源

最近需要使用 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";

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5700404.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存