zoukankan      html  css  js  c++  java
  • SpringCloud无废话入门04:Hystrix熔断器及监控

    1.断路器(Circuit Breaker)模式

            在上文中,我们人为停掉了一个provider,在实际的生产环境中,因为意外某个服务down掉,甚至某一层服务down掉也是会是有发生的。一旦发生这种情况,我们需要将损失减少到最低限度。

            那怎么减少损失。在电力系统中,如果某个电器发生过载等问题,该段电路的继电器中的保险丝就会熔断。在分布式系统中,我们也可以设计这样的模式,并为它赋有专有名词:断路器(Circuit Breaker)模式。

            其基本模式在Martin Fowler的一篇文章中进行过专有描述,其大概的架构图如下:

            它描述了两个状态:close、open,以及一个动作:trip,

            close状态下, client通过熔断器向supplier发起的服务请求, supplier的返回值经过熔断器返回client。

            open状态下,c client通过熔断器向supplier发起的服务请求,熔断器直接返回值给client, client和supplier之间被隔断。

            Trip动作: 如果在close状态下, supplier持续超时报错, 熔断器就进行trip,然后熔断器将状态从close进入open。

            除了基本模式,Martin Fowler还描述了一种扩展模式。即熔断器定期探测supplier的服务的状态, 一旦服务恢复, 就将状态设置回close,并且熔断器进行重试时的状态为half-open状态。

            对于熔断器模式,不再详细展开。接下来我们看看在SpringCloud的系统中,熔断器是怎么提供的。

    2.Hystrix熔断与服务降级

            SpringCloud Netflix实现的熔断器叫Hystrix。我们首先看看微服务架构下, 浏览器如何访问后台服务,

            然后,服务异常情况下,Hystrix通过提供Fallback进行了熔断的保护,

            Hystrix设定的熔断阈值是:5秒之内发生20次调用失败,然后熔断器就会被处于open状态, 在熔断状态下,服务不再被调用, Hystrix提供一个Fallback回调。当然,这个回调可以由我们实现。

            Fallback是一种降级操作,这种行为我们定义为:服务降级。 

    3.实现

            首先,在服务接口的声明中,加入FeignClient注解的fallback属性,

    package com.zuikc;

    import org.springframework.cloud.openfeign.FeignClient;

    import org.springframework.web.bind.annotation.RequestMapping;

    @FeignClient(value = "hello-service",fallback = FeignFallBack.class)

    public interface HelloService {

        //服务中方法的映射路径

        @RequestMapping("/hello")

        String hello();

    }

            注意,假设这里我们没有使用FeignClient注解,而是原先那种url方式来启动LB的话,那么,那么就需要在具体的service中直接使用注解:@HystrixCommand(fallbackMethod = "helloFallBack"),来实现熔断和服务降级。helloFallBack可以是具体服务类中的一个指定的方法。

            接下来,然后接着说FeignClient,实现该FeignFallBack类,

    package com.zuikc;

    import org.springframework.stereotype.Component;

    /**

     * @ClassName FeignFallBack

     * @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”

     * @Author 码农星球

     **/

    @Component

    public class FeignFallBack implements HelloService {

        //实现的方法是服务调用的降级方法

        @Override

        public String hello() {

            return "error";

        }

    }

            这个类实现了HelloService。如果provider调用失败,就会执行该类的实现。

            最后,还得配置,

    server:

      port: 9291

    spring:

      application:

        name: Ribbon-Consumer

    eureka:

      client:

        service-url:

          defaultZone: http://localhost:9091/eureka/

    #providers这个是自己命名的,ribbon,listOfServer这两个是规定的

    providers:

      ribbon:

        listOfServers: http://localhost:9191/eureka,http://localhost:9192/eureka

    feign:

      hystrix:

        enabled: true

            粗体部分就是新加的配置。Feign默认禁用熔断器,所以我们需要配置为enabled。

            经过以上功能的改进后,停止provider吧,看看结果是什么呢?

    4.监控

            SpringCloud做的比dubbo好的一点就是其监控非常的明了。我们可以用hystrix的监控方便的看到熔断器的数据指标。

            要想监控这些数据,我们还得继续改造我们的项目。

            首先,在provider的pom中加入依赖:

            <dependency>

                <groupId>org.springframework.boot</groupId>

                <artifactId>spring-boot-actuator</artifactId>

            </dependency>

            其次,在我们ribbon项目中,加入依赖:

            <dependency>

                <groupId>com.netflix.hystrix</groupId>

                <artifactId>hystrix-javanica</artifactId>

                <version>RELEASE</version>

            </dependency>

            <dependency>

                <groupId>org.springframework.cloud</groupId>

                <artifactId>spring-cloud-netflix-hystrix-dashboard</artifactId>

            </dependency>

            第一个依赖表示我们得有监控,第二个依赖表示我们得有一个仪表盘。

            接着,让我们在ribbon的启动类中加入一个servlet,如下:

    package com.zuikc;

    import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import org.springframework.boot.web.servlet.ServletRegistrationBean;

    import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

    import org.springframework.cloud.client.loadbalancer.LoadBalanced;

    import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

    import org.springframework.cloud.openfeign.EnableFeignClients;

    import org.springframework.context.annotation.Bean;

    import org.springframework.web.client.RestTemplate;

    /**

     * @ClassName ServiceRibbonApplication

     * @Description 我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系“码农星球”

     * @Author 码农星球

     **/

    @SpringBootApplication

    @EnableDiscoveryClient

    @EnableFeignClients

    @EnableCircuitBreaker

    @EnableHystrixDashboard

    public class ServiceRibbonApplication {

        public static void main(String[] args) {

            SpringApplication.run(ServiceRibbonApplication.class, args);

        }

        @Bean

        @LoadBalanced

        RestTemplate restTemplate() {

            return new RestTemplate();

        }

        @Bean

        public ServletRegistrationBean getServlet(){

            HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();

            ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);

            registrationBean.setLoadOnStartup(1);

            registrationBean.addUrlMappings("/actuator/hystrix.stream");

            registrationBean.setName("HystrixMetricsStreamServlet");

            return registrationBean;

        }

    }

            在这个启动类中,除了这个servlet要注册之外,就是加入

            @EnableCircuitBreaker

            @EnableHystrixDashboard

            这两个注解了。注意,由于hystrix默认就是支持熔断器的,所以@EnableCircuitBreaker注解之前我们并没有加。但是,这里要使用监控了,就必须要加入这个注解了。

            以上都操作完毕。来打开:http://localhost:9291/hystrix

            然后在出来的如下界面中,首先填入servlet的地址,其次点击下面的monitor按钮,

            最终进入到如下这个监控仪表盘界面。每一次对provider的调用,都会清清楚楚被仪表盘呈现出来。

            Ok。本章告一段落。

            感谢关注“码农星球”。本文版权属于“码农星球”。我们提供咨询和培训服务,关于本文有任何困惑,请关注并联系我们。

    本文的参考1:

    https://martinfowler.com/bliki/CircuitBreaker.html

    本文的参考2:

    http://cloud.spring.io/spring-cloud-netflix/multi/multi__circuit_breaker_hystrix_clients.html

  • 相关阅读:
    51 Nod 1013 3的幂的和 矩阵链乘法||逆元+快速幂
    poj3580 序列之王 fhqtreap
    bzoj1503: [NOI2004]郁闷的出纳员 fhqtreap版
    bzoj1251: 序列终结者 fhqtreap写法
    bzoj4864: [BeiJing 2017 Wc]神秘物质
    bzoj3786 星际探索 splay dfs序
    bzoj1861 书架 splay版
    bzoj1503 郁闷的出纳员 splay版
    网络转载:局域网安全:解决ARP攻击的方法和原理
    黑客的故事
  • 原文地址:https://www.cnblogs.com/luminji/p/10615653.html
Copyright © 2011-2022 走看看