springcloud系列18——全局和单个禁用Feign Client对Hystrix的支持

全局禁用

这个比较简单,在application.yml中配置:

feign:
  hystrix:
    enabled: false

加上这段配置就可以了。

禁用某个Feign Client对Hystrix的支持

1.新建一个配置类

@Configuration
public class Configuration2
{

    //Configuration2里面加上这个就禁用了UserFeignClient2的Hystrix
    @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        //feignBuilder方法默认返回HystrixFeign.Builder也就是说Feign默认支持Hystrix
        //现在改成Feign.Builder就禁用了Hystrix的支持
        return Feign.builder();
    }
}

2.在FeignClient的注解上加入configturation

@FeignClient(name = "microservice-springcloud-movie",configuration = Configuration2.class,fallback = UserHystrixClientFallback2.class)
public interface UserFeignClient2
{
    @RequestMapping(value = "/user/list")
    List<User> showUserList();
}

禁用单个Hystrix的示例代码

说明:模块名:microservice-springcloud-movie-feign-without-hystrix

该模块创建了2个Feign Client,Feign Client1支持Hystrix,Feign Client2不支持Hystrix。

application.yaml

server:
  port: 7801

spring:
  application:
    name: microservice-springcloud-movie-feign-without-hystrix

eureka:
  client:
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

# 启用feign对Hystrix的支持
feign:
  hystrix:
    enabled: true
#设置全局的超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 5000

主类:

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class MovieAppFeignWithoutHystrix
{
    public static void main( String[] args )
    {
        SpringApplication.run(MovieAppFeignWithoutHystrix.class,args);
    }
}
@FeignClient(name = "microservice-springcloud-user",fallback = UserHystrixClientFallback.class)
public interface UserFeignClient {
    @RequestMapping(value = "/sample/{userId}",method = RequestMethod.GET)
    User findUserById(@PathVariable("userId") Long userId);
}
@FeignClient(name = "microservice-springcloud-movie",configuration = Configuration2.class,fallback = UserHystrixClientFallback2.class)
public interface UserFeignClient2
{
    @RequestMapping(value = "/user/list")
    List<User> showUserList();
}
@Component
public class UserHystrixClientFallback implements UserFeignClient
{
    @Override
    public User findUserById(Long userId)
    {
        User user = new User();
        user.setId(0L);
        return user;
    }
}
@Component
public class UserHystrixClientFallback2 implements UserFeignClient2
{
    @Override
    public List<User> showUserList()
    {
        System.out.println("UserHystrixClientFallback2.showUserList() called.");
        return null;
    }
}

@Configuration
public class Configuration2
{

    //Configuration2里面加上这个就禁用了UserFeignClient2的Hystrix
    @Bean
    @Scope("prototype")
    public Feign.Builder feignBuilder() {
        //feignBuilder方法默认返回HystrixFeign.Builder也就是说Feign默认支持Hystrix
        //现在改成Feign.Builder就禁用了Hystrix的支持
        return Feign.builder();
    }
}
@RestController
public class MovieController {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private UserFeignClient2 userFeignClient2;

    @GetMapping("/user/{userId}")
    public User findUserById(@PathVariable Long userId) {
        return this.userFeignClient.findUserById(userId);
    }

    @GetMapping("/user/list")
    public List<User> listUser() {
        return this.userFeignClient2.showUserList();
    }
}
public class User {
    private Long id;
    private String username;
    private String name;
    private BigDecimal balance;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public BigDecimal getBalance() {
        return balance;
    }

    public void setBalance(BigDecimal balance) {
        this.balance = balance;
    }
}

测试:
1.启动eurake server;
2.启动user服务;
3.启动movie服务;
4.启动microservice-springcloud-movie-feign-without-hystrix。

浏览器访问/user/1,正常:

浏览器访问/user/list,正常:

关闭movie服务,再次访问/user/list

提示连接超时,说明Feign Client2的Hystrix是失效的。

关闭user服务,再次访问/user/1

返回的是我们定义的回退方法的结果,说明Feign Client1的Hystrix是生效的。

如上,就达到了禁用某个Feign Client对Hystrix支持的效果。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值