SpringCloud OpenFeign设置circuitbreaker=true后Feign的超时时间(readTimeout)失效

问题:   

        OpenFeign设置circuitbreaker=true后Feign的超时设置不起作用,一秒钟直接走熔断处理返回。

解决办法:

        原因是Hystrix的超时时间判断早于OpenFeign设置的超时时间,Hystrix没有设置超时时间默认是1秒钟,如果没有设置或者设置的超时时间小于OpenFeign设置的超时时间,则直接走熔断处理直接返回。

        解决办法是设置Hystrix的超时时间大于OpenFeign设置的超时时间。

具体实例:

使用OpenFeign时,在项目pom.xml中引入maven依赖:

<!-- OpenFeign 相关依赖  start -->
        <!-- OpenFeign 是SpringCloud在Feign的基础上支持了SpringMVC的注解,如@RequestMapping等。
        OpenFeign的@FeignClient可以解析SpringMVC的@RequestMapping注解下的接口,
        并通过动态代理的方式产生实现类,实现类中做负载均衡并调用其他服务。 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--注意!! 加入spring-cloud-loadbalancer依赖 并且在nacos中排除ribbon依赖,不然loadbalancer无效 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
        <!-- OpenFeign 相关依赖  end -->

启动类中加 @EnableFeignClients @EnableHystrix 两个注解,具体如下:

package com.pbh.pbhplatform.auth;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class PbhPlatformAuthApplication {
    public static void main(String[] args) {
        SpringApplication.run(PbhPlatformAuthApplication.class, args);
    }
}

远程调用短信中心接口,并做熔断处理,具体如下:

package com.pbh.pbhplatform.auth.openfeign;

import com.pbh.pbhplatform.auth.openfeign.impl.SmscenterApiImpl;
import com.pbh.pbhplatform.auth.openfeign.request.SendSmsRequest;
import com.pbh.pbhplatform.core.vo.ResponseData;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 远程调用短信中心接口  并做熔断处理
 *
 * @author pengbh
 * @since 2023/05/23
 */
@FeignClient(value = "pbh-platform-base-smscenter", fallback = SmscenterApiImpl.class)
public interface SmscenterApi {

    /**
     * 发送短信
     *
     * @param request 请求参数
     * @return 结果
     */
    @PostMapping("/pbh-smscenter/rpc/api/sms/send")
    ResponseData<Boolean> sendSms(@RequestBody SendSmsRequest request);

}
package com.pbh.pbhplatform.auth.openfeign.impl;

import com.pbh.pbhplatform.auth.openfeign.SmscenterApi;
import com.pbh.pbhplatform.auth.openfeign.request.SendSmsRequest;
import com.pbh.pbhplatform.core.vo.ResponseData;
import org.springframework.stereotype.Service;

/**
 * 短信中心接口熔断处理
 *
 * @author pengbh
 * @since 2023/3/25
 */
@Service
public class SmscenterApiImpl implements SmscenterApi {
    /**
     * 发送短信
     *
     * @param request 请求参数
     * @return 结果
     */
    @Override
    public ResponseData<Boolean> sendSms(SendSmsRequest request) {
        return ResponseData.buildError("服务器开小差了,请稍后重试");
    }
}

配置文件pbh-platform-auth-dev.yml,添加如下配置:

# Feign 配置
feign:
  # 开启熔断机制
  circuitbreaker:
    enabled: true
  # 设置 Feign 客户端超时时间
  client:
    config:
      # default 表示全局配置,如要针对某个服务,则填写对应的服务名即可
      default:
        # Feign 客户端建立连接超时时间
        connectTimeout: 10000
        # Feign 客户端建立连接后读取资源超时时间
        readTimeout: 10000

# Hystrix 配置
# 熔器断 Hystrix 的超时判断在 Feign 的超时判断时间,Hystrix默认超时时间是1秒
# 熔断器 Hystrix 的超时时间必须不小于 Feign 设置的超时时间,否则 Feign 设置的超时时间设置的超时时间无效
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值