SpringCloud (three) .Hystrix fuse

A server avalanche effect

     There are multiple calls in the case of micro-services SpringCloud, when the service provider is not available, the call fails repeatedly may result in the caller's service is not available, and gradually extended to the entire system is not available, this server is called avalanche effect the following figure shows the service failure spread, then SpringCloud, the solution to this effect becomes particularly important, SpringCloud by Hystrix of the fuse mechanism to improve fault tolerance services.

II. Principle fuse

     Hystrix as a circuit breaker in the power system in a given period to detect multiple similar mistakes, it will be forced to follow multiple calls quickly failed, the application will not access remote services, no longer performs error access may fail, thereby enables the application to continue execution without waiting for bug fixes, or wait for the timeout to produce for a long time, but also has the ability to fuse whether diagnostic error correction, error correction when the service will try to call again. Diagram as shown below:

III. Configuration Hystrix

  Since Hystrix just used to call the end of the service, so the above referenced spring-cloud-consumer project to transform, because Feign already has Hystrix dependence, there is no need to add a new configuration 

 1. Add the configuration file  feign.hystrix.enabled=true

 2. Implement HelloRemote interface override method

@Component
public class HelloRemoteHystrix implements HelloRemote{

    @Override
    public String hello(@RequestParam(value = "name") String name) {
        return "hello" +name+", this messge send failed ";
    }
}

3. Configure function will be out fallback

@FeignClient(name= "spring-cloud-producer",fallback = HelloRemoteHystrix.class)
public interface HelloRemote {

    @RequestMapping(value = "/hello")
    public String hello(@RequestParam(value = "name") String name);

}

4. Test, starting Eureka, producer, consumer, enter http: // localhost: 8092 / hello111 / zhangsan, display hello zhangsan, this is first name; closed producer, refresh the page, display hello zhangsan, this messge send failed, the fuse success .

 

 

   

Guess you like

Origin www.cnblogs.com/hs5201314tx/p/11518041.html