Skip to content

[pull] master from dunwu:master #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"compact": false
}
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ insert_final_newline = true
[*.{bat, cmd}]
end_of_line = crlf

[*.{java, gradle, groovy, kt, sh, xml}]
[*.{java, gradle, groovy, kt, sh}]
indent_size = 4

[*.md]
Expand Down
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*.less text
*.sql text
*.properties text
*.md text

# unix style
*.sh text eol=lf
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

strategy:
matrix:
node-version: [14.x]
node-version: [16.x]

steps:
# 使用的动作。格式:userName/repoName。作用:检出仓库,获取源码。 官方actions库:https://github.com/actions
Expand Down
9 changes: 7 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,31 @@ hs_err_pid*

# maven plugin temp files
.flattened-pom.xml
package-lock.json


# ------------------------------- javascript -------------------------------
# dependencies
node_modules

# temp folders
.temp
build
dist
_book
_jsdoc
.temp
.deploy*/

# temp files
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
bundle*.js
.DS_Store
Thumbs.db
db.json
book.pdf
package-lock.json


# ------------------------------- intellij -------------------------------
Expand Down
180 changes: 93 additions & 87 deletions README.md

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions codes/javadb/elasticsearch/elasticsearch6/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
</parent>

<groupId>io.github.dunwu</groupId>
<artifactId>javadb-elasticsearch6</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
<java.version>1.8</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.25</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.4.3</version>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.4.3</version>
</dependency>
</dependencies>
</dependencyManagement>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package io.github.dunwu.javadb.elasticsearch;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;

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

/**
* Elasticsearch 客户端实例工厂
*
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @date 2024-02-07
*/
@Slf4j
public class ElasticsearchFactory {

public static int CONNECT_TIMEOUT_MILLIS = 1000;

public static int SOCKET_TIMEOUT_MILLIS = 30000;

public static int CONNECTION_REQUEST_TIMEOUT_MILLIS = 500;

public static int MAX_CONN_TOTAL = 30;

public static int MAX_CONN_PER_ROUTE = 10;

public static RestClient newRestClient() {
// 从配置中心读取环境变量
String env = "test";
return newRestClient(env);
}

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

public static RestClient newRestClient(Collection<String> hosts) {
HttpHost[] httpHosts = toHttpHostList(hosts);
RestClientBuilder builder = getRestClientBuilder(httpHosts);
if (builder == null) {
return null;
}
try {
return builder.build();
} catch (Exception e) {
log.error("【ES】connect failed.", e);
return null;
}
}

public static RestHighLevelClient newRestHighLevelClient() {
// 从配置中心读取环境变量
String env = "test";
return newRestHighLevelClient(env);
}

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

public static RestHighLevelClient newRestHighLevelClient(Collection<String> hosts) {
HttpHost[] httpHosts = toHttpHostList(hosts);
RestClientBuilder builder = getRestClientBuilder(httpHosts);
if (builder == null) {
return null;
}
try {
return new RestHighLevelClient(builder);
} catch (Exception e) {
log.error("【ES】connect failed.", e);
return null;
}
}

public static ElasticsearchTemplate newElasticsearchTemplate() {
// 从配置中心读取环境变量
String env = "test";
return newElasticsearchTemplate(env);
}

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

public static ElasticsearchTemplate newElasticsearchTemplate(Collection<String> hosts) {
RestHighLevelClient client = newRestHighLevelClient(hosts);
if (client == null) {
return null;
}
return new ElasticsearchTemplate(client);
}

public static ElasticsearchTemplate newElasticsearchTemplate(RestHighLevelClient client) {
if (client == null) {
return null;
}
return new ElasticsearchTemplate(client);
}

public static RestClientBuilder getRestClientBuilder(HttpHost[] httpHosts) {
if (ArrayUtil.isEmpty(httpHosts)) {
log.error("【ES】connect failed. hosts are empty.");
return null;
}
RestClientBuilder restClientBuilder = RestClient.builder(httpHosts);
restClientBuilder.setRequestConfigCallback(builder -> {
builder.setConnectTimeout(CONNECT_TIMEOUT_MILLIS);
builder.setSocketTimeout(SOCKET_TIMEOUT_MILLIS);
builder.setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT_MILLIS);
return builder;
});
restClientBuilder.setHttpClientConfigCallback(builder -> {
builder.setMaxConnTotal(MAX_CONN_TOTAL);
builder.setMaxConnPerRoute(MAX_CONN_PER_ROUTE);
return builder;
});
return restClientBuilder;
}

private static HttpHost[] toHttpHostList(Collection<String> hosts) {
if (CollectionUtil.isEmpty(hosts)) {
return new HttpHost[0];
}
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";
return getDefaultEsAddress(env);
}

private static String getDefaultEsAddress(String env) {
String defaultAddress;
switch (env) {
case "prd":
defaultAddress = "127.0.0.1:9200,127.0.0.2:9200,127.0.0.3:9200";
break;
case "pre":
defaultAddress = "127.0.0.1:9200";
break;
case "test":
default:
defaultAddress = "127.0.0.1:9200";
break;
}
return defaultAddress;
}

}
Loading