From 4d418fa1a1f679cec9b1fe8092f73005bc0f8614 Mon Sep 17 00:00:00 2001 From: waynaqua <166974380@qq.com> Date: Fri, 6 Aug 2021 17:49:12 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E5=95=86=E5=9F=8E):=20es=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...roller.java => ElasticTestController.java} | 2 +- .../api/controller/shop/GoodsController.java | 48 +++++++-------- .../data/elastic/config/ElasticConfig.java | 7 +++ .../elastic/exception/ElasticException.java | 32 ++++++++++ .../data/elastic/manager/ElasticDocument.java | 60 ++++++++----------- 5 files changed, 88 insertions(+), 61 deletions(-) rename waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/{ElasticController.java => ElasticTestController.java} (99%) create mode 100644 waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/exception/ElasticException.java diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/ElasticController.java b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/ElasticTestController.java similarity index 99% rename from waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/ElasticController.java rename to waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/ElasticTestController.java index c6c2fd7..606e41d 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/ElasticController.java +++ b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/ElasticTestController.java @@ -30,7 +30,7 @@ import java.util.stream.Collectors; @RestController @RequestMapping("elastic") -public class ElasticController { +public class ElasticTestController { @Autowired private ElasticDocument elasticDocument; diff --git a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java index 55dffdd..dae0a3e 100644 --- a/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java +++ b/waynboot-admin-api/src/main/java/com/wayn/admin/api/controller/shop/GoodsController.java @@ -76,32 +76,32 @@ public class GoodsController extends BaseController { return R.error("正在同步,请稍等"); } boolean flag = false; - redisCache.setCacheObject(SysConstants.REDIS_ES_GOODS_INDEX, true, 3, TimeUnit.MINUTES); - try { + redisCache.setCacheObject(SysConstants.REDIS_ES_GOODS_INDEX, true, 30, TimeUnit.MINUTES); + try (InputStream inputStream = this.getClass().getResourceAsStream(SysConstants.ES_INDEX_GOODS_FILENAME)) { elasticDocument.deleteIndex(SysConstants.ES_GOODS_INDEX); - InputStream inputStream = this.getClass().getResourceAsStream(SysConstants.ES_INDEX_GOODS_FILENAME); - if (elasticDocument.createIndex(SysConstants.ES_GOODS_INDEX, FileUtils.getContent(inputStream))) { - List list = iGoodsService.list(); - List entities = new ArrayList<>(); - for (Goods goods : list) { - ElasticEntity elasticEntity = new ElasticEntity(); - Map map = new HashMap<>(); - elasticEntity.setId(goods.getId().toString()); - map.put("id", goods.getId()); - map.put("name", goods.getName()); - map.put("sales", goods.getActualSales() + goods.getVirtualSales()); - map.put("isHot", goods.getIsHot()); - map.put("isNew", goods.getIsNew()); - map.put("countPrice", goods.getCounterPrice()); - map.put("retailPrice", goods.getRetailPrice()); - map.put("keyword", goods.getKeywords().split(",")); - map.put("isOnSale", goods.getIsOnSale()); - map.put("createTime", goods.getCreateTime()); - elasticEntity.setData(map); - entities.add(elasticEntity); - } - flag = elasticDocument.insertBatch("goods", entities); + if (!elasticDocument.createIndex(SysConstants.ES_GOODS_INDEX, FileUtils.getContent(inputStream))) { + return R.error("创建索引失败!"); } + List list = iGoodsService.list(); + List entities = new ArrayList<>(); + for (Goods goods : list) { + ElasticEntity elasticEntity = new ElasticEntity(); + Map map = new HashMap<>(); + elasticEntity.setId(goods.getId().toString()); + map.put("id", goods.getId()); + map.put("name", goods.getName()); + map.put("sales", goods.getActualSales() + goods.getVirtualSales()); + map.put("isHot", goods.getIsHot()); + map.put("isNew", goods.getIsNew()); + map.put("countPrice", goods.getCounterPrice()); + map.put("retailPrice", goods.getRetailPrice()); + map.put("keyword", goods.getKeywords().split(",")); + map.put("isOnSale", goods.getIsOnSale()); + map.put("createTime", goods.getCreateTime()); + elasticEntity.setData(map); + entities.add(elasticEntity); + } + flag = elasticDocument.insertBatch("goods", entities); } catch (Exception e) { log.error(e.getMessage(), e); } finally { diff --git a/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/config/ElasticConfig.java b/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/config/ElasticConfig.java index aef7c19..28992d3 100644 --- a/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/config/ElasticConfig.java +++ b/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/config/ElasticConfig.java @@ -1,5 +1,6 @@ package com.wayn.data.elastic.config; +import lombok.Data; import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestClientBuilder; @@ -9,6 +10,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +@Data @Configuration public class ElasticConfig { @Value("${es.host}") @@ -17,6 +19,11 @@ public class ElasticConfig { public int port; @Value("${es.scheme}") public String scheme; + @Value("${es.shards}") + public int shards; + @Value("${es.replicas}") + public int replicas; + @Bean public RestClientBuilder restClientBuilder() { diff --git a/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/exception/ElasticException.java b/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/exception/ElasticException.java new file mode 100644 index 0000000..288c9e9 --- /dev/null +++ b/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/exception/ElasticException.java @@ -0,0 +1,32 @@ +package com.wayn.data.elastic.exception; + +import lombok.Data; +import lombok.EqualsAndHashCode; + +/** + * elastic异常 + */ +@Data +@EqualsAndHashCode(callSuper = true) +public class ElasticException extends RuntimeException { + + private static final long serialVersionUID = 9005063206372011860L; + + private Integer code; + + private String msg; + + public ElasticException(String msg, Integer code) { + this.code = code; + this.msg = msg; + } + + public ElasticException(String message, Throwable cause) { + super(message, cause); + } + + public ElasticException(String message) { + super(message); + } + +} diff --git a/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/manager/ElasticDocument.java b/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/manager/ElasticDocument.java index a63e1c8..c65f7ca 100644 --- a/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/manager/ElasticDocument.java +++ b/waynboot-data/waynboot-data-elastic/src/main/java/com/wayn/data/elastic/manager/ElasticDocument.java @@ -1,7 +1,10 @@ package com.wayn.data.elastic.manager; import com.alibaba.fastjson.JSON; +import com.wayn.data.elastic.config.ElasticConfig; +import com.wayn.data.elastic.exception.ElasticException; import lombok.extern.slf4j.Slf4j; +import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; @@ -30,6 +33,7 @@ import org.elasticsearch.search.builder.SearchSourceBuilder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -39,32 +43,27 @@ import java.util.List; public class ElasticDocument { @Autowired - RestHighLevelClient restHighLevelClient; + private ElasticConfig elasticConfig; + + @Autowired + private RestHighLevelClient restHighLevelClient; /** * 创建索引 * * @param idxName 缩影名称 - * @param idxSQL 缩影定义 + * @param idxSQL 索引创建语句 */ - public boolean createIndex(String idxName, String idxSQL) { - try { - if (this.indexExist(idxName)) { - log.error(" idxName={} 已经存在,idxSql={}", idxName, idxSQL); - return false; - } - CreateIndexRequest request = new CreateIndexRequest(idxName); - buildSetting(request); - request.mapping(idxSQL, XContentType.JSON); - // request.settings() 手工指定Setting - CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); - if (!res.isAcknowledged()) { - return false; - } - } catch (Exception e) { - log.error(e.getMessage(), e);; + public boolean createIndex(String idxName, String idxSQL) throws IOException { + if (indexExist(idxName)) { + throw new ElasticException(String.format("idxName=%s 已经存在,idxSql=%s", idxName, idxSQL)); } - return true; + CreateIndexRequest request = new CreateIndexRequest(idxName); + buildSetting(request); + request.mapping(idxSQL, XContentType.JSON); + // request.settings() 手工指定Setting + CreateIndexResponse res = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT); + return res.isAcknowledged(); } /** @@ -72,9 +71,9 @@ public class ElasticDocument { * * @param idxName 索引名称 * @return boolean - * @throws Exception 异常 + * @throws IOException 异常 */ - public boolean indexExist(String idxName) throws Exception { + public boolean indexExist(String idxName) throws IOException { GetIndexRequest request = new GetIndexRequest(idxName); request.local(false); request.humanReadable(true); @@ -84,23 +83,13 @@ public class ElasticDocument { } /** - * 断某个index是否存在 - * - * @param idxName index名 - * @return boolean - */ - public boolean isExistsIndex(String idxName) throws Exception { - return restHighLevelClient.indices().exists(new GetIndexRequest(idxName), RequestOptions.DEFAULT); - } - - /** - * 设置分片 + * 设置分片和副本 * - * @param request + * @param request 创建索引请求 */ public void buildSetting(CreateIndexRequest request) { - request.settings(Settings.builder().put("index.number_of_shards", 3) - .put("index.number_of_replicas", 2)); + request.settings(Settings.builder().put("index.number_of_shards", elasticConfig.getShards()) + .put("index.number_of_replicas", elasticConfig.getReplicas())); } /** @@ -112,7 +101,6 @@ public class ElasticDocument { log.info("Data : id={},entity={}", entity.getId(), JSON.toJSONString(entity.getData())); request.id(entity.getId()); request.source(entity.getData(), XContentType.JSON); - // request.source(JSON.toJSONString(entity.getData()), XContentType.JSON); try { IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT); ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();