|
| 1 | +package io.github.dunwu.javadb.elasticsearch; |
| 2 | + |
| 3 | +import cn.hutool.core.collection.CollectionUtil; |
| 4 | +import cn.hutool.core.util.ArrayUtil; |
| 5 | +import cn.hutool.core.util.StrUtil; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
| 7 | +import org.apache.http.HttpHost; |
| 8 | +import org.elasticsearch.client.RestClient; |
| 9 | +import org.elasticsearch.client.RestClientBuilder; |
| 10 | +import org.elasticsearch.client.RestHighLevelClient; |
| 11 | + |
| 12 | +import java.util.List; |
| 13 | +import java.util.stream.Collectors; |
| 14 | + |
| 15 | +/** |
| 16 | + * Elasticsearch 客户端实例工厂 |
| 17 | + * |
| 18 | + * @author <a href="mailto:forbreak@163.com">Zhang Peng</a> |
| 19 | + * @date 2024-02-07 |
| 20 | + */ |
| 21 | +@Slf4j |
| 22 | +public class ElasticsearchFactory { |
| 23 | + |
| 24 | + public static int CONNECT_TIMEOUT_MILLIS = 1000; |
| 25 | + |
| 26 | + public static int SOCKET_TIMEOUT_MILLIS = 30000; |
| 27 | + |
| 28 | + public static int CONNECTION_REQUEST_TIMEOUT_MILLIS = 500; |
| 29 | + |
| 30 | + public static int MAX_CONN_TOTAL = 30; |
| 31 | + |
| 32 | + public static int MAX_CONN_PER_ROUTE = 10; |
| 33 | + |
| 34 | + public static RestClient newRestClient() { |
| 35 | + // 从配置中心读取环境变量 |
| 36 | + String env = "test"; |
| 37 | + return newRestClient(env); |
| 38 | + } |
| 39 | + |
| 40 | + public static RestClient newRestClient(String env) { |
| 41 | + String hosts = getDefaultEsAddress(env); |
| 42 | + return newRestClient(toHttpHostList(hosts)); |
| 43 | + } |
| 44 | + |
| 45 | + public static RestClient newRestClient(HttpHost[] httpHosts) { |
| 46 | + RestClientBuilder builder = getRestClientBuilder(httpHosts); |
| 47 | + if (builder == null) { |
| 48 | + return null; |
| 49 | + } |
| 50 | + try { |
| 51 | + return builder.build(); |
| 52 | + } catch (Exception e) { |
| 53 | + log.error("【ES】connect failed.", e); |
| 54 | + return null; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public static RestHighLevelClient newRestHighLevelClient() { |
| 59 | + // 从配置中心读取环境变量 |
| 60 | + String env = "test"; |
| 61 | + return newRestHighLevelClient(env); |
| 62 | + } |
| 63 | + |
| 64 | + public static RestHighLevelClient newRestHighLevelClient(String env) { |
| 65 | + String hosts = getDefaultEsAddress(env); |
| 66 | + return newRestHighLevelClient(toHttpHostList(hosts)); |
| 67 | + } |
| 68 | + |
| 69 | + public static RestHighLevelClient newRestHighLevelClient(HttpHost[] httpHosts) { |
| 70 | + RestClientBuilder builder = getRestClientBuilder(httpHosts); |
| 71 | + if (builder == null) { |
| 72 | + return null; |
| 73 | + } |
| 74 | + try { |
| 75 | + return new RestHighLevelClient(builder); |
| 76 | + } catch (Exception e) { |
| 77 | + log.error("【ES】connect failed.", e); |
| 78 | + return null; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static ElasticsearchTemplate newElasticsearchTemplate() { |
| 83 | + // 从配置中心读取环境变量 |
| 84 | + String env = "test"; |
| 85 | + return newElasticsearchTemplate(env); |
| 86 | + } |
| 87 | + |
| 88 | + public static ElasticsearchTemplate newElasticsearchTemplate(String env) { |
| 89 | + String hosts = getDefaultEsAddress(env); |
| 90 | + return newElasticsearchTemplate(toHttpHostList(hosts)); |
| 91 | + } |
| 92 | + |
| 93 | + public static ElasticsearchTemplate newElasticsearchTemplate(HttpHost[] httpHosts) { |
| 94 | + RestHighLevelClient client = newRestHighLevelClient(httpHosts); |
| 95 | + if (client == null) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + return new ElasticsearchTemplate(client); |
| 99 | + } |
| 100 | + |
| 101 | + public static ElasticsearchTemplate newElasticsearchTemplate(RestHighLevelClient client) { |
| 102 | + if (client == null) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + return new ElasticsearchTemplate(client); |
| 106 | + } |
| 107 | + |
| 108 | + public static RestClientBuilder getRestClientBuilder(HttpHost[] httpHosts) { |
| 109 | + if (ArrayUtil.isEmpty(httpHosts)) { |
| 110 | + log.error("【ES】connect failed. hosts are empty."); |
| 111 | + return null; |
| 112 | + } |
| 113 | + RestClientBuilder restClientBuilder = RestClient.builder(httpHosts); |
| 114 | + restClientBuilder.setRequestConfigCallback(builder -> { |
| 115 | + builder.setConnectTimeout(CONNECT_TIMEOUT_MILLIS); |
| 116 | + builder.setSocketTimeout(SOCKET_TIMEOUT_MILLIS); |
| 117 | + builder.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT_MILLIS); |
| 118 | + return builder; |
| 119 | + }); |
| 120 | + restClientBuilder.setHttpClientConfigCallback(builder -> { |
| 121 | + builder.setMaxConnTotal(MAX_CONN_TOTAL); |
| 122 | + builder.setMaxConnPerRoute(MAX_CONN_PER_ROUTE); |
| 123 | + return builder; |
| 124 | + }); |
| 125 | + return restClientBuilder; |
| 126 | + } |
| 127 | + |
| 128 | + private static HttpHost[] toHttpHostList(String hosts) { |
| 129 | + if (StrUtil.isBlank(hosts)) { |
| 130 | + return null; |
| 131 | + } |
| 132 | + List<String> strList = StrUtil.split(hosts, ","); |
| 133 | + List<HttpHost> list = strList.stream().map(str -> { |
| 134 | + List<String> params = StrUtil.split(str, ":"); |
| 135 | + return new HttpHost(params.get(0), Integer.parseInt(params.get(1)), "http"); |
| 136 | + }).collect(Collectors.toList()); |
| 137 | + if (CollectionUtil.isEmpty(list)) { |
| 138 | + return new HttpHost[0]; |
| 139 | + } |
| 140 | + return list.toArray(new HttpHost[0]); |
| 141 | + } |
| 142 | + |
| 143 | + public static String getDefaultEsAddress() { |
| 144 | + // 从配置中心读取环境变量 |
| 145 | + String env = "test"; |
| 146 | + return getDefaultEsAddress(env); |
| 147 | + } |
| 148 | + |
| 149 | + private static String getDefaultEsAddress(String env) { |
| 150 | + String defaultAddress; |
| 151 | + switch (env) { |
| 152 | + case "prd": |
| 153 | + defaultAddress = "127.0.0.1:9200,127.0.0.2:9200,127.0.0.3:9200"; |
| 154 | + break; |
| 155 | + case "pre": |
| 156 | + defaultAddress = "127.0.0.1:9200"; |
| 157 | + break; |
| 158 | + case "test": |
| 159 | + default: |
| 160 | + defaultAddress = "127.0.0.1:9200"; |
| 161 | + break; |
| 162 | + } |
| 163 | + return defaultAddress; |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments