• 简单实现Springcloud跨项目相互调用


    目录

    一、前置准备

    二、项目准备

    三、实现跨项目调用


    一、前置准备

    我们本次是使用的电商项目中的商品、订单、用户为案例进行讲解。

     技术选型

    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. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0modelVersion>
    6. <groupId>com.zqgroupId>
    7. <artifactId>springcloud-shopartifactId>
    8. <version>1.0-SNAPSHOTversion>
    9. <packaging>pompackaging>
    10. <properties>
    11. <java.version>1.8java.version>
    12. <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    13. <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
    14. <spring-boot.version>2.3.2.RELEASEspring-boot.version>
    15. <spring-cloud.version>Hoxton.SR9spring-cloud.version>
    16. <spring-cloud-alibaba.version>2.2.6.RELEASEspring-cloud-alibaba.version>
    17. properties>
    18. <dependencyManagement>
    19. <dependencies>
    20. <dependency>
    21. <groupId>org.springframework.bootgroupId>
    22. <artifactId>spring-boot-dependenciesartifactId>
    23. <version>${spring-boot.version}version>
    24. <type>pomtype>
    25. <scope>importscope>
    26. dependency>
    27. dependencies>
    28. dependencyManagement>
    29. project>

    在这里我们需要注意的一点事Springcloud与Springboot的版本问题:

    这里给出对应的版本对应关系表:

    3、创建基础模块

    基础模块也是Maven项目所以步骤和上面创建父工程一样

     4、导入基础模块的pom依赖

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <parent>
    6. <artifactId>springcloud-shopartifactId>
    7. <groupId>com.zqgroupId>
    8. <version>1.0-SNAPSHOTversion>
    9. parent>
    10. <modelVersion>4.0.0modelVersion>
    11. <artifactId>shop-commonartifactId>
    12. <dependencies>
    13. <dependency>
    14. <groupId>org.springframework.bootgroupId>
    15. <artifactId>spring-boot-starter-data-jpaartifactId>
    16. dependency>
    17. <dependency>
    18. <groupId>org.projectlombokgroupId>
    19. <artifactId>lombokartifactId>
    20. dependency>
    21. <dependency>
    22. <groupId>com.alibabagroupId>
    23. <artifactId>fastjsonartifactId>
    24. <version>1.2.56version>
    25. dependency>
    26. <dependency>
    27. <groupId>mysqlgroupId>
    28. <artifactId>mysql-connector-javaartifactId>
    29. <version>5.1.44version>
    30. dependency>
    31. dependencies>
    32. project>

    在这里区别创建Maven和Springboot项目,我们创建Maven项目,在父工程的pom.xml中会自动的产生子模块的标记:而springboot项目需要自己手动添加 

    5、创建实体类放于基础模块

    这里小编就是放入的三个实体类

     6、创建用户微服务

    导入用户服务的Pom依赖

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>springcloud-shopartifactId>
    6. <groupId>com.zqgroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <modelVersion>4.0.0modelVersion>
    10. <artifactId>shop-userartifactId>
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.springframework.bootgroupId>
    14. <artifactId>spring-boot-starter-webartifactId>
    15. dependency>
    16. <dependency>
    17. <groupId>com.zqgroupId>
    18. <artifactId>shop-commonartifactId>
    19. <version>1.0-SNAPSHOTversion>
    20. dependency>
    21. dependencies>
    22. project>

    7、创建商品微服务

    导入商品微服务的pom依赖

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>springcloud-shopartifactId>
    6. <groupId>com.zqgroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <modelVersion>4.0.0modelVersion>
    10. <artifactId>shop-productartifactId>
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.springframework.bootgroupId>
    14. <artifactId>spring-boot-starter-webartifactId>
    15. dependency>
    16. <dependency>
    17. <groupId>com.zqgroupId>
    18. <artifactId>shop-commonartifactId>
    19. <version>1.0-SNAPSHOTversion>
    20. dependency>
    21. dependencies>
    22. project>

    8、创建订单微服务

    导入订单的Pom依赖

    1. "1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    4. <parent>
    5. <artifactId>springcloud-shopartifactId>
    6. <groupId>com.zqgroupId>
    7. <version>1.0-SNAPSHOTversion>
    8. parent>
    9. <modelVersion>4.0.0modelVersion>
    10. <artifactId>shop-orderartifactId>
    11. <dependencies>
    12. <dependency>
    13. <groupId>org.springframework.bootgroupId>
    14. <artifactId>spring-boot-starter-webartifactId>
    15. dependency>
    16. <dependency>
    17. <groupId>com.zqgroupId>
    18. <artifactId>shop-commonartifactId>
    19. <version>1.0-SNAPSHOTversion>
    20. dependency>
    21. dependencies>
    22. project>

    9、修改三个微服务的访问端口

    到这里我们的准备OK了,接下来就是代码实现微服务的相互调用了。

    三、实现跨项目调用编码

    3.1用户微服务Controller代码编写

    1. package com.zq.shopuser.controller;
    2. import com.zq.entity.User;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. /**
    7. * @author张强
    8. * @site www.zq.com
    9. * @create  2022-11-25 15:26
    10. */
    11. @RequestMapping("/user")
    12. @RestController
    13. public class UserController {
    14. @RequestMapping("/get/{id}")
    15. public User get(@PathVariable("id") Integer id){
    16. // 查询数据库
    17. return new User(id,"zq","123456","15679031652");
    18. }
    19. }

    3.2商品微服务Controller代码编写

    1. package com.zq.shopproduct.controller;
    2. import com.zq.entity.Product;
    3. import org.springframework.web.bind.annotation.PathVariable;
    4. import org.springframework.web.bind.annotation.RequestMapping;
    5. import org.springframework.web.bind.annotation.RestController;
    6. /**
    7. * @author张强
    8. * @site www.zq.com
    9. * @create  2022-11-25 15:31
    10. */
    11. @RestController
    12. @RequestMapping("/product")
    13. public class ProductController {
    14. @RequestMapping("/get/{pid}")
    15. public Product get(@PathVariable("pid") Integer pid){
    16. Product p = new Product(pid, "香荷泥煎面", 36d, 36);
    17. return p;
    18. }
    19. }

    3.3订单微服务Controller代码编写

    1. package com.zq.shoporder.controller;
    2. import com.zq.entity.Order;
    3. import com.zq.entity.Product;
    4. import com.zq.entity.User;
    5. import org.springframework.beans.factory.annotation.Autowired;
    6. import org.springframework.web.bind.annotation.PathVariable;
    7. import org.springframework.web.bind.annotation.RequestMapping;
    8. import org.springframework.web.bind.annotation.RestController;
    9. import org.springframework.web.client.RestTemplate;
    10. /**
    11. * @author张强
    12. * @site www.zq.com
    13. * @create  2022-11-25 15:35
    14. */
    15. @RequestMapping("/order")
    16. @RestController
    17. public class OrderController {
    18. @Autowired
    19. private RestTemplate restTemplate;
    20. @RequestMapping("/get/{uid}/{pid}")
    21. public Order get(@PathVariable("uid") Integer uid,
    22. @PathVariable("pid") Integer pid){
    23. User user = restTemplate.getForObject("http://localhost:8070/user/get/" + uid, User.class);
    24. Product product = restTemplate.getForObject("http://localhost:8090/product/get/" + pid, Product.class);
    25. Order order = new Order();
    26. order.setNumber(2);
    27. order.setOid(System.currentTimeMillis());
    28. order.setPid(pid);
    29. order.setPname(product.getPname());
    30. order.setPprice(product.getPprice() * order.getNumber());
    31. order.setUid(user.getUid());
    32. order.setUsername(user.getUsername());
    33. return order;
    34. }
    35. }

    测试结果:

  • 相关阅读:
    Java基于SpringBoot+Vue的图书管理系统
    js【详解】ajax (含XMLHttpRequest、 同源策略、跨域、JSONP)
    Bootstrap5 表格
    idea打开项目,不显示代码文件夹目录如何解决
    手把手带你编写属于自己的 Starter
    算法 数据结构 递归冒泡算法 java冒泡算法 优化递归冒泡 数据结构(九)
    istio学习笔记-安装
    Linux i2ctool 工具的使用方法
    Prototype(原型模式)
    VUE 常用炫酷动画库(拿来即用系列)
  • 原文地址:https://blog.csdn.net/weixin_66110079/article/details/128020564