简介

在微服务架构中,微服务之间的依赖关系错综复杂,难免的某些服务会出现故障,导致服务调用方出现远程调度的线程阻塞。在高负载的场景下,如果不做任何处理,可能会引起级联故障,导致服务调用方的资源耗尽甚至整个系统奔溃。Hystrix是一个由Netflix开源的一个延迟和容错库,它通过添加延迟容忍和容错逻辑来帮助控制这些微服务之间的交互。Hystrix通过隔离服务之间的访问点、停止跨服务的级联故障并提供回退选项来实现这一点,所有这些选项都提高了系统的总体弹性。

项目介绍

  1. sc-parent,父模块(请参照SpringCloud学习笔记(1):Eureka注册中心)
  2. sc-eureka,注册中心(请参照SpringCloud学习笔记(1):Eureka注册中心)
  3. sc-provider,提供者(请参照SpringCloud学习笔记(1):Eureka注册中心)
  4. sc-consumer-hystrix-ribbon,使用Hystrix+Ribbon的消费者
  5. sc-consumer-hystrix-feign,使用Hystrix+Feign的消费者

在Ribbon上使用Hystrix

1.在父模块下创建子模块项目sc-consumer-hystrix-ribbon,pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cf</groupId>
<artifactId>sc-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>sc-consumer-hystrix-ribbon</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
</project>

2.创建启动类consumer.ConsumerApplication:

package consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableCircuitBreaker
public class ConsumerApplication { public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
} //为RestTemplate整合Ribbon,使其具备负载均衡的能力
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}

3.创建调用提供者服务的Controller:consumer.controller.ConsumerController

package consumer.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate; @HystrixCommand(fallbackMethod="getBookListFallBack")
@GetMapping("/getBookList")
public String getBookList(){
return restTemplate.getForObject("http://sc-provider/book/list", String.class);
} public String getBookListFallBack(){
return "[\"Java入门到放弃\"]";
}
}

@HystrixCommand:表示将getBookList方法作为hystrix命令调用的方法。

fallbackMethod:指定处理回退逻辑的方法,这里是getBookListFallBack方法,当getBookList方法跑出异常时将会调用getBookListFallBack方法。

注意:回退方法应该与作为hystrix命令调用的方法具有相同的签名。

4.创建application.yml:

server:
port: 8083 spring:
application:
name: sc-consumer-hystrix-ribbon eureka:
client:
registerWithEureka: false
serviceUrl:
defaultZone: http://localhost:8080/eureka/

5.测试

依次启动注册中心sc-eureka、提供者sc-provider、消费者sc-consumer-hystrix-ribbon,并访问http://localhost:8083/getBookList,结果显示如下:

这是提供者正常返回的值,接下来将提供者sc-provider关闭,再次访问http://localhost:8083/getBookList,结果显示如下:

因为将提供者sc-provider关闭后,消费者再访问提供者时会报错,Hystrix捕获异常后会直接调用回退方法也就是getBookListFallBack方法。

在Feign上使用Hystrix

1.在父模块下创建子模块项目sc-consumer-hystrix-feign,pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.cf</groupId>
<artifactId>sc-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>sc-consumer-hystrix-feign</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
</project>

2.创建启动类feign.FeignApplication:

package feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}

3.创建Feign声明式接口:feign.inter.BookService

package feign.inter;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping; import feign.fallback.BookFallBack; @FeignClient(value="sc-provider", fallbackFactory=BookFallBack.class)
public interface BookService { @GetMapping("/book/list")
public String getBookList();
}

@FeignClient注解中的fallbackFactory属性是指定的Feign客户端界面定义回退工厂。

4.创建调用提供者服务的Controller:

package feign.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import feign.inter.BookService; @RequestMapping("/feign")
@RestController
public class FeignController {
@Autowired
private BookService bookService; @GetMapping("/getBookList")
public String getBookList(){
return bookService.getBookList();
}
}

5.创建application.yml:

server:
port: 8084 spring:
application:
name: sc-consumer-hystrix-feign eureka:
client:
registerWithEureka: false
serviceUrl:
defaultZone: http://localhost:8080/eureka/ feign:
hystrix:
enabled: true #开启hystrix支持

6.创建回退工厂类:

package feign.fallback;
import org.springframework.stereotype.Component; import feign.hystrix.FallbackFactory;
import feign.inter.BookService; @Component
public class BookFallBack implements FallbackFactory<BookService>{
@Override
public BookService create(Throwable cause) {
return new BookService() {
@Override
public String getBookList() {
return "[\"Java入门到放弃\"]";
}
};
}
}

create方法返回一个回退实例,回退实例为Feign声明式接口BookService的实现类,提供了与BookService相对应的回退方法,BookService接口调用失败时将会调用该实现类中的回退方法。

7.测试:

依次启动注册中心sc-eureka、提供者sc-provider、消费者sc-consumer-hystrix-feign,并访问http://localhost:8084/feign/getBookList,结果显示如下:

这是提供者正常返回的值,接下来将提供者sc-provider关闭,再次访问http://localhost:8084/feign/getBookList,结果显示如下:

8.查看回退原因

修改回退工厂类BookFallBack:

@Component
public class BookFallBack implements FallbackFactory<BookService>{
@Override
public BookService create(Throwable cause) {
return new BookService() {
@Override
public String getBookList() {
//将回退原因输出到控制台
cause.printStackTrace(System.out);
return "[\"Java入门到放弃\"]";
}
};
}
}

依次启动注册中心sc-eureka、消费者sc-consumer-hystrix-feign,并访问http://localhost:8084/feign/getBookList,控制台输出:

com.netflix.hystrix.exception.HystrixTimeoutException
at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator$1.run(AbstractCommand.java:1142)
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.call(HystrixContextRunnable.java:41)
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable$1.call(HystrixContextRunnable.java:37)
at com.netflix.hystrix.strategy.concurrency.HystrixContextRunnable.run(HystrixContextRunnable.java:57)
at com.netflix.hystrix.AbstractCommand$HystrixObservableTimeoutOperator$2.tick(AbstractCommand.java:1159)
......

SpringCloud学习笔记(4):Hystrix容错机制的更多相关文章

  1. SpringCloud学习笔记(3)——Hystrix

    参考Spring Cloud官方文档第13.14.15章 13. Circuit Breaker: Hystrix Clients Netflix提供了一个叫Hystrix的类库,它实现了断路器模式. ...

  2. SpringCloud学习笔记(5):Hystrix Dashboard可视化监控数据

    简介 上篇文章中讲了使用Hystrix实现容错,除此之外,Hystrix还提供了近乎实时的监控.本文将介绍如何进行服务监控以及使用Hystrix Dashboard来让监控数据图形化. 项目介绍 sc ...

  3. SpringCloud学习笔记:服务支撑组件

    SpringCloud学习笔记:服务支撑组件 服务支撑组件 在微服务的演进过程中,为了最大化利用微服务的优势,保障系统的高可用性,需要通过一些服务支撑组件来协助服务间有效的协作.各个服务支撑组件的原理 ...

  4. SpringCloud学习笔记(3):使用Feign实现声明式服务调用

    简介 Feign是一个声明式的Web Service客户端,它简化了Web服务客户端的编写操作,相对于Ribbon+RestTemplate的方式,开发者只需通过简单的接口和注解来调用HTTP API ...

  5. Java虚拟机学习笔记——JVM垃圾回收机制

    Java虚拟机学习笔记——JVM垃圾回收机制 Java垃圾回收基于虚拟机的自动内存管理机制,我们不需要为每一个对象进行释放内存,不容易发生内存泄漏和内存溢出问题. 但是自动内存管理机制不是万能药,我们 ...

  6. Android:日常学习笔记(9)———探究广播机制

    Android:日常学习笔记(9)———探究广播机制 引入广播机制 Andorid广播机制 广播是任何应用均可接收的消息.系统将针对系统事件(例如:系统启动或设备开始充电时)传递各种广播.通过将 In ...

  7. SpringCloud学习笔记(2):使用Ribbon负载均衡

    简介 Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡工具,在注册中心对Ribbon客户端进行注册后,Ribbon可以基于某种负载均衡算法,如轮询(默认 ...

  8. SpringCloud学习笔记(6):使用Zuul构建服务网关

    简介 Zuul是Netflix提供的一个开源的API网关服务器,SpringCloud对Zuul进行了整合和增强.服务网关Zuul聚合了所有微服务接口,并统一对外暴露,外部客户端只需与服务网关交互即可 ...

  9. SpringCloud学习笔记(7):使用Spring Cloud Config配置中心

    简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...

随机推荐

  1. Web 字体 font-family 再探秘

    之前写过一篇关于Web字体简介及使用技巧的文章: 你该知道的字体 font-family. 该篇文章基本没有太多移动端的字体选择及分析.并且过了这么久,如今的 Web 字体又有了一些新的东西,遂有此文 ...

  2. webgl(three.js)实现室内定位,楼宇bim、实时定位三维可视化解决方案

    (写在前面,谈谈物联网展会)上次深圳会展中心举行物联网展会,到了展会一看,80%以上的物联网应用都是在搞RFID,室内定位,我一度怀疑物联网落地方案的方向局限性与市场导向,后来多方面了解才明白,展会上 ...

  3. hadoop开启Service Level Authorization 服务级认证-SIMPLE认证-过程中遇到的坑

    背景描述: 最近在进行安全扫描的时候,说hadoop存在漏洞,Hadoop 未授权访问[原理扫描],然后就参考官方文档及一些资料,在测试环境中进行了开启,中间就遇到了很多的坑,或者说自己没有想明白的问 ...

  4. 8.14 day32 TCP服务端并发 GIL解释器锁 python多线程是否有用 死锁与递归锁 信号量event事件线程q

    TCP服务端支持并发 解决方式:开多线程 服务端 基础版 import socket """ 服务端 1.要有固定的IP和PORT 2.24小时不间断提供服务 3.能够支 ...

  5. CAD2015 C#二次开发 字体变形

    开发环境:VS2012问题描述:一个简单的WinForm窗口,一个群组控件和一个Label,都是微软雅黑12pxCAD2015下,看起来却不一样,一个明显细得多. CAD2014下,无此问题.实验了C ...

  6. 危险的Hystrix线程池

    本文介绍Hystrix线程池的工作原理和参数配置,指出存在的问题并提供规避方案,阅读本文需要对Hystrix有一定的了解. 文本讨论的内容,基于hystrix 1.5.18: <dependen ...

  7. 图解Java数据结构之队列

    本篇文章,将对队列进行一个深入的解析. 使用场景 队列在日常生活中十分常见,例如:银行排队办理业务.食堂排队打饭等等,这些都是队列的应用.那么队列有什么特点呢? 我们知道排队的原则就是先来后到,排在前 ...

  8. 常用Http status code 如何记

    一直记不住http常用的status code,最近思考可以这样想.http无非就是客户端和服务端之间请求嘛.结果么要么成功,要么失败. 成功了,可以提示信息 -- Informational 1xx ...

  9. 最近学习了限流与RateLimiter

    前言 分布式环境下应对高并发保证服务稳定几招,按照个人理解,优先级从高到低分别为缓存.限流.降级.熔断,每招都有它的作用,本文重点就讲讲限流这部分. 坦白讲,其实上面的说法也不准确,因为服务降级.熔断 ...

  10. macbook 安装redis流程及问题总结

    Mac安装redis流程和总结 一.redis安装流程: 1.进入redis官网-->点击download-->选择稳定版本(stable)-->点击Download即可. 2.将下 ...