SpringClound——Feign和OpenFeign区别和OpenFeign的使用 - Go语言中文社区

SpringClound——Feign和OpenFeign区别和OpenFeign的使用


Feign和OpenFeign的主要区别如下图

在这里插入图片描述

那我们怎么使用OpenFeign呢?

1:OpenFeign服务调用

pom.xml

 <!-- OpenFeign -->
       <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

PaymentService .java

public interface PaymentService {
    public int create(Payment payment);
    public Payment getPaymentById(@Param("id") Long id);
}

OrderFeignController .java

@RestController
@Slf4j
public class OrderFeignController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/feign/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id) {
        return paymentFeignService.getPaymentById(id);
    }

    @GetMapping("/consumer/feign/payment/timeout")
    public String getPaymentById() {
        // openfeign-ribbon,客户端默认等待1秒钟
        return paymentFeignService.paymentFeignTimeout();
    }
}

application.yml

server:
  port: 80

spring:
  application:
    name: cloud-order-feign-service

eureka:
  client:
    # true表示将自己注册进Eureka Server,默认为true
    register-with-eureka: true
    # 是否从Eureka Server抓取以有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合Ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka

2:OpenFeign超时控制

  • 超时控制介绍·
    默认Feign客户端只等待一秒钟,但是服务霭处理需要超过1秒,导致Feign客户端不想等待了,直接返回报错。为了避免这样的情况,有时候我们需要设置Feign客户端的超时控制。
  • 解决方法
    ym|文件中开启配置

application.yml

# 设置feign客户端超时时间(OpenFeign默认支持Ribbon)
ribbon:
  # 指的是建立连接后从服务器读取到可用资源所用时间
  ReadTimeout: 5000
  # 指的是建立连接所用的时间,适用于网络状况正常的情况下,两端连接所用的时间
  ConnectTimeout: 5000

么配置上面的yml文件时,如果我们调用如下的方法
会报错

 @GetMapping("/feign/timeout")
    public String paymentFeignTimeout() {
        try {
            TimeUnit.SECONDS.sleep(4);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return serverPort;
    }

在这里插入图片描述
配置了,就会出现以下结果
在这里插入图片描述

3:OpenFeign日志增强

  • Feign提供了日志打印功能,我们可以通过配置来调整日志级别,从而了解Feign中Http请求的细节。说白了就是对Feign接口的调用情况进行监控和输出
  • 日志级别
    1:NONE:默认的,不显示任何日志;
    2:BASIC:仅记录请求方法、URL、 响应状态码及执行时间;
    3:HEADERS:除了BASIC中定义的信息之外,还有请求和响应的头信息;
    4:FULL:除了HEADERS中定义的信息之外,还有请求和响应的正文及元数据。

怎么使用日志?
第一步:FeignConfig.java

@Configuration
public class FeignConfig {

    @Bean
    public Logger.Level feignLoggerLevel(){
        return Logger.Level.FULL;
    }
}

第二步:application.yml

logging:
  level:
    # feign日志已什么级别监控哪个接口
    com.atguigu.springcloud.service.PaymentFeignService: debug

第三步:测试该接口下的方法,得到如下日志
在这里插入图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_44891295/article/details/105177224
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢