spring cloud: Hystrix circuit breaker (fuse)

1. Hystrix Client

Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture there are usually multiple layers of service calls. 
write picture description here

Service failure at a low level of service can lead to cascading failures all the way to the user. When calling a specific service reaches a certain threshold (20 failures in 5 seconds by default), the circuit breaker is opened. In error cases and an open circuit breaker rollback should be available by the developer. 
write picture description here

There is a circuit breaker that prevents cascading failures and allows shutting down services for a period of time to heal. Rollback will be called by other hystrix protections, static data or sanity nulls. 
code show as below:

@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 is provided by the Netflix contrib library called javanica. Spring Cloud automatically wraps Spring beans with annotated proxies to connect to Hystrix circuit breakers. The circuit breaker calculates when to open and close the circuit breaker, and what to do in the event of a failure. 
To configure @HystrixCommand you can use the @HystrixProperty annotation for a list of commandProperties properties. For details, see https://github.com/Netflix/Hystrix/tree/master/hystrix-contrib/hystrix-javanica#configuration  
https://github.com/Netflix/Hystrix/wiki/Configuration

1.1 Propagating security context or using spring scope

If you want some thread local context to propagate to @HystrixCommand the default declaration will not work because it executes the command in the thread pool (in case of timeout). 
You can switch Hystrix to use some configuration with the same thread caller, or directly in the annotation to have it use a different "Isolation Strategy". 
E.g:

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

For details, please refer to https://github.com/Netflix/Hystrix/wiki/Configuration

1.2 Health Monitoring

The state of connected circuit breakers is also exposed on the calling application's /health endpoint.

{
    "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 metrics stream)

Hystrix metrics stream is implemented in spring-boot-starter-actuator. Exposes /hystrix.stream as a management endpoint.

    <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

One of the main benefits of Hystrix is ​​that it collects metrics about each HystrixCommand group. The Hystrix dashboard shows each circuit breaker in a healthy and efficient way. 
write picture description here 
Running the Hystrix Dashboard requires annotating @EnableHystrixDashboard on the spring boot main class. Then visit /hystrix to view the dashboard, and use /hystrix.stream to monitor in the hystrix client application.

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 can be defined using SPEL expressions, in turbine.clusterNameExpression. 
The default value is appName, which means that the eureka service ID will eventually be used as the key of the cluster. For example, the InstanceInfo of customers has an appName of CUSTOMERS. Another example is turbine.clusterNameExpression=aSGName, which will get the cluster name from AWS ASG name.

another example:

turbine:
  aggregator:
    clusterConfig: SYSTEM,USER
  appConfig: customers,stores,ui,admin
  clusterNameExpression: metadata['cluster']
  • 1
  • 2
  • 3
  • 4
  • 5
  • 1
  • 2
  • 3
  • 4
  • 5

In this case, the cluster name is mapped from 4 services from their metadata, expecting to contain "SYSTEM" and "USER".

All apps use default, you need a literal expression (with single quotes):

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

spring cloud provides a spring-cloud-starter-turbine with all the dependencies you need to run a turbo server. Create a spring boot application with @EnableTurbine.

2.2 turbine AMQP

In some environments (such as in PaaS), typical turbo model metrics from all distributed Hystrix commands do not work. In this case, you might want your Hystrix commands to push metrics to turbine, and spring cloud, which is going to use AMQP messaging. All you need to do is add a dependency on spring-cloud-netflix-hystrix-amqp on the client side and make sure the generation rabbitmq is available. (For details, see the Spring Boot documentation on how to configure client credentials, but it should work on a local proxy or cloud).

 

http://blog.csdn.net/zhuchuangang/article/details/51289593

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326722698&siteId=291194637