spring boot集成ES 7.15.x以上版本代码

  |   0 评论   |   0 浏览

1、配置maven

 <dependency>
    <groupId>co.elastic.clients</groupId>
    <artifactId>elasticsearch-java</artifactId>
    <version>7.17.5</version>
 </dependency>

 <dependency>
     <groupId>jakarta.json</groupId>
     <artifactId>jakarta.json-api</artifactId>
     <version>2.0.1</version>
 </dependency>

2、配置

elasticsearch:
  cluster-nodes: 192.168.100.101:9200,192.168.100.102:9200,192.168.100.103:9200
  schema: http
  connect-timeout: 15000
  socket-timeout: 15000
  connection-request-timeout: 20000
  max-connect-num: 100
  max-connect-PerRoute: 100
  username: elastic
  password: xxxx@2022

3、java代码

@Data
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticSearchProperties {
	/**
	 * 协议
	 */
	private String schema;

	/**
	 * 集群地址,如果有多个用“,”隔开
	 */
	private String clusterNodes;

	/**
	 * 连接超时时间
	 */
	private int connectTimeout;

	/**
	 * Socket 连接超时时间
	 */
	private int socketTimeout;

	/**
	 * 获取连接的超时时间
	 */
	private int connectionRequestTimeout;

	/**
	 * 最大连接数
	 */
	private int maxConnectNum;

	/**
	 * 最大路由连接数
	 */
	private int maxConnectPerRoute;

	/**
	 * 连接ES的用户名
	 */
	private String username;

	/**
	 * 密码
	 */
	private String password;




}

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 org.apache.commons.codec.binary.Base64;
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.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
public class ElasticSearchConfiguration  {

	private final ElasticSearchProperties elasticSearchProperties;

	public ElasticSearchConfiguration(ElasticSearchProperties elasticSearchProperties) {
		this.elasticSearchProperties = elasticSearchProperties;
	}

	@Bean
	public RestClient restClient() {

		final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
		//设置账号密码
		credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(elasticSearchProperties.getUsername(), elasticSearchProperties.getPassword()));

		// 构建连接对象
		RestClientBuilder builder = RestClient.builder(getHttpHost());

		// 设置用户名和密码
		builder.setHttpClientConfigCallback(httpClientBuilder -> {
			httpClientBuilder.disableAuthCaching();
			httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
			return httpClientBuilder;
		});

		String auth = Base64.encodeBase64String((elasticSearchProperties.getUsername()+":"+elasticSearchProperties.getPassword()).getBytes());

		builder.setDefaultHeaders(new BasicHeader[]{ new BasicHeader("Authorization","Basic "+auth) });

		// 异步连接延时配置
		builder.setRequestConfigCallback(requestConfigBuilder -> {
			requestConfigBuilder.setConnectTimeout(elasticSearchProperties.getConnectTimeout());
			requestConfigBuilder.setSocketTimeout(elasticSearchProperties.getSocketTimeout());
			requestConfigBuilder.setConnectionRequestTimeout(elasticSearchProperties.getConnectionRequestTimeout());
			return requestConfigBuilder;
		});

		// 异步连接数配置
		builder.setHttpClientConfigCallback(httpClientBuilder -> {
			httpClientBuilder.setMaxConnTotal(elasticSearchProperties.getMaxConnectNum());
			httpClientBuilder.setMaxConnPerRoute(elasticSearchProperties.getMaxConnectPerRoute());
			return httpClientBuilder;
		});

		return builder.build();
	}


	private HttpHost[]  getHttpHost (){
		List<HttpHost> hostList = new ArrayList<>();
		String [] clusterNodes = elasticSearchProperties.getClusterNodes().split(",");
		for(String node : clusterNodes){
			String[] addrs = node.split(":");
			hostList.add(new HttpHost(addrs[0], Integer.parseInt(addrs[1]),elasticSearchProperties.getSchema()));
		}
		return hostList.toArray(new HttpHost[]{});
	}


	@Bean
	public ElasticsearchTransport elasticsearchTransport (RestClient restClient) {
		return new RestClientTransport(restClient, new JacksonJsonpMapper());
	}

	@Bean
	public ElasticsearchClient elasticsearchClient (ElasticsearchTransport transport) {
		return new ElasticsearchClient(transport);
	}







}


标题:spring boot集成ES 7.15.x以上版本代码
作者:zytops
地址:https://zytops.com/articles/2022/11/21/1669020096402.html