当前位置: 首页 > news >正文

elasticsearch更换为opensearch

1.环境

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

2. pom依赖

springboot版本

    <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.0.2</version><relativePath/> </parent>

opensearch依赖

      <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的动态数据源依赖不一样)

        <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配置

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  {@AutowiredResourceLoader resourceLoader;@Beanpublic 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();}@Beanpublic 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);// 初始化 SSLContextSSLContext sslContext = SSLContext.getInstance("TLS");sslContext.init(null, trustManagerFactory.getTrustManagers(), null);return sslContext;}}

4.notice

  • springboot 的opensearchtemplate依赖于elasticsearchtemplate,所以之前es的依赖需要保留。
  • opensearch的maven版本只适配springboot3.x。所以需要升级springboot版本,并适配其他依赖
http://www.dtcms.com/a/399666.html

相关文章:

  • 微服务和大数据在架构上的相似之处
  • 专业优化网站建设襄城县城乡建设管理局网站
  • 电子商务网站设计的基本要求如何查看网站跳出率
  • 用vio_uart_rpc协议,测试IIC接口的AT24C64
  • 杭州网站快速备案正规电商培训班
  • 台州做网站多少钱镇江佳鑫网络科技有限公司
  • APM v4.1.1 | 免费音乐听歌B站油管音乐播放器
  • 牛客算法基础noob51 杨辉三角
  • SVN忽略文件不生效
  • MyBatis开启自动下划线转驼峰
  • 自己做网站 搜索功能开发汽车商城网站模板免费下载
  • 加拿大住宅代理指南(2025年更新)
  • 专门建站的公司制作u盘启动盘
  • redis的set集合的常规使用
  • 【C++模板编程】从泛型思想到实战应用
  • auto 关键字
  • 工程项目建设自学网站哪家网站开发好
  • 企业网站建设文档微信开发应用平台
  • 大语言模型本身有记忆功能吗?
  • 解决Initializing Python failed: Failed to import encodings module错误
  • 上海做网站哪里有wordpress环境安装
  • 兖州市做网站wordpress电子商务视频教程
  • 优秀网站模板中国10大装修公司排名
  • python++springboot+nodejs微信小程序高校实验室管理系统 实验室预约登记 设备借用管理 实验记录审核系统
  • 外贸精品网站建设网站运营介绍
  • 构建神经网络的两大核心工具
  • 品牌高端网站制作金蝶软件多少钱
  • 基于STM32舞台彩灯控制器设计app控制系统
  • 2D激光定位与建图
  • shell编程:grep - 文本搜索利器(1)