From 580b23885dfb3cef3a95e11d9ef238c5b531e73a Mon Sep 17 00:00:00 2001 From: YunaiV Date: Mon, 23 Sep 2019 22:45:06 +0800 Subject: [PATCH] =?UTF-8?q?-=20demo=20=E9=A1=B9=E7=9B=AE=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=20RPC=20=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/demo-application/pom.xml | 11 ++++ .../demo/application/DemoApplication.java | 12 +++++ .../controller/DemoProductController.java | 12 +++++ .../src/main/resources/application-dev.yaml | 0 .../src/main/resources/application-local.yaml | 13 +++++ .../src/main/resources/application-pre.yaml | 0 .../src/main/resources/application-prod.yaml | 0 .../src/main/resources/application-uat.yaml | 0 .../src/main/resources/application.yaml | 7 +++ .../application.yaml => business-local.yaml} | 0 .../mall/demo/job/handler/DemoJobHandler.java | 4 ++ .../iocoder/mall/demo/job/package-info.java | 1 + .../cn/iocoder/mall/demo/mq/package-info.java | 1 + .../demo/rpc/api/DemoProductRpcService.java | 9 ++++ .../iocoder/mall/demo/rpc/package-info.java | 1 + .../mall/demo/rpc/vo/DemoProductVO.java | 22 ++++++++ demo/demo-rpc-service/pom.xml | 50 +++++++++++++++++++ .../demo/rpc/convert/DemoProductConvert.java | 17 +++++++ .../iocoder/mall/demo/rpc/package-info.java | 1 + .../service/DemoProductRpcServiceImpl.java | 23 +++++++++ .../src/main/resources/rpc-local.yaml | 18 +++++++ 21 files changed, 202 insertions(+) create mode 100644 demo/demo-application/src/main/resources/application-dev.yaml create mode 100644 demo/demo-application/src/main/resources/application-local.yaml create mode 100644 demo/demo-application/src/main/resources/application-pre.yaml create mode 100644 demo/demo-application/src/main/resources/application-prod.yaml create mode 100644 demo/demo-application/src/main/resources/application-uat.yaml rename demo/demo-business/src/main/resources/{config/application.yaml => business-local.yaml} (100%) create mode 100644 demo/demo-job/src/main/java/cn/iocoder/mall/demo/job/handler/DemoJobHandler.java create mode 100644 demo/demo-job/src/main/java/cn/iocoder/mall/demo/job/package-info.java create mode 100644 demo/demo-mq/src/main/java/cn/iocoder/mall/demo/mq/package-info.java create mode 100644 demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/api/DemoProductRpcService.java create mode 100644 demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/package-info.java create mode 100644 demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/vo/DemoProductVO.java create mode 100644 demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/convert/DemoProductConvert.java create mode 100644 demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/package-info.java create mode 100644 demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/service/DemoProductRpcServiceImpl.java create mode 100644 demo/demo-rpc-service/src/main/resources/rpc-local.yaml diff --git a/demo/demo-application/pom.xml b/demo/demo-application/pom.xml index c3b9596fe..f13a618eb 100644 --- a/demo/demo-application/pom.xml +++ b/demo/demo-application/pom.xml @@ -49,6 +49,17 @@ 1.0-SNAPSHOT + + cn.iocoder.mall + demo-rpc-service-api + 1.0-SNAPSHOT + + + cn.iocoder.mall + demo-rpc-service + 1.0-SNAPSHOT + + org.springframework.boot diff --git a/demo/demo-application/src/main/java/cn/iocoder/mall/demo/application/DemoApplication.java b/demo/demo-application/src/main/java/cn/iocoder/mall/demo/application/DemoApplication.java index b8d86eb43..71b2cc9b5 100644 --- a/demo/demo-application/src/main/java/cn/iocoder/mall/demo/application/DemoApplication.java +++ b/demo/demo-application/src/main/java/cn/iocoder/mall/demo/application/DemoApplication.java @@ -2,13 +2,25 @@ package cn.iocoder.mall.demo.application; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.config.ConfigFileApplicationListener; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication(scanBasePackages = {"cn.iocoder.mall.demo"}) @EnableAsync(proxyTargetClass = true) +//@PropertySource("classpath*:/application-dubbo.yaml") public class DemoApplication { + /** + * 设置需要读取的配置文件的名字。 + * 基于 {@link org.springframework.boot.context.config.ConfigFileApplicationListener#CONFIG_NAME_PROPERTY} 实现。 + */ + private static final String CONFIG_NAME_VALUE = "application,rpc,business"; + public static void main(String[] args) { + // 设置环境变量 + System.setProperty(ConfigFileApplicationListener.CONFIG_NAME_PROPERTY, CONFIG_NAME_VALUE); + + // 启动 Spring Boot 应用 SpringApplication.run(DemoApplication.class, args); } diff --git a/demo/demo-application/src/main/java/cn/iocoder/mall/demo/application/controller/DemoProductController.java b/demo/demo-application/src/main/java/cn/iocoder/mall/demo/application/controller/DemoProductController.java index 3dc6ebcf7..f5f717ee5 100644 --- a/demo/demo-application/src/main/java/cn/iocoder/mall/demo/application/controller/DemoProductController.java +++ b/demo/demo-application/src/main/java/cn/iocoder/mall/demo/application/controller/DemoProductController.java @@ -5,6 +5,8 @@ import cn.iocoder.mall.demo.application.convert.DemoProductConvert; import cn.iocoder.mall.demo.application.vo.DemoProductVO; import cn.iocoder.mall.demo.business.api.DemoProductService; import cn.iocoder.mall.demo.business.bo.product.DemoProductBO; +import cn.iocoder.mall.demo.rpc.api.DemoProductRpcService; +import org.apache.dubbo.config.annotation.Reference; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -18,10 +20,20 @@ public class DemoProductController { @Autowired private DemoProductService productService; + @Reference(validation = "true", version = "${dubbo.consumer.DemoProductRpcService.version}") + private DemoProductRpcService productRpcService; + @GetMapping("/get") public CommonResult get(@RequestParam("id") Integer id) { DemoProductBO product = productService.get(id); return CommonResult.success(DemoProductConvert.INSTANCE.convert(product)); } + // TODO 芋艿,这里只是做一个 demo 。实际一般不会这么玩,更多是内嵌的,像 {@link #get(Integer id)} 的情况。 + @GetMapping("/get2") + public CommonResult get2(@RequestParam("id") Integer id) { + cn.iocoder.mall.demo.rpc.vo.DemoProductVO product = productRpcService.get(id); + return null; + } + } diff --git a/demo/demo-application/src/main/resources/application-dev.yaml b/demo/demo-application/src/main/resources/application-dev.yaml new file mode 100644 index 000000000..e69de29bb diff --git a/demo/demo-application/src/main/resources/application-local.yaml b/demo/demo-application/src/main/resources/application-local.yaml new file mode 100644 index 000000000..7973f5c06 --- /dev/null +++ b/demo/demo-application/src/main/resources/application-local.yaml @@ -0,0 +1,13 @@ +dubbo: + application: + name: demo-service + registry: + address: zookeeper://127.0.0.1:2181 + protocol: + port: -1 + name: dubbo + scan: + base-packages: cn.iocoder.mall.demo.rpc.service + consumer: + DemoProductRpcService: + version: 1.0.0 diff --git a/demo/demo-application/src/main/resources/application-pre.yaml b/demo/demo-application/src/main/resources/application-pre.yaml new file mode 100644 index 000000000..e69de29bb diff --git a/demo/demo-application/src/main/resources/application-prod.yaml b/demo/demo-application/src/main/resources/application-prod.yaml new file mode 100644 index 000000000..e69de29bb diff --git a/demo/demo-application/src/main/resources/application-uat.yaml b/demo/demo-application/src/main/resources/application-uat.yaml new file mode 100644 index 000000000..e69de29bb diff --git a/demo/demo-application/src/main/resources/application.yaml b/demo/demo-application/src/main/resources/application.yaml index 73ee32099..982596533 100644 --- a/demo/demo-application/src/main/resources/application.yaml +++ b/demo/demo-application/src/main/resources/application.yaml @@ -1,3 +1,10 @@ +# spring +spring: + application: + name: demo-application + profiles: + active: local + # server server: port: 8080 diff --git a/demo/demo-business/src/main/resources/config/application.yaml b/demo/demo-business/src/main/resources/business-local.yaml similarity index 100% rename from demo/demo-business/src/main/resources/config/application.yaml rename to demo/demo-business/src/main/resources/business-local.yaml diff --git a/demo/demo-job/src/main/java/cn/iocoder/mall/demo/job/handler/DemoJobHandler.java b/demo/demo-job/src/main/java/cn/iocoder/mall/demo/job/handler/DemoJobHandler.java new file mode 100644 index 000000000..0f3156dea --- /dev/null +++ b/demo/demo-job/src/main/java/cn/iocoder/mall/demo/job/handler/DemoJobHandler.java @@ -0,0 +1,4 @@ +package cn.iocoder.mall.demo.job.handler; + +public class DemoJobHandler { +} diff --git a/demo/demo-job/src/main/java/cn/iocoder/mall/demo/job/package-info.java b/demo/demo-job/src/main/java/cn/iocoder/mall/demo/job/package-info.java new file mode 100644 index 000000000..30d04308f --- /dev/null +++ b/demo/demo-job/src/main/java/cn/iocoder/mall/demo/job/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.mall.demo.job; diff --git a/demo/demo-mq/src/main/java/cn/iocoder/mall/demo/mq/package-info.java b/demo/demo-mq/src/main/java/cn/iocoder/mall/demo/mq/package-info.java new file mode 100644 index 000000000..8172d958c --- /dev/null +++ b/demo/demo-mq/src/main/java/cn/iocoder/mall/demo/mq/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.mall.demo.mq; diff --git a/demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/api/DemoProductRpcService.java b/demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/api/DemoProductRpcService.java new file mode 100644 index 000000000..e99647d0d --- /dev/null +++ b/demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/api/DemoProductRpcService.java @@ -0,0 +1,9 @@ +package cn.iocoder.mall.demo.rpc.api; + +import cn.iocoder.mall.demo.rpc.vo.DemoProductVO; + +public interface DemoProductRpcService { + + DemoProductVO get(Integer id); + +} diff --git a/demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/package-info.java b/demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/package-info.java new file mode 100644 index 000000000..6fdbe595a --- /dev/null +++ b/demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.mall.demo.rpc; diff --git a/demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/vo/DemoProductVO.java b/demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/vo/DemoProductVO.java new file mode 100644 index 000000000..06c0c5bd1 --- /dev/null +++ b/demo/demo-rpc-service-api/src/main/java/cn/iocoder/mall/demo/rpc/vo/DemoProductVO.java @@ -0,0 +1,22 @@ +package cn.iocoder.mall.demo.rpc.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@ApiModel("Demo 商品 BO") +@Data +public class DemoProductVO { + + @ApiModelProperty(value = "编号", required = true, example = "1") + private Integer id; + + @ApiModelProperty(value = "华为 Mate30 Pro", required = true, example = "小王") + private String name; + + @ApiModelProperty(value = "价格,单位:分", required = true, example = "10") + private Integer price; + @ApiModelProperty(value = "库存数量", required = true, example = "100") + private Integer quantity; + +} diff --git a/demo/demo-rpc-service/pom.xml b/demo/demo-rpc-service/pom.xml index 669b313f1..db628e433 100644 --- a/demo/demo-rpc-service/pom.xml +++ b/demo/demo-rpc-service/pom.xml @@ -11,5 +11,55 @@ demo-rpc-service + + + + cn.iocoder.mall + demo-rpc-service-api + 1.0-SNAPSHOT + + + + cn.iocoder.mall + demo-business-api + 1.0-SNAPSHOT + + + cn.iocoder.mall + demo-business + 1.0-SNAPSHOT + + + + + org.apache.dubbo + dubbo + + + org.apache.dubbo + dubbo-spring-boot-starter + + + + + org.apache.curator + curator-framework + + + org.apache.curator + curator-recipes + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + diff --git a/demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/convert/DemoProductConvert.java b/demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/convert/DemoProductConvert.java new file mode 100644 index 000000000..d0eb97d87 --- /dev/null +++ b/demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/convert/DemoProductConvert.java @@ -0,0 +1,17 @@ +package cn.iocoder.mall.demo.rpc.convert; + +import cn.iocoder.mall.demo.business.bo.product.DemoProductBO; +import cn.iocoder.mall.demo.rpc.vo.DemoProductVO; +import org.mapstruct.Mapper; +import org.mapstruct.Mappings; +import org.mapstruct.factory.Mappers; + +@Mapper +public interface DemoProductConvert { + + DemoProductConvert INSTANCE = Mappers.getMapper(DemoProductConvert.class); + + @Mappings({}) + DemoProductVO convert(DemoProductBO object); + +} diff --git a/demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/package-info.java b/demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/package-info.java new file mode 100644 index 000000000..6fdbe595a --- /dev/null +++ b/demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/package-info.java @@ -0,0 +1 @@ +package cn.iocoder.mall.demo.rpc; diff --git a/demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/service/DemoProductRpcServiceImpl.java b/demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/service/DemoProductRpcServiceImpl.java new file mode 100644 index 000000000..2d74c7841 --- /dev/null +++ b/demo/demo-rpc-service/src/main/java/cn/iocoder/mall/demo/rpc/service/DemoProductRpcServiceImpl.java @@ -0,0 +1,23 @@ +package cn.iocoder.mall.demo.rpc.service; + +import cn.iocoder.mall.demo.business.api.DemoProductService; +import cn.iocoder.mall.demo.business.bo.product.DemoProductBO; +import cn.iocoder.mall.demo.rpc.api.DemoProductRpcService; +import cn.iocoder.mall.demo.rpc.convert.DemoProductConvert; +import cn.iocoder.mall.demo.rpc.vo.DemoProductVO; +import org.apache.dubbo.config.annotation.Service; +import org.springframework.beans.factory.annotation.Autowired; + +@Service(validation = "true", version = "${dubbo.provider.DemoProductRpcService.version}") +public class DemoProductRpcServiceImpl implements DemoProductRpcService { + + @Autowired + private DemoProductService productService; + + @Override + public DemoProductVO get(Integer id) { + DemoProductBO product = productService.get(id); + return DemoProductConvert.INSTANCE.convert(product); + } + +} diff --git a/demo/demo-rpc-service/src/main/resources/rpc-local.yaml b/demo/demo-rpc-service/src/main/resources/rpc-local.yaml new file mode 100644 index 000000000..6f49e31e7 --- /dev/null +++ b/demo/demo-rpc-service/src/main/resources/rpc-local.yaml @@ -0,0 +1,18 @@ +# dubbo +dubbo: + application: + name: demo-service + registry: + address: zookeeper://127.0.0.1:2181 + protocol: + port: -1 + name: dubbo + scan: + base-packages: cn.iocoder.mall.demo.rpc.service +# consumer: +# ProductSpuService: +# version: 1.0.0 + provider: +# filter: -exception + DemoProductRpcService: + version: 1.0.0