阅读背景:

15 Spring Cloud微服务 Zuul服务容错和Hystrix_张金玉的专栏

来源:互联网 
在微服务架构中,通常会有多个服务层调用,如果某个服务不可用,导致级联故障,整个系统不可用。
A ->B ->C    B->C不可用 。雪崩效应是极度避免的。
Spring Cloud Hystrix
。防止雪崩利器
。基于Netflix对应的Hystrix

Spring Cloud Hystrix:(1)熔断机制(2)Hystrix Dashboard
。服务降级:双11,和秒杀系统中提示网络挂了等,区分业务,优先核心服务,非核心服务不可用或弱可用
           通过HystrixCommand注解指定
           fallbackMethod(回退函数)中具体实现降级逻辑

。服务熔断
。依赖隔离
。监控(Hystrix Dashboard)

能够调用(没有实现服务降级)
order->server->controller->HystrixController.java
@RestController
public class HystrixController {
    @GetMapping("/getProductInfoList")
    public string getProductInfoList(){
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject("https://127.0.0.1:8005/product/listForOrder",
                    Arrays.asList("1577772823823"),
                    String.class
                    );
    }
}

实现服务降级
(1)引入依赖
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
(2)在启动类上加一个注解
@EnableFeignClients(basePackages = "com.imooc.product.client")
//@SpringBootApplication  <----------1
//@EnableDiscoveryClient  <----------2
//@EnableCircuitBreaker   <----------3    如果需要加入1 2 3 只需要加入@SpringCloudApplication就可以了
@SpringCloudApplication
@ComponentScan(basePackages = "com.imooc")
public class OrderApplication {

   public static void main(String[] args) {
      SpringApplication.run(OrderApplication.class, args);
   }
}

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

   //超时配置
// @HystrixCommand(commandProperties = {
//       @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
// })

// @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"),  //错误率
// })
   @HystrixCommand        <----------------------------
   @GetMapping("/getProductInfoList")
   public String getProductInfoList(@RequestParam("number") Integer number) {
      if (number % 2 == 0) {
         return "success";
      }

      RestTemplate restTemplate = new RestTemplate();
      return restTemplate.postForObject("https://127.0.0.1:8005/product/listForOrder",
            Arrays.asList("157875196366160022"),
            String.class);

//    throw new RuntimeException("发送异常了");
   }

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

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

解析:
@DefaultProperties(defaultFallback = "defaultFallback") 默认提示服务降级
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),              //设置熔断
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),    //请求数达到后才计算
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), //休眠时间窗
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),  //错误率



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

熔断主要实现下面四个注解
@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"),  //错误率
})

测试
   @HystrixCommand    <-------------------加入注解
   @GetMapping("/getProductInfoList")
   public String getProductInfoList(@RequestParam("number") Integer number) {
      if (number % 2 == 0) {
         return "success";
      }
偶数表示成功,否则2秒请求fallback
https://localhost:8081/getProductInfoList?number=2  //成功,不停的刷新60%会触发熔断,断开服务,不支持访问
https://localhost:8081/getProductInfoList?number=1  //触发降级

Circuit Breaker:断路器,及时切断故障,切断主逻辑调用(电路保险丝-原理)


第一种:重试
第二种:断路器

上面用四行注解来实现
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"),              //设置熔断
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),    //请求数达到后才计算
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), //休眠时间窗
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),  //错误率
可以用下面的配置来实现
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
          #2秒
            timeoutInMilliseconds: 3000
为一个方法单独设置超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
          #2秒
            timeoutInMilliseconds: 1000
    getProductInfoList:
      execution:
        isolation:
          thread:
          #2秒
            timeoutInMilliseconds: 3000在微服务架构中,通常会有多个服务层调用,如果某个服务不可用,导致级联故障,整个系统不可用。
A



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: