个人博客地址: https://blog.zjzaki.com/archives/1692016538452
我们本次是使用的电商项目中的商品、订单、用户为案例进行讲解。
1.案例准备
1.1.技术选型
- maven:3.6.3
- 数据库:MySQL 8.0
- 持久层: SpingData Jpa/Mybatis-plus
- 其他: SpringCloud Alibaba 技术栈
1.2.模块设计
- springcloud-shop父工程
- shop-common 公共模块【实体类】
- shop-user 用户微服务 【端口: 807x】
- shop-product 商品微服务 【端口: 808x】
- shop-order 订单微服务 【端口: 809x】
1.3.微服务调用
在微服务架构中,最常见的场景就是微服务之间的相互调用。我们以电商系统中常见的用户下单为例来演示微服务的调用:客户向订单微服务发起一个下单的请求,在进行保存订单之前需要调用商品微服务查询商品的信息。
我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者。
在这种场景下,订单微服务就是一个服务消费者, 商品微服务就是一个服务提供者。
2.创建父工程
2.1.选择maven项目
2.2.填写信息
2.3.修改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"> <modelVersion>4.0.0</modelVersion> <groupId>com.zjzaki</groupId> <artifactId>SpringCloud-shop</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <!--依赖版本的锁定--> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.2.RELEASE</spring-boot.version> <spring-cloud.version>Hoxton.SR9</spring-cloud.version> <spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version> </properties> <dependencyManagement> <dependencies> <!-- SpringBoot 依赖配置 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
PS:版本对应
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
3.创建基础模块
3.1.新建shop-common模块
选择Maven
填写信息
3.2.修改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>SpringCloud-shop</artifactId> <groupId>com.zjzaki</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>shop-common</artifactId> <!--依赖--> <dependencies> <!-- <dependency>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-starter-data-jpa</artifactId>--> <!-- </dependency>--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.56</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> </dependencies> </project>
3.3.新建model包,创建实体类
package com.zjzaki.model; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; //用户 //@Entity(name = "shop_user")//实体类跟数据表的对应 @Data//不再去写set和get方法 @NoArgsConstructor @AllArgsConstructor public class User { //@Id //@GeneratedValue(strategy = GenerationType.IDENTITY)//数据库自增 private Integer uid;//主键 private String username;//用户名 private String password;//密码 private String telephone;//手机号 }
package com.zjzaki.model; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; //商品 //@Entity(name = "shop_product") @Data @NoArgsConstructor @AllArgsConstructor public class Product { //@Id //@GeneratedValue(strategy = GenerationType.IDENTITY) private Integer pid;//主键 private String pname;//商品名称 private Double pprice;//商品价格 private Integer stock;//库存 }
package com.zjzaki.model; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; //订单 //@Entity(name = "shop_order") @Data @AllArgsConstructor @NoArgsConstructor public class Order { //@Id //@GeneratedValue(strategy = GenerationType.IDENTITY) private Long oid;//订单id //用户 private Integer uid;//用户id private String username;//用户名 //商品 private Integer pid;//商品id private String pname;//商品名称 private Double pprice;//商品单价 //数量 private Integer number;//购买数量 }
3.4.创建用户微服务
3.4.1.创建模块
3.4.2.修改父工程的pom文件
<modules> <module>shop-common</module> <module>shop-user</module> </modules>
3.4.3.修改shop-user的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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>SpringCloud-shop</artifactId> <groupId>com.zjzaki</groupId> <version>1.0-SNAPSHOT</version> </parent> <!-- <modelVersion>4.0.0</modelVersion>--> <artifactId>shop-user</artifactId> <dependencies> <!--springboot-web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--shop-common--> <dependency> <groupId>com.zjzaki</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.4.4.修改application.yml
server: port: 8071 spring: application: name: service-user
3.4.5.启动测试
PS:如果出现如下报错,删除掉测试类或者添加junit依赖
无报错启动正常
3.5.创建商品微服务
与3.4类似,此处简写
3.5.1.创建一个shop-product的模块,并修改pom文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>SpringCloud-shop</artifactId> <groupId>com.zjzaki</groupId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>shop-product</artifactId> <dependencies> <!--springboot-web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--shop-common--> <dependency> <groupId>com.zking</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
3.5.2.修改父工程的pom文件
<modules> <module>shop-common</module> <module>shop-user</module> <module>shop-product</module> </modules>
3.5.3.修改application.yml
server: port: 8081 spring: application: name: service-product
3.5.4.新建controller包,创建ProductController
package com.zjzaki.shopproduct.controller; import com.alibaba.fastjson.JSON; import com.zjzaki.model.Product; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @Slf4j public class ProductController { /** * 商品信息查询 */ @RequestMapping("/product/{pid}") public Product product(@PathVariable("pid") Integer pid) { log.info("接下来要进行{}号商品信息的查询", pid); Product product = new Product(pid,"这是商品",1000d,100); log.info("商品信息查询成功,内容为{}", JSON.toJSONString(product)); return product; } }
3.5.5.启动测试
访问: http://localhost:8081/product/1
出现如下图表示成功
3.6.创建订单服务
与3.4类似,此处简写
3.6.1.创建一个shop-order的模块,修改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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <artifactId>SpringCloud-shop</artifactId> <groupId>com.zjzaki</groupId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>shop-order</artifactId> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--springboot-web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--shop-common--> <dependency> <groupId>com.zjzaki</groupId> <artifactId>shop-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> </project>
3.6.2.修改父工程的pom.xml
<modules> <module>shop-common</module> <module>shop-user</module> <module>shop-product</module> <module>shop-order</module> </modules>
3.6.3.修改application.yml
server: port: 8091 spring: application: name: service-order
3.6.4.修改ShopOrderApplication
package com.zjzaki.shoporder; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class ShopOrderApplication { public static void main(String[] args) { SpringApplication.run(ShopOrderApplication.class, args); } @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } }
3.6.5.新建controller包,创建OrderController
package com.zjzaki.shoporder.controller; import com.alibaba.fastjson.JSON; import com.zjzaki.model.Order; import com.zjzaki.model.Product; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @Slf4j public class OrderController { @Autowired private RestTemplate restTemplate; /** * 准备买1件商品 * @param pid 商品id * @return 商品信息 */ @GetMapping("/order/prod/{pid}") public Order order(@PathVariable("pid") Integer pid) { log.info(">>客户下单,这时候要调用商品微服务查询商品信息"); //通过restTemplate调用商品微服务 Product product = restTemplate.getForObject( "http://localhost:8081/product/" + pid, Product.class); log.info(">>商品信息,查询结果:" + JSON.toJSONString(product)); Order order = new Order(); order.setUid(1); order.setUsername("测试用户"); order.setPid(product.getPid()); order.setPname(product.getPname()); order.setPprice(product.getPprice()); order.setNumber(1); return order; } }
3.6.6.启动测试
PS:此处访问时需要启动前面的两个微服务
访问:http://localhost:8091/order/prod/1
如下图表示成功