首页 > 解决方案 > 如何使 HystrixCommand 适用于服务类中的所有功能

问题描述

在一个服务类中,有四个函数,所有这些函数都使用RestTemplate来远程调用第三个服务器。

以下是示例:

@Service
public class PaymentService
{
    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
    })
    public String function1(@PathVariable("id") Integer id)
    {
        // some restTemplate action
    }

    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
    })
    public String function2(@PathVariable("id") Integer id)
    {
        // some restTemplate action
    }

    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback", commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),
    })
    public String function3(@PathVariable("id") Integer id)
    {
        // some restTemplate action
    }

    public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id)
    {
        return "Server [PaymentService][paymentCircuitBreaker_fallback] id=" + id;
    }

}

但是三个函数使得@HystrixCommand 头部和属性都一样,所以有没有办法只写一次?

标签: javahystrix

解决方案


推荐阅读