TransportClient基于Elasticsearch6.8.6 X-PACK

Elasticsearch6.8.6开启X-PACK服务后,Transport连接方式

转载,请注明出处:https://www.cnblogs.com/cchilei/p/13086160.html

pom文件

    <repositories>
        <!-- add the elasticsearch repo -->
        <repository>
            <id>elasticsearch-releases</id>
            <url>https://artifacts.elastic.co/maven</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.1.0.RELEASE</version>
        </dependency>
        <!-- add the x-pack jar as a dependency -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>x-pack-transport</artifactId>
            <version>6.8.6</version>
        </dependency>
    </dependencies>

1、没有密钥连接(博主没有成功,不知道为啥,还在解决)

官网:https://www.elastic.co/guide/en/elasticsearch/reference/6.8/java-clients.html

import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;

TransportClient client = new PreBuiltXPackTransportClient(Settings.builder()
        .put("cluster.name", "myClusterName")
        .put("xpack.security.user", "transport_client_user:x-pack-test-password")
        ...
        .build())
    .addTransportAddress(new TransportAddress("localhost", 9300))
    .addTransportAddress(new TransportAddress("localhost", 9301));

2、密钥连接(亲测成功)

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Configuration
public class xpackdemo {

    @Bean
    public TransportClient transportClient() {
        TransportClient client = null;
        try {
            Settings settings = Settings.builder()
                    .put("cluster.name", "mycluster")
                    .put("xpack.security.user", "elastic:123456")
                    .put("xpack.ssl.key", "/demo/cert/x-pack/instance/instance.key")
                    .put("xpack.ssl.certificate", "/demo/cert/x-pack/instance/instance.crt")
                    .put("xpack.ssl.certificate_authorities", "/demo/cert/x-pack/ca/ca.crt")
                    .put("xpack.security.transport.ssl.verification_mode", "certificate")
                    .put("xpack.security.transport.ssl.enabled", "true")
                    .build();
            client = new PreBuiltXPackTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return client;
    }
}

注:

/opt/cert/x-pack/instance/instance.key

/opt/cert/x-pack/instance/instance.crt

/opt/cert/x-pack/ca/ca.crt

是从config/x-pack文件夹中拷贝出来,放到程序中的

参考elasticsearch6.8.6配置xpack(生成密钥):https://www.cnblogs.com/cchilei/p/13085842.html

posted @ 2020-06-10 16:30  cchilei  阅读(2272)  评论(1编辑  收藏  举报