Hystrix-javanica的HelloWorld

王守钰 2020-05-20 14:05:13

依赖引入

<hystrix.version>1.5.18</hystrix.version>
<!-- hystrix javanica -->
<dependency>
	<groupId>com.netflix.hystrix</groupId>
	<artifactId>hystrix-javanica</artifactId>
	<version>${hystrix.version}</version>
</dependency>

配置启用

import com.netflix.hystrix.contrib.javanica.aop.aspectj.HystrixCommandAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 王守钰
 * @program quan
 * @date 2020年05月19日 12:44
 * @description: 熔断器配置
 */
@Configuration
public class HystrixConfig {

    @Bean
    public HystrixCommandAspect hystrixAspect() {
        return new HystrixCommandAspect();
    }

}

HystrixCommand

@HystrixCommand(groupKey = "myHystrixGroup",
        commandKey = "helloWorldCommandKey",
        threadPoolKey = "helloWorldThreadPool")
@GetMapping(value = "/hello")
public String hello(){
    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "HelloWorld";
}

测试请求

@Test
public void test(){
    RestTemplate restTemplate = new RestTemplate();
    for (int i=0; i< 20; i++){
        new Thread(()->{
            String result = restTemplate.getForObject("http://127.0.0.1:8080/hystrix/hello", String.class);
            log.info("{} execute result:{}", Thread.currentThread().getName(), result);
        }).start();
    }
    while (true){}
}

执行结果

10:24:30.747 [Thread-12] INFO com.wangshouyu.hystrix.HystrixControllerTest - Thread-12 execute result:HelloWorld
10:24:30.747 [Thread-10] INFO com.wangshouyu.hystrix.HystrixControllerTest - Thread-10 execute result:HelloWorld
10:24:30.747 [Thread-9] INFO com.wangshouyu.hystrix.HystrixControllerTest - Thread-9 execute result:HelloWorld
10:24:30.747 [Thread-16] INFO com.wangshouyu.hystrix.HystrixControllerTest - Thread-16 execute result:HelloWorld
10:24:30.747 [Thread-18] INFO com.wangshouyu.hystrix.HystrixControllerTest - Thread-18 execute result:HelloWorld
10:24:30.747 [Thread-1] INFO com.wangshouyu.hystrix.HystrixControllerTest - Thread-1 execute result:HelloWorld
10:24:30.747 [Thread-5] INFO com.wangshouyu.hystrix.HystrixControllerTest - Thread-5 execute result:HelloWorld
10:24:30.747 [Thread-11] INFO com.wangshouyu.hystrix.HystrixControllerTest - Thread-11 execute result:HelloWorld
10:24:30.747 [Thread-19] INFO com.wangshouyu.hystrix.HystrixControllerTest - Thread-19 execute result:HelloWorld
10:24:30.747 [Thread-14] INFO com.wangshouyu.hystrix.HystrixControllerTest - Thread-14 execute result:HelloWorld

降级配置

@HystrixCommand(groupKey = "myHystrixGroup",
        commandKey = "helloWorldCommandKey",
        threadPoolKey = "helloWorldThreadPool",
        fallbackMethod = "fallback")
@GetMapping(value = "/hello")
public String hello(){
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "HelloWorld";
}

public String fallback(){
    return "fallback";
}

多级降级操作

@HystrixCommand(groupKey = "myHystrixGroup",
        commandKey = "helloWorldCommandKey",
        threadPoolKey = "helloWorldThreadPool",
        fallbackMethod = "fallback")
@GetMapping(value = "/hello")
public String hello(){
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "HelloWorld";
}

@HystrixCommand(fallbackMethod = "fallback1")
public String fallback(){
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return "fallback";
}

public String fallback1(){
    return "fallback1";
}