zoukankan      html  css  js  c++  java
  • SpringCloud之Hystrix断路器[六]

    SpringCloud之Hystrix断路器

    为什么要使用断路器,断路器的作用

      在分布式项目中,我们可能有很多很复杂的项目,如果没有断路器,假如一台电脑宕掉了,面临的将是雪崩效应,整个项目的瘫痪,但如果有一些合理的保护措施的话,那就另当别论了,就像我们自己家用电,电闸跳了,也是我们自己家停电,而不会导致整个地方的停电,断路器就起到了这个作用。(及时保护,及时通知,遇到解决不了的问题及时跳闸,使我们的负载均衡能够连接别的集群)

      

    Hystrix简单的小案例

      1.导入Gradle依赖

      //熔断器
        compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-hystrix'
        compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-hystrix-dashboard'  2.

      2.在SpringCloud服务生产者Service中 哪里需要熔断,哪里加入注解

     @HystrixCommand(fallbackMethod = "error")
        public List<DtoClasses> selectAll() {
            System.out.println(classesMapper);
            return ClassesEntityToDtoClasses.dtoTools(classesMapper.findByClassesEntity());
        }
        @GetMapping("/error")
        
        public List<DtoClasses> error() {
           List<DtoClasses> list=new ArrayList<>();
            list.get(0).setClssName("出错了");
            list.get(0).setId(-1);
            return list;
        }

    以上代码,顾名思义,加入@Hystrixcommand注解 如果这个方法出现任何错误,或者死循环等等可能让服务止步不前的状况时,它将进入降级方法

    error来进行降级处理,以免整个服务宕掉。这仅仅是一种方法,隔离分为 线程隔离,信号量的隔离。

      3.在启动类上添加注解

      @EnableCircuitBreaker    启动熔断器
      @EnableHystrixDashboard  监视熔断器

    @MapperScan("com.zheng.mapper")
    @EnableEurekaServer
    @SpringBootApplication
    @EnableCircuitBreaker
    @EnableHystrixDashboard
    public class ServiceApplication {
        public static void main(String[] args) {
            SpringApplication.run(ServiceApplication.class,args);
        }
    }

    具体代码:

    全部代码地址:https://github.com/zgc456/SpringCloud-Summary

    里面包含ribbon zuul feign hystrix 等等只看Hystrix即可

    
    
    
  • 相关阅读:
    terminal
    变量提升、函数提升
    ssh传输文件
    mocha测试框架
    npm-run 自动化
    webpack
    浅析babel
    构建工具gulp
    C++中TRACE宏及assert()函数的使用
    memcpy函数-C语言
  • 原文地址:https://www.cnblogs.com/zheng1/p/8574050.html
Copyright © 2011-2022 走看看