订单系统-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]]

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值