Spring Cloud 学习笔记(四)-Spring Cloud Hystrix

  • Spring Cloud 学习笔记(四)-Spring Cloud Hystrix

由于前一阵子项目的原因,今天才继续弄上,今天想学习一下Hystrix组件
这个组件还挺抽象的,最开始我一直没太明白,看了很多其他人的文章去
大概的了解了一下。相关的文章可以参考看过的链接:Hystrix-开源容错系统

** 搭建过程

  1. 在原有工程基础上,增加了依赖spring-cloud-starter-netflix-hystrix,这里提一下官方的文档少了一层,坑死

    <dependency>
    	<groupId>org.springframework.cloud</groupId>
    	<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    
  2. 修改原有的类, 增加一个方法专门用以测试, 现在类上方添加@EnableCircuitBreaker注解

    public static int time = 1;
    
    @HystrixCommand(fallbackMethod = "error")
    @RequestMapping("/test-hystrix")
    public String testHystrix() {
    	try {
    		TimeUnit.SECONDS.sleep(time++);
    	} catch (InterruptedException e) {
    		// TODO Auto-generated catch block
    		e.printStackTrace();
    	}
    	return "Hystrix not work";
    }
    
    public String error() {
    	return "Now hystrix is working.";
    }
    
  3. 修改配置文件

    # Hystrix
    hystrix:
      stream:
    	maxConcurrentConnections: 2
      command:
    	default:
    	  execution:
    		isolation:
    		  thread:
    			timeoutInMilliseconds: 10000
    
  4. 访问服务,并且重复发送几次,使之达到超过线程等待时间的错误,发现很神奇的熔断器就生效了

由于这两天也还是比较忙,今天就还是先弄这么一个组件,后续有时间再继续弄~

posted @ 2018-09-20 16:25  A_Little_Dream  阅读(118)  评论(0编辑  收藏  举报