diff --git a/HomeWork-common/HomeWork-common-core/src/main/java/HomeWork/common/core/constant/ServiceNameConstants.java b/HomeWork-common/HomeWork-common-core/src/main/java/HomeWork/common/core/constant/ServiceNameConstants.java index 63abb97..2da99a8 100644 --- a/HomeWork-common/HomeWork-common-core/src/main/java/HomeWork/common/core/constant/ServiceNameConstants.java +++ b/HomeWork-common/HomeWork-common-core/src/main/java/HomeWork/common/core/constant/ServiceNameConstants.java @@ -2,7 +2,7 @@ package HomeWork.common.core.constant; /** * 服务名称 - * + * * @author ruoyi */ public class ServiceNameConstants @@ -21,4 +21,6 @@ public class ServiceNameConstants * 文件服务的serviceid */ public static final String FILE_SERVICE = "HomeWork-file"; + + public static final String ES_SERVICE = "bawei-es"; } diff --git a/HomeWork-common/HomeWork-common-security/src/main/java/HomeWork/common/security/annotation/EnableRyFeignClients.java b/HomeWork-common/HomeWork-common-security/src/main/java/HomeWork/common/security/annotation/EnableRyFeignClients.java index b3b3946..d68344a 100644 --- a/HomeWork-common/HomeWork-common-security/src/main/java/HomeWork/common/security/annotation/EnableRyFeignClients.java +++ b/HomeWork-common/HomeWork-common-security/src/main/java/HomeWork/common/security/annotation/EnableRyFeignClients.java @@ -6,7 +6,7 @@ import java.lang.annotation.*; /** * 自定义feign注解 * 添加basePackages路径 - * + * * @author ruoyi */ @Target(ElementType.TYPE) @@ -17,7 +17,7 @@ public @interface EnableRyFeignClients { String[] value() default {}; - String[] basePackages() default { "HomeWork" }; + String[] basePackages() default { "HomeWork" , "com.bawei" }; Class[] basePackageClasses() default {}; diff --git a/HomeWork-modules/HomeWork-MedicalTreatment/src/main/java/HomeWork/service/impl/TreeServiceImpl.java b/HomeWork-modules/HomeWork-MedicalTreatment/src/main/java/HomeWork/service/impl/TreeServiceImpl.java index 13f53af..3af027a 100644 --- a/HomeWork-modules/HomeWork-MedicalTreatment/src/main/java/HomeWork/service/impl/TreeServiceImpl.java +++ b/HomeWork-modules/HomeWork-MedicalTreatment/src/main/java/HomeWork/service/impl/TreeServiceImpl.java @@ -120,5 +120,6 @@ public class TreeServiceImpl implements TreeService { treeVos.add(treeVo1); } return treeVos; - } + }/**/ + } diff --git a/HomeWork-modules/HomeWork-es/base-es-common/pom.xml b/HomeWork-modules/HomeWork-es/base-es-common/pom.xml new file mode 100644 index 0000000..e27ce94 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-common/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + HomeWork + base-es + 3.6.3 + + + base-es-common + + + + + HomeWork + HomeWork-common-core + + + + + diff --git a/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/BasePage.java b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/BasePage.java new file mode 100644 index 0000000..f9658c8 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/BasePage.java @@ -0,0 +1,14 @@ +package com.bawei.es.domain; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@ApiModel("分页") +@Data +public class BasePage { + @ApiModelProperty(value = "页码",required = true) + private Integer pageNum; + @ApiModelProperty(value = "每页数量",required = true) + private Integer pageSize; +} diff --git a/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocInfoReq.java b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocInfoReq.java new file mode 100644 index 0000000..5a71cd7 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocInfoReq.java @@ -0,0 +1,4 @@ +package com.bawei.es.domain; + +public class DocInfoReq { +} diff --git a/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/Document.java b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/Document.java new file mode 100644 index 0000000..4af4fa3 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/Document.java @@ -0,0 +1,16 @@ +package com.bawei.es.domain; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@Data +@ApiModel("文档") +public class Document { + @ApiModelProperty(value = "id",required = false) + private String id; + @ApiModelProperty(value = "姓名",required = false) + private String name; + @ApiModelProperty(value = "年龄",required = false) + private Integer age; +} diff --git a/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentDelReq.java b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentDelReq.java new file mode 100644 index 0000000..58c95e7 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentDelReq.java @@ -0,0 +1,19 @@ +package com.bawei.es.domain; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; +import java.util.List; + +@ApiModel("文档删除") +@Data +public class DocumentDelReq { + @ApiModelProperty(value = "索引名称",required = true) + @NotNull(message = "索引名称不能为空") + private String indexName; + @ApiModelProperty(value = "文档id", required = true) + @NotNull(message = "id不能为空") + private List ids; +} diff --git a/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentListReq.java b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentListReq.java new file mode 100644 index 0000000..364a5cd --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentListReq.java @@ -0,0 +1,26 @@ +package com.bawei.es.domain; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; +import java.util.List; + +@Data +@ApiModel("文档查询") +public class DocumentListReq { + + @ApiModelProperty(value = "索引名称",required = true) + @NotNull(message = "索引名称不能为空") + private String indexName; + @ApiModelProperty(value = "全文检索", required = false) + private String keyWord; + @ApiModelProperty(value = "分页参数", required = false) + private BasePage basePage; + @ApiModelProperty("检索字段") + private List queryFields; + @ApiModelProperty("返回字段") + private List returnFields; + +} diff --git a/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentReq.java b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentReq.java new file mode 100644 index 0000000..93bc308 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentReq.java @@ -0,0 +1,26 @@ +package com.bawei.es.domain; + +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; +import java.util.List; +import java.util.Map; + +@Data +@Api(tags = "文档索引") +public class DocumentReq { + @ApiModelProperty(value = "索引名称",required = true) + @NotNull(message = "索引名称不能为空") + private String indexName; + + @ApiModelProperty(value = "是否制定文档ID") + private boolean isNeedId = false; + + @ApiModelProperty(value = "文档ID对应字段") + private String idField; + + @ApiModelProperty(value = "文档内容") + private List> documentContent; +} diff --git a/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentUpdReq.java b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentUpdReq.java new file mode 100644 index 0000000..7ec4c5a --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/DocumentUpdReq.java @@ -0,0 +1,21 @@ +package com.bawei.es.domain; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +@ApiModel("文档删除") +@Data +public class DocumentUpdReq { + @ApiModelProperty(value = "索引名称",required = true) + @NotNull(message = "索引名称不能为空") + private String indexName; + @ApiModelProperty(value = "文档内容", required = true) + @NotNull(message = "id不能为空") + private String id; + @ApiModelProperty(value = "文档内容", required = true) + @NotNull(message = "文档内容不能为空") + private Document document; +} diff --git a/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/IndexEntry.java b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/IndexEntry.java new file mode 100644 index 0000000..c87cb7e --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/IndexEntry.java @@ -0,0 +1,20 @@ +package com.bawei.es.domain; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; +import java.util.Map; + +@Data +@ApiModel("索引信息") +public class IndexEntry { + @ApiModelProperty(value = "索引名称",required = true) + @NotNull(message = "索引名称不能为空") + private String indexName; + @ApiModelProperty(value = "索引mapping",required = false) + private Map indexMapping; + @ApiModelProperty(value = "索引參數",required = false) + private Map settings; +} diff --git a/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/IndexInfoReq.java b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/IndexInfoReq.java new file mode 100644 index 0000000..22259a2 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/IndexInfoReq.java @@ -0,0 +1,20 @@ +package com.bawei.es.domain; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; +import java.util.Map; + +@Data +@ApiModel("索引信息") +public class IndexInfoReq { + @ApiModelProperty(value = "索引名称",required = true) + @NotNull(message = "索引名称不能为空") + private String indexName; + @ApiModelProperty(value = "索引mapping",required = false) + private Map indexMapping; + @ApiModelProperty(value = "索引參數",required = false) + private Map settings; +} diff --git a/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/IndexInfoResp.java b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/IndexInfoResp.java new file mode 100644 index 0000000..1dddb15 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-common/src/main/java/com/bawei/es/domain/IndexInfoResp.java @@ -0,0 +1,15 @@ +package com.bawei.es.domain; + +import io.swagger.annotations.ApiModel; +import lombok.Data; + +import java.util.Map; + +@Data +@ApiModel("索引对象") +public class IndexInfoResp { + private String indexName; + private Map aliases; + private Map mappings; + private Map settings; +} diff --git a/HomeWork-modules/HomeWork-es/base-es-remote/pom.xml b/HomeWork-modules/HomeWork-es/base-es-remote/pom.xml new file mode 100644 index 0000000..7507ecb --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-remote/pom.xml @@ -0,0 +1,22 @@ + + + 4.0.0 + + HomeWork + base-es + 3.6.3 + + + base-es-remote + + + + HomeWork + base-es-common + 3.6.3 + + + + diff --git a/HomeWork-modules/HomeWork-es/base-es-remote/src/main/java/com/bawei/es/remote/api/RemoteEsDocService.java b/HomeWork-modules/HomeWork-es/base-es-remote/src/main/java/com/bawei/es/remote/api/RemoteEsDocService.java new file mode 100644 index 0000000..409c6b4 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-remote/src/main/java/com/bawei/es/remote/api/RemoteEsDocService.java @@ -0,0 +1,32 @@ +package com.bawei.es.remote.api; + +import HomeWork.common.core.constant.ServiceNameConstants; +import HomeWork.common.core.domain.R; +import com.bawei.es.domain.*; +import com.bawei.es.remote.factory.RemoteEsDocumentFallbackFactory; +import io.swagger.annotations.ApiOperation; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.*; + +/** + * 用户服务 + * + * @author bawei + * + */ +@FeignClient(contextId = "RemoteEsDocService", value = ServiceNameConstants.ES_SERVICE, fallbackFactory = RemoteEsDocumentFallbackFactory.class) +public interface RemoteEsDocService +{ + @ApiOperation("文档插入") + @PostMapping("/es/document/add") + public R add(@RequestBody DocumentReq documentReq); + + @ApiOperation("文档删除") + @PostMapping("/es/document/del") + public R del(@RequestBody DocumentDelReq documentDelReq); + + @ApiOperation("文档查询") + @PostMapping("/list") + public R list(@RequestBody DocumentListReq documentListReq); +} diff --git a/HomeWork-modules/HomeWork-es/base-es-remote/src/main/java/com/bawei/es/remote/factory/RemoteEsDocumentFallbackFactory.java b/HomeWork-modules/HomeWork-es/base-es-remote/src/main/java/com/bawei/es/remote/factory/RemoteEsDocumentFallbackFactory.java new file mode 100644 index 0000000..1f990b5 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-remote/src/main/java/com/bawei/es/remote/factory/RemoteEsDocumentFallbackFactory.java @@ -0,0 +1,44 @@ +package com.bawei.es.remote.factory; + + +import HomeWork.common.core.domain.R; +import com.bawei.es.domain.DocumentDelReq; +import com.bawei.es.domain.DocumentListReq; +import com.bawei.es.domain.DocumentReq; +import com.bawei.es.remote.api.RemoteEsDocService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.openfeign.FallbackFactory; +import org.springframework.stereotype.Component; + +/** + * 日志服务降级处理 + * + * @author bawei + */ +@Component +public class RemoteEsDocumentFallbackFactory implements FallbackFactory +{ + private static final Logger log = LoggerFactory.getLogger(RemoteEsDocumentFallbackFactory.class); + + @Override + public RemoteEsDocService create(Throwable cause) { + log.error("日志服务调用失败:{}", cause.getMessage()); + return new RemoteEsDocService(){ + @Override + public R add(DocumentReq documentReq) { + return R.fail("调用es服务插入数据失败,请稍后重试"); + } + + @Override + public R del(DocumentDelReq documentDelReq) { + return R.fail("调用es服务删除数据失败,请稍后重试"); + } + + @Override + public R list(DocumentListReq documentListReq) { + return R.fail("调用es服务查询数据失败,请稍后重试"); + } + }; + } +} diff --git a/HomeWork-modules/HomeWork-es/base-es-remote/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/HomeWork-modules/HomeWork-es/base-es-remote/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..3051cdc --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-remote/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.bawei.es.remote.factory.RemoteEsDocumentFallbackFactory diff --git a/HomeWork-modules/HomeWork-es/base-es-server/pom.xml b/HomeWork-modules/HomeWork-es/base-es-server/pom.xml new file mode 100644 index 0000000..7a6529b --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/pom.xml @@ -0,0 +1,136 @@ + + + 4.0.0 + + HomeWork + base-es + 3.6.3 + + + base-es-server + + + 17 + 17 + UTF-8 + + + + + commons-io + commons-io + 2.11.0 + + + + + com.baidu.aip + java-sdk + 4.6.0 + + + org.json + json + 20160810 + + + com.google.code.gson + gson + 2.9.0 + + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + + org.springframework.boot + spring-boot-starter-actuator + + + spring-boot-autoconfigure + org.springframework.boot + + + + + + + io.springfox + springfox-swagger-ui + ${swagger.fox.version} + + + + + + HomeWork + HomeWork-common-datascope + + + + + HomeWork + HomeWork-common-log + + + + + HomeWork + HomeWork-common-swagger + + + HomeWork + base-es-remote + 3.6.3 + + + mybatis-spring-boot-starter + org.mybatis.spring.boot + + + + + + org.springframework.boot + spring-boot-starter-data-elasticsearch + + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + + diff --git a/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/EsApp.java b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/EsApp.java new file mode 100644 index 0000000..3645695 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/EsApp.java @@ -0,0 +1,17 @@ +package com.bawei.es; + +import HomeWork.common.security.annotation.EnableCustomConfig; +import HomeWork.common.security.annotation.EnableRyFeignClients; +import HomeWork.common.swagger.annotation.EnableCustomSwagger2; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@EnableCustomConfig +@EnableCustomSwagger2 +@EnableRyFeignClients +@SpringBootApplication +public class EsApp { + public static void main(String[] args) { + SpringApplication.run(EsApp.class); + } +} diff --git a/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/config/ElasticsearchConfig.java b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/config/ElasticsearchConfig.java new file mode 100644 index 0000000..4f6baaf --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/config/ElasticsearchConfig.java @@ -0,0 +1,27 @@ +package com.bawei.es.config; + +import lombok.Data; +import org.apache.http.HttpHost; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestClientBuilder; +import org.elasticsearch.client.RestHighLevelClient; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration; + +@ConfigurationProperties(prefix = "elasticsearch") +@Configuration +@Data +public class ElasticsearchConfig extends AbstractElasticsearchConfiguration { + private String host; + private Integer port; + @Override + public RestHighLevelClient elasticsearchClient() { + if (host == null || port == null) { + throw new RuntimeException("elasticsearch配置错误"); + } + RestClientBuilder builder = RestClient.builder(new HttpHost(host, port)); + RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder); + return restHighLevelClient; + } +} diff --git a/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/controller/EsDocumentController.java b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/controller/EsDocumentController.java new file mode 100644 index 0000000..bf83093 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/controller/EsDocumentController.java @@ -0,0 +1,63 @@ +package com.bawei.es.controller; + +import HomeWork.common.core.domain.R; +import com.bawei.es.domain.DocumentDelReq; +import com.bawei.es.domain.DocumentListReq; +import com.bawei.es.domain.DocumentReq; +import com.bawei.es.domain.DocumentUpdReq; +import com.bawei.es.server.EsDocumentServer; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Api("es文档管理") +@AllArgsConstructor +@RestController +@RequestMapping("/es/document") +public class EsDocumentController { + + private EsDocumentServer esDocumentServer; + + /** + * 文档插入 + * @param documentReq + * @return + */ + @ApiOperation("文档插入") + @PostMapping("/add") + public R add(@RequestBody DocumentReq documentReq){ + esDocumentServer.add(documentReq); + return R.ok(); + } + + /** + * 文档删除 + * @param documentDelReq + * @return + */ + @ApiOperation("文档删除") + @PostMapping("/del") + public R del(@RequestBody DocumentDelReq documentDelReq){ + esDocumentServer.del(documentDelReq); + return R.ok(); + } + + @ApiOperation("文档修改") + @PostMapping("/upd") + public R upd(@RequestBody DocumentUpdReq documentReq){ + esDocumentServer.upd(documentReq); + return R.ok(); + } + + @ApiOperation("文档查询") + @PostMapping("/list") + public R list(@RequestBody DocumentListReq documentListReq){ + return R.ok(esDocumentServer.list(documentListReq)); + } + + +} diff --git a/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/controller/EsIndexController.java b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/controller/EsIndexController.java new file mode 100644 index 0000000..57ea8f1 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/controller/EsIndexController.java @@ -0,0 +1,55 @@ +package com.bawei.es.controller; + +import HomeWork.common.core.domain.R; +import com.bawei.es.domain.IndexEntry; +import com.bawei.es.domain.IndexInfoReq; +import com.bawei.es.domain.IndexInfoResp; +import com.bawei.es.server.EsIndexService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/es/index") +@Api(tags = "es索引管理") +public class EsIndexController { + @Autowired + private EsIndexService esIndexService; + @RequestMapping("/create") + @ApiOperation("创建索引") + public R createIndex(@RequestBody IndexInfoReq indexInfoReq) { + esIndexService.createIndex(indexInfoReq); + return R.ok(); + } + + @PostMapping("/del/{indexName}") + @ApiOperation("删除索引") + public R delIndex(@PathVariable String indexName){ + esIndexService.delIndex(indexName); + return R.ok(); + } + + @PostMapping("/get/{indexName}") + @ApiOperation("判断索引是否存在") + public R isExistIndex(@PathVariable String indexName){ + boolean isExist = esIndexService.isExistIndex(indexName); + return R.ok(isExist); + } + + @PostMapping("/find/{indexName}") + @ApiOperation("查找索引信息") + public R find(@PathVariable String indexName){ + IndexInfoResp indexInfoResp=esIndexService.find(indexName); + return R.ok(indexInfoResp); + } + + @PostMapping("/upd") + @ApiOperation("修改") + public R updIndex(@RequestBody IndexEntry indexEntry){ + esIndexService.updIndex(indexEntry); + return R.ok(); + } + + +} diff --git a/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/EsDocumentServer.java b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/EsDocumentServer.java new file mode 100644 index 0000000..13c7361 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/EsDocumentServer.java @@ -0,0 +1,18 @@ +package com.bawei.es.server; + +import com.bawei.es.domain.*; + +import java.util.ArrayList; +import java.util.Map; + +public interface EsDocumentServer { + void add(DocumentReq documentReq); + + void del(DocumentDelReq documentDelReq); + + void upd(DocumentUpdReq documentReq); + + + ArrayList> list(DocumentListReq documentListReq); + +} diff --git a/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/EsIndexService.java b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/EsIndexService.java new file mode 100644 index 0000000..99bd071 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/EsIndexService.java @@ -0,0 +1,19 @@ +package com.bawei.es.server; + +import com.bawei.es.domain.DocumentReq; +import com.bawei.es.domain.IndexEntry; +import com.bawei.es.domain.IndexInfoReq; +import com.bawei.es.domain.IndexInfoResp; + +public interface EsIndexService { + void createIndex(IndexInfoReq indexInfoReq); + + void delIndex(String indexName); + + boolean isExistIndex(String indexName); + + IndexInfoResp find(String indexName); + + void updIndex(IndexEntry indexEntry); + +} diff --git a/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/impl/EsDocumentServerImpl.java b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/impl/EsDocumentServerImpl.java new file mode 100644 index 0000000..a7b439e --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/impl/EsDocumentServerImpl.java @@ -0,0 +1,186 @@ +package com.bawei.es.server.impl; + +import com.bawei.es.domain.*; +import com.bawei.es.server.EsDocumentServer; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.elasticsearch.action.bulk.BulkRequest; +import org.elasticsearch.action.delete.DeleteRequest; +import org.elasticsearch.action.index.IndexRequest; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.action.update.UpdateRequest; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.GetIndexRequest; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.elasticsearch.xcontent.XContentFactory; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +@Service +@AllArgsConstructor +@Slf4j +public class EsDocumentServerImpl implements EsDocumentServer { + private RestHighLevelClient restHighLevelClient; + @Override + public void add(DocumentReq documentReq) { + //打印请求参数 + log.info("参数为{}",documentReq); + //效验索引是否存在 + if (findIndexName(documentReq.getIndexName())){ + //批量添加 + BulkRequest bulkRequest = new BulkRequest(); + List> data = documentReq.getDocumentContent(); + //判断数据是否为空 + if (data==null||data.size()==0){ + throw new RuntimeException("数据为空"); + } + //循环集合 + for (Map datum : data) { + //创建索引请求indexRequest + IndexRequest indexRequest = new IndexRequest(documentReq.getIndexName()); + //遍历数据 + log.info("数据{}",datum); + if (documentReq.getIdField()!=null&&datum.containsKey(documentReq.getIdField())){ + //获取data中的map取id + String uuid = datum.get(documentReq.getIdField()).toString(); + //判断isNeedId是否==true和uuid非空验证 + if (documentReq.isNeedId()==true&&uuid!=null){ + //设置id + indexRequest.id(uuid); + } + } + //设置内容 + indexRequest.source(datum); + bulkRequest.add(indexRequest); + } + try { + restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT); + } catch (Exception e) { + log.info("es插入失败异常信息{}",e); + throw new RuntimeException("es插入失败"); + } + }else { + throw new RuntimeException("索引名不存在"); + } + + + } + + @Override + public void del(DocumentDelReq documentDelReq) { + if (findIndexName(documentDelReq.getIndexName())){ + BulkRequest bulkRequest = new BulkRequest(); + try { + for (String id : documentDelReq.getIds()) { + DeleteRequest deleteRequest = new DeleteRequest(documentDelReq.getIndexName(), id); + bulkRequest.add(deleteRequest); + } + restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT); + } catch (Exception e) { + throw new RuntimeException(e); + } + }else { + throw new RuntimeException("索引名不存在"); + } + } + + @Override + public void upd(DocumentUpdReq documentReq) { + log.info("打印请求{}",documentReq); + if (findIndexName(documentReq.getIndexName())){ + String name = documentReq.getDocument().getName(); + Integer age = documentReq.getDocument().getAge(); + try { + UpdateRequest updateRequest = new UpdateRequest(); + updateRequest.id(documentReq.getId()); + updateRequest.index(documentReq.getIndexName()); + updateRequest.doc( + XContentFactory.jsonBuilder().startObject() + .field("name",name) + .field("age",age) + .endObject() + ); + restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT); + } catch (Exception e) { + throw new RuntimeException(e); + } + }else { + throw new RuntimeException("索引名不存在"); + } + } + + @Override + public ArrayList> list(DocumentListReq documentListReq) { + //返回结果 + ArrayList> maps = new ArrayList<>(); + //输出请求参数 + log.info("打印请求参数{}",documentListReq); + //判断索引是否存在 + if (findIndexName(documentListReq.getIndexName())){ + // 搜索源构建对象 + SearchRequest searchRequest = new SearchRequest(documentListReq.getIndexName()); + // 搜索源构建对象 + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); + // 搜索方式 + BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); + //全文检索 + if(documentListReq.getKeyWord()!=null&&!documentListReq.getKeyWord().equals("")){ + boolQuery.must(QueryBuilders.matchQuery(documentListReq.getQueryFields().get(0),documentListReq.getKeyWord())); + } + searchSourceBuilder.query(boolQuery); + //判断分页参数是否为空 + if(documentListReq.getBasePage().getPageNum()!=null&&documentListReq.getBasePage().getPageSize()!=null){ + searchSourceBuilder.from((documentListReq.getBasePage().getPageNum()-1)*documentListReq.getBasePage().getPageSize()); + searchSourceBuilder.size(documentListReq.getBasePage().getPageSize()); + } + //检索字段 + if (documentListReq.getQueryFields()!=null&&!documentListReq.getQueryFields().equals("")){ + String[] array = documentListReq.getReturnFields().toArray(new String[0]); + searchSourceBuilder.fetchSource(array,null); + } + try { + //向es发送请求 + SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); + SearchHits hits = search.getHits(); + //循环查询结果 + SearchHit[] hits1 = hits.getHits(); + for (SearchHit documentFields : hits1) { + //转型 + Map sourceAsMap = documentFields.getSourceAsMap(); + log.info("遍历结果{}",sourceAsMap); + maps.add(sourceAsMap); + } + return maps; + } catch (Exception e) { + throw new RuntimeException(e); + } + + }else { + throw new RuntimeException("索引名不存在"); + } + } + + + public boolean findIndexName(String indexName){ + if (indexName!=null){ + try { + GetIndexRequest getIndexRequest = new GetIndexRequest(indexName); + return restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT); + } catch (Exception e) { + log.info("查询异常信息{}",e); + throw new RuntimeException("索引查询失败"); + } + }else { + throw new RuntimeException("索引名为空"); + } + } +} diff --git a/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/impl/EsIndexServiceImpl.java b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/impl/EsIndexServiceImpl.java new file mode 100644 index 0000000..5299d8a --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/src/main/java/com/bawei/es/server/impl/EsIndexServiceImpl.java @@ -0,0 +1,136 @@ +package com.bawei.es.server.impl; + +import com.alibaba.fastjson.JSONObject; +import com.bawei.es.domain.DocumentReq; +import com.bawei.es.domain.IndexEntry; +import com.bawei.es.domain.IndexInfoReq; +import com.bawei.es.domain.IndexInfoResp; +import com.bawei.es.server.EsIndexService; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.GetIndexRequest; +import org.elasticsearch.client.indices.GetIndexResponse; +import org.elasticsearch.client.indices.PutMappingRequest; +import org.elasticsearch.cluster.metadata.AliasMetadata; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.xcontent.XContentType; +import org.springframework.stereotype.Service; +import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +@Service +@Slf4j +@AllArgsConstructor +public class EsIndexServiceImpl implements EsIndexService { + + private RestHighLevelClient restHighLevelClient; + + /** + * 创建索引 + * @param indexInfoReq + */ + @Override + public void createIndex(IndexInfoReq indexInfoReq) { + //打印日志 + log.info("创建索引{}", JSONObject.toJSONString(indexInfoReq)); + //判断名字是否存在 + boolean existIndex = isExistIndex(indexInfoReq.getIndexName()); + if (existIndex){ + throw new RuntimeException("索引:"+indexInfoReq.getIndexName()+"索引已存在"); + } + //创建索引 + CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexInfoReq.getIndexName()); + if(indexInfoReq.getIndexMapping()!=null){ + String jsonString = JSONObject.toJSONString(indexInfoReq.getIndexMapping()); + createIndexRequest.mapping(indexInfoReq.getIndexName(),jsonString,XContentType.JSON); + } + try { + restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT); + } catch (Exception e) { + throw new RuntimeException("创建失败",e); + } + } + @Override + public void delIndex(String indexName) { + try { + boolean existIndex = isExistIndex(indexName); + if (!existIndex){ + throw new RuntimeException("索引:"+indexName+"不存在"); + } + DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName); + restHighLevelClient.indices().delete(deleteIndexRequest,RequestOptions.DEFAULT); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * 判断索引是否存在 + * @param indexName + * @return + */ + public boolean isExistIndex(String indexName) { + GetIndexRequest getIndexResult = new GetIndexRequest(indexName); + try { + boolean exists = restHighLevelClient.indices().exists(getIndexResult, RequestOptions.DEFAULT); + return exists; + } catch (Exception e) { + throw new RuntimeException("查询索引失败",e); + } + } + + /** + * 查找索引 + * @param indexName + * @return + */ + @Override + public IndexInfoResp find(String indexName) { + if(isExistIndex(indexName)){ + IndexInfoResp indexInfoResp = new IndexInfoResp(); + GetIndexRequest getIndexRequest = new GetIndexRequest(indexName); + try { + IndexInfoResp indexInfoResp1 = new IndexInfoResp(); + GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(getIndexRequest, RequestOptions.DEFAULT); + indexInfoResp1.setIndexName(getIndexResponse.getIndices()[0]); + indexInfoResp1.setMappings(getIndexResponse.getMappings().get(indexName).getSourceAsMap()); + Settings settings = getIndexResponse.getSettings().get(indexName); + Set strings = settings.keySet(); + HashMap map = new HashMap<>(); + strings.forEach(key->{ + map.put(key,settings.get(key)); + }); + indexInfoResp1.setSettings(map); + Map> aliases = getIndexResponse.getAliases(); + Map aliasMap=JSONObject.parseObject(JSONObject.toJSONString(aliases),Map.class); + indexInfoResp.setAliases(aliasMap); + return indexInfoResp; + } catch (Exception e) { + throw new RuntimeException("索引失败",e); + } + }else { + throw new RuntimeException("索引名不存在"); + } + } + + @Override + public void updIndex(IndexEntry indexEntry) { + try { + if (isExistIndex(indexEntry.getIndexName())){ + PutMappingRequest putMappingRequest = new PutMappingRequest(indexEntry.getIndexName()); + String jsonString = JSONObject.toJSONString(indexEntry.getIndexMapping()); + putMappingRequest.source(jsonString,XContentType.JSON); + restHighLevelClient.indices().putMapping(putMappingRequest,RequestOptions.DEFAULT); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} diff --git a/HomeWork-modules/HomeWork-es/base-es-server/src/main/resources/banner.txt b/HomeWork-modules/HomeWork-es/base-es-server/src/main/resources/banner.txt new file mode 100644 index 0000000..fbd45f5 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/src/main/resources/banner.txt @@ -0,0 +1,10 @@ +Spring Boot Version: ${spring-boot.version} +Spring Application Name: ${spring.application.name} + _ _ + (_) | | + _ __ _ _ ___ _ _ _ ______ ___ _ _ ___ | |_ ___ _ __ ___ +| '__|| | | | / _ \ | | | || ||______|/ __|| | | |/ __|| __| / _ \| '_ ` _ \ +| | | |_| || (_) || |_| || | \__ \| |_| |\__ \| |_ | __/| | | | | | +|_| \__,_| \___/ \__, ||_| |___/ \__, ||___/ \__| \___||_| |_| |_| + __/ | __/ | + |___/ |___/ \ No newline at end of file diff --git a/HomeWork-modules/HomeWork-es/base-es-server/src/main/resources/bootstrap.yml b/HomeWork-modules/HomeWork-es/base-es-server/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..d1e04db --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/src/main/resources/bootstrap.yml @@ -0,0 +1,39 @@ +# Tomcat +server: + port: 9204 + +# Spring +spring: + redis: + host: 101.34.77.101 + port: 6379 + password: cbx@123 + application: + # 应用名称 + name: bawei-es + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 101.34.77.101:8848 + namespace: 20041112 + config: + # 配置中心地址 + server-addr: 101.34.77.101:8848 + namespace: 20041112 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} +# feign 配置 +feign: + compression: + request: + enabled: true + min-request-size: 10000 + response: + enabled: true diff --git a/HomeWork-modules/HomeWork-es/base-es-server/src/main/resources/logback.xml b/HomeWork-modules/HomeWork-es/base-es-server/src/main/resources/logback.xml new file mode 100644 index 0000000..b60ada6 --- /dev/null +++ b/HomeWork-modules/HomeWork-es/base-es-server/src/main/resources/logback.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + ${log.pattern} + + + + + + ${log.path}/info.log + + + + ${log.path}/info.%d{yyyy-MM-dd}.log + + 60 + + + ${log.pattern} + + + + INFO + + ACCEPT + + DENY + + + + + ${log.path}/error.log + + + + ${log.path}/error.%d{yyyy-MM-dd}.log + + 60 + + + ${log.pattern} + + + + ERROR + + ACCEPT + + DENY + + + + + + + + + + + + + + + + + + diff --git a/HomeWork-modules/HomeWork-es/pom.xml b/HomeWork-modules/HomeWork-es/pom.xml index 5fb6bd7..bb97e97 100644 --- a/HomeWork-modules/HomeWork-es/pom.xml +++ b/HomeWork-modules/HomeWork-es/pom.xml @@ -2,14 +2,20 @@ - 4.0.0 HomeWork HomeWork-modules 3.6.3 + 4.0.0 - HomeWork-es + base-es + pom + + base-es-common + base-es-server + base-es-remote + 17 @@ -17,4 +23,8 @@ UTF-8 + + bawei-es系统模块 + + diff --git a/HomeWork-modules/HomeWork-es/src/main/java/HomeWork/Main.java b/HomeWork-modules/HomeWork-es/src/main/java/HomeWork/Main.java deleted file mode 100644 index 1fb2371..0000000 --- a/HomeWork-modules/HomeWork-es/src/main/java/HomeWork/Main.java +++ /dev/null @@ -1,7 +0,0 @@ -package HomeWork; - -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} \ No newline at end of file diff --git a/HomeWork-modules/HomeWork-kafka/pom.xml b/HomeWork-modules/HomeWork-kafka/pom.xml index cdef7e1..183d891 100644 --- a/HomeWork-modules/HomeWork-kafka/pom.xml +++ b/HomeWork-modules/HomeWork-kafka/pom.xml @@ -2,19 +2,125 @@ - 4.0.0 HomeWork HomeWork-modules 3.6.3 + 4.0.0 HomeWork-kafka - - 17 - 17 - UTF-8 - + + + commons-io + commons-io + 2.11.0 + + + + + com.baidu.aip + java-sdk + 4.6.0 + + + org.json + json + 20160810 + + + com.google.code.gson + gson + 2.9.0 + + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-config + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + + + org.springframework.boot + spring-boot-starter-actuator + + + + + io.springfox + springfox-swagger-ui + ${swagger.fox.version} + + + + + + HomeWork + HomeWork-common-datascope + + + + + HomeWork + HomeWork-common-log + + + + + HomeWork + HomeWork-common-swagger + + + + org.springframework.kafka + spring-kafka + + + + HomeWork + base-es-remote + 3.6.3 + + + mybatis-spring-boot-starter + org.mybatis.spring.boot + + + + + + + + ${project.artifactId} + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + diff --git a/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/HomeWorkKafkaApp.java b/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/HomeWorkKafkaApp.java new file mode 100644 index 0000000..f787760 --- /dev/null +++ b/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/HomeWorkKafkaApp.java @@ -0,0 +1,20 @@ +package HomeWork; + +import HomeWork.common.security.annotation.EnableCustomConfig; +import HomeWork.common.security.annotation.EnableRyFeignClients; +import HomeWork.common.swagger.annotation.EnableCustomSwagger2; +import com.bawei.es.remote.api.RemoteEsDocService; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@EnableCustomConfig +@EnableCustomSwagger2 +@EnableRyFeignClients +@SpringBootApplication +@ComponentScan(basePackageClasses = {RemoteEsDocService.class},basePackages = {"HomeWork","com.bawei"}) +public class HomeWorkKafkaApp { + public static void main(String[] args) { + SpringApplication.run(HomeWorkKafkaApp.class); + } +} diff --git a/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/Main.java b/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/Main.java deleted file mode 100644 index 1fb2371..0000000 --- a/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/Main.java +++ /dev/null @@ -1,7 +0,0 @@ -package HomeWork; - -public class Main { - public static void main(String[] args) { - System.out.println("Hello world!"); - } -} \ No newline at end of file diff --git a/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/consumer/Consumer.java b/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/consumer/Consumer.java new file mode 100644 index 0000000..70fb61b --- /dev/null +++ b/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/consumer/Consumer.java @@ -0,0 +1,45 @@ +package HomeWork.consumer; + +import com.bawei.es.domain.DocumentReq; +import com.bawei.es.remote.api.RemoteEsDocService; +import lombok.extern.slf4j.Slf4j; +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.springframework.kafka.annotation.KafkaListener; +import org.springframework.kafka.support.Acknowledgment; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Slf4j +@Component +public class Consumer { + + @Resource + private RemoteEsDocService remoteEsDocService; + /** + * 开启批量消费 + * @param records + * @param ack + */ + @KafkaListener(topics = {"test"},groupId = "2004") + public void func(List> records, Acknowledgment ack){ + for (ConsumerRecord record : records) { + String topic = record.topic(); + log.info("订阅topic:{}, 数据内容:{}", topic, record.value()); + DocumentReq documentReq = new DocumentReq(); + documentReq.setIndexName("img"); + ArrayList> maps = new ArrayList<>(); + HashMap stringObjectHashMap = new HashMap<>(); + stringObjectHashMap.put("img",record.value()); + maps.add(stringObjectHashMap); + documentReq.setDocumentContent(maps); + remoteEsDocService.add(documentReq); + // 确认消息已消费 + ack.acknowledge(); + } + } +} diff --git a/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/controller/KafkaController.java b/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/controller/KafkaController.java new file mode 100644 index 0000000..e5b7572 --- /dev/null +++ b/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/controller/KafkaController.java @@ -0,0 +1,24 @@ +package HomeWork.controller; + +import HomeWork.common.core.domain.R; +import HomeWork.service.KafkaService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@RestController +@RequestMapping("/kafka") +public class KafkaController { + @Autowired + private KafkaService kafkaService; + + @PostMapping("/send") + public R send(@RequestParam String img) { + kafkaService.send(img); + return R.ok(); + } +} diff --git a/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/service/KafkaService.java b/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/service/KafkaService.java new file mode 100644 index 0000000..db0e789 --- /dev/null +++ b/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/service/KafkaService.java @@ -0,0 +1,8 @@ +package HomeWork.service; + +import java.util.List; + +public interface KafkaService { + void send(String img); + +} diff --git a/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/service/impl/KafkaServiceImpl.java b/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/service/impl/KafkaServiceImpl.java new file mode 100644 index 0000000..459b8e4 --- /dev/null +++ b/HomeWork-modules/HomeWork-kafka/src/main/java/HomeWork/service/impl/KafkaServiceImpl.java @@ -0,0 +1,33 @@ +package HomeWork.service.impl; + +import HomeWork.service.KafkaService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.kafka.core.KafkaTemplate; +import org.springframework.kafka.support.SendResult; +import org.springframework.stereotype.Service; +import org.springframework.util.concurrent.ListenableFutureCallback; + +import java.util.List; + +@Service +@Slf4j +public class KafkaServiceImpl implements KafkaService { + @Autowired + private KafkaTemplate kafkaTemplate; + @Override + public void send(String img) { + kafkaTemplate.send("test", img).addCallback(new ListenableFutureCallback>() { + @Override + public void onFailure(Throwable throwable) { + log.error("发送消息失败:{}", throwable.getMessage()); + } + + @Override + public void onSuccess(SendResult result) { + log.info("发送消息成功:{}-{}-{}", result.getRecordMetadata().topic(), result.getRecordMetadata().partition(), result.getRecordMetadata().offset()); + } + }); + log.info("生产了一条新数据:{}", img); + } +} diff --git a/HomeWork-modules/HomeWork-kafka/src/main/resources/bootstrap.yml b/HomeWork-modules/HomeWork-kafka/src/main/resources/bootstrap.yml new file mode 100644 index 0000000..ec971a7 --- /dev/null +++ b/HomeWork-modules/HomeWork-kafka/src/main/resources/bootstrap.yml @@ -0,0 +1,27 @@ +# Tomcat +server: + port: 9401 + +# Spring +spring: + application: + # 应用名称 + name: HomeWork-kafka + profiles: + # 环境配置 + active: dev + cloud: + nacos: + discovery: + # 服务注册地址 + server-addr: 101.34.77.101:8848 + namespace: 20041112 + config: + # 配置中心地址 + server-addr: 101.34.77.101:8848 + namespace: 20041112 + # 配置文件格式 + file-extension: yml + # 共享配置 + shared-configs: + - application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}