Services SpringCloud of micro fuses

   Service learning SpringCloud micro blog is a reference pure smile, to see him referred to fuse the stock market I can not help but Tucao about, remember that the implementation of the first day on the blown fuse, and now think it is still funny, from the previous national stocks and now the national real estate speculators, speculation is a word, the problem is now the housing market and the stock market before 5000, like a trade war coupled with an aging population and other factors, I do not know how long it can sustain, may be a pessimist I am not very optimistic.

   Fuse and fuse at home, like, start protection, voltage too high or too low can cause damage to electrical appliances, the role of the fuse is a direct cut off when the voltage is abnormal, and will not affect the use of other appliances, but only a fuse wire, broken little impact, is a way to prevent avalanches. Redis there are avalanches and penetration, SpringCloud also has avalanche. A service B service calls, service calls B C service, service call C D services, if D service hung up, and that if not protection, the service will hang C, C hanging B will hang, hang A also B hanging, which would have hung up, which requires a courage ton output capacity, and learning theory is much the same purpose. Once order was restored avalanche contrary, it should first restore D services, in turn restore the C, B, A. Otherwise, if the first recovery A, B or hang it up, that time A still hanging. The following is a comparison of the written presentation.

1. The circuit breaker mechanism

Circuit breakers is well understood that, when a backend service request failed Hystrix Command quantity exceeds a certain percentage (default 50%), the circuit breaker will switch to an open state (Open). In this case all requests are not sent directly to the failure of the backend service the circuit breaker remains in an open state after a period of time (5 seconds by default) automatically switches to the half-open state (hALF-oPEN). in this case a request to return the situation will determine if the request is successful, the closed state of the circuit breaker switch back ( CLOSED), or switch back to the open state (oPEN). Hystrix circuit breaker is like our family in the fuse, once the back-end service is not available, the circuit breaker will cut off the direct chain of requests, to avoid sending a large number of invalid requests affect system throughput and circuit breakers have the ability to self-testing and recovery.

2.Fallback

Fallback equivalent downgrade. For query operation, we can achieve a fallback method, when the back-end service request an exception, you can use the value fallback method returns. The return value is usually set default values ​​or from the cache fallback method.

3. Resource isolation

In Hystrix mainly by the thread pool resources to achieve isolation, usually in use when we will be divided into multiple threads pool based remote service call. For example call into goods and services Command A thread pool, Command call account services put B into the thread pool. the main advantage of this is that the operating environment was set apart. such services exist, even if the calling code bug or due to other causes their own time thread pool is exhausted, will not affect other services system. but the cost of maintaining multiple threads is to bring the pool system will bring additional performance overhead. If there are strict performance requirements and believe that your client code to invoke the service will not be a problem, you can use Hystrix signal pattern ( Semaphores) to isolate resources.

In SpringCloud achieve relatively simple, Feign has been so dependent on the Hystrix maven configuration without any changes, just three steps.

1. Profiles

EurekaConsumer on the basis of a blog, start fegin of Hystrix function in the properties file.

feign.hystrix.enabled=true

2. Create the callback class

package com.example.demo;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

@Component
public class HelloRemoteHystrix implements HelloRemote {
    @Override
    public String hello(@RequestParam(value = "name") String name) {
        return "hello " +name+", this messge send by Hystrix ";
    }
}

3. Add fallback attribute

Adds the specified fallback class in HelloRemote class, returns the contents fallback class in the service when the fuse.

package com.example.demo;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

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

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

}

Has been completed at this time to introduce fuse, we can test the next. In order to start the service EurekaServer, producers EurekaClient, consumers EurekaConsumer.

Now enter http: // localhost: 9001 / hello name = cuiyw? , The output is normal as follows:

When EurekaClient has stopped, enter HTTP: // localhost: 9001 / cuiyw the Hello name =? , Output is as follows, plus the return of the above-blown fallback class, indicating fuse success.

 Reference: http: //www.ityouknow.com/springcloud/2017/05/16/springcloud-hystrix.html

Guess you like

Origin www.cnblogs.com/5ishare/p/11184512.html