当前项目环境
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 当前版本的官方文档
在配置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();
}
}
}