Skip to content

Commit adcc304

Browse files
fxbinxkcoding
authored andcommitted
init
1 parent 1e4bc73 commit adcc304

File tree

18 files changed

+797
-0
lines changed

18 files changed

+797
-0
lines changed

pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<module>spring-boot-demo-ldap</module>
6666
<module>spring-boot-demo-dynamic-datasource</module>
6767
<module>spring-boot-demo-ratelimit-guava</module>
68+
<module>spring-boot-demo-elasticsearch-rest-high-level-client</module>
6869
</modules>
6970
<packaging>pom</packaging>
7071

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
<parent>
7+
<artifactId>spring-boot-demo</artifactId>
8+
<groupId>com.xkcoding</groupId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
</parent>
11+
12+
<artifactId>spring-boot-demo-elasticsearch-rest-high-level-client</artifactId>
13+
<name>spring-boot-demo-elasticsearch-rest-high-level-client</name>
14+
<description>Demo project for Spring Boot</description>
15+
16+
<properties>
17+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
18+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
19+
<java.version>1.8</java.version>
20+
</properties>
21+
22+
<dependencies>
23+
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter</artifactId>
27+
</dependency>
28+
29+
<!-- test -->
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-test</artifactId>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<!-- validator -->
37+
<dependency>
38+
<groupId>org.hibernate.validator</groupId>
39+
<artifactId>hibernate-validator</artifactId>
40+
<scope>compile</scope>
41+
</dependency>
42+
43+
<!--
44+
You can easily generate your own configuration metadata file from items annotated with
45+
@ConfigurationProperties by using the spring-boot-configuration-processor jar.
46+
-->
47+
<dependency>
48+
<groupId>org.springframework.boot</groupId>
49+
<artifactId>spring-boot-configuration-processor</artifactId>
50+
</dependency>
51+
52+
<!-- elasticsearch -->
53+
<dependency>
54+
<groupId>org.elasticsearch</groupId>
55+
<artifactId>elasticsearch</artifactId>
56+
<version>7.3.0</version>
57+
</dependency>
58+
59+
<!-- elasticsearch-rest-high-level-client -->
60+
<dependency>
61+
<groupId>org.elasticsearch.client</groupId>
62+
<artifactId>elasticsearch-rest-high-level-client</artifactId>
63+
<version>7.3.0</version>
64+
<exclusions>
65+
<!-- <exclusion>-->
66+
<!-- <groupId>org.elasticsearch.client</groupId>-->
67+
<!-- <artifactId>elasticsearch-rest-client</artifactId>-->
68+
<!-- </exclusion>-->
69+
<exclusion>
70+
<groupId>org.elasticsearch</groupId>
71+
<artifactId>elasticsearch</artifactId>
72+
</exclusion>
73+
</exclusions>
74+
</dependency>
75+
76+
<!-- lombok -->
77+
<dependency>
78+
<groupId>org.projectlombok</groupId>
79+
<artifactId>lombok</artifactId>
80+
<optional>true</optional>
81+
</dependency>
82+
83+
</dependencies>
84+
85+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.xlcoding.elasticsearch;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
/**
7+
* ElasticsearchApplication
8+
*
9+
* @author fxbin
10+
* @version v1.0
11+
* @since 2019/9/15 23:10
12+
*/
13+
@SpringBootApplication
14+
public class ElasticsearchApplication {
15+
16+
public static void main(String[] args) {
17+
SpringApplication.run(ElasticsearchApplication.class, args);
18+
}
19+
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.xlcoding.elasticsearch.autoconfigure;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
import org.springframework.boot.context.properties.ConfigurationProperties;
8+
import org.springframework.stereotype.Component;
9+
10+
import javax.validation.constraints.NotNull;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
/**
15+
* ElasticsearchProperties
16+
*
17+
* @author fxbin
18+
* @version v1.0
19+
* @since 2019/9/15 22:58
20+
*/
21+
@Data
22+
@Builder
23+
@Component
24+
@NoArgsConstructor
25+
@AllArgsConstructor
26+
@ConfigurationProperties(prefix = "demo.data.elasticsearch")
27+
public class ElasticsearchProperties {
28+
29+
/**
30+
* 请求协议
31+
*/
32+
private String schema = "http";
33+
34+
/**
35+
* 集群名称
36+
*/
37+
private String clusterName = "elasticsearch";
38+
39+
/**
40+
* 集群节点
41+
*/
42+
@NotNull(message = "集群节点不允许为空")
43+
private List<String> clusterNodes = new ArrayList<>();
44+
45+
/**
46+
* 连接超时时间(毫秒)
47+
*/
48+
private Integer connectTimeout = 1000;
49+
50+
/**
51+
* socket 超时时间
52+
*/
53+
private Integer socketTimeout = 30000;
54+
55+
/**
56+
* 连接请求超时时间
57+
*/
58+
private Integer connectionRequestTimeout = 500;
59+
60+
/**
61+
* 每个路由的最大连接数量
62+
*/
63+
private Integer maxConnectPerRoute = 10;
64+
65+
/**
66+
* 最大连接总数量
67+
*/
68+
private Integer maxConnectTotal = 30;
69+
70+
/**
71+
* 索引配置信息
72+
*/
73+
private Index index;
74+
75+
@Data
76+
@Builder
77+
public static class Index {
78+
79+
/**
80+
* 分片数量
81+
*/
82+
protected Integer numberOfShards = 3;
83+
84+
/**
85+
* 副本数量
86+
*/
87+
protected Integer numberOfReplicas = 2;
88+
89+
}
90+
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.xlcoding.elasticsearch.contants;
2+
3+
/**
4+
* DataTypeTransfer
5+
*
6+
* @author fxbin
7+
* @version 1.0v
8+
* @since 2019/9/16 18:00
9+
*/
10+
public enum DataTypeTransfer {
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.xlcoding.elasticsearch.contants;
2+
3+
/**
4+
* ElasticsearchConstant
5+
*
6+
* @author fxbin
7+
* @version v1.0
8+
* @since 2019/9/15 23:03
9+
*/
10+
public interface ElasticsearchConstant {
11+
12+
/**
13+
* 索引名称
14+
*/
15+
String INDEX_NAME = "person";
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.xlcoding.elasticsearch.exception;
2+
3+
import com.xlcoding.elasticsearch.model.ResultCode;
4+
import lombok.Getter;
5+
6+
/**
7+
* ElasticsearchException
8+
*
9+
* @author fxbin
10+
* @version v1.0
11+
* @since 2019/8/26 1:53
12+
*/
13+
public class ElasticsearchException extends RuntimeException {
14+
15+
@Getter
16+
private int errcode;
17+
18+
@Getter
19+
private String errmsg;
20+
21+
public ElasticsearchException(ResultCode resultCode) {
22+
this(resultCode.getCode(), resultCode.getMsg());
23+
}
24+
25+
public ElasticsearchException(String message) {
26+
super(message);
27+
}
28+
29+
public ElasticsearchException(Integer errcode, String errmsg) {
30+
super(errmsg);
31+
this.errcode = errcode;
32+
this.errmsg = errmsg;
33+
}
34+
35+
public ElasticsearchException(String message, Throwable cause) {
36+
super(message, cause);
37+
}
38+
39+
public ElasticsearchException(Throwable cause) {
40+
super(cause);
41+
}
42+
43+
public ElasticsearchException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
44+
super(message, cause, enableSuppression, writableStackTrace);
45+
}
46+
47+
}

0 commit comments

Comments
 (0)