环境:jdk11
elasticsearch版本:8.5.2
spring boot版本:2.3.12.RELEASE
官网给的依赖
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</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),然后再启动,获取日志信息
-> Elasticsearch security features have been automatically configured!
-> Authentication is enabled and cluster connections are encrypted.
-> Password for the elastic user (reset with `bin/elasticsearch-reset-password -u elastic`):
lhQpLELkjkrawaBoaz0Q
-> HTTP CA certificate SHA-256 fingerprint:
a52dd93511e8c6045e21f16654b77c9ee0f34aea26d9f40320b531c474676228
...
使用安全的认证的 ElasticsearchClient
String fingerprint = "<certificate fingerprint>";
SSLContext sslContext = TransportUtils
.sslContextFromCaFingerprint(fingerprint);
BasicCredentialsProvider credsProv = new BasicCredentialsProvider();
credsProv.setCredentials(
AuthScope.ANY, new UsernamePasswordCredentials(login, password)
);
RestClient restClient = RestClient
.builder(new HttpHost(host, port, "https"))
.setHttpClientConfigCallback(hc -> hc
.setSSLContext(sslContext)
.setDefaultCredentialsProvider(credsProv)
)
.build();
// Create the transport and the API client
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
ElasticsearchClient client = new ElasticsearchClient(transport);
然后就可以正常创建索引了
总结:有时候想简单的经历一些事情,在没有完全了解的情况下动手,容易掉坑里