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 the basis of its transformation. The transformation of feign is the same as this.
The corresponding dependencies are introduced in the POM project file:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId></dependency>
Turn on HD
Modifying the Ribbonconsumerapplication.java class
At the entrance of the program Ribbonconsumerapplication class, plus @EnableHystrix annotations to open the circuit breaker, this is necessary, and need to declare the break point @HystrixCommand in the program; plus @ Enablehystrixdashboard annotation, open hystrixdashboard, welcome to study the relevant technology to learn about the source of friends directly seeking exchange 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@SpringBootApplicationpublic class RibbonConsumerApplication { @LoadBalanced @Bean RestTemplate restTemplate() { return new RestTemplate(); public static void main(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;* 描述:调用提供者的 `home` 方法* @author yanpenglei@RestControllerpublic class ConsumerController { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "defaultStores") @GetMapping(value = "/hello") public String hello() { return restTemplate.getForEntity("http://eureka-provider/", String.class).getBody(); public String defaultStores() { return "feign + hystrix Dashboard ,提供者服务挂了";
@HystrixCommand shows that this method is Hystrix package, can be isolated, degraded, fast failure, fast retry, etc. hystrix related functions of dependent services the annotation properties are many, and the following is a few
Fallbackmethod Downgrade method
Commandproperties General Configuration Properties, you can configure hystrixcommand corresponding properties, such as the use of thread pool or signal isolation, fuse fuse rules, etc.
Ignoreexceptions ignored exception, default hystrixbadrequestexception not counted as failed
Groupkey () group name, using the class name by default
Commandkey command name, default use method name
More detailed source code reference: http://xxx/honghu/technology.html
Springcloud Circuit Breaker Monitoring (Hystrix Dashboard) (ix)