SpringCloud教程第四篇:Hystrix

在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。
为了解决这个问题,业界提出了断路器模型。

一、hystrix简介

Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.
—-摘自官网

Netflix开源了Hystrix组件,实现了断路器模式,SpringCloud对这一组件进行了整合。 在微服务架构中,一个请求需要调用多个服务是非常常见的。

较底层的服务如果出现故障,会导致连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystrix 是5秒20次) 断路器将会被打开。

断路打开后,可用避免连锁故障,fallback方法可以直接返回一个固定值。

Hystrix被设计的目标是:

  • 对通过第三方客户端库访问的依赖项(通常是通过网络)的延迟和故障进行保护和控制。
  • 在复杂的分布式系统中阻止级联故障。
  • 快速失败,快速恢复。
  • 回退,尽可能优雅地降级。
  • 启用近实时监控、警报和操作控制。

二、准备工作

这篇文章基于上一篇文章的工程,首先启动上一篇文章的工程,启动eureka-server 工程;启动eureka-client工程,它的端口为8762。

三、在ribbon中使用断路器

1. 改造service-ribbon工程,首先在pom.xml中加入hystrix的依赖:

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

2. 在启动类上加@EnableHystrix注解开启Hystrix,代码如下:

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class ServiceRibbonApplication {

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

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    /**
     *  负载均衡策略:不加这个Bean默认是轮询,RandomRule是随机访问。
     */
	//@Bean
    public IRule ribbonRule() {
        return new RandomRule();
    }

}

3. 改造TestService类,在test方法上加上@HystrixCommand注解。该注解对该方法创建了熔断器的功能,并指定了fallbackMethod熔断方法,熔断方法直接返回了一个字符串,代码如下:

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.utour.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @Description
 * @Author wangxm
 * @Date 2019/10/23 17:16
 **/
@Service
public class TestServiceImpl implements TestService {

    @Autowired
    private RestTemplate restTemplate;

    @Override
    @HystrixCommand(fallbackMethod = "testError")
    public String test() {
        return restTemplate.getForObject("http://EUREKA-CLIENT/test/come", String.class);
    }

    public String testError() {
        return "hystrix fallbackMethod";
    }

}

4. 启动 service-ribbon 工程,访问http://localhost:8764/hh/hi,浏览器显示:

come on:8762

此时关闭 eureka-client 工程,再访问http://localhost:8764/hh/hi,浏览器显示:

hystrix fallbackMethod

这就说明当 eureka-client 工程不可用的时候,service-ribbon调用 eureka-client 的API接口时,会执行快速失败,直接返回一组字符串,而不是等待响应超时,这很好的控制了容器的线程阻塞。

四、在feign中使用断路器

1. Feign是自带断路器的,在D版本的Spring Cloud中,它没有默认打开,需要在配置文件中配置打开它,在配置文件加以下代码:

feign:
  hystrix:
    enabled: true

2. 改造 service-feign 工程,只需要在FeignClient注解中加上fallback的指定类就行了,代码如下:

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @Description
 * @Author wangxm
 * @Date 2019/10/24 11:20
 **/
@FeignClient(value = "eureka-client", fallback = TestFeignHystrix.class)
public interface TestFeign {

    @GetMapping("/test/come")
    String hihi();
    
}

TestFeignHystrix 需要实现TestFeign 接口,并注入到Ioc容器中,代码如下:

import org.springframework.stereotype.Component;

/**
 * @Description
 * @Author wangxm
 * @Date 2019/10/24 21:33
 **/
@Component
public class TestFeignHystrix implements TestFeign {

    @Override
    public String hihi() {
        return "hystrix fallbackMethod";
    }
}

3. 启动service-feign工程,浏览器打开http://localhost:8765feign/test,注意此时eureka-client工程没有启动,浏览器显示:

hystrix fallbackMethod

打开eureka-client工程,再次访问,浏览器显示:

come on:8762

这证明断路器起到作用了。

五、Hystrix Dashboard (断路器:Hystrix 仪表盘)

Hystrix提供了对于微服务调用状态的监控信息,但是需要结合spring-boot-actuator模块一起使用。

Hystrix Dashboard主要用来实时监控Hystrix的各项指标信息。通过Hystrix Dashboard反馈的实时信息,可以帮助我们快速发现系统中存在的问题。

1. 改造service-ribbon工程,引入spring-cloud-starter-hystrix-dashboard的起步依赖,代码如下:

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

2. 在启动类中加入@EnableHystrixDashboard注解,开启hystrixDashboard功能,代码如下:

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableHystrixDashboard
@EnableHystrix
@EnableEurekaClient
@SpringBootApplication
public class ServiceRibbonApplication {

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

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    /**
     *  负载均衡策略:不加这个Bean默认是轮询,RandomRule是随机访问。
     */
	//@Bean
    public IRule ribbonRule() {
        return new RandomRule();
    }

}

3. 启动service-ribbon工程,访问http://localhost:8764/hystrix,出现如下界面:
HystrixDashboard首页
从首页中我们可以看出并没有具体的监控信息,从页面上的内容我们可以知道,Hystrix Dashboard共支持三种不同的监控方式:

  • 默认的集群监控http://turbine-hostname:port/turbine.stream
  • 指定的集群监控http://turbine-hostname:port/turbine.stream?cluster=[clusterName]
  • 单体应用的监控http://hystrix-app:port/actuator/hystrix.stream

页面上的几个参数区域:

  • 最上面的输入框:输入上面所说的三种监控方式的地址,用于访问具体的监控信息页面。
  • Delay:该参数用来控制服务器上轮询监控信息的延迟时间,默认2000毫秒。
  • Title:该参数对应头部标题Hystrix Stream之后的内容,默认会使用具体监控实例的Url。
需要注意的是:在Spring Cloud Finchley 版本以前访问路径是/hystrix.stream,如果是Finchley的话就需要加入上面的配置。因为spring Boot 2.0.x以后的actuator只暴露了info和health2个端点,这里我们把所有端点开放,include: '*'代表开放所有端点,如下:
management:
  endpoints:
    web:
      exposure:
        include: '*'

4. 点击Monitor Stream,就会跳转到具体的监控页面,如下:
监控页面
下面来介绍下图形中各元素的具体含义:

  • 实心圆:共有两种含义。通过颜色的变化代表了实例的健康程度,它的健康度从绿色、黄色、橙色、红色递减。通过圆的大小来代表请求流量的大小,流量越大该实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例。
  • 曲线:记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势。
  • 其他数量指标其他数量指标
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值