zoukankan      html  css  js  c++  java
  • 微服务深入浅出(6)-- 熔断器Hystrix

    概念

    在分布式系统中,一种不可避免的情况就是某些服务会出现故障,导致依赖他们的其他服务出现远程调度的线程问题(雪崩效应)。而Hystrix提供的熔断器,通过隔离服务的访问点,能阻止这种分布式系统中出现的联动故障,并提供故障的解决方案,从而提高了整个分布式系统的弹性。

    设计原则

    1、防止单个服务的故障耗尽整个服务的servlet容器的线程资源

    2、快速失败机制,如果某个服务出现故障则调用该服务的请求快速失败,而不是线程等待

    3、提供回退方案

    4、使用熔断机制,防止故障扩散到其他服务

    5、提供熔断器的监控组件Hystrix Dashboard,实时监控熔断器状态。提供Turbine聚合多喝Dashboard

    工作机制

    1、当某个API接口失败的次数在一定时间内小鱼设定的阀值时,熔断器处于关闭状态,该API正常提供服务。

    2、当失败次数大于设定的阀值的时候,Hystrix判定改API接口出现故障,打开熔断器,这时候该请求API接口会执行快速失败的逻辑(fallback回退的逻辑)而不执行业务逻辑,请求的线程不会处于阻塞状态。

    3、处于打开状态的熔断器,一段时间后会处于半打开状态,并将一定数量的请求执行业务逻辑,剩余的请求会执行快速失败。若执行的业务逻辑请求失败,则熔断器继续打开,若成功则熔断器关闭。

    RestTemplate+Ribbon上使用熔断器

    1、引入依赖starter-hystrix后,在启动类加上@EnableHystrix注解就可以开启熔断器功能

    2、在方法上添加@HystrixCommand注解,设置fallBackMethod属性,指定fallback回调,最好返回一些静态字符串,不需要处理发杂逻辑什么的,这样可以方便执行快速失败,释放线程资源。

    Feign上使用熔断器

    1、配置文件启用

    feign:
      hystrix:
        enabled: true

    2、定义快速失败处理类

    public class Hystrix implements  EurekaClientFeign {
        @Override
        public String sayHiFromClientEureka(String name) {
            return "hi, " + name + ",  sorry error!";
        }
    }

    3、@FeignClient注解的fallback配置加上快速失败的处理类

    @FeignClient(value = "hi-service", configuration = FeignConfig.class, fallback = Hystrix.class)
    public interface EurekaClientFeign {
    
        @GetMapping(value = "/hi")
        String sayHiFromClientEureka(@RequestParam(value = "name") String name);
    
    }

    Hystrix Dashboard监控熔断器状态

    本案例使用Feign讲解

    1、添加依赖:

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    2、启动类添加@EnableHystrixDashboard注解和ServletRegistrationBean配置

    @SpringBootApplication
    @EnableEurekaClient
    @EnableFeignClients
    @EnableHystrixDashboard
    @EnableCircuitBreaker
    public class DemoApplication {

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

    @Bean
    public ServletRegistrationBean getServlet() {
    HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
    registrationBean.setLoadOnStartup(1);
    registrationBean.addUrlMappings("/hystrix.stream");
    registrationBean.setName("HystrixMetricsStreamServlet");
    return registrationBean;
    }
    }

    3、访问监控后台

    http://localhost:8765/hystrix.stream

    http://localhost:8765/hystrix

    可以看到单体应用的监控

    使用Turbine聚合监控

    默认的集群监控:通过URL:http://turbine-hostname:port/turbine.stream开启,实现对默认集群的监控。

    指定的集群监控:通过URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]

    单体应用的监控:通过URL:http://hystrix-app:port/hystrix.stream开启,实现对具体某个服务实例的监控。

  • 相关阅读:
    SQL语句的优化(转载)
    使用经纬度得到位置Geocorder
    android自带下拉刷新SwipeRefreshLayout
    线程池ScheduledThreadPool
    线程池SingleThreadPool
    线程池CachedThreadPool
    线程池FixedThreadPool
    线程池ThreadPoolExecutor
    Bitmap缩放(三)
    Bitmap缩放(二)
  • 原文地址:https://www.cnblogs.com/ijavanese/p/9193551.html
Copyright © 2011-2022 走看看