记一次使用RestHighLevelClient连接ElasticSearch 7.12.0 https 域名遇到的坑

先贴代码

    @Bean
    @Override
    public RestHighLevelClient elasticsearchClient() {
        //连接 HTTPS 协议 使用 账号密码验证
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(Constant.ES_USERNAME, Constant.ES_PASSWORD));
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        //使用 new 方式生成 HttpHost 只能生成 HTTP 协议的连接,即使 传入 'https://xxx' 生成的结果也会变成 'http://https://xxx'
                        //new HttpHost(Constant.ES_HOST);
                        HttpHost.create(Constant.ES_HOST)
                ).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        httpClientBuilder.disableAuthCaching();
                        return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                    }
                })
        );

        //   原生连接本地
        /*RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
        return client;*/
    }

由于运维方提供的服务器域名地址为 https 协议,没有留意,导致代码测试时,遇到如下情况

  • postman 测试直连 es 进行 CRUD 返回结果一切正常
  • 后端运行代码后,查询正常,但是增删改报出异常 308 ,以及无法解析返回响应体 ElasticsearchStatusException[Unable to parse response body]
  • postman 中把 https 改为 http 依旧可以正常操作,但是后端依旧报一样的错
posted @ 2021-06-25 17:56  德华。  阅读(1812)  评论(0编辑  收藏  举报