springboot进行elasticsearch的开发

2022-11-16 09:11:59 浏览数 (1)

目录

第一步:进行es的相关依赖的处理

第二步:定义全局的变量,用于在开发环境或者说uat环境动态变更处理

第三步:定义springboot的es的配置类

第一步:进行es的相关依赖的处理

es依赖包的版本设置

<elasticsearch.version>6.4.3</elasticsearch.version>

<spring.data.elasticsearch.version>3.2.1.RELEASE</spring.data.elasticsearch.version>

<!-- elasticsearch -->

<dependency>

<groupId>org.elasticsearch</groupId>

<artifactId>elasticsearch</artifactId>

<version>${elasticsearch.version}</version>

<exclusions>

<exclusion>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-api</artifactId>

</exclusion>

</exclusions>

</dependency>

<dependency>

<groupId>org.apache.logging.log4j</groupId>

<artifactId>log4j-api</artifactId>

<version>2.17.2</version>

</dependency>

<dependency>

<groupId>org.elasticsearch.client</groupId>

<artifactId>transport</artifactId>

<version>${elasticsearch.version}</version>

</dependency>

<dependency>

<groupId>org.elasticsearch.client</groupId>

<artifactId>elasticsearch-rest-client</artifactId>

<version>${elasticsearch.version}</version>

</dependency>

<dependency>

<groupId>org.elasticsearch.client</groupId>

<artifactId>elasticsearch-rest-high-level-client</artifactId>

<version>${elasticsearch.version}</version>

</dependency>

<dependency>

<groupId>org.elasticsearch.plugin</groupId>

<artifactId>transport-netty4-client</artifactId>

<version>${elasticsearch.version}</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-elasticsearch</artifactId>

</dependency>

第二步:定义全局的变量,用于在开发环境或者说uat环境动态变更处理

编辑

第三步:定义springboot的es的配置类

import java.util.ArrayList;

import java.util.List;

import org.apache.commons.lang.StringUtils;

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.elasticsearch.client.RestHighLevelClient;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

public class SpringBootElasticsearchConfig {

private Logger LOGGER = LoggerFactory.getLogger(SpringBootElasticsearchConfig.class);

@Value("${system.elasticsearch.schema:http}")

private String schema;

@Value("${system.elasticsearch.address:#{null}}")

private String address;

@Value("${system.elasticsearch.userName:#{null}}")

private String userName;

@Value("${system.elasticsearch.password:#{null}}")

private String password;

@Value("${system.elasticsearch.connectTimeout:10000}")

private int connectTimeout;

@Value("${system.elasticsearch.socketTimeout:60000}")

private int socketTimeout;

@Value("${system.elasticsearch.connectionRequestTimeout:10000}")

private int connectionRequestTimeout;

@Value("${system.elasticsearch.maxConnectTotal:1000}")

private int maxConnectTotal;

@Value("${system.elasticsearch.maxConnectPerRoute:500}")

private int maxConnectPerRoute;

@Value("${system.elasticsearch.keepAliveTime:3600000}")

private long keepAliveTime;

public SpringBootElasticsearchConfig() {

}

@Bean(

name = {"restHighLevelClient"},

destroyMethod = "close"

)

public RestHighLevelClient restHighLevelClient() {

if (!StringUtils.isNotBlank(this.address)) {

return null;

} else {

this.LOGGER.info("start init elasticsearch address:" this.address);

List<HttpHost> hostLists = new ArrayList();

String[] hostList = this.address.split(",");

String[] var3 = hostList;

int var4 = hostList.length;

for(int var5 = 0; var5 < var4; var5) {

String addr = var3[var5];

String host = addr.trim().split(":")[0];

String port = addr.trim().split(":")[1];

hostLists.add(new HttpHost(host, Integer.parseInt(port), this.schema));

}

HttpHost[] httpHost = (HttpHost[])hostLists.toArray(new HttpHost[0]);

RestClientBuilder builder = RestClient.builder(httpHost);

builder.setRequestConfigCallback((requestConfigBuilder) -> {

requestConfigBuilder.setConnectTimeout(this.connectTimeout);

requestConfigBuilder.setSocketTimeout(this.socketTimeout);

requestConfigBuilder.setConnectionRequestTimeout(this.connectionRequestTimeout);

return requestConfigBuilder;

});

builder.setHttpClientConfigCallback((httpClientBuilder) -> {

httpClientBuilder.setMaxConnTotal(this.maxConnectTotal);

httpClientBuilder.setMaxConnPerRoute(this.maxConnectPerRoute);

httpClientBuilder.setKeepAliveStrategy((response, context) -> {

return this.keepAliveTime;

});

if (StringUtils.isNotBlank(this.userName)) {

this.LOGGER.info("start setting username and password of elasticsearch");

CredentialsProvider credentialsProvider = new BasicCredentialsProvider();

credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(this.userName, this.password));

httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);

}

return httpClientBuilder;

});

return new RestHighLevelClient(builder);

}

}

}

0 人点赞