springcloud Hoxton.SR3帮助文档之Spring Cloud Netflix-----Hystrix

5.3. Circuit Breaker: Spring Cloud Circuit Breaker With Hystrix

5.3.1. Disabling Spring Cloud Circuit Breaker Hystrix

spring.cloud.circuitbreaker.hystrix.enabled = false

5.3.2. Configuring Hystrix Circuit Breakers

Default Configuration

要为所有断路器提供默认配置,请创建一个自定义bean,该bean传递给HystrixCircuitBreakerFactory或reactivehystrixcircuit itbreakerfactory。可以使用configureDefault方法提供默认配置。

@Bean
public Customizer<HystrixCircuitBreakerFactory> defaultConfig() {
    return factory -> factory.configureDefault(id -> HystrixCommand.Setter
            .withGroupKey(HystrixCommandGroupKey.Factory.asKey(id))
            .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
            .withExecutionTimeoutInMilliseconds(4000)));
}

Reactive Example

@Bean
public Customizer<ReactiveHystrixCircuitBreakerFactory> defaultConfig() {
    return factory -> factory.configureDefault(id -> HystrixObservableCommand.Setter
            .withGroupKey(HystrixCommandGroupKey.Factory.asKey(id))
            .andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
                    .withExecutionTimeoutInMilliseconds(4000)));
}

Specific Circuit Breaker Configuration

与提供默认配置类似,您可以创建一个自定义bean,该bean通过hystrixcircuit itbreakerfactory传递:

@Bean
public Customizer<HystrixCircuitBreakerFactory> customizer() {
    return factory -> factory.configure(builder -> builder.commandProperties(
                    HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(2000)), "foo", "bar");
}

Reactive Example

@Bean
public Customizer<ReactiveHystrixCircuitBreakerFactory> customizer() {
    return factory -> factory.configure(builder -> builder.commandProperties(
                    HystrixCommandProperties.Setter().withExecutionTimeoutInMilliseconds(2000)), "foo", "bar");
}

5.4. Circuit Breaker: Hystrix Clients

Netflix创建了一个名为Hystrix的库来实现断路器模式。在微服务架构中,通常会有多个服务调用层,如下例所示:
图 1. 微服务图
较低级别服务中的服务故障可能会导致级联故障一直到用户。在metrics.rollingStats.timeInMilliseconds定义的滚动窗口中,当对特定服务的调用超过circuitBreaker.requestVolumeThreshold(默认值:20个请求),且故障百分比大于circuitBreaker.errorThresholdPercentage(默认值:>50%)时(默认值:10秒),电路打开,不进行调用。在出现错误和开路的情况下,开发人员可以提供一个后备方案。
图2.Hystrix回退可以防止级联失败
有一个开放的电路可以停止级联故障,并允许不堪重负或失败的服务有时间恢复。failback可以是另一个受Hystrix保护的调用、静态数据或一个合理的空值。回退可能被链接,以便第一个回退执行一些其他业务调用,而这些调用又返回到静态数据。

更多请百度:雪崩效应

5.4.1. How to Include Hystrix

group ID:org.springframework.cloud
artifact ID:spring-cloud-starter-netflix-hystrix

下面的例子显示一个最小的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<String, Object> parameters) {
        //do stuff that might fail
    }

    public Object defaultStores(Map<String, Object> parameters) {
        return /* something useful */;
    }
}

@HystrixCommand是由一个名为“javanica”的Netflix contrib库提供的。Spring Cloud自动将带有该注释的Spring bean包装在连接到Hystrix断路器的代理中。断路器计算何时打开和关闭电路,以及在出现故障时该做什么。
要配置@HystrixCommand,您可以使用带有@HystrixProperty注释列表的commandProperties属性。See here for more details. See the Hystrix wiki for details on the properties available.

5.4.2. Propagating the Security Context or Using Spring Scopes

如果希望某些线程本地上下文传播到@HystrixCommand中,则默认声明不起作用,因为它在线程池中执行该命令(在超时的情况下)。您可以通过配置或直接在注释中要求它使用不同的“隔离策略”,将Hystrix切换为使用与调用者相同的线程。下面的例子演示了如何在注释中设置线程:

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

同样的事情也适用于使用@SessionScope或@RequestScope的情况。如果遇到运行时异常说它找不到作用域上下文,则需要使用相同的线程。
您还可以选择设置hystrix.shareSecurityContext属性为真。自动配置一个Hystrix并发策略插件钩子,将SecurityContext从主线程转移到Hystrix命令使用的线程。Hystrix不允许注册多个Hystrix并发策略,因此可以通过将自己的HystrixConcurrencyStrategy声明为Spring bean来使用扩展机制。Spring Cloud在Spring上下文中查找您的实现,并将其封装在自己的插件中。

5.4.3. Health Indicator

所连接的断路器的状态也暴露在调用应用程序的/健康端点中,如下例所示:

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

5.4.4. Hystrix Metrics Stream

要启用Hystrix度量流,请包含对spring-boot-starter-actuator和设置management.endpoints.web.exposure.include: hystrix.stream的依赖项。这样做将/actuator/hystrix.stream暴露为一个管理端点,如下面的示例所示:

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

5.5. Circuit Breaker: Hystrix Dashboard

Hystrix的主要优点之一是它收集的关于每个HystrixCommand的一组度量标准。Hystrix仪表板以有效的方式显示每个断路器的健康状况。
hystrix 仪表盘

5.6. Hystrix Timeouts And Ribbon Clients

使用包装功能区客户端的Hystrix命令时,您希望确保您的Hystrix超时配置为比配置功能区超时更长,包括可能进行的任何潜在重试。例如,如果您的Ribbon连接超时时间为1秒,而Ribbon客户端可能重试请求三次,那么您的Hystrix超时时间应该略多于3秒。

5.6.1. How to Include the Hystrix Dashboard

group ID:org.springframework.cloud
artifact ID:spring-cloud-starter-netflix-hystrix-dashboard

5.6.2. Turbine

查看单个实例的Hystrix数据对于系统的整体健康状况不是很有用。Turbine是一个应用程序,它将所有相关的/hystrix.stream端点聚合到一个合并的/turbine.stream中,以便在Hystrix仪表板中使用。个别实例通过Eureka定位。运行Turbine需要使用@EnableTurbine注释来注释您的主类(例如,通过使用spring-cloud-starter-netflix-turbine来设置类路径)。Turbine 1 wiki 文件中所有的配置属性都适用。唯一的区别是turbine.instanceUrlSuffix不需要端口预写,因为这是自动处理的,除非turbine.instanceInsertPort=false
默认情况下,Turbine通过查找Eureka中的主机名和端口条目,然后将/hystrix.stream附加到注册实例上,来查找/hystrix.stream端点。如果实例的元数据包含management.port,则使用它代替/hystrix.stream端点的端口值。默认情况下,名为management.port的元数据条目等于management.port配置属性。它可以通过以下配置覆盖:

eureka:
  instance:
    metadata-map:
      management.port: ${management.port:8081}

turbine.appConfig配置键是用于查找实例的Eureka serviceid列表。然后在Hystrix仪表板中使用涡轮机流,其URL类似于以下内容:

my.turbine.server:8080/turbine.stream?cluster=CLUSTERNAME

如果名称是默认的,则可以省略cluster参数。cluster参数必须匹配turbine.aggregator.clusterConfig中的条目。从Eureka返回的值是大写的。因此,如果有一个名为customers的应用程序在Eureka注册,下面的示例就可以工作:

turbine:
  aggregator:
    clusterConfig: CUSTOMERS
  appConfig: customers

如果您需要自定义turbine应该使用哪些集群名称(因为您不希望在turbine.aggregator.clusterConfig配置中存储集群名称),请提供TurbineClustersProvider类型的bean。

5.6.3. Turbine Stream

发布了41 篇原创文章 · 获赞 22 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq1049545450/article/details/105135532