Use X-Pack to connect to the ES cluster (Alibaba Cloud ES cluster configuration is 5.3.3) The port is 9300

Did not really get to know the 6 . Version ElasticSearch officials have not recommended this port 9300 to communicate the recommended 9200 Client to connect to the cluster because the iterative ES ES itself is likely to soon eliminate the port 9300 course Single node does not matter, but the actual environment is not recommended to use single node

Use X-pack to connect to the ES cluster (I use SpringBoot here)

1.Pom coordinates

		<!--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 file writing

#ES 集群配置(version:5.3.3)
elasticSearch:
  host: es-cn-*************.public.elasticsearch.aliyuncs.com
  port: 9300
  cluster-name: ************
  user.password: elastic:*******

3. Write ES configuration class

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;
    }

}

Note: You can refer to the official Alibaba Cloud documentation, but you must pay attention to the problem of the version. The problem of the version is very fatal for the SpringBoot development of x-pack. Everyone should know that this kind of problem is difficult and difficult to solve. At the time, I personally went to see mvn- Tree went to look at the interdependent versions as a whole, and then changed the address of the maven local warehouse and downloaded it again. This is a headache. It is clear that the Demo can complete the connection, but when I inject it into the project by Configuration, it doesn’t work. This method still supports ElasticTemplate. This method also supports the development of @Document. I personally think that this method is much simpler than the Client method. However, since the official does not recommend using 9300, there is a reason for this after all. The method is indeed unsafe. x-pack is no longer being maintained in 2017. There are still many unknown problems. Alibaba Cloud currently only supports this connection method in 5.3.3. The rest of them all use 9200 for communication. TCP is not supported.

Guess you like

Origin blog.csdn.net/qq_43565087/article/details/107605812