目录
我们本次是使用的电商项目中的商品、订单、用户为案例进行讲解。
maven:3.5.4
数据库:MySQL 5.7
持久层: SpingData Jpa/Mybatis-plus
其他: SpringCloud Alibaba 技术栈
springcloud-shop父工程
shop-common 公共模块【实体类】
shop-user 用户微服务 【端口: 8070】
shop-product 商品微服务 【端口: 8080】
shop-order 订单微服务 【端口: 8090】
接下来我们做实现微服务的项目准备环节:
1、创建父工程
父工程是maven项目
创建完成过后更改maven地址!!!
2、导入pom依赖
"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.0modelVersion> <groupId>com.zqgroupId> <artifactId>springcloud-shopartifactId> <version>1.0-SNAPSHOTversion> <packaging>pompackaging> <properties> <java.version>1.8java.version> <project.build.sourceEncoding>UTF-8project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding> <spring-boot.version>2.3.2.RELEASEspring-boot.version> <spring-cloud.version>Hoxton.SR9spring-cloud.version> <spring-cloud-alibaba.version>2.2.6.RELEASEspring-cloud-alibaba.version> properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-dependenciesartifactId> <version>${spring-boot.version}version> <type>pomtype> <scope>importscope> dependency> dependencies> dependencyManagement> project>在这里我们需要注意的一点事Springcloud与Springboot的版本问题:
这里给出对应的版本对应关系表:
3、创建基础模块
基础模块也是Maven项目所以步骤和上面创建父工程一样
4、导入基础模块的pom依赖
"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-shopartifactId> <groupId>com.zqgroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>shop-commonartifactId> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-data-jpaartifactId> dependency> <dependency> <groupId>org.projectlombokgroupId> <artifactId>lombokartifactId> dependency> <dependency> <groupId>com.alibabagroupId> <artifactId>fastjsonartifactId> <version>1.2.56version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>5.1.44version> dependency> dependencies> project>在这里区别创建Maven和Springboot项目,我们创建Maven项目,在父工程的pom.xml中会自动的产生子模块的标记:而springboot项目需要自己手动添加
5、创建实体类放于基础模块
这里小编就是放入的三个实体类
6、创建用户微服务
导入用户服务的Pom依赖
"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"> <parent> <artifactId>springcloud-shopartifactId> <groupId>com.zqgroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>shop-userartifactId> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>com.zqgroupId> <artifactId>shop-commonartifactId> <version>1.0-SNAPSHOTversion> dependency> dependencies> project>7、创建商品微服务
导入商品微服务的pom依赖
"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"> <parent> <artifactId>springcloud-shopartifactId> <groupId>com.zqgroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>shop-productartifactId> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>com.zqgroupId> <artifactId>shop-commonartifactId> <version>1.0-SNAPSHOTversion> dependency> dependencies> project>8、创建订单微服务
导入订单的Pom依赖
"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"> <parent> <artifactId>springcloud-shopartifactId> <groupId>com.zqgroupId> <version>1.0-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <artifactId>shop-orderartifactId> <dependencies> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>com.zqgroupId> <artifactId>shop-commonartifactId> <version>1.0-SNAPSHOTversion> dependency> dependencies> project>9、修改三个微服务的访问端口
到这里我们的准备OK了,接下来就是代码实现微服务的相互调用了。
- package com.zq.shopuser.controller;
-
- import com.zq.entity.User;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @author张强
- * @site www.zq.com
- * @create 2022-11-25 15:26
- */
- @RequestMapping("/user")
- @RestController
- public class UserController {
- @RequestMapping("/get/{id}")
- public User get(@PathVariable("id") Integer id){
- // 查询数据库
- return new User(id,"zq","123456","15679031652");
- }
- }
- package com.zq.shopproduct.controller;
-
- import com.zq.entity.Product;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @author张强
- * @site www.zq.com
- * @create 2022-11-25 15:31
- */
- @RestController
- @RequestMapping("/product")
- public class ProductController {
- @RequestMapping("/get/{pid}")
- public Product get(@PathVariable("pid") Integer pid){
- Product p = new Product(pid, "香荷泥煎面", 36d, 36);
- return p;
- }
- }
- package com.zq.shoporder.controller;
-
- import com.zq.entity.Order;
- import com.zq.entity.Product;
- import com.zq.entity.User;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import org.springframework.web.client.RestTemplate;
-
- /**
- * @author张强
- * @site www.zq.com
- * @create 2022-11-25 15:35
- */
- @RequestMapping("/order")
- @RestController
- public class OrderController {
-
- @Autowired
- private RestTemplate restTemplate;
-
- @RequestMapping("/get/{uid}/{pid}")
- public Order get(@PathVariable("uid") Integer uid,
- @PathVariable("pid") Integer pid){
- User user = restTemplate.getForObject("http://localhost:8070/user/get/" + uid, User.class);
- Product product = restTemplate.getForObject("http://localhost:8090/product/get/" + pid, Product.class);
- Order order = new Order();
- order.setNumber(2);
- order.setOid(System.currentTimeMillis());
- order.setPid(pid);
- order.setPname(product.getPname());
- order.setPprice(product.getPprice() * order.getNumber());
- order.setUid(user.getUid());
- order.setUsername(user.getUsername());
- return order;
- }
- }