- demo 项目,增加 RPC 接口

pull/2/MERGE
YunaiV 2019-09-23 22:45:06 +08:00
parent 683b9a7a19
commit 580b23885d
21 changed files with 202 additions and 0 deletions

View File

@ -49,6 +49,17 @@
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>demo-rpc-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>demo-rpc-service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- Web 相关 -->
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -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);
}

View File

@ -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<DemoProductVO> 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<DemoProductVO> get2(@RequestParam("id") Integer id) {
cn.iocoder.mall.demo.rpc.vo.DemoProductVO product = productRpcService.get(id);
return null;
}
}

View File

@ -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

View File

@ -1,3 +1,10 @@
# spring
spring:
application:
name: demo-application
profiles:
active: local
# server
server:
port: 8080

View File

@ -0,0 +1,4 @@
package cn.iocoder.mall.demo.job.handler;
public class DemoJobHandler {
}

View File

@ -0,0 +1 @@
package cn.iocoder.mall.demo.job;

View File

@ -0,0 +1 @@
package cn.iocoder.mall.demo.mq;

View File

@ -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);
}

View File

@ -0,0 +1 @@
package cn.iocoder.mall.demo.rpc;

View File

@ -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;
}

View File

@ -11,5 +11,55 @@
<artifactId>demo-rpc-service</artifactId>
<dependencies>
<!-- Mall 相关 -->
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>demo-rpc-service-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>demo-business-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>cn.iocoder.mall</groupId>
<artifactId>demo-business</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- RPC 相关 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<!-- Registry 和 Config 相关 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 提供给 mapstruct 使用 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

View File

@ -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);
}

View File

@ -0,0 +1 @@
package cn.iocoder.mall.demo.rpc;

View File

@ -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);
}
}

View File

@ -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