SpringCloud breaker Hystrix comprehensive analysis

In the micro-service scenario, service calls usually have a lot of layers. If a problem occurs underlying service, the fault will be spread up to the user. We need a mechanism that, when the underlying service is not available, you can interrupt transmission failure. This is the role of the circuit breaker. He was last re-guarantee service stability of the system.

In springcloud the circuit breaker assembly is Hystrix. Hystrix Netflix is ​​also part of a kit. His function is that when a call to a service within a certain period of time (default 10s, configured by metrics.rollingStats.timeInMilliseconds), there are more than a certain number of times (default 20, configured by circuitBreaker.requestVolumeThreshold parameters) and a failure rate of more than a certain value (default 50% by circuitBreaker.errorThresholdPercentage configuration), the circuit breaker will open the service. Return to a fallback set by the developer

Another fallback may be protected by a Hystrix service calls, it may be a fixed value. fallback chain can also be designed as a call to perform some logic, and then return fallback.

how to use

Netflix breaker is installed on the service consumer. We need to do is open the circuit breaker on the service consumer and configure.

Ribbon consumers:

1, rely on the introduction of

2, switch

@EnableCircuitBreaker can only add annotations on the startup class

3, in the service plus fallback

4, testing the service provider to turn off

After a while, and then start the service provider can be observed, automatic recovery service consumers.

Note that, fallbackMethod configuration method must have the intention and methods are annotated into @HystrixCommand parameters and return values

Otherwise it will error fallback method was not found: defaultFallback

Feign in use

@EnableFeignClients circuit breaker has been turned on by default function, so no comment on the plus @EnableCircuitBreaker start classes here

Need only specify fallback fallback parameter in the method in @FeignClient

Because @FeignClient annotation is the interface, so we have to create an alternative class, just as mock

Create a class that implements the interface HelloService

last step

Turn the breaker in the configuration file in application.yml

In early versions, Feign circuit breaker is enabled by default. Later, someone mentioned issue, I think it is not convenient. By using Feign use the circuit-breaker function to default, causing some problems. D version from the beginning of the back breaker is off by default, need to manually open.

We can see the same effect and Ribbon

Precautions

If you need to use the Thread Local execution method of property that can not be passed to the default fallback method, because in Hystrix, the main resource isolation is achieved by a thread pool usually used when We will call the remote service division a plurality of thread pool. goods and services such as call into Command a thread pool, call the account services Command B into the thread pool. the main advantage of this is that the operating environment was set apart. in this way, even if the code to call the service a bug or due to other causes their own time thread pool is exhausted, the service will not affect other systems, but the cost of maintaining multiple threads is to bring the pool system will bring additional performance overhead. If there is a performance strict requirements are also convinced that their own client code to invoke the service will not be a problem, you can use Hystrix signal pattern (Semaphores) to isolate resources.

You need to do the following configuration

Hystrix Dashboard

Since the circuit breaker can detect the availability of the service, but not intuitive. Hystrix Dashboard can provide the availability of a monitoring platform for easy viewing services.

1. Create a new project

Because Dashboard is a unified circuit breaker monitoring platform, so we create a new project. And now serves consumers do not mix

2, dependent on the introduction of

3, annotation startup class

@EnableHystrixDashboard start with class notes

Boot referral, access / hystrix

See above description, we can know, in three different monitoring methods were Hystrix Dashboard support

1, the default cluster monitoring: through URL: turbine-hostname: port / turbine.stream open, realize the control to the default cluster.

2, designated cluster monitoring: through URL: turbine-hostname:? Port / turbine.stream cluster = [clusterName] open, to achieve control of clusterName cluster.

3, single application monitoring: via URL / hystrix-app: port / hystrix.stream open, to achieve control of a specific service instances.

Note that there is something wrong, we have said before, from springboot2 start, actuator is coupled to the request path / actuator, which is hystrix-app: port / actuator / hystrix.stream fishes

Delay: delay time control poll monitoring information on the server, the default is 2000 ms, the client can reduce the consumption of CPU and network configuration through the property.

Title: This parameter can demonstrate appropriate title.

Monitoring a single instance of service

Let's test the first three, a single instance of the monitoring service. This is best understood

For Robbon engineering, plus on top of that we have a good breaker. Now need to do is dependent on the introduction actuator

Then exposed hystrix.stream Interface

Start the application.

Now I windows10.microdone.cn:8781/actuator/hystrix.stream service fill, click Monitor

Request more than a few clicks, you can see the success or failure if the request is monitored up

For Feign

We have said above, Feign it has integrated breaker. But if you want to expose hystrix.stream interfaces, or must the introduction of spring-cloud-starter-netflix-hystrix dependent, marked @EnableCircuitBreaker annotation notes on startup class

Then test, and the same effect Ribbon

Turbine

turbine is what the concept?

Turbine is an independent project of Netflix, the usefulness of the data circuit breaker separate a springcloud instance is not large, but also a lot of trouble, like the above we have two consumer items, you need to open two page view, can not put all these monitoring polymerization look at before?

Of course we can, so only the Turbine project

Turbine principle is that by registering themselves into the registry, find hystrix service on the same registry, and aggregate data. Again, on display on the dashboard by exposing their endpoints.

operating:

1. Create an application

2, the introduction of dependency:

Start class notes:

@EnableTurbine @EnableDiscoveryClient

YML 配置:

Start, we are now up turbine port 8792

In the hystrix page, fill in the address monitor 127.0.0.1:8792/turbine.stream

Click monitor

You can see that the consumer feign and ribbon set of two projects together

Service Name HelloService # hello (String) method and hiService is the name of Hystrix fallback

About default

cluster-name-expression cluster name, can be a spel expressions, here is the default default. If you want to customize the name of a cluster is changed to the following configuration:

Turbine: # applications to monitor, separated by commas plurality App-config: Feign-Consumer, Consumer-Ribbon # specify which clusters polymerization, using a plurality of "," split default default Aggregator: Cluster-config: Ribbon, Feign # to use with the application being monitored. Assume that the application you want to monitor configured eureka.instance.metadata-map.cluster: ABC, you need to configure, while turbine.aggregator.clusterConfig: ABC Cluster-name-expression The: the Metadata [ 'Cluster'] # make on the same host service is performed by a combination of host name and port number to distinguish, will host to differentiate default service, which would make the machine at the time of commissioning, different services on the unit to be aggregated into one service statistics. combine-host-port: true

This configuration is used two clusters, ribbon and feign

cluster-name-expression is to take with metadata cluster value

This will need to be monitored corresponds to modify the application configuration files,

ribbon Consumer Project:

feign Consumer Project:

All restart it

Go hystrix page

This time again 127.0.0.1:8792/turbine.stream Rom, because the cluster name has been changed, there is no longer the default cluster

127.0.0.1:8792/turbine.stream?cluster=feign were used to monitor and ribbon

Such distinction is the role, if the company has a lot of services that can be viewed by differentiating clusters respectively.

Reprinted from: https:? //Baijiahao.baidu.com/s id = 1623004854011062838 & wfr = spider & for = pc

Guess you like

Origin www.cnblogs.com/aboutYouH/p/12552062.html