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

1.Hystrix客户端

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

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

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

@SpringBootApplication
@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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
@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 */;
    }
}
 
 
  • 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud中配置熔断可以使用断路来实现容错机制。首先,您需要在pom.xml文件中添加以下依赖项: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> ``` 接下来,在您的应用程序主类上添加@EnableCircuitBreaker注解,以启用断路功能。然后,在您希望应用断路的方法上添加@HystrixCommand注解,以定义断路的行为。您可以在@HystrixCommand注解中指定fallbackMethod,以在触发熔断时调用备用方法。 例如,以下是一个使用Spring Cloud熔断配置的示例: ```java import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.hystrix.EnableHystrix; @SpringBootApplication @EnableCircuitBreaker public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` ```java import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @Service public class YourService { private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "fallbackMethod") public String yourMethod() { // 调用其他服务的代码 } public String fallbackMethod() { // 备用方法的实现 } } ``` 通过这种方式,您可以配置Spring Cloud熔断来处理服务故障,并提供备用方法来处理熔断情况。这样可以确保系统在发生故障时仍然能够提供可靠的服务。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Spring Cloud Gateway熔断限流配置](https://blog.csdn.net/exception_class/article/details/130237653)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [Spring Cloud配置(四)熔断](https://blog.csdn.net/vtopqx/article/details/81746084)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值