使用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.csdn.net/qq_43565087/article/details/107605812
今日推荐