spring cloud: Hystrix断路器(熔断器)

来源:互联网 发布:听歌识曲的软件 编辑:程序博客网 时间:2024/04/27 16:37

1.Hystrix客户端

Netflix已经创建了一个名为Hystrix的库,实现了断路器的模式。在microservice架构通常有多个层的服务调用。 
这里写图片描述

低水平的服务的服务失败会导致级联故障一直给到用户。当调用一个特定的服务达到一定阈值(默认5秒失败20次),打开断路器。在错误的情况下和一个开启的断路回滚应可以由开发人员提供。 
这里写图片描述

有一个断路器阻止级联失败并且允许关闭服务一段时间进行愈合。回滚会被其他hystrix保护调用,静态数据或健全的空值。 
代码如下:

@SpringBootApplication@EnableCircuitBreakerpublic class Application {    public static void main(String[] args) {        new SpringApplicationBuilder(Application.class).web(true).run(args);    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
@Componentpublic 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 */;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

@HystrixCommand是由Netflix contrib 库提供,叫做javanica。spring cloud自动包装Spring bean与注释的代理连接到Hystrix断路器。断路器计算何时打开和关闭断路,并在失败的情况下做什么。 
配置@HystrixCommand可以使用commandProperties属性的列表@HystrixProperty注释。详细请看https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#configuration 
https://github.com/Netflix/Hystrix/wiki/Configuration

1.1 传播安全上下文或者使用spring范围

如果你想要一些线程本地上下文传播到@HystrixCommand默认声明将不会工作,因为它执行线程池中的命令(在超时的情况下)。 
可以切换Hystrix使用一些配置用相同的线程调用者,或直接在注释,让它使用不同的“隔离策略”(Isolation Strategy)。 
例如:

@HystrixCommand(fallbackMethod = "stubMyService",    commandProperties = {      @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")    })...
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

详细内容请参考https://github.com/Netflix/Hystrix/wiki/Configuration

1.2 健康监控

连接的断路器的状态也暴露在调用应用程序的/health端点。

{    "hystrix": {        "openCircuitBreakers": [            "StoreIntegration::getStoresByLocationLink"        ],        "status": "CIRCUIT_OPEN"    },    "status": "UP"}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.3 Hystrix Metrics Stream(hystrix指标流)

spring-boot-starter-actuator中实现了Hystrix metrics stream。暴露/hystrix.stream作为一个管理端点。

    <dependency>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-actuator</artifactId>    </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

2.Hystrix dashboard

Hystrix的主要好处之一是它收集关于每个HystrixCommand组指标。Hystrix仪表板显示每个断路器的健康高效的方式。 
这里写图片描述 
运行Hystrix仪表板需要在spring boot主类上标注@EnableHystrixDashboard。然后访问/ hystrix查看仪表盘,在hystrix客户端应用使用/hystrix.stream监控。

2.1 turbine

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

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

turbine:  aggregator:    clusterConfig: CUSTOMERS  appConfig: customers
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

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']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

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

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

turbine:  appConfig: customers,stores  clusterNameExpression: 'default'
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

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

2.2 turbine AMQP

在某些环境中(如在PaaS),典型的turbine模型的指标从所有分布式Hystrix命令不起作用。在这种情况下,你可能想要你Hystrix命令推动指标turbine,和spring cloud,就要使用AMQP消息传递。所有您需要做的是在客户端添加一个依赖spring-cloud-netflix-hystrix-amqp并确保代rabbitmq可用。(有关详细信息,请参阅弹簧引导文档如何配置客户端凭据,但它应该工作的当地代理或云计算)。

0 0
原创粉丝点击