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
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值