自学内容网 自学内容网

java操作ES(一)RestHighLevelClient(2)集成与demo

一、集成方法

1、pom依赖
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.x.x</version> <!-- 请使用与你的Elasticsearch版本相匹配的版本 -->
</dependency>
2、配置文件
demo.es.active=true
demo.es.hosts= xxx.xx.xxx.xx
demo.es.path-prefix=xxx
demo.es.connect-timeout=5000
demo.es.socket-timeout=10000
demo.es.max-conn-total=100
demo.es.max-conn-per-route=100
3、javaBean配置
@Slf4j
@Configuration
@ConditionalOnExpression("${demo.es.active:false}")
public class RestHighLevelClientConfig {

    @Value("${demo.es.hosts}")
    private String hosts;

    @Value("${demo.es.connect-timeout}")
    private int connectTimeout;

    @Value("${demo.es.socket-timeout}")
    private int socketTimeout;

    @Value("${demo.es.max-conn-total}")
    private int maxConnTotal;

    @Value("${demo.es.max-conn-per-route}")
    private int maxConnPerRoute;

    @Value("${demo.es.path-prefix}")
    private String pathPrefix;

    @Bean("restHighLevelClient")
    public RestHighLevelClient getRestHighLevelClient() {
        RestClientBuilder builder = RestClient.builder(resolveHosts(hosts));
        builder.setHttpClientConfigCallback(
                httpClientBuilder -> {
                    
                    httpClientBuilder.setSSLHostnameVerifier((hostname, session) -> {
                        try {
                            URL url = new URL(hosts);
                            return hostname.equalsIgnoreCase(url.getHost());
                        } catch (MalformedURLException | RuntimeException e) {
                            return false;
                        }
                    });
                    httpClientBuilder.setMaxConnTotal(maxConnTotal);
                    httpClientBuilder.setMaxConnPerRoute(maxConnPerRoute);
                    httpClientBuilder.setSSLContext(getSSLContext());
                    return httpClientBuilder;
                });

        builder.setPathPrefix(pathPrefix);
        builder.setRequestConfigCallback(
                requestConfigBuilder ->
                        requestConfigBuilder
                                .setConnectTimeout(connectTimeout)
                                .setSocketTimeout(socketTimeout));

        return new RestHighLevelClient(builder);
    }

    private HttpHost[] resolveHosts(String hosts) {
        if (StringUtils.isBlank(hosts)) {
            return new HttpHost[0];
        }
        return Arrays.stream(StringUtils.split(hosts, ","))
                .filter(StringUtils::isNotBlank)
                .map(HttpHost::create)
                .toArray(HttpHost[]::new);
    }

    private SSLContext getSSLContext() {
        return SSLContexts.createDefault();
    }
}
4、使用

 代码中直接注入RestHighLevelClient 使用即可。

@Autowired 
private RestHighLevelClient restHighLevelClient;

二、demo


原文地址:https://blog.csdn.net/w_t_y_y/article/details/143629274

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!