环境:Spring Boot、Maven

 

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

在pom.xml添加依赖:

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>x-pack-transport</artifactId>
            <version>6.1.2</version>
        </dependency>

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());

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐