springboot集成elasticsearch6.8.23设置密码xpack连接,及遇到的None of the configured nodes are available
遇到一個 None of the configured nodes are available 的坑
一、背景:
因現網掃描出來幾個漏洞,目前版本使用的springboot2.1.17.RELEASE + elasticsearch6.4.3。
所以需要改造升級:
1、把es升級為elasticsearch6.8.23;
2、給es添加密碼;
二、查詢資料:
1、ElasticSearch6.8.13解決Log4j CVE-2021-44228漏洞_wwnaitang的博客-CSDN博客
2、springboot集成elasticsearch6.81設置密碼xpack連接_小棟喲的博客-CSDN博客_springboot配置es密碼
3、運維(14) docker-compose部署Elasticsearch并設置賬號密碼_鄭清的博客-CSDN博客
三、環境:
1、使用docker-compose部署elasticsearch6.8.23
docker-compose.yml文件
version: '3' services:elasticsearch:image: docker.io/elasticsearch:6.8.23 container_name: elasticsearchrestart: alwaysenvironment:- cluster.name=elasticsearch-cluster- bootstrap.memory_lock=true- http.cors.enabled=true- http.cors.allow-origin=*- discovery.type=single-node- "ES_JAVA_OPTS=-Xms512m -Xmx512m"volumes:- /data/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml- /data/elasticsearch/data:/usr/share/elasticsearch/data- /data/elasticsearch/logs:/user/share/elasticsearch/logsports:- 9200:9200- 9300:9300elasticsearch.yml文件(很多報None of the configured nodes are available的坑就在這里,結尾特殊說明)
cluster.name: "elasticsearch-cluster" network.host: 0.0.0.0 http.port: 9200 http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-headers: Authorization xpack.security.enabled: true xpack.security.transport.ssl.enabled: truedocker-compose啟動elasticsearch會報錯,因為沒加權限,所以加下elasticsearch文件夾的權限
chmod 777 -R elasticsearch/elasticsearch6.8.23部署成功,開始設置密碼
# 進入容器 docker exec -it elasticsearch /bin/bash# 設置密碼-隨機生成密碼 # elasticsearch-setup-passwords auto # 設置密碼-手動設置密碼 elasticsearch-setup-passwords interactive# 選擇Y 一直輸入123456(當然自定義了)# 最后測試訪問 curl 127.0.0.1:9200 -u elastic:123456# 修改elastic密碼為123456 (注:執行命令時會讓認證之前賬號密碼) curl -H "Content-Type:application/json" -XPOST -u elastic 'http://127.0.0.1:9200/_xpack/security/user/elastic/_password' -d '{ "password" : "123456" }'四、代碼
1、Spring Data Elasticsearch有版本要求,參考?Spring Data Elasticsearch - Reference Documentation
因此升級elasticsearch6.8.23時順帶把springboot也升級為了2.2.13.RELEASE
2、升級maven包
<properties><java.version>1.8</java.version><elasticsearch.version>6.8.23</elasticsearch.version><spring-boot.version>2.2.13.RELEASE</spring-boot.version></properties><dependencyManagement><dependencies><!--把es版本管理起來,因為springboot2.2.13中使用的es是6.8.13--><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>${elasticsearch.version}</version></dependency><dependency><groupId>org.elasticsearch.plugin</groupId><artifactId>transport-netty4-client</artifactId><version>${elasticsearch.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>transport</artifactId><version>${elasticsearch.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>x-pack-transport</artifactId><version>${elasticsearch.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-client</artifactId><version>${elasticsearch.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>${elasticsearch.version}</version></dependency></dependencies></dependencyManagement><dependencies><!-- es相關 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId><exclusions><exclusion><groupId>org.elasticsearch.client</groupId><artifactId>transport</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.elasticsearch.plugin</groupId><artifactId>transport-netty4-client</artifactId></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>transport</artifactId></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>x-pack-transport</artifactId></dependency></dependencies>3、配置
刪除原來的elasticsearch配置 spring:data:elasticsearch:cluster-name: elasticsearch-clustercluster-nodes: 10.10.10.10:9300,11.11.11.11:9300repositories:enabled: true新增自定義elasticsearch配置 elasticsearch:cluster-nodes: 10.10.10.10:9300,11.11.11.11:9300cluster-name: elasticsearch-clustercluster-password: elastic:1234564、ElasticsearchConfig.class代碼
import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import java.net.InetAddress; import java.net.UnknownHostException;/*** Created by feng on 2022/9/2.*/ @Slf4j @Configuration @ConfigurationProperties(prefix = "elasticsearch") @Data public class ElasticsearchConfig {private String clusterName;private String clusterNodes;private String clusterPassword;/*** elasticsearch客戶端注入(配置)** @return* @throws UnknownHostException*/@Beanpublic TransportClient transportClient() throws UnknownHostException {TransportClient client = new PreBuiltXPackTransportClient(Settings.builder().put("cluster.name", clusterName).put("xpack.security.user", clusterPassword)//增加嗅探機制,找到ES集群,非集群置為false,設置為true時會自動嗅探整個集群的狀態,把集群中其他ES節點的ip添加到本地的客戶端列表.put("client.transport.sniff", false).build());//單個IP//.addTransportAddress(new TransportAddress(InetAddress.getByName("10.10.10.10"), 9300));//多個IPString[] hostNamesPort = clusterNodes.split(",");String host;int port;String[] temp;if (0 != hostNamesPort.length) {for (String hostPort : hostNamesPort) {try {temp = hostPort.split(":");host = temp[0].trim();port = Integer.parseInt(temp[1].trim());client.addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));} catch (UnknownHostException e) {e.printStackTrace();}}}log.info("初始化ElasticsearchTemplate成功");return client;} }5、啟動運行,報NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{Hqhw6raLSIGlpNeHyzxyrA}{xx.xx.xx.xx}{xx.xx.xx.xx:9300}]]
度娘了None of the configured nodes are available的幾種可能的原因:
- 查看配置的ip和端口號是否正確,端口號需要配置tcp端口9300
 - 查看cluster name是否正確
 - 使用的es client版本需要與ES Server版本一致
 - 如果集群開啟了訪問鑒權,將用戶名,密碼傳入
 - client.transport.sniff設置成false,ip:port通過addTransportAddress傳入
 - netty包沖突,直接引入es client用的netty包版本
 - springboot和elasticsearch版本不匹配
 
等等,檢查了都沒發現問題,后面看了一下elasticsearch的日志,發現報not an SSL/TLS record
[WARN ][o.e.x.s.t.n.SecurityNetty4ServerTransport] [wqbVDEq] exception caught on transport layer [NettyTcpChannel{localAddress=0.0.0.0/0.0.0.0:9300, remoteAddress=/127.0.0.1:37168}], closing connection io.netty.handler.codec.DecoderException: io.netty.handler.ssl.NotSslRecordException: not an SSL/TLS record: 455300000027000000000000000908004d3603000016696e7465726e616c3a7463702f68616e647368616b6500Elasticsearch有2種,一種是僅設置密碼,沒有秘鑰證書的,另外一種是設置密碼也設置了秘鑰證書,我只需要第一種僅設置密碼。所以日志可以看出我們把Elasticsearch配置成了需要ssl證書了。
所以改下elasticsearch.yml文件
cluster.name: "elasticsearch-cluster" network.host: 0.0.0.0 http.port: 9200 http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-headers: Authorization #開啟設置密碼 xpack.security.enabled: true #是否開啟ssl證書秘鑰 xpack.security.transport.ssl.enabled: false關閉xpack.security.transport.ssl.enabled: false?后重新部署elasticsearch
啟動項目測試成功。
五、后續
but,but,but,后續聯調發現這2個參數都要設置ture,不然es就設置不了密碼,上面關閉xpack.security.transport.ssl.enabled: false的方式時靈時不靈的。
xpack.security.enabled: true xpack.security.transport.ssl.enabled: true設置為true后,還是沒有繞過None of the configured nodes are available問題。有知道的大神可以留言給后面的同行。
-------------------------------------------------------我是虛線--------------------------------------------------------------
最后還是死心了,放棄transport模式,擁抱High-Level Rest吧。
maven如下:
<properties><java.version>1.8</java.version><elasticsearch.version>6.8.23</elasticsearch.version><spring-boot.version>2.2.13.RELEASE</spring-boot.version></properties><dependencyManagement><dependencies><!--把es版本管理起來,因為springboot2.2.13中使用的es是6.8.13--><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>${elasticsearch.version}</version></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>${elasticsearch.version}</version></dependency></dependencies></dependencyManagement><dependencies><!-- es相關 干掉 spring-boot-starter-data-elasticsearch --><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId></dependency><dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId></dependency></dependencies>?配置如下:(9300改為了9200)
elasticsearch:cluster-nodes: 10.10.10.10:9200cluster-user-name: elasticcluster-password: 123456cluster-scheme: httpElasticsearchConfig.class代碼
import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpHost; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.impl.client.BasicCredentialsProvider; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;import java.util.ArrayList; import java.util.List;import org.springframework.boot.context.properties.ConfigurationProperties;/*** Created by feng on 2022/9/2.*/ @Slf4j @Configuration @ConfigurationProperties(prefix = "elasticsearch") @Data public class ElasticsearchConfig {private String clusterNodes;private String clusterUserName;private String clusterPassword;private String clusterScheme;private HttpHost[] getHttpHosts() {List<HttpHost> hostList = new ArrayList<>();String[] addressArray = clusterNodes.split(",");for (String address : addressArray) {String host = address.split(":")[0];Integer port = Integer.parseInt(address.split(":")[1]);hostList.add(new HttpHost(host, port, clusterScheme));}return hostList.toArray(new HttpHost[]{});}/*** 創建帶HTTP Basic Auth認證rest客戶端*/@Bean(name = "client")public RestHighLevelClient restHighLevelClient() {RestClientBuilder builder = RestClient.builder(getHttpHosts());//設置賬號密碼final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(clusterUserName, clusterPassword));builder.setHttpClientConfigCallback(httpClientBuilder -> {httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);return httpClientBuilder;});return new RestHighLevelClient(builder);} }操作es文章就多了,各位請移位:
(三)ES基于Rest Client的JAVA API開發_猿來如此dj的博客-CSDN博客_es restclient
中間件:ES組件RestHighLevelClient用法詳解 - 騰訊云開發者社區-騰訊云
【ES學習系列】RestHighLevelClient使用實例 - 21karat - 博客園
RestHighLevelClient的基本使用_看見雨聲的博客-CSDN博客_resthighlevelclient使用
RestHighLevelClient的簡單使用_明快de玄米61的博客-CSDN博客_resthighlevelclient使用
Java查詢Elasticsearch(RestHighLevelClient)_MrWangf的博客-CSDN博客_resthighlevelclient查詢
總結
以上是生活随笔為你收集整理的springboot集成elasticsearch6.8.23设置密码xpack连接,及遇到的None of the configured nodes are available的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 计算机启动提示找不到硬盘,开机时硬盘不启
 - 下一篇: 实训9——蓝牙修改开锁密码