elasticsearch-6.1.2 x-pack java transport client客户端连接

环境:Spring Boot、Maven

 

创建config目录,将es集群的认证文件elastic-certificates.p12拷贝到config/

在pom.xml添加依赖:

        
            org.elasticsearch.client
            x-pack-transport
            6.1.2
        

在Spring Boot的配置文件中添加:

elasticsearch.x-pack-security=elastic:elastic
elasticsearch.x-pack-security-keystore-filepath=config/elastic-certificates.p12
elasticsearch.x-pack-security-keystore-password=elastic

连接代码:

String clusterName = environment.getProperty("elasticsearch.cluster-name");
        String url = environment.getProperty("elasticsearch.cluster-nodes");
        String xpackUser = environment.getProperty("elasticsearch.x-pack-security");
        String keyStoreFilePath = environment.getProperty("elasticsearch.x-pack-security-keystore-filepath");
        String keyStorePassword = environment.getProperty("elasticsearch.x-pack-security-keystore-password");

TransportClient client = new PreBuiltXPackTransportClient(
                Settings.builder()
                        .put("cluster.name", clusterName)
                        .put("client.transport.sniff", false)
                        .put("xpack.security.enabled", true)
                        .put("xpack.security.transport.ssl.enabled", true)
                        .put("xpack.security.user", xpackUser)
                        .put("xpack.security.transport.ssl.keystore.path", keyStoreFilePath)
                        .put("xpack.security.transport.ssl.keystore.password", keyStorePassword)
                        .put("xpack.security.transport.ssl.verification_mode", "certificate")
                        .build());

 

你可能感兴趣的:(elasticsearch)