SpringCloud无废话入门04:Hystrix熔断器及监控

1.断路器(Circuit Breaker)模式html

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

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

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

        它描述了两个状态:close、open,以及一个动做:trip,浏览器

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

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

        Trip动做: 若是在close状态下, supplier持续超时报错, 熔断器就进行trip,而后熔断器将状态从close进入open。分布式

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

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

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

相关文章
相关标签/搜索