99import org .elasticsearch .client .RestClientBuilder ;
1010import org .elasticsearch .client .RestHighLevelClient ;
1111
12+ import java .util .Collection ;
1213import java .util .List ;
1314import java .util .stream .Collectors ;
1415
@@ -38,11 +39,13 @@ public static RestClient newRestClient() {
3839 }
3940
4041 public static RestClient newRestClient (String env ) {
41- String hosts = getDefaultEsAddress (env );
42- return newRestClient (toHttpHostList (hosts ));
42+ String hostsConfig = getDefaultEsAddress (env );
43+ List <String > hosts = StrUtil .split (hostsConfig , "," );
44+ return newRestClient (hosts );
4345 }
4446
45- public static RestClient newRestClient (HttpHost [] httpHosts ) {
47+ public static RestClient newRestClient (Collection <String > hosts ) {
48+ HttpHost [] httpHosts = toHttpHostList (hosts );
4649 RestClientBuilder builder = getRestClientBuilder (httpHosts );
4750 if (builder == null ) {
4851 return null ;
@@ -62,11 +65,13 @@ public static RestHighLevelClient newRestHighLevelClient() {
6265 }
6366
6467 public static RestHighLevelClient newRestHighLevelClient (String env ) {
65- String hosts = getDefaultEsAddress (env );
66- return newRestHighLevelClient (toHttpHostList (hosts ));
68+ String hostsConfig = getDefaultEsAddress (env );
69+ List <String > hosts = StrUtil .split (hostsConfig , "," );
70+ return newRestHighLevelClient (hosts );
6771 }
6872
69- public static RestHighLevelClient newRestHighLevelClient (HttpHost [] httpHosts ) {
73+ public static RestHighLevelClient newRestHighLevelClient (Collection <String > hosts ) {
74+ HttpHost [] httpHosts = toHttpHostList (hosts );
7075 RestClientBuilder builder = getRestClientBuilder (httpHosts );
7176 if (builder == null ) {
7277 return null ;
@@ -86,12 +91,13 @@ public static ElasticsearchTemplate newElasticsearchTemplate() {
8691 }
8792
8893 public static ElasticsearchTemplate newElasticsearchTemplate (String env ) {
89- String hosts = getDefaultEsAddress (env );
90- return newElasticsearchTemplate (toHttpHostList (hosts ));
94+ String hostsConfig = getDefaultEsAddress (env );
95+ List <String > hosts = StrUtil .split (hostsConfig , "," );
96+ return newElasticsearchTemplate (hosts );
9197 }
9298
93- public static ElasticsearchTemplate newElasticsearchTemplate (HttpHost [] httpHosts ) {
94- RestHighLevelClient client = newRestHighLevelClient (httpHosts );
99+ public static ElasticsearchTemplate newElasticsearchTemplate (Collection < String > hosts ) {
100+ RestHighLevelClient client = newRestHighLevelClient (hosts );
95101 if (client == null ) {
96102 return null ;
97103 }
@@ -125,21 +131,22 @@ public static RestClientBuilder getRestClientBuilder(HttpHost[] httpHosts) {
125131 return restClientBuilder ;
126132 }
127133
128- private static HttpHost [] toHttpHostList (String hosts ) {
129- if (StrUtil . isBlank (hosts )) {
130- return null ;
134+ private static HttpHost [] toHttpHostList (Collection < String > hosts ) {
135+ if (CollectionUtil . isEmpty (hosts )) {
136+ return new HttpHost [ 0 ] ;
131137 }
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 ());
138+ List <HttpHost > list = hosts .stream ().map (ElasticsearchFactory ::toHttpHost ).collect (Collectors .toList ());
137139 if (CollectionUtil .isEmpty (list )) {
138140 return new HttpHost [0 ];
139141 }
140142 return list .toArray (new HttpHost [0 ]);
141143 }
142144
145+ public static HttpHost toHttpHost (String host ) {
146+ List <String > params = StrUtil .split (host , ":" );
147+ return new HttpHost (params .get (0 ), Integer .parseInt (params .get (1 )), "http" );
148+ }
149+
143150 public static String getDefaultEsAddress () {
144151 // 从配置中心读取环境变量
145152 String env = "test" ;
0 commit comments