0
点赞
收藏
分享

微信扫一扫

Circuit Breaker: Hystrix


整理之前的笔记,发现有一些内容没有发出来,陆续发出来。。。

Circuit Breaker: Hystrix Clients

Netflix 创建了一个叫做Hystrix的库,这个库实现了熔断器模式。在微服务架构中,经常会有一次请求调用多个微服务的情况。

Circuit Breaker: Hystrix_hystrix


一个底层的服务调用失败可能导致所有上层服务的级联调用失败。当调用一个特定的服务达到一定的阀值时(Hystrix默认是5秒钟20次调用失败),熔断器电路会自动打开。开发者只需要提供一个fallback 方案就行了。

Circuit Breaker: Hystrix_微服务_02


熔断器生效的情况下,可以停止服务级联失效,给了失效的服务一定的时间修复。fallback 可以是另一个受Hystrix 保护的服务,静态的数据或者是一个有意义的空值。

How to Include Hystrix

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

[Example boot app:]

@SpringBootApplication
@EnableCircuitBreaker
public class Application {

public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}

}

@Component
public class StoreIntegration {

@HystrixCommand(fallbackMethod = "defaultStores")
public Object getStores(Map<String, Object> parameters) {
//do stuff that might fail
}

public Object defaultStores(Map<String, Object> parameters) {
return /* something useful */;
}
}

@HystrixCommand是Netflix contrib library "javanica"提供的。Spring Cloud在一个代理中自动的包装@HystrixCommand注解过的方法,使其链接到一个Hystrix 的熔断器。熔断器自己计算什么时候打开关闭熔断器闸门以及在调用失败时可以干什么。

可以使用@HystrixCommand 的commandProperties 属性给HystrixCommand 配置一个@HystrixProperty列表。
​​​更多信息​​

​​Hystrix wiki-commandProperties所有可用的配置​​

Propagating the Security Context or using Spring Scopes

如果你希望一些thread local的上下文传播到@HystrixCommand的实现中,默认将不会起作用,因为HystrixCommand的执行时在一个线程池中。你可以通过设置Isolation Strategy,将Hystrix 配置成HystrixCommand的执行跟请求调用在同一个线程里面。

@HystrixCommand(fallbackMethod = "stubMyService",
commandProperties = {
@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
}
)
...

如果你使用@SessionScope或者@RequestScope,将会有同样的效果。

你还有一个选项就是设置hystrix.shareSecurityContext属性为true。这将会自动配置一个Hystrix并发策略的插件钩子,这个钩子会将SecurityContext 从主线程传递到Hystrix command执行的线程。Hystrix不允许设置多个并发策略,通过声明自己的HystrixConcurrencyStrategy实现作为一个spring bean,可以扩展成可以设置多个并发策略。Spring Cloud将会在Spring的上下文中寻找HystrixConcurrencyStrategy的实现,然后包装成自己的插件。

Health Indicator

熔断器的状态暴露在/health endpoint中。

{
"hystrix": {
"openCircuitBreakers": [
"StoreIntegration::getStoresByLocationLink"
],
"status": "CIRCUIT_OPEN"
},
"status": "UP"
}

Hystrix Metrics Stream

引入spring-boot-starter-actuator可以启动Hystrix的metrics stream功能,信息暴露在/hystrix.stream endpoint上。

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Circuit Breaker: Hystrix Dashboard

Hystrix会从每一个HystrixCommand中收集度量信息。Hystrix Dashboard 以一个高效的方式展示每一个熔断器的将康状态。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-D5L9TSQf-1569737545043)(httphttp://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.3.1.RELEASE/images/Hystrix.png)]

Hystrix Timeouts And Ribbon Clients

当使用Hystrix command来包装Ribbon Client时,你肯定希望Hystrix 的超时时间要长于Ribbon的超时时间,因为有可能一次请求会包含多次尝试。例如,如果你的Ribbon 链接超时是1秒,Ribbon Client的一次请求可能需要尝试3次,也就是说,Hystrix 的超时时间应该稍稍多余3秒。

How to Include Hystrix Dashboard

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

使用@EnableHystrixDashboard注解你的Spring Boot主类可以启动Hystrix Dashboard。可以使用Hystrix Dashboard访问Hystrix应用的/hystrix和/hystrix.stream端口来获取一个友好的信息展示方式。


举报

相关推荐

0 条评论