全局禁用

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

1
2
3
feign:
  hystrix:
    enabled: false

加上这段配置就可以了。

禁用某个Feign Client对Hystrix的支持

1.新建一个配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
@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

1
2
3
4
5
6
@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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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

主类:

1
2
3
4
5
6
7
8
9
10
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class MovieAppFeignWithoutHystrix
{
    public static void main( String[] args )
    {
        SpringApplication.run(MovieAppFeignWithoutHystrix.class,args);
    }
}

1
2
3
4
5
@FeignClient(name = "microservice-springcloud-user",fallback = UserHystrixClientFallback.class)
public interface UserFeignClient {
    @RequestMapping(value = "/sample/{userId}",method = RequestMethod.GET)
    User findUserById(@PathVariable("userId") Long userId);
}
1
2
3
4
5
6
@FeignClient(name = "microservice-springcloud-movie",configuration = Configuration2.class,fallback = UserHystrixClientFallback2.class)
public interface UserFeignClient2
{
    @RequestMapping(value = "/user/list")
    List<User> showUserList();
}
1
2
3
4
5
6
7
8
9
10
11
@Component
public class UserHystrixClientFallback implements UserFeignClient
{
    @Override
    public User findUserById(Long userId)
    {
        User user = new User();
        user.setId(0L);
        return user;
    }
}
1
2
3
4
5
6
7
8
9
10
@Component
public class UserHystrixClientFallback2 implements UserFeignClient2
{
    @Override
    public List<User> showUserList()
    {
        System.out.println("UserHystrixClientFallback2.showUserList() called.");
        return null;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
@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();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@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();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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支持的效果。