一、订单子模块
pom.xml
<?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">
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.zte.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-consumer-order 8002</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
</project>
application.yaml
server:
port: 8001
servlet:
context-path: /zte-order-provider
spring:
application:
name: zte-order-provider
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://120.79.28.20:3308/cloud
username: root
password: 123456
mybatis:
mapper-locations: classpath:com.zte.cloud.bo.*.xml
type-aliases-package: com.zte.cloud.bo
统一页面返回值
package com.zte.cloud.utils;
@Data
public class ServiceData implements Serializable {
private static final long serialVersionUID = -3645478571353275780L;
private String code;
private String msg;
private Object obj;
}
二、支付子模块
application.yaml
server:
port: 8002
servlet:
context-path: /zte-payment-consumer
spring:
application:
name: zte-payment-consumer
远程调用类-RestTemplate
package com.zte.cloud.config;
@Configuration
public class RestTemplateInjector {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
controller
package com.zte.cloud.controller;
@RestController
@RequestMapping("/payment")
public class PaymentController {
@Autowired
private RestTemplate restTemplate;
private static String ORDER_URL = "http://localhost:8001/zte-order-provider";
@PostMapping("/insert")
public ServiceData insertOne(@RequestBody Phone phone){
ServiceData serviceData = restTemplate.postForObject(ORDER_URL + "/order/insert", phone, ServiceData.class);
return serviceData;
}
@GetMapping("/get/{id}")
public ServiceData getById(@PathVariable("id") Integer id){
ServiceData serviceData = restTemplate.getForObject(ORDER_URL + "/order/get/" + id, ServiceData.class);
return serviceData;
}
}
三、工程重构----抽取
<dependency>
<groupId>com.zte.cloud</groupId>
<artifactId>api-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
- 将公用的类抽取到common工程,并将common工程打包放在maven仓库中;
- 在其工程的pom.xml文件中引入common工程的依赖;
- 修改其他工程中mapper的别名配置的包:common工程对应的包结构;