|
| 1 | +package com.xlcoding.elasticsearch.autoconfigure; |
| 2 | + |
| 3 | +import org.apache.http.HttpHost; |
| 4 | +import org.elasticsearch.client.RestClient; |
| 5 | +import org.elasticsearch.client.RestClientBuilder; |
| 6 | +import org.elasticsearch.client.RestHighLevelClient; |
| 7 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 8 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 9 | +import org.springframework.context.annotation.Bean; |
| 10 | +import org.springframework.util.Assert; |
| 11 | +import org.springframework.util.StringUtils; |
| 12 | + |
| 13 | +import javax.annotation.Resource; |
| 14 | +import javax.validation.constraints.NotNull; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | + |
| 18 | +/** |
| 19 | + * ElasticsearchAutoConfiguration |
| 20 | + * |
| 21 | + * @author fxbin |
| 22 | + * @version v1.0 |
| 23 | + * @since 2019/9/15 22:59 |
| 24 | + */ |
| 25 | +@EnableConfigurationProperties(ElasticsearchProperties.class) |
| 26 | +public class ElasticsearchAutoConfiguration { |
| 27 | + |
| 28 | + @SuppressWarnings("NullableProblems") |
| 29 | + @NotNull |
| 30 | + @Resource |
| 31 | + private ElasticsearchProperties elasticsearchProperties; |
| 32 | + |
| 33 | + private List<HttpHost> httpHosts = new ArrayList<>(); |
| 34 | + |
| 35 | + @Bean |
| 36 | + @ConditionalOnMissingBean |
| 37 | + public RestHighLevelClient restHighLevelClient() { |
| 38 | + |
| 39 | + List<String> clusterNodes = elasticsearchProperties.getClusterNodes(); |
| 40 | + clusterNodes.forEach(node -> { |
| 41 | + try { |
| 42 | + String[] parts = StringUtils.split(node, ":"); |
| 43 | + Assert.notNull(parts, "Must defined"); |
| 44 | + Assert.state(parts.length == 2, "Must be defined as 'host:port'"); |
| 45 | + httpHosts.add(new HttpHost(parts[0], Integer.parseInt(parts[1]), elasticsearchProperties.getSchema())); |
| 46 | + } catch (Exception e) { |
| 47 | + throw new IllegalStateException( |
| 48 | + "Invalid ES nodes " + "property '" + node + "'", e); |
| 49 | + } |
| 50 | + }); |
| 51 | + RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[0])); |
| 52 | + |
| 53 | + return getRestHighLevelClient(builder, elasticsearchProperties); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + /** |
| 58 | + * get restHistLevelClient |
| 59 | + * |
| 60 | + * @author fxbin |
| 61 | + * @param builder RestClientBuilder |
| 62 | + * @param elasticsearchProperties elasticsearch default properties |
| 63 | + * @return {@link org.elasticsearch.client.RestHighLevelClient} |
| 64 | + */ |
| 65 | + private static RestHighLevelClient getRestHighLevelClient(RestClientBuilder builder, ElasticsearchProperties elasticsearchProperties) { |
| 66 | + |
| 67 | + // Callback used the default {@link RequestConfig} being set to the {@link CloseableHttpClient} |
| 68 | + builder.setRequestConfigCallback(requestConfigBuilder -> { |
| 69 | + requestConfigBuilder.setConnectTimeout(elasticsearchProperties.getConnectTimeout()); |
| 70 | + requestConfigBuilder.setSocketTimeout(elasticsearchProperties.getSocketTimeout()); |
| 71 | + requestConfigBuilder.setConnectionRequestTimeout(elasticsearchProperties.getConnectionRequestTimeout()); |
| 72 | + return requestConfigBuilder; |
| 73 | + }); |
| 74 | + |
| 75 | + // Callback used to customize the {@link CloseableHttpClient} instance used by a {@link RestClient} instance. |
| 76 | + builder.setHttpClientConfigCallback(httpClientBuilder -> { |
| 77 | + httpClientBuilder.setMaxConnTotal(elasticsearchProperties.getMaxConnectTotal()); |
| 78 | + httpClientBuilder.setMaxConnPerRoute(elasticsearchProperties.getMaxConnectPerRoute()); |
| 79 | + return httpClientBuilder; |
| 80 | + }); |
| 81 | + return new RestHighLevelClient(builder); |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | +} |
0 commit comments