目录
一、前置准备
二、项目准备
三、实现跨项目调用
我们本次是使用的电商项目中的商品、订单、用户为案例进行讲解。
技术选型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依赖
4.0.0 com.zq springcloud-shop 1.0-SNAPSHOT pom 1.8 UTF-8 UTF-8 2.3.2.RELEASE Hoxton.SR9 2.2.6.RELEASE org.springframework.boot spring-boot-dependencies ${spring-boot.version} pom import 在这里我们需要注意的一点事Springcloud与Springboot的版本问题:
这里给出对应的版本对应关系表:
3、创建基础模块
基础模块也是Maven项目所以步骤和上面创建父工程一样
4、导入基础模块的pom依赖
springcloud-shop com.zq 1.0-SNAPSHOT 4.0.0 shop-common org.springframework.boot spring-boot-starter-data-jpa org.projectlombok lombok com.alibaba fastjson 1.2.56 mysql mysql-connector-java 5.1.44 在这里区别创建Maven和Springboot项目,我们创建Maven项目,在父工程的pom.xml中会自动的产生子模块的标记:而springboot项目需要自己手动添加
5、创建实体类放于基础模块
这里小编就是放入的三个实体类
6、创建用户微服务
导入用户服务的Pom依赖
springcloud-shop com.zq 1.0-SNAPSHOT 4.0.0 shop-user org.springframework.boot spring-boot-starter-web com.zq shop-common 1.0-SNAPSHOT 7、创建商品微服务
导入商品微服务的pom依赖
springcloud-shop com.zq 1.0-SNAPSHOT 4.0.0 shop-product org.springframework.boot spring-boot-starter-web com.zq shop-common 1.0-SNAPSHOT 8、创建订单微服务
导入订单的Pom依赖
springcloud-shop com.zq 1.0-SNAPSHOT 4.0.0 shop-order org.springframework.boot spring-boot-starter-web com.zq shop-common 1.0-SNAPSHOT 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 {@Autowiredprivate 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;}
}

上一篇:“调”字换偏旁并组词
下一篇:疯狂猜成语草木打一成语疯狂看图