欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ElasticSearch6.3.2开启x-pack后客户端调用

程序员文章站 2022-05-19 10:04:45
...

问题:在ElasticSearch6.3.2开启x-pack后如何使用java客户端访问?

ElasticSearch6.3.2 java客户端调用

这里引入的是spring-boot-starter-data-elasticsearch(底层用的也是TransportClient

开启x-pack后需要用到x-pack-transport(官网文档版本是6.2.4-这里有详细介绍)

ElasticSearch6.3版本已经安装了x-pack  x-pack默认已经安装位置

/usr/local/software/elasticsearch-6.3.2/modules

ElasticSearch6.3.2开启x-pack后客户端调用

license时间是一个月

curl -XGET -u elastic:ilikees2019 'http://127.0.0.1:9200/_license'

ElasticSearch6.3.2开启x-pack后客户端调用

集成过程记录如下:

1、pom.xml文件

这里使用springboot版本是2.1.3.RELEASE

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
</parent>
<!--spring data 操作es-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
 <!--add the x-pack 及依赖 开始-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>x-pack-transport</artifactId>
            <version>${elasticsearch.version}</version>
        </dependency>
        <dependency>
            <groupId>com.unboundid</groupId>
            <artifactId>unboundid-ldapsdk</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>x-pack-core</artifactId>
            <version>6.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.65</version>
        </dependency>
        <!--add the x-pack 及依赖 结束-->

x-pack-core.jar可以从/usr/local/software/elasticsearch-6.3.2/modules/x-pack/x-pack-core/x-pack-core-6.3.2.jar 下载到本地maven库中

其他依赖与安全认证的算法包如bouncycastle等也需要自己引入下

2、创建XPackClient

   @Bean
    public TransportClient getTransportClient() throws UnknownHostException{
        Settings.Builder builder = Settings.builder();
        builder.put("cluster.name", "myClusterName");//集群名称
        builder.put("client.transport.sniff", true);//自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
        builder.put("xpack.security.transport.ssl.enabled", false);//设置xpack ssl这里设置为false
        builder.put("xpack.security.user", "xxxxx");//集群设置的密码
        builder.build()
        TransportClient transportClient = new PreBuiltXPackTransportClient(settings())
                .addTransportAddress(new TransportAddress(InetAddress.getByName("xxxxx"), 9300));//集群的ip地址
        log.info("transportClient.listedNodes():{}",transportClient.listedNodes());
        log.info("ElasticsearchXPackClient 启动成功");
        return transportClient;
    }

 

3、测试使用 

 因为上面pom.xml文件中用的是spring-boot-starter-data-elasticsearch 所以也可以直接使用ElasticsearchRepository,最后调用的还是配置的TransportClient

@Autowired
    private TransportClient transportClient; 
@Test
    public void testFindEsTransport() {
        // 发起请求得到响应
        GetResponse response=transportClient.prepareGet("bank","account","9").get();
        System.out.println(response.getSource());
    }