1、Hystrix参数配置文档 

2、Hystrix参数配置示例

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.mimaxueyuan.consumer.turbine.service.MyService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;

@RestController
public class HystrixController {

    @Autowired
    private MyService myService;

    /**
     * @HystrixCommand由名为“javanica”的Netflix contrib库提供 。 
     * Spring Cloud在连接到Hystrix断路器的代理中使用该注解自动包装Spring bean。
     *  断路器计算何时打开和关闭电路,以及在发生故障时应该做什么。
     *
     * @author Kevin
     * @Title: get
     * @param id
     * @return
     * @return: String
     */
    @GetMapping("/get1/{id}")
    @HystrixCommand(fallbackMethod = "getError")
    public String get1(@PathVariable String id) {
        try {
            System.out.println(Thread.currentThread().getName()+":get1 before sleep 5s....");
            Thread.sleep(1000 * 5);
            System.out.println(Thread.currentThread().getName()+":get1 after sleep 5s....");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "kevin.get1." + id;
    }
 
    /**
     * 配置Hystrix隔离策略,默认为THREAD,隔离策略分为THREAD、SEMAPHORE两种
     * 
     * execution.isolation.strategy 默认为THREAD
     * 
     * @param id
     * @return
     * @author lynch
     */
    @GetMapping("/get2/{id}")
    @HystrixCommand(fallbackMethod = "getError", commandProperties = {
            // @HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY, value="THREAD")
            @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY, value = "SEMAPHORE") })
    public String get2(@PathVariable String id) {
        try {
            System.out.println(Thread.currentThread().getName()+":get2 before sleep 5s....");
            Thread.sleep(1000 * 5);
            System.out.println(Thread.currentThread().getName()+":get2 after sleep 5s....");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "kevin.get2." + id;
    }
    
    /**
     * 设置HystrixCommand的执行是否在超时发生时被中断。使用线程隔离时,是否对命令执行超时的线程调用中断(Thread.interrupt())操作
     * 
     * execution.isolation.thread.interruptOnTimeout 参数,默认为true
     * 
     * @param id
     * @return
     * @author lynch
     */
    @GetMapping("/get3/{id}")
    @HystrixCommand(fallbackMethod = "getError",commandProperties= {
            //设置超时的时候不中断线程,默认为true
            @HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_INTERRUPT_ON_TIMEOUT,value="false")
    })
    public String get3(@PathVariable String id) {
        try {
            System.out.println(Thread.currentThread().getName()+":get3 before sleep 5s....");
            Thread.sleep(1000 * 5);
            System.out.println(Thread.currentThread().getName()+":get3 after sleep 5s....");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "kevin.get3." + id;
    }
 
    /**
     * 设置调用线程产生的HystrixCommand.getFallback()方法的允许最大请求数目。 如果达到最大并发数目,后续请求将会被拒绝,如果没有实现回退,则抛出异常。
     * (这里需要注意一点,这个变量的命名不是很规范,它实际上对THREAD和SEMAPHORE两种隔离策略都生效)
     * 
     * fallback.isolation.semaphore.maxConcurrentRequests默认为10
     * 
     * @param id
     * @return
     * @author lynch
     */
    @GetMapping("/get4/{id}")
    @HystrixCommand(fallbackMethod = "getError",commandProperties= {
            @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY, value = "SEMAPHORE"),
            @HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS,value="1")
    })
    public String get4(@PathVariable String id) {
        try {
            System.out.println(Thread.currentThread().getName()+":get4 before sleep 5s....");
            Thread.sleep(1000 * 5);
            System.out.println(Thread.currentThread().getName()+":get4 after sleep 5s....");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "kevin.get4." + id;
    }
    
    
    /**
     * 设置HystrixCommand的执行是否有超时限制。
     * 
     * execution.timeout.enabled 默认为true, 是否超时
     *
     * @param id
     * @return
     */
    @GetMapping("/get5/{id}")
    @HystrixCommand(fallbackMethod = "getError",commandProperties= {
            @HystrixProperty(name=HystrixPropertiesManager.EXECUTION_TIMEOUT_ENABLED,value="false")
    })
    public String get5(@PathVariable String id) {
        try {
            System.out.println(Thread.currentThread().getName()+":get5 before sleep 5s....");
            Thread.sleep(1000 * 5);
            System.out.println(Thread.currentThread().getName()+":get5 after sleep 5s....");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "kevin.get5." + id;
    }

    /**
     * 设置调用者等待命令执行的超时限制,超过此时间,HystrixCommand被标记为TIMEOUT,并执行回退逻辑。
     * 注意:超时会作用在HystrixCommand.queue(),即使调用者没有调用get()去获得Future对象。
     * 
     * execution.isolation.thread.timeoutInMilliseconds 默认为1000
     * 
     * @param id
     * @return
     * @author lynch
     */
    @GetMapping("/get6/{id}")
    @HystrixCommand(fallbackMethod = "getError",commandProperties= {
            @HystrixProperty(name=HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS,value="6000")
    })
    public String get6(@PathVariable String id) {
        try {
            System.out.println(Thread.currentThread().getName()+":get6 before sleep 5s....");
            Thread.sleep(1000 * 5);
            System.out.println(Thread.currentThread().getName()+":get6 after sleep 5s....");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "kevin.get6." + id;
    }
    
    public String getError(String id) {
        System.out.println(Thread.currentThread().getName()+":getError before ....");
        myService.execute();
        System.out.println(Thread.currentThread().getName()+":getError after ....");
        return "超时错误,使用断路器返回" + id;
    }
}