Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
fxbin authored and xkcoding committed Sep 17, 2019
1 parent 1e4bc73 commit adcc304
Show file tree
Hide file tree
Showing 18 changed files with 797 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<module>spring-boot-demo-ldap</module>
<module>spring-boot-demo-dynamic-datasource</module>
<module>spring-boot-demo-ratelimit-guava</module>
<module>spring-boot-demo-elasticsearch-rest-high-level-client</module>
</modules>
<packaging>pom</packaging>

Expand Down
85 changes: 85 additions & 0 deletions spring-boot-demo-elasticsearch-rest-high-level-client/pom.xml
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>
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);
}

}
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);
}


}
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;

}

}
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 {
}
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";

}
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);
}

}
Loading

0 comments on commit adcc304

Please sign in to comment.