您的位置:首页 > 编程语言 > Java开发

Hystrix Clients译 断路器(熔断器)

2016-11-24 17:23 99 查看

电子断路器:Hystrix Clients

Netflix创建了一个库叫Hystrix实现了电子断路器模块。在一个微服务架构中它一般有多个服务调用层。



一个底层的服务错误会引起级联错误一直反馈到用户。当调用一个特定的服务到达一定的阀值后(20 failures in 5 seconds is the default in Hystrix),回路开启然后调用也不会成功。一些错误情况下可以由程序员提供

open circuit a fallback。



Having an open circuit stops cascading failures and allows overwhelmed or failing services time to heal. The fallback can be another Hystrix protected call, static data or a sane empty value. Fallbacks may be chained so the first fallback makes some other business call which in turn falls back to static data.

如何引入Hystrix

在你的项目中通过 org.springframework.cloud 和 spring-cloud-starter-hystrix 引入Hystrix。查看详情并设置你的系统使用当前的spring cloud Release

例如:

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

@Component
public class StoreIntegration{
@HystrixCommand(fallbakMethod="defaultStores")
public Object getStore(Map<String,Object> parameters){
//do stuff that might fail
}
public Object defaultStores(Map<String,Object> paramters) {
return /*something useful*/;
}
}


Netflix的普通发布库叫Javanica提供了@HystrixCommand注解。Spring Cloud使用注解自动适配spring bean使用代理去连接到Hystrix断路器。断路器计算何时打开和关闭断路,并决定在失败的情况下做什么。

配置@HystrixCommand你可以使用commandProperties属性,它有@HystrixProperty的注解列表。通过这里查看更多详情.访问Hystrix wiki查看更多可用的属性。

Propagating the Security Context or using Spring Scopes

如果你想把本地线程上下文传播到@HystrixCommand,默认的声明将不可用因为它是在一个线程池中被启动的。你可以选择让Hystrix使用同一个线程,通过一些配置,或直接写在注解上,通过使用isolation strategy属性。例如:

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


同样的方式适用于如果你用@SessionScope 或者 @RequestScope。你应该知道什么时候去做这件事因为有些运行时异常报找不到scoped上下文。

你还可以选择设置 hystrix.shareSecurityContext 属性为true。设置这个值会自动配置一个Hystrix兵法策略会把securityContext从主线程传输到你使用的Hystrix command。Hystrix does not allow multiple hystrix concurrency strategy to be registered so an extension mechanism is available by declaring your own HystrixConcurrencyStrategy as a Spring bean. Spring Cloud will lookup for your implementation within the Spring context and wrap it inside its own plugin

Health Indicator

断路器的状态同样暴露在/health端点上。

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


Hystrix Metrics Stream

使用Hystrix metrics stream需要引入依赖 spring-boot-starter-actuator。这会暴露/hystrix.stream作为一个管理端点。

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


Circuit Breaker: Hystrix Dashboard

Hystrix的主要好处就是她收集了关于每个HystrixCommand的指标。Hystrix仪表盘用一种高效的方式展示了断路器的健康数据。



如何引入Hystrix仪表盘

…org.springframework.cloud and artifact id spring-cloud-starter-hystrix-dashboard…Spring Cloud Project page

在Spring boot main class上加@EnableHystrixDashboard可以运行Hystrix仪表盘,然后可以访问/hystrix并把仪表盘指向一个个体实例/hystrix.stream端点在一个应用中。

Turbine

看一个实例Hystrix数据对于整个系统的健康不是很有用. Turbine 是一个应用程序,该应用程序汇集了所有相关的/hystrix.stream端点到 /turbine.stream用于Hystrix仪表板。运行turbine使用@EnableTurbine注解你的主类,使用spring-cloud-starter-turbine这个jar。配置请参考 the Turbine 1 wiki 唯一的区别是turbine.instanceUrlSuffix不需要端口号前缀,因为这是自动处理,除非turbine.instanceInsertPort=false。

turbine.appConfig配置是一个eureka服务ID列表,turbine将使用这个配置查询实例。turbine stream在hystrix dashboard中使用如下的url配置: http://my.turbine.server:8080/turbine.stream?cluster=,如果集群的名称是default,集群参数可以忽略)。这个cluster参数必须和turbine.aggregator.clusterConfig匹配。从eureka返回的值都是大写的,因此我们希望下面的例子可以工作,如果一个app使用eureka注册,并且被叫做”customers”:

turbine:
aggregator:
clusterConfig: CUSTOMERS
appConfig: customers


clusterName可以使用SPEL表达式定义,在turbine.clusterNameExpression。 默认值是appName,意思是eureka服务ID最终将作为集群的key,例如customers的 InstanceInfo有一个CUSTOMERS的appName。另外一个例子是turbine.clusterNameExpression=aSGName,将从AWS ASG name获取集群名称。作者例子:

turbine:
aggregator:
clusterConfig: SYSTEM,USER
appConfig: customers,stores,ui,admin
clusterNameExpression: metadata['cluster']


在这种情况下,集群名称从4个服务从其元数据映射,期望包含“SYSTEM”和“USER”。

所有的app使用default,你需要一个文字表达式(使用单引号):

turbine:
appConfig: customers,stores
clusterNameExpression: "'default'"


spring cloud提供一个spring-cloud-starter-turbine,所有依赖项你需要运行一个turbine服务器。使用@EnableTurbine创建一个spring boot应用。

注意:默认情况下Spring Cloud 允许 Turbine 在集群的每个主机下使用主机名和端口运行多个进程。如果你想在集群中的每个主机使用本机原生Netfix行为且不允许多个进程创建运行Turbine。(实例id的key为主机名)然后设置属性turbine.combineHostPort=false

Turbine Stream

在一些环境(Pass), 在所有分布式下典型的Turbine 模型的Hystrix 命令都不工作,在这种情况下,你可能想要 Hystrix 命令 推送到 Tuibine, 和Spring Cloud进行消息传递,那么你需要要做的是在客户端添加一个依赖spring-cloud-netflix-hystrix-stream和你所选择的 spring-cloud-starter-stream-*的依赖(相关信息请查看 Spring Cloud Stream 方档,以及如何配置客户端凭证,和工作时的要本地代理)

创建一个带有注解 @EnableTurbineStream 的Spring boot 应用服务器,端口默认 8989 (Hystrix 仪表盘的URL都使用此端口), 如果你想自定义端口,可以配置 server.port 或 turbine.stream.port 任一个,如果你使用了 spring-boot-starter-web 和 spring-boot-starter-actuator ,那么你可以提供(使用Tomcat默认情况下) management.port 不同的端口,并打开这个单独的执行器端口

你可以把Dashboard指向Turbine Stream Server来代替个别Hystrix streams。如果Tubine Stream 使用你本机的8989端口运行,然后把 http://myhost:8989在流输入字段Hystrix仪表板 Circuits 将由各自的 serverId关缀,紧随其后的是一个点,然后是circuit 名称
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Hystrix spring netflix