使用X-Pack连接ES集群(阿里云ES 集群配置为5.3.3) 端口为9300
当初确实是没有去了解在6..的版本ElasticSearch官方已经不推荐使用 9300这个端口进行通信了 推荐使用9200 Client 来对ES 集群进行连接 因为ES 本身的迭代很快 很有可能淘汰掉 9300这个端口 当然单节点 就无所谓了 但实际的环境是不推荐采用单节点的
使用X-pack进行ES 集群的连接(我这里采用的是 SpringBoot的方式)
1.Pom坐标
<!--x-pack-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>x-pack-transport</artifactId>
<version>5.3.3</version>
</dependency>
<!--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>
<!--log4j-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency>
2.yml文件编写
#ES 集群配置(version:5.3.3)
elasticSearch:
host: es-cn-*************.public.elasticsearch.aliyuncs.com
port: 9300
cluster-name: ************
user.password: elastic:*******
3.编写ES 配置类
package com.search.config;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.net.InetAddress;
import java.net.UnknownHostException;
@Configuration
public class ElasticsearchConfig {
/**
* es集群读取路径
*/
public static final String X_PACK_ES_NAME = "cluster.name";
/**
* x-pack用户名密码读取路径
*/
public static final String X_PACK_USER = "xpack.security.user";
@Value("${elasticSearch.cluster-name}")
private String esClusterName;
@Value("${elasticSearch.host}")
private String esHost;
@Value("${elasticSearch.port}")
private int esPort;
@Value("${elasticSearch.user.password}")
private String esUserPassword;
private final Logger LOGGER = LoggerFactory.getLogger(ElasticsearchConfig.class);
/**
* elasticsearch客户端注入(配置)
*
* @return
* @throws UnknownHostException
*/
@Bean
public TransportClient transportClient() throws UnknownHostException {
TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
.put(X_PACK_ES_NAME, esClusterName) //替换为对应阿里云Elasticsearch实例的ID
.put(X_PACK_USER, esUserPassword) //阿里云Elasticsearch实例的用户名、密码
.put("client.transport.sniff", false) //设置sniff为false
.build())
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(esHost), esPort));
return client;
}
}
注意: 可以参考阿里云官方文档 但是一定要注意到版本的问题 版本的问题 对于x-pack 的SpringBoot开发很是致命 大家应该都知道 这种问题很难很难解决 当时我个人是去看了mvn-tree 去整体的去看了下相互依赖的版本 后来重新更换了 maven 本地仓库的地址 重新的进行了下载 这个事情很是头疼 明明 Demo可以完成连接 但是当我以Configuration的方式注入到项目中就不太行了 这种方式 依然是支持 ElasticTemplate 这种方式 同样也是支持@Document这种方式的开发 我个人认为 这种方式 反而要比 Client 那种方式简单很多 但是 官方既然 不推荐使用9300 也是有原因的 毕竟这种方式 确实也是不安全 x-pack 在2017年的时候已经不在做维护了 所有还有很多未知的问题 阿里云目前 只有5.3.3支持这种连接方式 剩下通通 都采用9200 进行通信 不支持TCP
本文作者:张三Blog
本文链接:https://www.cnblogs.com/zhangsan-plus/p/16503303.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了