首页 > 其他分享 >SpringBoot2.x 版本集成elasticsearch 8.x

SpringBoot2.x 版本集成elasticsearch 8.x

时间:2024-10-12 16:50:56浏览次数:5  
标签:集成 7.15 java client SpringBoot2 elasticsearch org import

  之前使用的elasticsearch 7.14.2,Springboot版本是2.4.13(这个版本坑比较多,用的人也比较少,找问题真的很痛苦)。

   es中间件升级到8.13.3之后,之前的代码在使用保存和编辑之后,es数据里面是都操作成功,但是代码接口却会报错。

at java.util.Objects.requireNonNull(Objects.java:203) ~[na:1.8.0_272]
    at org.elasticsearch.action.DocWriteResponse.<init>(DocWriteResponse.java:116) ~[elasticsearch-7.15.2.jar:7.15.2]
    at org.elasticsearch.action.index.IndexResponse.<init>(IndexResponse.java:43) ~[elasticsearch-7.15.2.jar:7.15.2]
    at org.elasticsearch.action.index.IndexResponse.<init>(IndexResponse.java:28) ~[elasticsearch-7.15.2.jar:7.15.2]
    at org.elasticsearch.action.index.IndexResponse$Builder.build(IndexResponse.java:96) ~[elasticsearch-7.15.2.jar:7.15.2]
    at org.elasticsearch.action.index.IndexResponse$Builder.build(IndexResponse.java:93) ~[elasticsearch-7.15.2.jar:7.15.2]
    at org.elasticsearch.action.bulk.BulkItemResponse.fromXContent(BulkItemResponse.java:148) ~[elasticsearch-7.15.2.jar:7.15.2]
    at org.elasticsearch.action.bulk.BulkResponse.fromXContent(BulkResponse.java:177) ~[elasticsearch-7.15.2.jar:7.15.2]
    at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:2011) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
    at org.elasticsearch.client.RestHighLevelClient.lambda$performRequestAndParseEntity$8(RestHighLevelClient.java:1673) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
    at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1749) ~[elasticsearch-rest-high-level-client-7.15.2.jar:7.15.2]
    ... 14 common frames omitted

Caused by: java.io.IOException: Unable to parse response body for Response{requestLine=POST /_bulk?timeout=1m HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}

Caused by: java.lang.NullPointerException: null

Caused by: java.lang.RuntimeException: Bulk request failed

Caused by: java.io.IOException: Unable to parse response body for Response{requestLine=POST /_bulk?timeout=1m HTTP/1.1, host=http://localhost:9200, response=HTTP/1.1 200 OK}

  这个是请求es返回的数据有异常。也就是说低版本的es和版本的es返回的数据结构是有差别的,所以归根到底就是客户端和服务端的版本不一致。es官方也说了,高版本的es推荐使用java 17之后的版本,springboot推荐使用3.0的。但是不能说升级一个中间件结果把整个系统都升级一遍,所以官方也给出了一个兼容的模式,具体的代码放在下面,尤其是maven的依赖包版本不要错误。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-elasticsearch</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.4.2</version>
<scope>compile</scope>

</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.17.4</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>7.17.4</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>7.17.4</version>
</dependency>
import cn.hutool.core.convert.Convert;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.message.BasicHeader;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestHighLevelClientBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

import javax.annotation.PreDestroy;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@Configuration
public class RestClientConfig extends AbstractElasticsearchConfiguration {


    @Autowired
    private ElasticsearchProperties elasticsearchProperties;
    private RestHighLevelClient client;

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        RestClientBuilder restBuilder = RestClient
                .builder(this.getHttpHosts());

        restBuilder.setHttpClientConfigCallback(httpClientBuilder ->
                httpClientBuilder
                        .setKeepAliveStrategy(getConnectionKeepAliveStrategy())
                        .setMaxConnPerRoute(10).
                        setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build()));
        String username = elasticsearchProperties.getAccount().getUsername();
        String password = elasticsearchProperties.getAccount().getPassword();
        SSLContextBuilder sscb = SSLContexts.custom();
        try {
            sscb.loadTrustMaterial((chain, authType) -> {
                // 在这里跳过证书信息校验
                //System.out.println("暂时isTrusted|" + authType + "|" + Arrays.toString(chain));
                return true;
            });
        } catch (NoSuchAlgorithmException | KeyStoreException e) {
            e.printStackTrace();
        }
        if (username != null && password != null) {
            final CredentialsProvider credential = new BasicCredentialsProvider();
            credential.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
            restBuilder.setHttpClientConfigCallback(httpClientBuilder ->
            {
                try {
                    return httpClientBuilder
                            .setSSLContext(sscb.build())
                            .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                            .setDefaultCredentialsProvider(credential)
                            .setKeepAliveStrategy(getConnectionKeepAliveStrategy())
                            .setMaxConnPerRoute(10)
                            .setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(1).build());
                } catch (NoSuchAlgorithmException e) {
                    throw new RuntimeException(e);
                } catch (KeyManagementException e) {
                    throw new RuntimeException(e);
                }
            });
        }
        restBuilder.setDefaultHeaders(compatibilityHeaders());
        restBuilder.setRequestConfigCallback(requestConfigBuilder ->
                requestConfigBuilder.setConnectTimeout(1000) //time until a connection with the server is established.
                        .setSocketTimeout(12 * 1000) //time of inactivity to wait for packets[data] to receive.
                        .setConnectionRequestTimeout(2 * 1000)); //time to fetch a connection from the connection pool 0 for infinite.

        client = new RestHighLevelClient(restBuilder);
        restBuilder.build();
        RestHighLevelClient esClient = new RestHighLevelClientBuilder(restBuilder.build())
                .setApiCompatibilityMode(true)
                .build();
        return esClient;
    }

    private Header[] compatibilityHeaders() {
        return new Header[]{
                new BasicHeader(HttpHeaders.ACCEPT, "application/vnd.elasticsearch+json;compatible-with=7"),
                new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.elasticsearch+json;compatible-with=7")
        };
    }

    private HttpHost[] getHttpHosts() {
        List<String> clusterNodes = elasticsearchProperties.getClusterNodes();
        HttpHost[] httpHosts = new HttpHost[clusterNodes.size()];
        for (int i = 0; i < clusterNodes.size(); i++) {
            String[] node = clusterNodes.get(i).split(":");
            httpHosts[i] = new HttpHost(node[0], Convert.toInt(node[1]), elasticsearchProperties.getSchema());
        }
        return httpHosts;
    }

    private ConnectionKeepAliveStrategy getConnectionKeepAliveStrategy() {
        return (response, context) -> 2 * 60 * 1000;
    }

    /**
     * it gets called when bean instance is getting removed from the context if
     * scope is not a prototype
     * If there is a method named shutdown or close then spring container will try
     * to automatically configure them as callback methods when bean is being
     * destroyed
     */
    @PreDestroy
    public void clientClose() {
        try {
            this.client.close();
        } catch (IOException e) {
        }
    }


    @Bean
    public ElasticsearchRestTemplate restTemplate(RestHighLevelClient restHighLevelClient){
        return new ElasticsearchRestTemplate(restHighLevelClient);
    }


}

所以选择中间件升级是一个比较严肃的事情,选择版本要谨慎。

标签:集成,7.15,java,client,SpringBoot2,elasticsearch,org,import
From: https://www.cnblogs.com/yangkangIT/p/18460859

相关文章

  • Spring Boot 集成 RabbitMQ 自定义 MessageConverter
    1.SpringBoot集成RabbitMQ自定义消息转换器1.1.版本说明1.2.概述1.3.Spring配置1.4.定义常量1.5.配置交换机和队列1.6.配置ObjectMapper1.7.配置MessageConverter1.8.测试1.SpringBoot集成RabbitMQ自定义消息转换器1.1.版本说明构件版......
  • SpringBoot2.x 版本集成elasticsearch 8.x(基于elasticsearch-java)
    上次elasticsearch到8.13.3之后,由于springboot版本2.4.13,jdk版本是11,使用springboot内置的ElasticsearchRestTemplate查询的时候做了兼容性之后虽然代码没有报错了,但是每次查询都会有一个警告[WRAN],如果查询比较频繁的时候日志里面看到的全都是这种警告信息:[ignore_throttle......
  • 智慧园区让建筑5A系统高效集成
    随着科技的不断发展,智慧园区已经成为现代城市建设的重要组成部分。智慧园区采用先进的信息技术和自动化系统,为企业和居民提供更加智能化、高效化和舒适化的工作和生活环境。其办公自动化系统、通讯自动化系统、消防自动化系统、安保自动化系统与楼宇自动控制系统则是智慧园区建......
  • 软考攻略/超详细/系统集成项目管理工程师/基础知识分享13
    5.3软件设计(掌握)        需求阶段解决“做什么”的问题,而软件设计阶段解决“怎么做”的问题。软件设计分为结构化设计与面向对象设计。5.3.1结构化设计(掌握)        结构化设计(SD)是一种面向数据流的方法,其目的在于确定软件结构。它以SRS和SA阶段所产生的D......
  • 三、Spring Boot集成Spring Security之securityFilterChain过滤器链详解
    二、默认过滤器链1、默认配置系统启动日志2、默认配置的过滤器及顺序如下org.springframework.security.web.session.DisableEncodeUrlFilterorg.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilterorg.springframework.security.web.c......
  • Elasticsearch安装学习
    前言:ElasticSearch官网:http://www.elasticsearch.org/先上一张elasticsearch的总体框架图:ElasticSearch是基于Lucene开发的分布式搜索框架,包含如下特性:分布式索引、搜索索引自动分片、负载均衡自动发现机器、组建集群支持Restful风格接口配置简单等。Elasticse......
  • Spring Boot 集成 RabbitMQ 多个 Broker 发送、消费消息
    1.SpringBoot集成RabbitMQ多个Broker发送、消费消息1.1.版本说明1.2.概述1.3.RabbitMQ信息1.4.Spring配置1.5.定义常量1.6.定义配置属性1.7.定义两个ConnectionFactory1.8.定义两个RabbitTemplate1.9.定义两个SimpleRabbitListenerContainerFacto......
  • electron-vite_4使用WebContentsView快速集成已有项目
    Web嵌入官方推荐使用WebContentsView;集成也比较简单,但还是需要你单独写点东西;src/main/index.ts进行修改import{app,shell,BrowserWindow,ipcMain,nativeImage,WebContentsView,dialog}from'electron';functioncreateWindow():void{//1.创建br......
  • AI-MO x Numina | 工具集成的数学推理
    第二届AIMO竞赛将于未来几周在Kaggle上正式启动,具体截止日期将在发布时公布。**这次比赛的难度将提升至IMO奥赛级别,**参赛者将面对100道全新数学题,要求AI展示真正的数学推理能力,而非简单的计算或猜测。本次进步奖的奖金池高达209.7万美元,比2024年7月颁发......
  • SpringBoot集成Redis
    Redis简介:是一个开源的、使用C语言编写的、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库主要特点速度快,Redis将数据存储在内存中,因此读写速度非常快,可以达到每秒数万次甚至更高的读写操作。这使得它非常适合处理高并发的场景,如缓存、实时排行榜等。数据类......