好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

spring cloud Hystrix断路器的使用(熔断器)

1.hystrix客户端

netflix已经创建了一个名为hystrix的库,实现了 断路器 的模式。在microservice架构通常有多个层的服务调用。

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

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

?

1

2

3

4

5

6

7

8

9

@ spring bootapplication

@enablecircuitbreaker

public 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

10

11

12

@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 库提供,叫做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)。

例如:

?

1

2

3

4

5

6

@hystrixcommand (fallbackmethod = "stubmyservice" ,

   commandproperties = {

    @hystrixproperty (name= "execution.isolation.strategy" , value= "semaphore" )

   }

)

...

详细内容请参考 https://github.com/netflix/hystrix/wiki/configuration

1.2 健康监控

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

?

1

2

3

4

5

6

7

8

9

{

   "hystrix" : {

     "opencircuitbreakers" : [

       "storeintegration::getstoresbylocationlink"

     ],

     "status" : "circuit_open"

   },

   "status" : "up"

}

1.3 hystrix metrics stream(hystrix指标流)

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

?

1

2

3

4

<dependency>

     <groupid>org.springframework.boot</groupid>

     <artifactid>spring-boot-starter-actuator</artifactid>

   </dependency>

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:

?

1

2

3

4

turbine:

  aggregator:

   clusterconfig: customers

  appconfig: customers

clustername可以使用spel表达式定义,在turbine.clusternameexpression。

默认值是appname,意思是eureka服务id最终将作为集群的key,例如customers的instanceinfo有一个customers的appname。另外一个例子是turbine.clusternameexpression=asgname,将从aws asg name获取集群名称。

另一个例子:

?

1

2

3

4

5

turbine:

  aggregator:

   clusterconfig: system,user

  appconfig: customers,stores,ui,admin

  clusternameexpression: metadata[ 'cluster' ]

在这种情况下,集群名称从4个服务从其元数据映射,期望包含[system]和[user]。

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

?

1

2

3

turbine:

  appconfig: customers,stores

  clusternameexpression: 'default'

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可用。(有关详细信息,请参阅弹簧引导文档如何配置客户端凭据,但它应该工作的当地代理或云计算)。

hystrix相关的其他文章:

hystrix缓存功能的使用: http://www.tuohang.net/article/166243.html

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://blog.csdn.net/zhuchuangang/article/details/51289593

查看更多关于spring cloud Hystrix断路器的使用(熔断器)的详细内容...

  阅读:48次