spring-boot-dubbo-zookeeper-hystrix-annotation

说明

使用注解的方式使用hystrix

我需要引入几个必要的jar

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-core</artifactId>
<version>1.5.12</version>
</dependency>

<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-metrics-event-stream</artifactId>
<version>1.5.12</version>
</dependency>

<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
<version>1.5.12</version>
</dependency>

<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>

然后是拦截中心

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
/**
* 拦截中心
* @author YI
* @date 2018-9-8 16:37:01
*/
@Configuration
public class HystrixConfig {

/**
* 用来拦截处理HystrixCommand注解
* @return
*/
@Bean
public HystrixCommandAspect hystrixAspect() {
return new HystrixCommandAspect();
}

/**
* 用来像监控中心Dashboard发送stream信息
* @return
*/
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registration.addUrlMappings("/hystrix.stream");
return registration;
}
}

如此简单我们就可以使用注解的方式提供熔断降级服务

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* 消费者
* @author YI
* @date 2018-8-20 17:05:30
*/
@RestController
public class HelloController {

@Reference(
version = "${hello.service.version}",
application = "${dubbo.application.id}",
registry = "${dubbo.registry.id}"
)
private HelloService helloService;

/**
* 出错时回调sayHelloFallback 方法
* @param name
* @return
*/
@GetMapping("hello/{name}")
@HystrixCommand(fallbackMethod = "sayHelloFallback",
commandProperties = {
@HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"),
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"),
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "2")},
threadPoolProperties = {
@HystrixProperty(name = "coreSize", value = "5"),
@HystrixProperty(name = "maximumSize", value = "5"),
@HystrixProperty(name = "maxQueueSize", value = "10")
})
public String sayHello(@PathVariable String name){
return helloService.sayHello(name);
}

/**
* 出错时回调sayGoodbyeFallback 方法
* @param name
* @return
*/
@HystrixCommand(fallbackMethod = "sayGoodbyeFallback")
@GetMapping("goodbye/{name}")
public String sayGoodbye(@PathVariable String name){
return helloService.sayGoodbye(name);
}

public String sayHelloFallback(String name){
System.out.println("sayHello回调函数");

return "OK";
}

public String sayGoodbyeFallback(String name){
System.out.println("sayGoodbye回调函数");

return "OK";
}
}

具体实现请看源码

源码

spring-boot-dubbo-zookeeper-hystrix-annotation

问题建议