SpringCloud Fegin的使用

前言

紧接我的上一篇博客

SpringCloud Nacos+ribbon的实现方法-优快云博客

这里将替换调原来使用restTemplate调用url和使用拼接字符串指定地址,这里先建议了解一下两种传统方法在controller的使用。

SpringCloud Nacos + Ribbon 调用服务的 2 种方法!_resttemplate nacos-优快云博客

 了解完后才能理解fegin的方便与可取之处

相当于是将其它模块的service和serviceimpl全部写在了FeginClient中

 这是消费者的结构,而提供者和综合类的结构不进行改变

OrderFeignClient

package com.gaolang.FeignClient;


import com.gaolang.entities.CommonResult;
import com.gaolang.entities.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

@FeignClient(value = "cloud-provider-payment8001") // 指定调用的服务名称
public interface OrderFeignClient {

    @PostMapping(value = "/payment/create")
    CommonResult create(Payment payment); // 创建支付记录

    @GetMapping(value = "/payment/{id}")
    CommonResult getPaymentById(@PathVariable("id") Long id); // 根据 ID 获取支付记录
}

OrderController

package com.gaolang.controller;

import com.gaolang.FeignClient.OrderFeignClient;
import com.gaolang.entities.CommonResult;
import com.gaolang.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {

//    @Resource
//    private RestTemplate restTemplate;
//
//    // 服务提供者的名称
//    private static final String PAYMENT_URL = "http://cloud-provider-payment8001";
//
//    @PostMapping("customer/payment/create")
//    public CommonResult<Payment> create(Payment payment) {
//        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
//    }
//
//    @GetMapping("customer/payment/{id}")
//    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
//        CommonResult result = restTemplate.getForObject(PAYMENT_URL + "/payment/" + id, CommonResult.class);
//        System.out.println("result: " + result);
//        return result;
//    }

    @Autowired
    private OrderFeignClient orderFeignClient; // 注入 Feign 客户端

    @PostMapping(value = "/customer/payment/create")
    public CommonResult<Payment>  create(Payment payment){
        return orderFeignClient.create(payment);
    }

    @GetMapping(value = "/customer/payment/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        CommonResult result  = orderFeignClient.getPaymentById(id);
        System.out.println("result: " + result);
        return result;
    }
}

application.yml

server:
  port: 80
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 # 配置nacos 服务端地址
  application:
    name: cloud-customer-order80

feign:
  client:
    config:
      default: # default全局的配置
        loggerLevel: BASIC # 日志级别,BASIC就是基本的请求和响应信息
  httpclient:
    enabled: true # 开启feign对HttpClient的支持
    max-connections: 200 # 最大的连接数
    max-connections-per-route: 50 # 每个路径的最大连接数

这里我搜集了大量的资料了解了下,后续会更加深入了解这部分的关系。SpringCloud Feign 远程调用(史上最详细讲解)-阿里云开发者社区

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值