日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot集成elasticsearch6.8.23设置密码xpack连接,及遇到的None of the configured nodes are available

發布時間:2023/12/8 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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:9300

elasticsearch.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: true

docker-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:123456

4、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: 455300000027000000000000000908004d3603000016696e7465726e616c3a7463702f68616e647368616b6500

Elasticsearch有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: http

ElasticsearchConfig.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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。