forked from xkcoding/spring-boot-demo
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
797 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
spring-boot-demo-elasticsearch-rest-high-level-client/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
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> | ||
<artifactId>spring-boot-demo</artifactId> | ||
<groupId>com.xkcoding</groupId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>spring-boot-demo-elasticsearch-rest-high-level-client</artifactId> | ||
<name>spring-boot-demo-elasticsearch-rest-high-level-client</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter</artifactId> | ||
</dependency> | ||
|
||
<!-- test --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<!-- validator --> | ||
<dependency> | ||
<groupId>org.hibernate.validator</groupId> | ||
<artifactId>hibernate-validator</artifactId> | ||
<scope>compile</scope> | ||
</dependency> | ||
|
||
<!-- | ||
You can easily generate your own configuration metadata file from items annotated with | ||
@ConfigurationProperties by using the spring-boot-configuration-processor jar. | ||
--> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-configuration-processor</artifactId> | ||
</dependency> | ||
|
||
<!-- elasticsearch --> | ||
<dependency> | ||
<groupId>org.elasticsearch</groupId> | ||
<artifactId>elasticsearch</artifactId> | ||
<version>7.3.0</version> | ||
</dependency> | ||
|
||
<!-- elasticsearch-rest-high-level-client --> | ||
<dependency> | ||
<groupId>org.elasticsearch.client</groupId> | ||
<artifactId>elasticsearch-rest-high-level-client</artifactId> | ||
<version>7.3.0</version> | ||
<exclusions> | ||
<!-- <exclusion>--> | ||
<!-- <groupId>org.elasticsearch.client</groupId>--> | ||
<!-- <artifactId>elasticsearch-rest-client</artifactId>--> | ||
<!-- </exclusion>--> | ||
<exclusion> | ||
<groupId>org.elasticsearch</groupId> | ||
<artifactId>elasticsearch</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<!-- lombok --> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
20 changes: 20 additions & 0 deletions
20
...-high-level-client/src/main/java/com/xlcoding/elasticsearch/ElasticsearchApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.xlcoding.elasticsearch; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* ElasticsearchApplication | ||
* | ||
* @author fxbin | ||
* @version v1.0 | ||
* @since 2019/9/15 23:10 | ||
*/ | ||
@SpringBootApplication | ||
public class ElasticsearchApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(ElasticsearchApplication.class, args); | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...rc/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.xlcoding.elasticsearch.autoconfigure; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.elasticsearch.client.RestClient; | ||
import org.elasticsearch.client.RestClientBuilder; | ||
import org.elasticsearch.client.RestHighLevelClient; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.StringUtils; | ||
|
||
import javax.annotation.Resource; | ||
import javax.validation.constraints.NotNull; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* ElasticsearchAutoConfiguration | ||
* | ||
* @author fxbin | ||
* @version v1.0 | ||
* @since 2019/9/15 22:59 | ||
*/ | ||
@EnableConfigurationProperties(ElasticsearchProperties.class) | ||
public class ElasticsearchAutoConfiguration { | ||
|
||
@SuppressWarnings("NullableProblems") | ||
@NotNull | ||
@Resource | ||
private ElasticsearchProperties elasticsearchProperties; | ||
|
||
private List<HttpHost> httpHosts = new ArrayList<>(); | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public RestHighLevelClient restHighLevelClient() { | ||
|
||
List<String> clusterNodes = elasticsearchProperties.getClusterNodes(); | ||
clusterNodes.forEach(node -> { | ||
try { | ||
String[] parts = StringUtils.split(node, ":"); | ||
Assert.notNull(parts, "Must defined"); | ||
Assert.state(parts.length == 2, "Must be defined as 'host:port'"); | ||
httpHosts.add(new HttpHost(parts[0], Integer.parseInt(parts[1]), elasticsearchProperties.getSchema())); | ||
} catch (Exception e) { | ||
throw new IllegalStateException( | ||
"Invalid ES nodes " + "property '" + node + "'", e); | ||
} | ||
}); | ||
RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[0])); | ||
|
||
return getRestHighLevelClient(builder, elasticsearchProperties); | ||
} | ||
|
||
|
||
/** | ||
* get restHistLevelClient | ||
* | ||
* @author fxbin | ||
* @param builder RestClientBuilder | ||
* @param elasticsearchProperties elasticsearch default properties | ||
* @return {@link org.elasticsearch.client.RestHighLevelClient} | ||
*/ | ||
private static RestHighLevelClient getRestHighLevelClient(RestClientBuilder builder, ElasticsearchProperties elasticsearchProperties) { | ||
|
||
// Callback used the default {@link RequestConfig} being set to the {@link CloseableHttpClient} | ||
builder.setRequestConfigCallback(requestConfigBuilder -> { | ||
requestConfigBuilder.setConnectTimeout(elasticsearchProperties.getConnectTimeout()); | ||
requestConfigBuilder.setSocketTimeout(elasticsearchProperties.getSocketTimeout()); | ||
requestConfigBuilder.setConnectionRequestTimeout(elasticsearchProperties.getConnectionRequestTimeout()); | ||
return requestConfigBuilder; | ||
}); | ||
|
||
// Callback used to customize the {@link CloseableHttpClient} instance used by a {@link RestClient} instance. | ||
builder.setHttpClientConfigCallback(httpClientBuilder -> { | ||
httpClientBuilder.setMaxConnTotal(elasticsearchProperties.getMaxConnectTotal()); | ||
httpClientBuilder.setMaxConnPerRoute(elasticsearchProperties.getMaxConnectPerRoute()); | ||
return httpClientBuilder; | ||
}); | ||
return new RestHighLevelClient(builder); | ||
} | ||
|
||
|
||
} |
91 changes: 91 additions & 0 deletions
91
...lient/src/main/java/com/xlcoding/elasticsearch/autoconfigure/ElasticsearchProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.xlcoding.elasticsearch.autoconfigure; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* ElasticsearchProperties | ||
* | ||
* @author fxbin | ||
* @version v1.0 | ||
* @since 2019/9/15 22:58 | ||
*/ | ||
@Data | ||
@Builder | ||
@Component | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@ConfigurationProperties(prefix = "demo.data.elasticsearch") | ||
public class ElasticsearchProperties { | ||
|
||
/** | ||
* 请求协议 | ||
*/ | ||
private String schema = "http"; | ||
|
||
/** | ||
* 集群名称 | ||
*/ | ||
private String clusterName = "elasticsearch"; | ||
|
||
/** | ||
* 集群节点 | ||
*/ | ||
@NotNull(message = "集群节点不允许为空") | ||
private List<String> clusterNodes = new ArrayList<>(); | ||
|
||
/** | ||
* 连接超时时间(毫秒) | ||
*/ | ||
private Integer connectTimeout = 1000; | ||
|
||
/** | ||
* socket 超时时间 | ||
*/ | ||
private Integer socketTimeout = 30000; | ||
|
||
/** | ||
* 连接请求超时时间 | ||
*/ | ||
private Integer connectionRequestTimeout = 500; | ||
|
||
/** | ||
* 每个路由的最大连接数量 | ||
*/ | ||
private Integer maxConnectPerRoute = 10; | ||
|
||
/** | ||
* 最大连接总数量 | ||
*/ | ||
private Integer maxConnectTotal = 30; | ||
|
||
/** | ||
* 索引配置信息 | ||
*/ | ||
private Index index; | ||
|
||
@Data | ||
@Builder | ||
public static class Index { | ||
|
||
/** | ||
* 分片数量 | ||
*/ | ||
protected Integer numberOfShards = 3; | ||
|
||
/** | ||
* 副本数量 | ||
*/ | ||
protected Integer numberOfReplicas = 2; | ||
|
||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...high-level-client/src/main/java/com/xlcoding/elasticsearch/contants/DataTypeTransfer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.xlcoding.elasticsearch.contants; | ||
|
||
/** | ||
* DataTypeTransfer | ||
* | ||
* @author fxbin | ||
* @version 1.0v | ||
* @since 2019/9/16 18:00 | ||
*/ | ||
public enum DataTypeTransfer { | ||
} |
17 changes: 17 additions & 0 deletions
17
...level-client/src/main/java/com/xlcoding/elasticsearch/contants/ElasticsearchConstant.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.xlcoding.elasticsearch.contants; | ||
|
||
/** | ||
* ElasticsearchConstant | ||
* | ||
* @author fxbin | ||
* @version v1.0 | ||
* @since 2019/9/15 23:03 | ||
*/ | ||
public interface ElasticsearchConstant { | ||
|
||
/** | ||
* 索引名称 | ||
*/ | ||
String INDEX_NAME = "person"; | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
...vel-client/src/main/java/com/xlcoding/elasticsearch/exception/ElasticsearchException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.xlcoding.elasticsearch.exception; | ||
|
||
import com.xlcoding.elasticsearch.model.ResultCode; | ||
import lombok.Getter; | ||
|
||
/** | ||
* ElasticsearchException | ||
* | ||
* @author fxbin | ||
* @version v1.0 | ||
* @since 2019/8/26 1:53 | ||
*/ | ||
public class ElasticsearchException extends RuntimeException { | ||
|
||
@Getter | ||
private int errcode; | ||
|
||
@Getter | ||
private String errmsg; | ||
|
||
public ElasticsearchException(ResultCode resultCode) { | ||
this(resultCode.getCode(), resultCode.getMsg()); | ||
} | ||
|
||
public ElasticsearchException(String message) { | ||
super(message); | ||
} | ||
|
||
public ElasticsearchException(Integer errcode, String errmsg) { | ||
super(errmsg); | ||
this.errcode = errcode; | ||
this.errmsg = errmsg; | ||
} | ||
|
||
public ElasticsearchException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public ElasticsearchException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public ElasticsearchException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
super(message, cause, enableSuppression, writableStackTrace); | ||
} | ||
|
||
} |
Oops, something went wrong.