1. Hystrix Circuit Breaker
- Hystrix is an open-source repository for latency and fault tolerance in distributed systems where many dependencies inevitably fail to be invoked.
such as timeouts, anomalies, etc., Hystrix can ensure that in the case of a dependency problem, will not cause the overall service failure, avoid cascading failures, to improve the sub-
The elasticity of the cloth-type system;
- "Circuit Breaker" itself is a switching device, when a service unit fails, through the fault monitoring of the circuit breaker (similar to the fuse fuse), to the caller
Returns an alternative response (FallBack) that is expected to be processed, rather than a long wait or throws an exception that the caller cannot handle, so
Ensure that the service caller's thread is not consumed for long periods of time, thus avoiding the spread of failures in distributed systems, or even avalanches.
1.1 Service Fuse
- The fusing mechanism is a kind of micro-service link protection mechanism to deal with avalanche effect.
- When a microservices on the fanout link is unavailable or the response time is too long, the service is degraded, which fuses the invocation of the node microservices and quickly returns an "error"
The call link is resumed when the node micro-service call is detected properly;
New microservicecloud-provider-dept-hystrix-8001
Reference microservicecloud-provider-dept-8001//pom.xml<!--hystrix--><dependency> <groupId> Org.springframework.cloud</groupid> <artifactId>spring-cloud-starter-hystrix</artifactId>< /dependency>//application.ymleureka:client: # Client registered in Eureka Service list service-url:defaultzone:http://eureka7001. Com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka Instance:instance-id: Microservicecloud-dept8001-hystrix # Custom Hystrix related service name information Prefer-ip-address:true # Access path can display IP address//modify Deptcontroller@re Stcontrollerpublic class Deptcontroller {@Autowired private deptservice deptservice; @Autowired private Discoveryclient client; @RequestMapping (value= "/dept/get/{id}", Method=requestmethod.get)//Once the service method failure is invoked and an error message is thrown, Automatically calls the specified method in the @hystrixcommand labeled Fallbackmethod call Class @HystrixCommand (fallbackmethod= "Processhystrix_get") public Dept G ET (@PathVariable ("id") Long ID) {Dept Dept = This.deptserVice.get (ID); if (null = = Dept) {Throw new RuntimeException ("The ID:" +id+ "has no corresponding information"); } return dept; } public Dept Processhystrix_get (@PathVariable ("id") Long ID) {return new Dept (). Setdeptno (ID). Setdname ("This ID:" + id+ "No corresponding information, null--@HystrixCommand"). Setdb_source ("No This database in MySQL"); }}//Modify the main startup class Deptprovider8001_hystrix_app and add new annotations @enablecircuitbreaker@springbootapplication@enableeurekaclient// After the service is started, the automatic registration into the Eureka Service @enablediscoveryclient//Service discovery @enablecircuitbreaker//support for Hystrix fusing mechanism public class Deptprovider8001_hystrix_app {public static void main (string[] args) {Springapplication.run (deptprovider8001_h Ystrix_app.class, args); }}//Test Access://http://localhost:8082/consumer/dept/get/112
1.2 Service downgrade
- Service downgrade: The overall resources are not fast enough, the pain will be some services first off, to tide over the difficulties, and then open back;
- Service demotion processing is done on the client side and has no relationship with the server;
- When a service is fused, the server will no longer be called, at which point the client can prepare a local fallback callback and return a default value;
Modify the MICROSERVICECLOUD-API project//According to the existing Deptclientservice interface, Create a new class that implements the Fallbackfactory interface deptclientservicefallbackfactory@component//Don't forget to add the public class Deptclientservicefallbackfactory implements fallbackfactory<deptclientservice> {@Override public Deptclientse Rvice Create (Throwable arg0) {return new Deptclientservice () {@Override public Dept get (lon G ID) {return new Dept (). Setdeptno (ID). Setdname ("The ID:" +id+ "does not have the corresponding information, consumer the client provides the downgrade information, the service provider is now closed") . Setdb_source ("No This database in MySQL"); } @Override Public list<dept> List () {return null; } @Override Public boolean add (Dept Dept) {return false; } }; }}//Modify MICROSERVICECLOUD-API project, Deptclientservice interface//Add Fallbackfactory attribute value @feignclient (value= "in annotation @feignclient) Microservicecloud-dept ", FALLBACKFACTORY=DEPTCLIENTSERVICEFAllbackfactory.class) public interface Deptclientservice {@RequestMapping (value= "/dept/get/{id}", method= requestmethod.get) public Dept GET (@PathVariable ("id") long ID); @RequestMapping (value= "/dept/list", method= requestmethod.get) public list<dept> list (); @RequestMapping (value= "/dept/add", method= requestmethod.post) Public boolean Add (Dept dept);} MVN clean//mvn install//Modify Microservicecloud-consumer-dept-feign project//Application.ymlfeign:hystrix:enabled:true Test access://http://localhost:8082/consumer/dept/get/1//then manually shut down the project: microservicecloud-provider-dept-8001
1.3 Service Monitoring (Hystrixdashboard) New Project Microservicecloud-consumer-hystrix-dashboard
pom.xml<!--Hystrix and Hystrix-dashboard related--><dependency> <groupid>org.springframework.cloud </groupId> <artifactId>spring-cloud-starter-hystrix</artifactId></dependency>< Dependency> <groupId>org.springframework.cloud</groupId> <artifactId> spring-cloud-starter-hystrix-dashboard</artifactid></dependency>//application.ymlserver:port:9001/ /main startup class: @SpringBootApplication @enablehystrixdashboardpublic class Deptconsumer_dashboard_app {public static void main ( String[] args) {Springapplication.run (deptconsumer_dashboard_app.class, args); }}//all provider MicroServices provided classes (8001/8002/8003) need to provide monitoring dependency configuration//(8001/8002/8003) pom.xml<!--Actuator monitoring Information Perfect-->< Dependency> <groupId>org.springframework.boot</groupId> <artifactId> spring-boot-starter-actuator</artifactid></dependency>//Start related projects// Microservicecloud-consumer-hystrix-dashboard//visit: http://localhost:9001/hystrix//3 Eureka Cluster//MICROSERVICECLOUD-PROVIDER-DEPT-HYSTRIX-8001//Access: HTTP://LOCALHOST:8001/DEPT/GET/1//access: Http://localhost:8001/hystrix.stream
Resources:
Springcloud Advanced Hystrix (Circuit breaker)