RPC快速入门
概述
关于rpc,只需要知道他是一种协议,项目之间能够远程调用函数。
快速入门
我们前边下载好的两个包,在idea中打开之后,我们创建这么几个文件夹。
至于是干什么的,以后细说。创建好之后我们在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 {@Autowiredprivate ProductService productService;@GetMapping("/hello")@Overridepublic 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 {@Resourceprivate RemoteProductService remoteProductService;@GetMapping("/test")public String test(){log.info(remoteProductService.toString());return remoteProductService.hello();}}
然后我们启动项目使用ApiPost软件进行接口的测试,记得启动本地的nacos。
我们先测试product的端口(9094):
然后呢测试order的端口(9093):