elasticsearch更换为opensearch

1.环境

  • springboot升级至3.x
  • jdk17
  • elasticsearch 7.1、opensearch 2.x

2. pom依赖

springboot版本

bash 复制代码
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/> 
    </parent>

opensearch依赖

bash 复制代码
      <dependency>
            <groupId>org.opensearch.client</groupId>
            <artifactId>spring-data-opensearch-starter</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

若使用动态数据源版本跟随升级(2.x和3.x springboot的动态数据源依赖不一样)

bash 复制代码
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
            <version>4.2.0</version>

3. opensearchtemplate配置

java 复制代码
package com.echosell.spider.appspider.config;

import com.echosell.spider.appspider.entity.properties.OsProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.opensearch.client.RestClient;
import org.opensearch.client.RestHighLevelClient;
import org.opensearch.data.client.orhlc.OpenSearchRestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.repository.init.ResourceReader;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.InputStream;
import java.security.KeyStore;
import java.util.concurrent.TimeUnit;


@Configuration
@Slf4j
public class OpenSearchTemplateConfig  {

    @Autowired
    ResourceLoader resourceLoader;

    @Bean
    public RestHighLevelClient restHighLevelClient(OsProperties osProperties) throws Exception {
        //解析hostlist配置信息
        String[] split = osProperties.getUris().split(",");
        //创建HttpHost数组,其中存放es主机和端口的配置信息
        HttpHost[] httpHostArray = new HttpHost[split.length];
        for(int i=0;i<split.length;i++){
            String item = split[i];
            httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http");
        }
        //创建RestHighLevelClient客户端
        return new RestHighLevelClient(RestClient.builder(httpHostArray).setHttpClientConfigCallback(httpClientBuilder -> {

            httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
            httpClientBuilder.setMaxConnTotal(osProperties.getMaxConnTotal());
            httpClientBuilder.setConnectionTimeToLive(osProperties.getConnTimeToLive(), TimeUnit.SECONDS);
            BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials(osProperties.getUsername(), osProperties.getPassword()));
            httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
            return httpClientBuilder;
        }));

//        ClientConfiguration.MaybeSecureClientConfigurationBuilder maybeSecureClientConfigurationBuilder = ClientConfiguration.builder().connectedTo(osProperties.getUris());
//        if(osProperties.getUseSsl()){
//            maybeSecureClientConfigurationBuilder.usingSsl(createSSLContext(osProperties.getKeyStore(), osProperties.getKeyStorePassword()));
//        }
//        maybeSecureClientConfigurationBuilder.withConnectTimeout(osProperties.getConnTimeToLive())
//                .withBasicAuth(osProperties.getUsername(), osProperties.getPassword())
//                .withSocketTimeout(osProperties.getConnectionTimeout())
//                .withClientConfigurer(clientConfigurer -> {
//                    return clientConfigurer;
//                });
//        return RestClients.create(maybeSecureClientConfigurationBuilder.build()).rest();

    }

    @Bean
    public OpenSearchRestTemplate openSearchRestTemplate(RestHighLevelClient restHighLevelClient){
        return new OpenSearchRestTemplate(restHighLevelClient);
    }




    private SSLContext createSSLContext(String keyStorePath, String keyStorePassword) throws Exception {
        // 加载密钥库
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        ClassLoader classLoader = ResourceReader.class.getClassLoader();
        // 读取文件内容...
        try (InputStream inputStream = classLoader.getResourceAsStream(keyStorePath)) {
            trustStore.load(inputStream, keyStorePassword.toCharArray());
        }

        // 创建信任管理器工厂
        TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);
        // 初始化 SSLContext
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagerFactory.getTrustManagers(), null);

        return sslContext;
    }



}

4.notice

  • springboot 的opensearchtemplate依赖于elasticsearchtemplate,所以之前es的依赖需要保留。
  • opensearch的maven版本只适配springboot3.x。所以需要升级springboot版本,并适配其他依赖
相关推荐
TTBIGDATA5 小时前
【Ambari开启Kerberos】KERBEROS SERVICE CHECK 报错
大数据·运维·hadoop·ambari·cdh·bigtop·ttbigdata
开利网络5 小时前
合规底线:健康产品营销的红线与避坑指南
大数据·前端·人工智能·云计算·1024程序员节
非著名架构师5 小时前
量化“天气风险”:金融与保险机构如何利用气候大数据实现精准定价与投资决策
大数据·人工智能·新能源风光提高精度·疾风气象大模型4.0
Hello.Reader6 小时前
用 CdcUp CLI 一键搭好 Flink CDC 演练环境
大数据·flink
努力的小郑6 小时前
Elasticsearch 避坑指南:我在项目中总结的 14 条实用经验
后端·elasticsearch·性能优化
熙梦数字化6 小时前
2025汽车零部件行业数字化转型落地方案
大数据·人工智能·汽车
Hello.Reader6 小时前
Flink CDC「Data Pipeline」定义与参数速查
大数据·flink
森语林溪9 小时前
大数据环境搭建从零开始(十四)CentOS 7 系统更新源更换详解:阿里云镜像源配置完整指南
大数据·linux·运维·阿里云·centos
杂家10 小时前
Zookeeper完全分布式部署(超详细)
大数据·分布式·zookeeper
snakecy11 小时前
树莓派学习资料共享
大数据·开发语言·学习·系统架构