Spring Cloud 服务容错和断路器Hystrix实践

一、雪崩效应

在微服务架构中,通常有多个服务层调用,如果某个服务不可用,造成调用的服务也不可用,造成整个系统不可用的情况,叫做雪崩效应

二、Hystrix介绍

防雪崩利器Hystrix,基于Netflix对应的Hystrix。

Hystrix功能: 服务降级,服务熔断,依赖隔离, 监控(Hystrix Dashboard)

Hystrix是线程池隔离,自动实现了依赖隔离。

1、服务降级

优先核心服务,非核心服务不可用或弱可用

通过HystrixCommand注解指定

fallbackMethod(回退函数)中具体实现降级逻辑

三、Order工程中使用RestTemplate调用Product中的方法

 @GetMapping("/getProductInfoList")
    public  String getProductInfoList(){
        RestTemplate restTemplate = new RestTemplate();
       return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class);

    }

  返回结果

 当Product服务关闭后,再次访问,将返回连接拒绝

然后使用Hystrix进行服务降级

在Order服务中增加引用

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

        <dependency>
            <groupId>com.netflix.hystrix</groupId>
            <artifactId>hystrix-javanica</artifactId>
            <version>RELEASE</version>
        </dependency>

  

增加注解EnableCircuitBreaker

或者用@SpringCloudApplication替换另外三个注解

 

增加HystrixController 类

@RestController
public class HystrixController {



    @HystrixCommand(fallbackMethod = "fallback")
    @GetMapping("/getProductInfoList")
    public  String getProductInfoList(){
        RestTemplate restTemplate = new RestTemplate();
       return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class);

    }

    private String fallback(){
        return "太拥挤了,请稍后再试~~";
    }
}

  

再次调用,Product服务此时是关闭的。

说明服务降级了。

上面是单独写了一个fallback方法,那如何统一处理呢?

调用接口:

四、超时设置

超时时间设置为3秒
   @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") //超时时间设置为3秒
    })
    @GetMapping("/getProductInfoList")
    public  String getProductInfoList(){
        RestTemplate restTemplate = new RestTemplate();
       return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class);

    }

  

五、熔断

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {



    //@HystrixCommand(fallbackMethod = "fallback")
    //2、超时设置
    /*@HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") //超时时间设置为3秒
    })*/
    //3.
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),//设置熔断
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60")
    })
    @GetMapping("/getProductInfoList")
    public  String getProductInfoList(@RequestParam("number") Integer number){
        if(number % 2 == 0){
            return  "success";
        }
        RestTemplate restTemplate = new RestTemplate();
       return restTemplate.postForObject("http://127.0.0.1:8091/product/listForOrder", Arrays.asList("157875196366160022"),String.class);

    }

    private String fallback(){
        return "太拥挤了,请稍后再试~~";
    }


    private String defaultFallback(){
        return "默认提示:太拥挤了,请稍后再试~~";
    }
}

  

Product工程中的方法

    @PostMapping("/listForOrder")
    public List<ProductInfo> listForOrder(@RequestBody List<String> productIdList){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return  productService.findList(productIdList);
    }

  

调用:

number为2时成功返回

number为1时,触发熔断

熔断:

不停的调用 http://localhost:8081/getProductInfoList?number=1 。

然后调用http://localhost:8081/getProductInfoList?number=2, 也出现拥挤提示

然后再次调用http://localhost:8081/getProductInfoList?number=2 就正常了。

原文地址:https://www.cnblogs.com/linlf03/p/10392738.html