Hystrix

1.服务容错和Hystrix

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

服务降级:优先核心服务,非核心服务不可用或弱可用。通过HystrixCommand注解指定,fallbackMethod(回退函数)中具体实现降级逻辑。
服务熔断:切断故障电路
依赖隔离:线程池隔离
监控(Hystrix DashBoard)

2.触发降级

1.导入依赖


    org.springframework.cloud
    spring-cloud-netflix-hystrix


    com.netflix.hystrix
    hystrix-javanica

2.启动类上加注解

@EnableCircuitBreaker

3.书写代码

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {
    @HystrixCommand(fallbackMethod = "fallback")
    @GetMapping("/getProductInfoList")
    public String getProductInfoList(@RequestParam("number") Integer number) {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject("http://localhost:8081/product/listForOrder", Arrays.asList("123456"), String.class);
    }
    private String fallback(){
        return "太拥挤了,请稍后再试!";
    }
}

3.超时设置

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {
    //超时时间
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    @GetMapping("/getProductInfoList")
    public String getProductInfoList(@RequestParam("number") Integer number) {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject("http://localhost:8081/product/listForOrder", Arrays.asList("123456"), String.class);
    }
    private String defaultFallback(){
        return "太拥挤了,请稍后再试!defaultFallback";
    }
}

4.探讨断路器模式

@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {
//最小请求数,时钟窗口,错误率
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "circuitBreaker.enabled",value = "true"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "3"),
            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")
    })
    @GetMapping("/getProductInfoList")
    public String getProductInfoList(@RequestParam("number") Integer number) {
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject("http://localhost:8081/product/listForOrder", Arrays.asList("123456"), String.class);
    }
    private String defaultFallback(){
        return "太拥挤了,请稍后再试!defaultFallback";
    }
}

5.使用配置项

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000
    getProductInfoList:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000

6.feign-hystrix的使用

feign:
  hystrix:
    enabled: true
@FeignClient(name = "product",fallback = ProductClient.ProductClientFallback.class)
public interface ProductClient {
    @PostMapping("/product/listForOrder")
    List listForOrder(@RequestBody List productIdList);
    @Component
    class ProductClientFallback implements ProductClient{
        @Override
        public List listForOrder(List productIdList) {
            return null;
        }
    }
}

7.hystriy-dashboard

1.导入依赖


    org.springframework.cloud
    spring-cloud-starter-netflix-hystrix-dashboard


    org.springframework.boot
    spring-boot-starter-actuator

2.主类上加注解

@EnableHystrixDashboard

3.访问/hystrix.stream

你可能感兴趣的:(springcloud)