Feign入门使用 OpenFeign 日志增强 超时控制

一、概述

Feign是一个声明式的web服务的客户端,Feign就是参考Ribbon添加了注解+接口的绑定器。

  • 我们封装一些客户端类来包装对其他服务的依赖调用。
  • Feign让我们只需要创建一个接口+注解就能够实现操作。
  • Feign集成了Ribbon

关于使用就是在接口添加特定注解就可以了。

为什么又要有Feign?

前面在使用Ribbon和RestTemplate时候,利用RestTemplate对http请求封装处理,但在实际开发当中,微服务之间的依赖调用可能不止移除,往往一个接口可能被多个地方调用,所以我们通常会对接口封装一些客户端类来包装对服务的调用。简化了Ribbon的同时,自动封装调用客户端的开发量。

尽量面向接口编程

Feign已经停止更新了,OpenFeign作为完全替换。

image-20230523215246251

二、使用

1. OpenFeign入门使用

@FeignClient

  1. 创建Modul 引入依赖
  <!--openfeign-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 引入自己定义的api通用包 -->
        <dependency>
            <groupId>com.atjianyi</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--        <dependency>-->
        <!--            <groupId>org.springframework.boot</groupId>-->
        <!--            <artifactId>spring-boot-devtools</artifactId>-->
        <!--            <scope>runtime</scope>-->
        <!--            <optional>true</optional>-->
        <!--        </dependency>-->

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
  1. yaml
server:
  port: 80
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/, http://eureka7002.com:7002/eureka/
    register-with-eureka: true 
  1. 创建客户端接口

image-20230524104552744

  1. 在Controller类当中调用

image-20230524104633448

  1. 主类当中开启OpenFeign

image-20230524104659270

2. 超时控制

在微服务当中,消费者调用提供者时,当中提供者提供的业务有长流程业务时,如果没有超时控制,可能出现调用超市失败的情况。

在Feign客户端当中,默认等待回调时间式 1s,若超过,则抛出一个错误,我们可以通过设置延长超时时间,来适应提供者可能的长流程业务。

  • 调用一个长流程业务,但没修改超时控制:

image-20230524110017259

添加超时控制配置解决

image-20230524110228683

3. 日志增强

Open Feign的日志级别,越往下输出越多。

  • NONE
  • BASIC
  • HEADERS
  • FULL
  1. 创建配置日志Bean
@Configuration
public class FeginConfig {
    @Bean
    Logger.Level feignLoggerLever(){
        return Logger.Level.FULL;
    }
}
  1. yaml日志监控
logging:
  level:
    com.atjianyi.cloud.client.PaymentClient: debug

image-20230524110632927

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值