Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 14b5853

Browse files
committed
feat: 更新 elasticsearch 示例
1 parent a1d04b3 commit 14b5853

File tree

18 files changed

+1012
-261
lines changed

18 files changed

+1012
-261
lines changed
Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<modelVersion>4.0.0</modelVersion>
55

6+
<parent>o
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.7.7</version>
10+
</parent>
11+
612
<groupId>io.github.dunwu</groupId>
713
<artifactId>javadb-elasticsearch6</artifactId>
814
<version>1.0.0</version>
@@ -17,37 +23,47 @@
1723
</properties>
1824

1925
<dependencies>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-aop</artifactId>
29+
</dependency>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
2034
<dependency>
2135
<groupId>org.elasticsearch.client</groupId>
2236
<artifactId>elasticsearch-rest-high-level-client</artifactId>
23-
<version>6.4.3</version>
2437
</dependency>
2538
<dependency>
2639
<groupId>org.projectlombok</groupId>
2740
<artifactId>lombok</artifactId>
28-
<version>1.18.22</version>
2941
</dependency>
3042
<dependency>
3143
<groupId>cn.hutool</groupId>
3244
<artifactId>hutool-all</artifactId>
33-
<version>5.7.20</version>
34-
</dependency>
35-
<dependency>
36-
<groupId>com.fasterxml.jackson.core</groupId>
37-
<artifactId>jackson-databind</artifactId>
38-
<version>2.15.2</version>
45+
<version>5.8.8</version>
3946
</dependency>
4047

4148
<dependency>
42-
<groupId>ch.qos.logback</groupId>
43-
<artifactId>logback-classic</artifactId>
44-
<version>1.2.10</version>
45-
</dependency>
46-
<dependency>
47-
<groupId>org.apache.logging.log4j</groupId>
48-
<artifactId>log4j-to-slf4j</artifactId>
49-
<version>2.17.1</version>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-starter-test</artifactId>
51+
<scope>test</scope>
5052
</dependency>
5153
</dependencies>
54+
<dependencyManagement>
55+
<dependencies>
56+
<dependency>
57+
<groupId>org.elasticsearch.client</groupId>
58+
<artifactId>elasticsearch-rest-high-level-client</artifactId>
59+
<version>6.4.3</version>
60+
</dependency>
61+
<dependency>
62+
<groupId>org.elasticsearch</groupId>
63+
<artifactId>elasticsearch</artifactId>
64+
<version>6.4.3</version>
65+
</dependency>
66+
</dependencies>
67+
</dependencyManagement>
5268

5369
</project>

‎codes/javadb/elasticsearch/elasticsearch6/src/main/java/io/github/dunwu/javadb/elasticsearch/Demo.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,28 @@
22

33
import io.github.dunwu.javadb.elasticsearch.entity.User;
44
import io.github.dunwu.javadb.elasticsearch.mapper.UserEsMapper;
5-
import io.github.dunwu.javadb.elasticsearch.util.ElasticsearchUtil;
6-
import org.elasticsearch.client.RestHighLevelClient;
75

86
import java.io.IOException;
97
import java.util.Arrays;
108
import java.util.List;
119

1210
public class Demo {
1311

14-
private static final String HOSTS = "127.0.0.1:9200";
15-
private static final RestHighLevelClient restHighLevelClient = ElasticsearchUtil.newRestHighLevelClient(HOSTS);
12+
private static final String env = "test";
13+
private static final ElasticsearchTemplate elasticsearchTemplate
14+
= ElasticsearchFactory.newElasticsearchTemplate(env);
1615

1716
public static void main(String[] args) throws IOException, InterruptedException {
1817

19-
UserEsMapper mapper = new UserEsMapper(restHighLevelClient);
18+
UserEsMapper mapper = new UserEsMapper(elasticsearchTemplate);
2019

2120
System.out.println("索引是否存在:" + mapper.isIndexExists());
2221

2322
User jack = User.builder().id(1L).username("jack").age(18).build();
2423
User tom = User.builder().id(2L).username("tom").age(20).build();
2524
List<User> users = Arrays.asList(jack, tom);
2625

27-
System.out.println("批量插入:" + mapper.batchInsert(users));
26+
System.out.println("批量插入:" + mapper.batchSave(users));
2827
System.out.println("根据ID查询:" + mapper.getById("1").toString());
2928
System.out.println("根据ID查询:" + mapper.pojoById("2").toString());
3029
System.out.println("根据ID批量查询:" + mapper.pojoListByIds(Arrays.asList("1", "2")).toString());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /