当前位置:   article > 正文

BS问题,elasticsearch 8.5.2 建立索引的时候报错 Connection is closed

BS问题,elasticsearch 8.5.2 建立索引的时候报错 Connection is closed

环境:jdk11 

elasticsearch版本:8.5.2

spring boot版本:2.3.12.RELEASE

官网给的依赖 

  1. <dependency>
  2. <groupId>co.elastic.clients</groupId>
  3. <artifactId>elasticsearch-java</artifactId>
  4. <version>8.5.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.fasterxml.jackson.core</groupId>
  8. <artifactId>jackson-databind</artifactId>
  9. <version>2.12.3</version>
  10. </dependency>

window 11 安装的elasticsearch

今天在调试elasticsearch的时候,按照官网的步骤

1、先获取 ElasticsearchClient

2、第一个示例

 这么简单的步骤居然报错了

看了一下控制台日志内容

 received plaintext http traffic on an https channel

 注意到https,好像哪里见到过

按照官网的意思:需要配置安全的https访问才行

To connect to the Elasticsearch cluster you’ll need to configure the Java API Client to use HTTPS with the generated CA certificate in order to make requests successfully.

启动elasticsearch的时候有日志输出,我的不是第一次启动了,所以日志里面没有,我的办法是删除elasticsearch项目文件夹,重新解压压缩文件(elasticsearch-8.5.2-windows-x86_64),然后再启动,获取日志信息

  1. -> Elasticsearch security features have been automatically configured!
  2. -> Authentication is enabled and cluster connections are encrypted.
  3. -> Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
  4. lhQpLELkjkrawaBoaz0Q
  5. -> HTTP CA certificate SHA-256 fingerprint:
  6. a52dd93511e8c6045e21f16654b77c9ee0f34aea26d9f40320b531c474676228
  7. ...

使用安全的认证的 ElasticsearchClient 

  1. String fingerprint = "<certificate fingerprint>";
  2. SSLContext sslContext = TransportUtils
  3. .sslContextFromCaFingerprint(fingerprint);
  4. BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
  5. credsProv.setCredentials(
  6. AuthScope.ANY, new UsernamePasswordCredentials(login, password)
  7. );
  8. RestClient restClient = RestClient
  9. .builder(new HttpHost(host, port, "https"))
  10. .setHttpClientConfigCallback(hc -> hc
  11. .setSSLContext(sslContext)
  12. .setDefaultCredentialsProvider(credsProv)
  13. )
  14. .build();
  15. // Create the transport and the API client
  16. ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
  17. ElasticsearchClient client = new ElasticsearchClient(transport);

 然后就可以正常创建索引了

总结:有时候想简单的经历一些事情,在没有完全了解的情况下动手,容易掉坑里

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/457598
推荐阅读
相关标签
  

闽ICP备14008679号