Skip to content

Commit

Permalink
feat: elasticsearch 6.x 示例
Browse files Browse the repository at this point in the history
  • Loading branch information
dunwu committed Apr 10, 2024
1 parent 14b5853 commit 72e3c11
Show file tree
Hide file tree
Showing 23 changed files with 1,894 additions and 494 deletions.
4 changes: 2 additions & 2 deletions codes/javadb/elasticsearch/elasticsearch6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>o
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
Expand Down Expand Up @@ -42,7 +42,7 @@
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.8</version>
<version>5.8.25</version>
</dependency>

<dependency>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -38,11 +39,13 @@ public static RestClient newRestClient() {
}

public static RestClient newRestClient(String env) {
String hosts = getDefaultEsAddress(env);
return newRestClient(toHttpHostList(hosts));
String hostsConfig = getDefaultEsAddress(env);
List<String> hosts = StrUtil.split(hostsConfig, ",");
return newRestClient(hosts);
}

public static RestClient newRestClient(HttpHost[] httpHosts) {
public static RestClient newRestClient(Collection<String> hosts) {
HttpHost[] httpHosts = toHttpHostList(hosts);
RestClientBuilder builder = getRestClientBuilder(httpHosts);
if (builder == null) {
return null;
Expand All @@ -62,11 +65,13 @@ public static RestHighLevelClient newRestHighLevelClient() {
}

public static RestHighLevelClient newRestHighLevelClient(String env) {
String hosts = getDefaultEsAddress(env);
return newRestHighLevelClient(toHttpHostList(hosts));
String hostsConfig = getDefaultEsAddress(env);
List<String> hosts = StrUtil.split(hostsConfig, ",");
return newRestHighLevelClient(hosts);
}

public static RestHighLevelClient newRestHighLevelClient(HttpHost[] httpHosts) {
public static RestHighLevelClient newRestHighLevelClient(Collection<String> hosts) {
HttpHost[] httpHosts = toHttpHostList(hosts);
RestClientBuilder builder = getRestClientBuilder(httpHosts);
if (builder == null) {
return null;
Expand All @@ -86,12 +91,13 @@ public static ElasticsearchTemplate newElasticsearchTemplate() {
}

public static ElasticsearchTemplate newElasticsearchTemplate(String env) {
String hosts = getDefaultEsAddress(env);
return newElasticsearchTemplate(toHttpHostList(hosts));
String hostsConfig = getDefaultEsAddress(env);
List<String> hosts = StrUtil.split(hostsConfig, ",");
return newElasticsearchTemplate(hosts);
}

public static ElasticsearchTemplate newElasticsearchTemplate(HttpHost[] httpHosts) {
RestHighLevelClient client = newRestHighLevelClient(httpHosts);
public static ElasticsearchTemplate newElasticsearchTemplate(Collection<String> hosts) {
RestHighLevelClient client = newRestHighLevelClient(hosts);
if (client == null) {
return null;
}
Expand Down Expand Up @@ -125,21 +131,22 @@ public static RestClientBuilder getRestClientBuilder(HttpHost[] httpHosts) {
return restClientBuilder;
}

private static HttpHost[] toHttpHostList(String hosts) {
if (StrUtil.isBlank(hosts)) {
return null;
private static HttpHost[] toHttpHostList(Collection<String> hosts) {
if (CollectionUtil.isEmpty(hosts)) {
return new HttpHost[0];
}
List<String> strList = StrUtil.split(hosts, ",");
List<HttpHost> list = strList.stream().map(str -> {
List<String> params = StrUtil.split(str, ":");
return new HttpHost(params.get(0), Integer.parseInt(params.get(1)), "http");
}).collect(Collectors.toList());
List<HttpHost> list = hosts.stream().map(ElasticsearchFactory::toHttpHost).collect(Collectors.toList());
if (CollectionUtil.isEmpty(list)) {
return new HttpHost[0];
}
return list.toArray(new HttpHost[0]);
}

public static HttpHost toHttpHost(String host) {
List<String> params = StrUtil.split(host, ":");
return new HttpHost(params.get(0), Integer.parseInt(params.get(1)), "http");
}

public static String getDefaultEsAddress() {
// 从配置中心读取环境变量
String env = "test";
Expand Down
Loading

0 comments on commit 72e3c11

Please sign in to comment.