订单系统-RPC快速入门

RPC快速入门

概述

关于rpc,只需要知道他是一种协议,项目之间能够远程调用函数。

快速入门

我们前边下载好的两个包,在idea中打开之后,我们创建这么几个文件夹。
![[Pasted image 20240326095149.png]]

![[Pasted image 20240326095240.png]]

至于是干什么的,以后细说。创建好之后我们在product-client子包中创建RemoteProductService接口:

package com.example.product.feign;

import com.example.product.feign.common.CommonResult;
import com.example.product.feign.entity.Price;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@FeignClient(value = "product-service")
public interface RemoteProductService {

    @GetMapping("/hello")
    String hello();
    
}

然后再product-service的controller中创建HelloController类并实现接口RemoteProductService:

package com.example.product.controller;

import com.example.product.feign.common.CommonResult;
import com.example.product.feign.entity.Price;
import com.example.product.feign.RemoteProductService;
import com.example.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class ProductController implements RemoteProductService {

    @Autowired
    private ProductService productService;


    @GetMapping("/hello")
    @Override
    public String hello() {
        return "hello world";
    }

}

完成之后,需要打开product的pom.xml文件,将下面几行注释掉,然后install,完成之后打开注释再次install。
![[Pasted image 20240326095803.png]]
接下来就是对order的操作,我们需要再order的pom.xml文件中导入

<dependency>
    <groupId>com.example</groupId>
    <artifactId>product-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>

这个就是我们刚刚打包的jar包,然后在order-service中导入依赖,等待依赖导入完成。完成之后在controller下创建一个controller进行测试:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>product-client</artifactId>
</dependency>
package com.example.order.controller;

import com.example.order.param.OrderParam;
import com.example.order.service.OrderService;
import com.example.order.vo.OrderResultVO;
import com.example.product.feign.RemoteProductService;
import com.example.product.feign.common.CommonResult;
import com.example.product.feign.entity.Price;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
public class OrderController {

    @Resource
    private RemoteProductService remoteProductService;

    @GetMapping("/test")
    public String test(){
        log.info(remoteProductService.toString());
        return remoteProductService.hello();
    }

}

然后我们启动项目使用ApiPost软件进行接口的测试,记得启动本地的nacos。
我们先测试product的端口(9094):
![[Pasted image 20240326100403.png]]

然后呢测试order的端口(9093):
![[Pasted image 20240326100439.png]]

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-03-26 14:20:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-26 14:20:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-26 14:20:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-26 14:20:02       20 阅读

热门阅读

  1. Centos docker安装及常用命令

    2024-03-26 14:20:02       17 阅读
  2. Git 的基本概念和使用方式

    2024-03-26 14:20:02       18 阅读
  3. 视频中的车流量统计_3.13

    2024-03-26 14:20:02       17 阅读
  4. Unity中使用AssetPostprocessor对模型动画处理

    2024-03-26 14:20:02       20 阅读
  5. Redis 教程系列之Redis 客户端连接(八)

    2024-03-26 14:20:02       15 阅读
  6. Redis 安装

    2024-03-26 14:20:02       19 阅读
  7. 设计模式概念、分类和原则

    2024-03-26 14:20:02       16 阅读