springboot引入elasticsearch依赖冲突问题记录

背景:项目要同时引入

<dependency>
       <groupId>org.elasticsearch</groupId>
       <artifactId>elasticsearch</artifactId>
</dependency>
<dependency>
       <groupId>org.elasticsearch.client</groupId>
       <artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

        需要用到elasticsearch低版本依赖(高版本该类过时)的TransportClient类,来获取索引的数据量大小(elasticsearch-rest-high-level-client依赖没有这个api),

        同时因为之前代码都使用elasticsearch-rest-high-level-client依赖进行写,所以尽可能不改动。

        问题:高版本elasticsearch依赖过时,elasticsearch-rest-high-level-client又没有获取索引的数据量大小的api,且elasticsearch-rest-high-level-client内部自带elasticsearch,所以需要依赖包版本都一致。

        方法:因此将es依赖包版本都降低,尽可能的新,同时TransportClient类不过时。

最终版本定为6.8.15,

<dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>6.8.15</version>
</dependency>
<dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>elasticsearch-rest-high-level-client</artifactId>
        <version>6.8.15</version>
</dependency>

        然后进行es获取索引数据量大小,

@Override
    public List<StoragePO> syncStorage(DatabasePO databasePO) {
        List<StoragePO> storagePOList = new ArrayList<>();
        ConnectionPO connectionPO = connectionDao.selectById(databasePO.getConnectionId());

        RestHighLevelClient restHighLevelClient = init(databasePO, new ArrayList<>());
        try {
            //连接数据库
            Settings settings = Settings.builder().put("cluster.name", "elasticsearch")
                    .put("client.transport.sniff", true)
                    .build();
            TransportClient client = new PreBuiltTransportClient(settings);
            TransportAddress address = new TransportAddress(InetAddress.getByName(connectionPO.getIp()), connectionPO.getPort());
            client.addTransportAddress(address);

            //获取所有表
            GetIndexRequest request = new GetIndexRequest("*");
            GetIndexResponse response = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);
            // 获取所有的索引
            String[] indices = response.getIndices();
            for (String index : indices) {
                StoragePO storagePO = storageService.genarateStorage(databasePO, index);
                IndicesStatsResponse indicesStatsResponse = client.admin().indices().prepareStats(index).all().execute().actionGet();
                CommonStats total = indicesStatsResponse.getTotal();
                storagePO.setCapacity((int) (total.getStore().getSizeInBytes() / 2.0));
                storagePOList.add(storagePO);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("同步存在错误:" + e.getMessage());
        }
        return storagePOList;
    }

        报错:NoNodeAvailableException[None of the configured nodes are available。

        看了网上的回答试了下都不大对,

 

        原因真正的ES连接端口为9200(elasticsearch-rest-high-level-client连接就用9200端口),

        但是elasticsearch所使用的端口就是9300,改完之后就可以用了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中引入Elasticsearch主要包括两个方面的内容。首先是使用ElasticsearchTemplate进行操作的方式,其主要包括创建Elasticsearch工程和引入相关依赖。其次是使用ElasticsearchRepository进行操作的方式,它是基于ElasticsearchTemplate封装的一个接口,用于更方便地操作Elasticsearch。 在创建Spring Boot项目时,需要引入Elasticsearch的相关依赖。可以在项目的pom.xml文件中添加如下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 这样就可以通过Spring Boot来使用Elasticsearch了。 通过以上的步骤,你就成功地在Spring Boot项目中引入Elasticsearch,可以开始使用ElasticsearchTemplate或ElasticsearchRepository来进行相关操作了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Spring Boot整合Elasticsearch详细教程!官方工具类该如何使用?一篇文章搞定!](https://blog.csdn.net/hzj_java/article/details/118096157)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [springboot引入Elasticsearch](https://blog.csdn.net/huang2802033667/article/details/129420627)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值