断路器Hystrix(Ribbon)

微服务架构中,根据业务划分成若干个服务,各单元应用间通过服务注册与订阅的方式互相依赖,依赖通过远程调用的方式执行,该方式难以避免因网络或自身原因而出现故障或者延迟,从而并不能保证服务的100%可用,此时若有大量的网络涌入,会形成任务累计,导致服务瘫痪,甚至导致服务“雪崩”。

- Hystrix

1.Netflix 已经为我们创建了 Hystrix 库来实现服务降级、服务熔断、线程隔离、请求缓存、请求合并以及服务监控(Hystrix Dashboard)等强大功能,在微服务架构中,多层服务调用是非常常见的。

正常情况正常情况

2.较底层的服务中的服务故障可能导致级联故障,当对特定的服务的调用达到一个阀值(Hystric 是5秒20次) 断路器将会被打开,故障百分比大于circuitBreaker.errorThresholdPercentage(默认值:> 50%)时metrics.rollingStats.timeInMilliseconds(默认10秒),断路打开后,开发人员可以回退机制。

异常情况异常情况

官方文档:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_circuit_breaker_hystrix_clients

- 准备工作

1.启动Consul
2.创建 battcn-provider 和 battcn-consumer

- battcn-provider

- pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

- ProviderApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ProviderApplication {

@Value("${spring.application.name}")
String applicationName;


public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}

@GetMapping("/hello")
public String hello() {
return "My Name's :" + applicationName + " Email:1837307557@qq.com";
}
}

- bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8765

spring:
application:
name: battcn-provider
cloud:
consul:
host: localhost
port: 8500
enabled: true
discovery:
enabled: true
prefer-ip-address: true

- battcn-consumer

- pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

- ConsumerApplication

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//@EnableDiscoveryClient
//@SpringBootApplication
//@EnableCircuitBreaker
/**
* SpringCloudApplication 一个注解顶上面三个,有兴趣的可以点进去看源码
*/
@SpringCloudApplication
public class ConsumerApplication {

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}

public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}

- HiController

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
/**
* 为了偷懒,就写一个文件了
*/
@RestController
public class HiController {

static Logger LOGGER = LoggerFactory.getLogger(HiController.class);
@Autowired
HiService hiService;

@GetMapping("/hi")
public String hi() {
return hiService.hello();
}

@Service
class HiService {
@Autowired
RestTemplate restTemplate;

@HystrixCommand(fallbackMethod = "ribbonFallback")
public String hello() {
return restTemplate.getForObject("http://battcn-provider/hello", String.class);
}

public String ribbonFallback() {
return "My Name's :ribbonFallback Email:1837307557@qq.com";
}
}
}

- bootstrap.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 8766

spring:
application:
name: battcn-consumer
cloud:
consul:
host: localhost
port: 8500
enabled: true
discovery:
enabled: true
prefer-ip-address: true

- 测试

启动:battcn-provider

启动:battcn-consumer

访问:http://localhost:8500/ 显示如下代表服务注册成功

查看注册中心查看注册中心

访问:http://localhost:8766/hi

1
2
3
My Name's :battcn-provider Email:1837307557@qq.com	#正确情况

My Name's :ribbonFallback Email:1837307557@qq.com #关闭battcn-provider后结果

- 源码

1.当我们开启Hystrix 的时候 Hystrix 会为我们注入 HystrixCommandAspect 切面,操作所有带HystrixCommand 注解,随后就是通过反射与Cglib创建代理然后发送请求,不管服务是否健壮都会先进入AOP切面然后才会执行后续操作(打脸轻点…)

源码源码

- 监控

1.在 ConsumerApplication 中添加 @EnableHystrixDashboard 的注解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@SpringCloudApplication
@EnableHystrixDashboard //多了个开启监控注解
public class ConsumerApplication {

@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}

public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}

}

2.在 pom.xml 中添加如下配置

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

3.访问:http://localhost:8766/hystrix

效果图效果图

4.访问N次:http://localhost:8766/hi

效果图效果图

我们可以看到请求成功,失败,等信息

原文地址:https://www.cnblogs.com/lywJ/p/10715542.html