SpringBoot整合ElasticSearch整合后访问controller接口报错:Unrecognized SSL message,plaintext connection?

后端报错显示:

java.io.IOException: Unrecognized SSL message, plaintext connection?
	at org.elasticsearch.client.RestClient.extractAndWrapCause(RestClient.java:935) ~[elasticsearch-rest-client-8.7.1.jar:8.7.1]
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:300) ~[elasticsearch-rest-client-8.7.1.jar:8.7.1]
	at org.elasticsearch.client.RestClient.performRequest(RestClient.java:288) ~[elasticsearch-rest-client-8.7.1.jar:8.7.1]
	at co.elastic.clients.transport.rest_client.RestClientTransport.performRequest(RestClientTransport.java:153) ~[elasticsearch-java-8.7.1.jar:na]
	at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.exists(ElasticsearchIndicesClient.java:620) ~[elasticsearch-java-8.7.1.jar:na]
	at co.elastic.clients.elasticsearch.indices.ElasticsearchIndicesClient.exists(ElasticsearchIndicesClient.java:636) ~[elasticsearch-java-8.7.1.jar:na]
	at com.es.demo.ESController.initElastic(ESController.java:31) ~[classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:190) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:138) ~[spring-web-5.2.9.RELEASE.jar:5.2.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.9.RELEASE.jar:5.2.9
在连接ES时,如果出现"javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection"错误,可能是由于尝试在使用SSL/TLS协议的端口上建立非加密的连接导致的。为了解决这个问题,你可以按照以下步骤进行操作: 1. 确保你正在使用正确的端口连接ES。默认情况下,ES使用的是9300端口进行节点间通信,而使用9200端口进行REST API访问。如果你正在使用REST API访问ES,请确保你正在使用9200端口。 2. 确保你的连接代码中使用了正确的SSL/TLS配置。如果你的ES集群启用了SSL/TLS加密,你需要在连接代码中配置正确的SSL/TLS证书和密钥。以下是一个示例代码片段,展示了如何使用Java的RestHighLevelClient连接ES并配置SSL/TLS: ```java import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.client.security.user.User; import org.elasticsearch.client.security.user.UserOptions; import org.elasticsearch.client.security.user.UserString; import org.elasticsearch.common.settings.Settings; import javax.net.ssl.SSLContext; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; public class ESConnectionExample { public static void main(String[] args) throws Exception { // 配置SSL/TLS信任所有证书 SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, new TrustManager[]{new X509TrustManager() { public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { } public X509Certificate[] getAcceptedIssuers() { return null; } }}, null); // 配置ES连接 RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "https")) .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setSSLContext(sslContext)); RestHighLevelClient client = new RestHighLevelClient(builder); // 使用client进行操作... // 关闭连接 client.close(); } } ``` 请注意,上述代码中的SSLContext配置了一个信任所有证书的TrustManager,这只是为了演示目的。在实际生产环境中,你应该配置正确的证书和密钥以确保安全性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值