1.前言
以下所有整合,都是基于ElasticSearch8.0进行开发,老版本会有很多不兼容的情况,请自行斟酌
2.Springboot与ElasticSearch版本对照表
参考地址:https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#repositories
3.引入
官方推荐使用RestClient,而RestClient需要我们自行配置。因此无需划分单数据源还是多数据源,初始化的引入模式就是多数据源模式了,嗨嗨嗨!!!!!
项目Github地址
https://github.com/GuardFTC/elasticsearch-test.git
引入依赖
<!--ElasticSearch依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!--ElasticSearch客户端依赖-->
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>8.0.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>jakarta.json</groupId>
<artifactId>jakarta.json-api</artifactId>
<version>2.1.0</version>
</dependency>
<!--lombok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--Hutool依赖-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.4</version>
</dependency>
编写配置文件
还有很多配置项,并没有一一写入。可以在后续结合项目,用到哪里再做补充
#elasticsearch配置
elasticsearch:
primary:
host: 120.48.107.224
port: 9201
username: username
password: password
connect-timeout: 6000
socket-timeout: 6000
connection-request-timeout: 2000
secondary:
host: 120.48.107.224
port: 9202
connect-timeout: 6000
socket-timeout: 6000
connection-request-timeout: 2000
编写配置属性类
import lombok.Data;
/**
* @author: 冯铁城 [17615007230@163.com]
* @date: 2022-08-18 11:33:32
* @describe: ElasticSearch数据源配置属性
*/
@Data
public class ElasticSearchProperties {
/**
* 主机
*/
private String host;
/**
* 端口
*/
private Integer port;
/**
* 用户名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 链接超时时间
*/
private Integer connectTimeout;
/**
* socket链接超时时间
*/
private Integer socketTimeout;
/**
* 请求链接超时时间
*/
private Integer connectionRequestTimeout;
}
编写配置类
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* @author: 冯铁城 [17615007230@163.com]
* @date: 2022-08-18 16:57:15
* @describe: ElasticSearch配置类
*/
@Configuration
public class ElasticSearchConfig {
@Primary
@Bean(name = "primaryProperties")
@ConfigurationProperties(prefix = "elasticsearch.primary")
public ElasticSearchProperties primaryProperties() {
return new ElasticSearchProperties();
}
@Primary
@Bean(name = "secondaryProperties")
@ConfigurationProperties(prefix = "elasticsearch.secondary")
public ElasticSearchProperties secondaryProperties() {
return new ElasticSearchProperties();
}
}
编写客户端配置类
import cn.hutool.core.util.StrUtil;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import lombok.RequiredArgsConstructor;
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.impl.client.BasicCredentialsProvider;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
/**
* @author: 冯铁城 [17615007230@163.com]
* @date: 2022-08-18 11:15:40
* @describe: ElasticSearch客户端链接配置
*/
@Configuration
@RequiredArgsConstructor
public class ElasticSearchClientConfig {
private final ElasticSearchConfig elasticSearchConfig;
@Primary
@Bean("primaryElasticsearchClient")
public ElasticsearchClient getPrimaryRestClient() {
//1.获取配置
ElasticSearchProperties primaryProperties = elasticSearchConfig.primaryProperties();
//2.生成客户端返回
return getElasticsearchClient(primaryProperties);
}
@Bean("secondaryElasticsearchClient")
public ElasticsearchClient getSecondaryRestClient() {
//1.获取配置
ElasticSearchProperties secondaryProperties = elasticSearchConfig.secondaryProperties();
//2.生成客户端返回
return getElasticsearchClient(secondaryProperties);
}
/**
* 创建ElasticSearch客户端
*
* @param properties ElasticSearch配置
* @return ElasticSearch客户端
*/
private ElasticsearchClient getElasticsearchClient(ElasticSearchProperties properties) {
//1.获取主机和端口以及超时配置
String host = properties.getHost();
Integer port = properties.getPort();
Integer connectTimeout = properties.getConnectTimeout();
Integer socketTimeout = properties.getSocketTimeout();
Integer connectionRequestTimeout = properties.getConnectionRequestTimeout();
//2.设置客户端IP端口
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
//3.设置链接相关参数
RestClientBuilder.RequestConfigCallback requestConfigCallback = requestConfigBuilder -> {
requestConfigBuilder.setConnectTimeout(connectTimeout);
requestConfigBuilder.setSocketTimeout(socketTimeout);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeout);
return requestConfigBuilder;
};
//4.设置客户端链接参数
builder.setRequestConfigCallback(requestConfigCallback);
//5.获取用户名密码
String username = properties.getUsername();
String password = properties.getPassword();
//6.用户名密码判空,判定是否需要创建登录凭证
if (StrUtil.isNotBlank(username) && StrUtil.isNotBlank(password)) {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//7.设置用户名密码
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
credentialsProvider.setCredentials(AuthScope.ANY, credentials);
//8.封装鉴权参数
builder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
}
//9.创建Rest客户端
RestClient restClient = builder.build();
//10.生成Elasticsearch客户端链接
ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
//11.生成Elasticsearch客户端并返回
return new ElasticsearchClient(transport);
}
}
编写单元测试使用
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
import java.io.IOException;
@SpringBootTest
class ElasticSearchClientConfigTest {
@Resource
@Qualifier("primaryElasticsearchClient")
private ElasticsearchClient primaryClient;
@Resource
@Qualifier("secondaryElasticsearchClient")
private ElasticsearchClient secondaryClient;
@Test
void testCreateIndex() throws IOException {
primaryClient.indices().create(c -> c.index("test_java_primary"));
secondaryClient.indices().create(c -> c.index("test_java_secondary"));
}
}
测试结果
如图,单元测试运行成功
如图,两个索引都创建成功