application.yml添加配置

spring:
     elasticsearch:
        rest:
          uris: http://172.66.20.2:9200,http://172.66.20.3:9200
          username: elasticsearch
          password: elasticsearch

java配置类:


import lombok.SneakyThrows;
import org.apache.commons.lang3.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.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.museframework.management.ManagementConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;

import javax.net.ssl.SSLContext;

@Configuration
@Import(ElasticsearchAutoConfiguration.class)
@EnableElasticsearchRepositories(basePackageClasses = OnlineTraceRespository.class)
@ConditionalOnProperty(value = ManagementConfiguration.MANANGEMENT_PREFIX + ".trace-query", havingValue = "true")
@ConditionalOnClass({ ElasticsearchTemplate.class, AbstractElasticsearchConfiguration.class })
public class TransTraceConfiguration extends AbstractElasticsearchConfiguration {
	@Value("#{'${spring.elasticsearch.rest.uris}'.split(',')}")
	String[] uris;
	@Value("${spring.elasticsearch.rest.username:}")
	String username;
	@Value("${spring.elasticsearch.rest.password:}")
	String password;

	@Value("${spring.elasticsearch.rest.socket-timeout}")
	int socketTimeout;

	@Override
	public RestHighLevelClient elasticsearchClient() {
		HttpHost[] hosts = new HttpHost[uris.length];

		for (int i = 0; i < uris.length; i++) {
			hosts[i] = (HttpHost.create(uris[i]));
		}

		RestClientBuilder clientBuilder = RestClient.builder(hosts);
		clientBuilder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
			@SneakyThrows
			@Override
			public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
				if (!StringUtils.isEmpty(username) &amp;&amp; !StringUtils.isEmpty(password)) {
					CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
					UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
					credentialsProvider.setCredentials(AuthScope.ANY, credentials);
					httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
				}
				IOReactorConfig ioReactorConfig = IOReactorConfig.custom().setIoThreadCount(100)
						.setConnectTimeout(10000).setSoTimeout(10000).build();
				httpClientBuilder.setDefaultIOReactorConfig(ioReactorConfig);
				SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
					@Override
					public boolean isTrusted(java.security.cert.X509Certificate[] arg0, String arg1) {
						return true;
					}
				}).build();
				httpClientBuilder.setSSLContext(sslContext);
				httpClientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
				return httpClientBuilder;
			}
		});

		return new RestHighLevelClient(clientBuilder);
	}
}

Repository类:


import com.ibm.banking.musemvp.parameter.entity.BatchTraceEntity;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface BatchTraceRepository extends ElasticsearchRepository<BatchTraceEntity, Long> {
//空类,哈哈,简单就是美
}

调用代码

    public List<BatchTraceEntity> searchJobSliceInfo(Integer jobId, String startEnd) {
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.termsQuery("mainJobId", jobId+"")); //ES里面这个字段文本类型,要转一下
        if (StringUtils.isNotBlank(startEnd)) {
            boolQueryBuilder.must(QueryBuilders.termsQuery("jobAction", startEnd.toLowerCase()));
        }
        ArrayList<BatchTraceEntity> slices = new ArrayList<>(40);
        batchTraceRepository.search(boolQueryBuilder).forEach(s -> slices.add(s));
        return slices;
    }

原文地址:https://blog.csdn.net/weixin_45357522/article/details/134729405

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_23344.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注