2019.3 计划翻译 Spring Cloud Netflix 和 Spring Cloud OpenFeign
跳转 Spring Cloud Greenwich.RELEASE 官方文档 原文
@[toc]d
Hystrix 是 Netflix 下一个实现了 circuit breaker pattern 的库,在微服务架构中,服务调用通常存在多层,如下图所示:
较低层级的服务故障将引起上层服务的级联故障。当在 metrics.rollingStats.timeInMilliseconds
(默认值为 10 秒)时间内对指定服务调用超过 circuitBreaker.requestVolumeThreshold
(默认值为 20 次请求)次,且失败的比例大于circuitBreaker.errorThresholdPercentage
(默认值为 50%),断路器打开同时服务不可达。在出现错误且断路器打开时,开发者可以提供一个回退逻辑。
图 13.2. Hystrix 回退防止级联故障
通过打开断路器防止级联故障,并给予不止所措甚至失败的服务一段时间,用以恢复正常,回退逻辑可以是另一个 Hystrix 保护的调用、静态数据或一个空值,一个请求的回退逻辑本身能被其他业务请求作为回退逻辑调用,这些调用最终都将得到静态数据。
在项目中,通过 starter 使用 group ID: org.springframework.cloud
artifact ID: spring-cloud-starter-netflix-hystrix
引入 Hystrix。构建系统时,使用当前的 Spring Cloud Release Train
,详情查阅 Spring Cloud Project page 。
一个使用 Eureka 服务端和 Hystrix 断路器的程序示例:
@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 parameters) {
//do stuff that might fail
}
public Object defaultStores(Map parameters) {
return /* something useful */;
}
}
@HystrixCommand
由名为 “javanica” 的 Netflix 库提供,Spring Cloud 将使用 @HystrixCommand 注解的 Spring beans 自动包装成一个连接到 Hystrix 断路器的代理。断路器自行计算何时打开和关闭,以及在故障时做什么。
要配置 @HystrixCommand
,你可以使用一个 @HystrixProperty
注解列表填充 commandProperties
属性,在 这里 查看更多详情,在 Hystrix 维基 查看更多可用属性。
如果你希望某些本地线程上下文传递到 @HystrixCommand
,默认的声明将失效,因为它在线程池中执行命令(超时时)。你可以通过配置或直接在注解中开启让 Hystrix 在同一个线程中调用,这要求使用不同的 “隔离策略”,在注解中设置线程的示例如下:
@HystrixCommand(fallbackMethod = "stubMyService",
commandProperties = {
@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
}
)
...
你可以使用 @SessionScope
或 @RequestScope
来完成同样的事情。如果遇到运行时异常,说明找不到作用域内的上下文,你需要使用同一个线程来执行。
你也可以设置 hystrix.shareSecurityContext
属性为 true
。这样会自动配置一个 Hystrix 并发策略插件钩子,用来将 SecurityContext
从主线程转换到 Hystrix 命令执行线程。Hystrix 不允许注册多个 Hystrix 并发策略,因此可以将自定义一个 HystrixConcurrencyStrategy
声明为 Spring bean 的形式作为扩展机制。Spring Cloud 将在 Spring 上下文中查找你的实现,并将其包装到自己的插件中。
连接断路器的状态同样通过调用应用程序的 /health
端点暴露,如下所示:
{
"hystrix": {
"openCircuitBreakers": [
"StoreIntegration::getStoresByLocationLink"
],
"status": "CIRCUIT_OPEN"
},
"status": "UP"
}
要使用 Hystrix 指标流,引入依赖 spring-boot-starter-actuator
并配置 management.endpoints.web.exposure.include: hystrix.stream
,暴露 /actuator/hystrix.stream
作为管理端点,如下所示:
org.springframework.boot
spring-boot-starter-actuator
Hystrix 的主要优点之一是它收集每个 HystrixCommand 的一套指标,Hystrix 仪表盘以有效的方式显示每个断路器的健康状况。
当使用包含 Ribbon 客户端的Hystrix 命令时,你需要确保 Hystrix 超时配置长于 Ribbon 超时配置,包括可能发生的任何潜在的重试,比如,你的 Ribbon 连接超时配置为一秒,Hystrix 可能重试三次请求,你的 Hystrix 超时配置应超过三秒钟。
在项目中,通过 starter 使用 group ID: org.springframework.cloud artifact ID: spring-cloud-starter-netflix-hystrix-dashboard 引入 Hystrix 仪表盘。构建系统时,使用当前的 Spring Cloud Release Train
,详情查阅 Spring Cloud Project page 。
要运行 Hystrix 仪表盘,使用 @EnableHystrixDashboard
注解修饰你的 Spring Boot 主类,然后访问 /hystrix
,并将仪表板指向 Hystrix 客户端应用程序中的单个实例 /hystrix.stream
端点。
连接到使用HTTPS的 /hystrix.stream
端点时,服务器使用的证书必须由JVM信任。如果证书不可信,需要将证书导入到 JVM,以便 Hystrix 仪表盘成功连接到流端点