我们本次是使用的电商项目中的商品、订单为案例进行讲解。
maven : 3.5.0
数据库:Mysql5.6以上
持久层:Mybatis-plus(MyBatis)
其他:SpringCloud Alibaba
springcloud-alibaba 父工程 《jar版本的管理》
shop-common 公共模块【实体类】 《实体类,公共依赖,工具类。》
shop-product 商品微服务 【端口: 8080~8089】
shop-order 订单微服务 【端口: 8090~8099】
-- ----------------------------
-- Table structure for shop_order
-- ----------------------------
DROP TABLE IF EXISTS `shop_order`;
CREATE TABLE `shop_order` (
`oid` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NULL DEFAULT NULL,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`pid` int(11) NULL DEFAULT NULL,
`pname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`pprice` decimal(10, 0) NULL DEFAULT NULL,
`number` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`oid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 7152 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Table structure for shop_product
-- ----------------------------
DROP TABLE IF EXISTS `shop_product`;
CREATE TABLE `shop_product` (
`pid` int(11) NOT NULL AUTO_INCREMENT,
`pname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`pprice` decimal(10, 0) NULL DEFAULT NULL,
`stock` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`pid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-- ----------------------------
-- Records of shop_product
-- ----------------------------
INSERT INTO `shop_product` VALUES (1, '华为', 3900, 6000);
INSERT INTO `shop_product` VALUES (2, 'vivo', 2999, 60000);
INSERT INTO `shop_product` VALUES (3, '小米', 6000, 6000);
SET FOREIGN_KEY_CHECKS = 1;
在微服务框架中,最常见的场景就是微服务之间的相互调用。我们以电商系统最常见的用户下单功能为例子来演示微服务的调用:客户端向订单微服务发送一个下单的请求,在进行保存订单之前需要调用商品微服务来查询商品的信息。
我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者。
在这种场景下,订单微服务就是一个服务消费者, 商品微服务就是一个服务提供者。
我们以本地项目进行演示。我们创建一个父项目、一个公共项目和两个微服务
创建一个maven工程,然后在pom.xml文件中添加下面内容
<parent>
<artifactId>spring-boot-starter-parentartifactId>
<groupId>org.springframework.bootgroupId>
<version>2.3.3.RELEASEversion>
parent>
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporing.outputEncoding>UTF-8project.reporing.outputEncoding>
<spring-cloud.version>Hoxton.SR8spring-cloud.version>
<spring-cloud-alibaba.version>2.2.5.RELEASEspring-cloud-alibaba.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${spring-cloud-alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
1.一定要在当前项目下创建,如下:
shop-common工程在CSDN工程下边
2.在shop-common的pom.xml中添加依赖在此公共工程里面定义的都是所有
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.1version>
dependency>
dependencies>
3.在shop-common中创建公共的实体类 Order 、 Prodcut
Order类:
@Data
@TableName("shop_order")
public class Order {
@TableId(type = IdType.AUTO)
private Integer oid;
private Integer uid;
private String username;
private Integer pid;
private String pname;
private Double pprice;
private Integer number;
}
product类:
@Data
@TableName("shop_product")
public class Product {
@TableId(type = IdType.AUTO)
private Integer pid;
private String pname;
private Double pprice;
private Integer stock;
}
这两个类要建好,与数据库对应。
<dependencies>
<dependency>
<groupId>org.examplegroupId>
<artifactId>shop-commonartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
dependencies>
2.手动在resources文件夹下创建application.properties并编写配置文件:
# 为该微服务设置端口号
server.port=8081
# 数据源的信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springcloud?serverTimezone=Asia/Shanghai&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
mybatis-plus.mapper-locations=classpath:mapper/*.xml
# dao生产sql日志
logging.level.com.aaa.dao=debug
3.创建主启动类:
@SpringBootApplication
@MapperScan("com.aaa.dao")
public class AppProduct {
public static void main(String[] args) {
SpringApplication.run(AppProduct.class,args);
}
}
4.创建dao接口继承BaseMapper(<泛型一定要加上>,这样就能用Mybaits-plus封装好的方法了):
public interface ProductDao extends BaseMapper<Product> {
}
5.编写业务代码:
我这边偷个懒,都写在controller内了,大家不要仿照我。
@RestController
@CrossOrigin
@RequestMapping("/product")
public class ProductController {
@Resource
private ProductDao dao;
@GetMapping("/findByIdProduct/{pid}")
public Product findByIdProduct(@PathVariable("pid") Integer pid ){
return dao.selectById(pid);
}
}
1.创建工程shop-order,和shop-product创建一样,都在CSDN工程下
2.pom.xml配置:
<dependencies>
<dependency>
<groupId>org.examplegroupId>
<artifactId>shop-commonartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
dependencies>
3.手动在resources文件夹下创建application.properties并编写配置文件:
server.port=8091
# 数据源的信息
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springcloud?serverTimezone=Asia/Shanghai&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
mybatis-plus.mapper-locations=classpath:mapper/*.xml
# dao生产sql日志
logging.level.com.aaa.dao=debug
4.创建主启动类:
@SpringBootApplication
@MapperScan("com.aaa.dao")
public class AppOrder {
public static void main(String[] args) {
SpringApplication.run(AppOrder.class,args);
}
}
5.创建dao接口继承BaseMapper(<泛型一定要加上>,这样就能用Mybaits-plus封装好的方法了):
public interface OrderDao extends BaseMapper<Order> {
}
6.编写业务代码:
@RestController
@CrossOrigin
@RequestMapping("/order")
public class OrderController_openfeign {
@Resource
private OrderDao dao;
//RestTemplate对象可以帮我们获取微服务对象
@Autowired
private RestTemplate restTemplate;
@Autowired
private ProductFeign productFeign;
@GetMapping("payOrder")
public String payOrder(Integer pid , Integer num){
Order order = new Order();
order.setUid(1);
order.setNumber(num);
order.setUsername("沈金坡");
order.setPid(pid);
//通过restTemplate.getForObject方法传入微服务请求路径,可以远程调用方法。
Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
if (product!=null){
order.setPname(product.getPname());
order.setPprice(product.getPprice());
}else {
throw new RuntimeException("您购买的商品已下架");
}
int i = dao.insert(order);
if(i>0){
return "下单成功";
}
return "失败";
}
}