OpenFeign的使用

1.在主启动类上添加@EnableFeignClients注解
image
2.配置服务层接口,在接口上添加@FeignClient(value = "服务注册中心的名字")
在这里名添加的接口需要和提供者的接口路径保持一致

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {
    @GetMapping(value = "/payment/get/{id}")
     CommonResult<Payment> getPaymentById(@PathVariable("id") Long id);
}

提供者的controller
image
3.创建消费者controller进行服务层调用

@RestController
@Slf4j
public class OrderFeignController {
    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id)
    {
        return paymentFeignService.getPaymentById(id);
    }
posted @ 2021-11-10 10:28  Primary丶  阅读(244)  评论(0编辑  收藏  举报