目录
1.准备两个数据库
将两张表分别放入两个数据库
注:这里的sql是8版本的,如果是5.7的就把所有的COLLATE utf8mb4_0900_ai_ci删除即可成功运行
订单表所在的数据库:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for shop_order
-- ----------------------------
DROP TABLE IF EXISTS `shop_order`;
CREATE TABLE `shop_order` (
`oid` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '订单id',
`uid` int(0) NULL DEFAULT NULL COMMENT '用户id',
`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户名',
`pid` bigint(0) NULL DEFAULT NULL COMMENT '商品id',
`pname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品名称',
`pprice` decimal(10, 2) NULL DEFAULT NULL COMMENT '商品价格',
`number` int(0) NULL DEFAULT NULL COMMENT '购买数量',
PRIMARY KEY (`oid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 44956 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of shop_order
-- ----------------------------
SET FOREIGN_KEY_CHECKS = 1;
商品表所在数据库:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for shop_product
-- ----------------------------
DROP TABLE IF EXISTS `shop_product`;
CREATE TABLE `shop_product` (
`pid` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '商品id',
`pname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品名',
`pprice` decimal(10, 2) NULL DEFAULT NULL COMMENT '商品价格',
`stock` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品库存',
PRIMARY KEY (`pid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of shop_product
-- ----------------------------
INSERT INTO `shop_product` VALUES (1, '华为手机', 1999.00, '100');
INSERT INTO `shop_product` VALUES (2, 'vivo手机', 2999.00, '100');
INSERT INTO `shop_product` VALUES (3, '小米', 2222.00, '1000');
SET FOREIGN_KEY_CHECKS = 1;
2.搭建父工程
(1)删除父工程的src目录,并新建三个maven模块,如图:
(2)父工程引入依赖:
<!--继承springboot父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<!--定义版本号-->
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF- 8</project.reporting.outputEncoding>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.3.RELEASE</spring-cloud-alibaba.version>
</properties>
<!--dependencyManagement: 它只负责jar的管理 不负责jar的下载 子类使用该类型的jar包时,直接引用而无需写版本-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
(3)common模块
- 相关依赖
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
- 实体类
@Data
@TableName("shop_order")
public class Order implements Serializable {
@TableId(value = "oid",type = IdType.AUTO)
private Integer oid;
private Integer uid;
private String username;
private Integer pid;
private String pname;
private BigDecimal pprice;
private Integer number;
}
@Data
@TableName("shop_product")
public class Product {
@TableId(value = "pid",type = IdType.AUTO)
private Integer pid;
private String pname;
private BigDecimal pprice;
private Integer stock;
}
(4)order模块
相关依赖:
<dependencies>
<!--公共模块引入-->
<dependency>
<groupId>com.aaa</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
在resource目录中创建application.yml文件
server:
port: 9001
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql:///springcloud_order?serverTimezone=Asia/Shanghai
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:/mapper/*.xml
启动类:
@SpringBootApplication
@MapperScan(basePackages = {"com.aaa.order.dao"})
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
//远程调用对象RestTemplate放入容器
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
controller层:
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private OrderService orderService;
//注入远程调用对象RestTemplate
@Autowired
private RestTemplate restTemplate;
@GetMapping("/insert")
public String insert(Integer pid,Integer num){
Order order=new Order();
order.setUid(1);
order.setUsername("wjq");
order.setNumber(num);
//1.远程调用product的服务查询商品对象
Product product = restTemplate.getForObject("http://localhost:8001/product/getById/" + pid, Product.class);
//2.商品数据添加到订单表
order.setPid(product.getPid());
order.setPname(product.getPname());
order.setPprice(product.getPprice());
int i = orderService.save(order);
return i > 0 ? "下单成功" : "下单失败";
}
}
dao层:
public interface OrderDao extends BaseMapper<Order> {
}
service层:
public interface OrderService {
int save(Order order);
}
impl:
@Service
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderDao;
@Override
public int save(Order order) {
return orderDao.insert(order);
}
}
(5)product模块
相关依赖:
<dependencies>
<!--公共模块引入-->
<dependency>
<groupId>com.aaa</groupId>
<artifactId>common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
在resource目录中创建application.yml文件
server:
port: 8001
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql:///springcloud_product?serverTimezone=Asia/Shanghai
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:/mapper/*.xml
启动类:
@SpringBootApplication
@MapperScan(basePackages = {"com.aaa.product.dao"})
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class,args);
}
}
controller层:
@RestController
@RequestMapping("product")
public class ProductController {
@Autowired
private ProductService productService;
@GetMapping("/getById/{id}")
public Product getById(@PathVariable Integer id){
Product product = productService.getById(id);
return product;
}
}
dao层:
public interface ProductDao extends BaseMapper<Product> {
}
service层:
public interface ProductDao extends BaseMapper<Product> {
}
impl:
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Override
public Product getById(Integer id) {
return productDao.selectById(id);
}
}
3.测试
分别启动商品和订单的启动类:
访问http://localhost:9001/order/insert?pid=1&num=1
查看order服务和product服务的控制台:
发现使用order服务远程调用成功!!!