Feat: [后台]:集成es,并从配置文件中读取es地址

This commit is contained in:
fanxb 2019-07-24 17:56:16 +08:00
parent e44bbe7ddf
commit ff48f01256
7 changed files with 129 additions and 31 deletions

View File

@ -11,11 +11,8 @@
<artifactId>bookmark-common</artifactId>
<dependencies>
<!--http请求工具依赖-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
@ -95,16 +92,13 @@
<version>1.2.56</version>
</dependency>
<!--es操作依赖-->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.2.0</version>
</dependency>
<!--单元测试-->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>

View File

@ -1,4 +1,4 @@
package com.fanxb.bookmark.common;
package com.fanxb.bookmark.common.constant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

View File

@ -0,0 +1,11 @@
package com.fanxb.bookmark.common.constant;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/7/23 14:34
*/
public class EsConstant {
}

View File

@ -0,0 +1,11 @@
package com.fanxb.bookmark.common.entity;
/**
* 类功能简述
* 类功能详述
*
* @author fanxb
* @date 2019/7/24 17:37
*/
public class EsInsertEntity {
}

View File

@ -1,16 +1,29 @@
package com.fanxb.bookmark.common.util;
import com.alibaba.fastjson.JSON;
import com.fanxb.bookmark.common.constant.EsConstant;
import com.fanxb.bookmark.common.entity.EsInsertEntity;
import com.fanxb.bookmark.common.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.lang.ref.ReferenceQueue;
import java.net.Authenticator;
import java.util.List;
/**
* 类功能简述
* 类功能详述
@ -19,34 +32,98 @@ import org.springframework.stereotype.Component;
* @date 2019/7/19 16:07
*/
@Component
@Slf4j
public class EsUtil {
@Value("${es.host}")
public String host;
@Value("${es.port}")
public int port;
@Value("${es.scheme}")
public String scheme;
public EsUtil() throws Exception {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("10.82.17.91", 9200, "http")));
CreateIndexRequest request = new CreateIndexRequest("test-index");
request.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2));
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("properties");
{
builder.startObject("column1");
{
builder.field("type", "");
builder.field("index", "not_analyzed");
}
builder.endObject();
public static RestHighLevelClient client = null;
@PostConstruct
public void init() {
try {
client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, scheme)));
if (this.indexExist(EsConstant.BOOKMARK_INDEX)) {
return;
}
builder.endObject();
CreateIndexRequest request = new CreateIndexRequest(EsConstant.BOOKMARK_INDEX);
request.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2));
request.mapping(EsConstant.CREATE_BOOKMARK_INDEX, XContentType.JSON);
CreateIndexResponse res = client.indices().create(request, RequestOptions.DEFAULT);
if (!res.isAcknowledged()) {
throw new CustomException("初始化失败");
}
} catch (Exception e) {
log.error("初始化es失败", e);
System.exit(0);
}
builder.endObject();
request.mapping(builder);
CreateIndexResponse res = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println(res);
}
/**
* Description: 判断某个index是否存在
*
* @param index index名
* @return boolean
* @author fanxb
* @date 2019/7/24 14:57
*/
public boolean indexExist(String index) throws Exception {
GetIndexRequest request = new GetIndexRequest(index);
request.local(false);
request.humanReadable(true);
request.includeDefaults(false);
return client.indices().exists(request, RequestOptions.DEFAULT);
}
/**
* Description: 插入一条记录
*
* @param index index
* @param id id
* @param o o
* @author fanxb
* @date 2019/7/24 15:02
*/
public void insertOne(String index, EsInsertEntity entity) {
IndexRequest request = new IndexRequest(index);
request.id(entity.getId());
request.source(JSON.toJSONString(entity.getData()), XContentType.JSON);
try {
client.index(request, RequestOptions.DEFAULT);
} catch (Exception e) {
throw new CustomException(e);
}
}
/**
* Description: 批量插入数据
*
* @param index index
* @param list 带插入列表
* @author fanxb
* @date 2019/7/24 17:38
*/
public void insertBatch(String index, List<? extends EsInsertEntity> list) {
BulkRequest request = new BulkRequest();
list.forEach(item -> request.add(new IndexRequest(index).id(item.getId()).source(JSON.toJSONString(item.getData()))));
try {
client.bulk(request, RequestOptions.DEFAULT);
} catch (Exception e) {
throw new CustomException(e);
}
}
public static void main(String[] args) throws Exception {
EsUtil util = new EsUtil();
System.out.println(util.indexExist("bookmark"));
}
}

View File

@ -1 +1 @@
<?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> <groupId>com.fanxb</groupId> <artifactId>bookMarkService</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>common</module> <module>business</module> <module>web</module> </modules> <properties> <project.build.sourceEncoding>utf-8</project.build.sourceEncoding> <project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding> <java.version>11</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>
<?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> <groupId>com.fanxb</groupId> <artifactId>bookMarkService</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>common</module> <module>business</module> <module>web</module> </modules> <properties> <project.build.sourceEncoding>utf-8</project.build.sourceEncoding> <project.reporting.outputEncoding>utf-8</project.reporting.outputEncoding> <java.version>11</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> </parent> <dependencyManagement> <dependencies> <!--定义es版本号,因为spring-boot-start-parent中引入了es的依赖会导致es的版本问题无法正常使用high-level-client--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.2.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch --> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.2.0</version> </dependency> <!--&lt;!&ndash; https://mvnrepository.com/artifact/org.elasticsearch.client/elasticsearch-rest-client &ndash;&gt;--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.2.0</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project>

View File

@ -60,3 +60,8 @@ mybatis:
# classpath后面加*,值里面的*才起作用
mapper-locations: classpath*:mapper/*.xml
debug: false
es:
host: localhost
port: 9200
scheme: http