Circuit Breaker
The circuit breaker model originates from Martin Fowler's circuit breaker. "Circuit Breaker" itself is a switch device, used in the circuit to protect the line overload, when the circuit has a short circuit, "circuit breaker" can timely cut off the fault circuit, to prevent the occurrence of overload, heat, and even fire serious consequences.
In a distributed architecture, the role of the circuit breaker pattern is similar, when a service unit fails (similar to a short-circuit with electrical appliances), through the fault monitoring of the circuit breaker (similar to a fuse fuse), to the caller returned an error response, rather than a long wait. This does not cause the thread to fail to be released for a long time because of the invocation of the fault service, thus avoiding the spread of the fault in the distributed system.
Circuit Breaker Monitoring
In the micro-service architecture, a circuit breaker model is presented to ensure the usability of the program and prevent the network from causing the error. The condition of the circuit breaker reflects the usability and robustness of a program, which is an important indicator. The Hystrix dashboard is a component of the circuit breaker State, providing data monitoring and a friendly graphical interface
Renovation Project
Copy the project spring-cloud-ribbon-consumer-hystrix
, modify the name spring-cloud-ribbon-consumer-hystrix-dashboard
on its basis to transform it. Feign
The transformation is the same as this.
The pom
corresponding dependencies are introduced in the project file:
<dependency> <groupId></groupId> <artifactId> spring-cloud-starter- </artifactId> </dependency> <dependency> <groupId></groupId> < Artifactid> Spring-boot-starter-</artifactId> </dependency> <dependency > <groupId></groupId> <artifactId> spring-cloud-starter-hystrix- </artifactId> </dependency>
Turn on HD
Modifying a RibbonConsumerApplication.java
class
In the program's entry RibbonConsumerApplication
class, plus @EnableHystrix
annotations to open the circuit breaker, this is necessary, and you need to declare the break point in the program @HystrixCommand;
@EnableHystrixDashboard
annotated, open HystrixDashboard,
You are welcome to study the relevant technology to understand the source of friends directly seeking exchange and sharing technology: 2147775633
Package Io.ymq.example.ribbon.consumer.hystrix;import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.springbootapplication;import Org.springframework.cloud.client.discovery.enablediscoveryclient;import Org.springframework.cloud.client.loadbalancer.loadbalanced;import Org.springframework.cloud.netflix.hystrix.enablehystrix;import Org.springframework.cloud.netflix.hystrix.dashboard.enablehystrixdashboard;import Org.springframework.context.annotation.Bean, .... Import org.springframework.web.client.resttemplate;@ Enablehystrix@enablediscoveryclient@enablehystrixdashboard@springbootapplication Public classribbonconsumerapplication {@LoadBalanced @beanresttemplate resttemplate () {return Newresttemplate (); Public Static voidMain (string[] args) {Springapplication.run (ribbonconsumerapplication.class, args);
Declaring a break point
Declaring a break point@HystrixCommand(fallbackMethod="defaultStores")
Package Io.ymq.example.ribbon.consumer.hystrix; import Com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; Import org.springframework.beans.factory.annotation.Autowired; Import org.springframework.stereotype.Component; Import org.springframework.web.bind.annotation.GetMapping; Import Org.springframework.web.bind.annotation.RestController; Import Org.springframework.web.client.RestTemplate; *Description: Call the provider's ' home ' method*@author Yanpenglei @RestController Public classConsumercontroller {@AutowiredPrivateresttemplate resttemplate; @HystrixCommand (Fallbackmethod="Defaultstores") @GetMapping (value="/hello") PublicString Hello () {returnResttemplate.getforentity ("http://eureka-provider/", String.class). GetBody (); PublicString defaultstores () {return "feign + hystrix Dashboard, provider service hung up";
@HystrixCommand
Indicates that the method is a hystrix
package, can be dependent on the service to isolate, downgrade, rapid failure, quick retry, and so on hystrix
related functions The annotation properties are many, the following explains several of them
fallbackMethod
Downgrade method
commandProperties
General configuration properties that can be configured HystrixCommand
, such as thread pool or semaphore isolation, fuse fuse rules, etc.
ignoreExceptions
Ignored exception, default does not HystrixBadRequestException
count to failed
groupKey()
Group name, using the class name by default
commandKey
Command name, default use method name
More detailed source code reference: http://minglisoft.cn/honghu/technology.html
Springcloud Circuit Breaker Monitoring (Hystrix Dashboard) (ix)