BS问题:@FeignClient 配置 fallback 不生效

当前项目环境

1、spring-cloud 版本:Hoxton.SR12

2、spring-boot 版本:2.3.12.RELEASE

3、jdk 1.8

@FeignClient唯一依赖
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>   
   <version>2.2.9.RELEASE</version>
</dependency>

openfeign 当前版本的官方文档

https://docs.spring.io/spring-cloud-openfeign/docs/2.2.9.RELEASE/reference/html/#spring-cloud-feign-hystrix

在配置fallbackBean的时候加了Feign.Builder Bean(这里是从网上找的配置),最终不生效的原因就是这个多余的配置

@Configuration
public class FooConfiguration {
    @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        return Feign.builder();
    }

    @Bean
    public OrgServiceFallback fb() {
        return new OrgServiceFallback();
      }
}

 这个Bean的配置是To disable ,就是使熔断失效的,fuck!!!

正确配置只需要

一、feign.hystrix.enabled=true

二、Hystrix is on the classpath ()

 

 

 

@FeignClient完整配置类

package com.base.uav.api.service;

import com.base.uav.api.dto.RequestDTO;
import feign.Feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

@FeignClient(value = "h5-uav",configuration = UavService.FooConfiguration.class,fallback = UavService.UavServiceFallBack.class)
public interface UavService {

    /**
     * 注意:带请求体参数的,feignClient默认发送POST请求,这里也需要设置为post方式
     * @param requestDTO
     * @return
     */
    @PostMapping(value = "/aerial/private/uav/get")
    String get(@RequestBody RequestDTO requestDTO);
    class UavServiceFallBack implements UavService {
        public String get(RequestDTO requestDTO) {
            return "请求快速失败";
        }
    }

    @Configuration
     class FooConfiguration {

        @Bean
        public UavServiceFallBack fb() {
            return new UavServiceFallBack();
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值