(Forty-two) java version of spring cloud micro Services Architecture b2b2c e-commerce platform -eureka cluster integration framework hystrix

Following the project before continuing integration hystrix framework, hystrix framework for Netflix module is a fault-tolerant framework. When the user accesses the service the caller, if the service provider can not lead to abnormal situation returns to normal timeout request occurs, the service did not know the caller, continued request, this will lead to the collapse of services.

The traditional solution: add time-out mechanism, operation and maintenance of human flesh, and hystrix precisely to solve this problem, it adds fault tolerance between the caller and the service provider when the service provider can not provide services, service blocked caller will request according to the timeout, and invokes rollback logic.

1, add hystrix relies

<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

2, the circuit breaker was added @EnableCircuitBreaker application.java class annotation.

3, the class is added in the controller and setting a maximum timeout thread @DefaultProperties value, we / hello interface adds a thread blocked for 3 seconds, the timeout hystrix set to 2 seconds, so that the back-off method logic. @HystrixCommand in fallbackMethod parameters on the interface method is the method name fallback logic. helloFallback () method as a fallback method.

@RestController
@DefaultProperties(groupKey = "hello-groupKey",
        commandProperties = {
            // 超时时间(毫秒)
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
        },
        threadPoolProperties = {
            // 最大线程数
            @HystrixProperty(name = "coreSize", value = "2")
        })
public class MyRestController {

    @Autowired
    private IService iService;

    @GetMapping(value = "/hello", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @HystrixCommand(fallbackMethod = "helloFallback", commandKey = "hello-commandKey")
    public String hello() throws InterruptedException {
        Thread.sleep(3000);
        String hello = iService.hello();
        return "hello: " + hello;
    }

    public String helloFallback() {
        return "helloFallback";
    }

}

Guess you like

Origin blog.csdn.net/vvx0206/article/details/95164526