在之前的架构的基础上我们会发现,一旦级别低的服务宕了,会导致调用它的服务也挂掉,这样容易产生级联效应(雪崩效应),为了防止这种情况的出现,我引入了Hystrix来处理,先介绍ribbon使用Hystrix

首先引入以来pom.xml:

<!-- Hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>com.netflix.hystrix</groupId>
<artifactId>hystrix-javanica</artifactId>
</dependency>
<!-- spring boot低版本使用上面的,高版本使用下面注释的内容,因为最新的hystrix隶属于netfix下 -->
<!-- <dependency> -->
<!-- <groupId>org.springframework.cloud</groupId> -->
<!-- <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> -->
<!-- </dependency> -->
<!-- <dependency> -->
<!-- <groupId>org.springframework.cloud</groupId> -->
<!-- <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> -->
<!-- </dependency> -->

接着在启动类的上面加入Hystrix的注解@EnableCircuitBreaker

最后在MovieController中加入如下代码

@HystrixCommand(fallbackMethod = "fallbackfindUserByNameEn") //ribbon使用Hystrix
@ApiOperation(value = "查询用户ByName", notes = "查询用户By中文名")//方法说明
@ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Movie.class)})//响应数据说明,可以有多个
@ApiImplicitParam(name = "name", value = "用户名", paramType = "path", required = true, dataType = "String")
@GetMapping(value = "/findUserByName/{name}",produces = { "application/json;charset=UTF-8" })
public User findUserByName(@PathVariable String name) {
return this.restTemplate.getForObject("http://xing-user/user/findByName/"+name, User.class);
}

这里解释一下@HystrixCommand(fallbackMethod = "fallbackfindUserByNameEn")是调用服务异常那就去执行fallbackfindUserByNameEn方法,当然你可能在别的博客里看到有如下的写法:

@HystrixCommand(fallbackMethod = "fallbackfindUserByNameEn",commandProperties = {@HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")}),但是官方文档里面推荐写成我上面的代码中的格式,不要写commandProperties属性,
commandProperties这段属性的意思是调用fallbackfindUserByNameEn方法和执行findUserByNameEn方法在同一个线程中执行,官方不推荐添加这一段,官方文档推荐出现运行时找不到上下文异常的时候再加上这段代码,下面是官方文档的截图

记录一个异常,今天启动movie1的时候出现

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration': Singleton bean creation not allowed while the singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)的异常,找了很久才发现是我开了IDEA,IDEA中我启动了一个项目,导致了端口占用,关闭IDEA中的项目就行。

下面我们介绍Feign中使用Hystrix

参照之前博客中的movie服务中的UserInterface(使用Feign调用user服务的接口类),在这个类的注解中加入标红的代码@FeignClient(name = "xing-user" ,fallback = UserInterfaceFallback.class)//服务名,之后在UserInterface这个java类的下面加一个类

@Component
class UserInterfaceFallback implements UserInterface {
@Override
public User findByNameEn(String nameEn) {
User user = new User();
user.setName("");
user.setNameEn("");
user.setId(0);
return user;
}
当然UserInterfaceFallback这个类也可以是单独写成一个java文件,没有非要写在UserInterface类同一个java文件中。测试成功可以实现断路功能。如果想在Feign中禁用Hystrix可以在yml中加入这个配置即可feign.hystrix.enabled=false
注意:这里提醒一点一定要加@Component这个注解,我看官方文档里面好像没有加,不加的话会有如下异常: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.xing.movie.FeignInteface.UserInterface':
FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: No fallback instance of type class com.xing.movie.FeignInteface.UserInterfaceFallback found for feign client xing-user 使用Hystrix Dashboard
Hystrix Dashboard,它主要用来实时监控Hystrix的各项指标信息。通过Hystrix Dashboard反馈的实时信息,可以帮助我们快速发现系统中存在的问题,下面我把它引入到我的项目中,使用很简单只要两步就行
第一步: 在pom.xml文件中加入Dashboard的依赖
<!-- Hystrxi dashboard的依赖,实时监控Hystrix的各项指标反馈实时信息 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>

第二步: 在启动类中加入@EnableHystrixDashboard注解就行了,之后启动你的项目,访问http://127.0.0.1:8081/hystrix/会看到下面这个界面

通过Hystrix Dashboard主页面的文字介绍,我们可以知道,Hystrix Dashboard共支持三种不同的监控方式

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

  指定的集群监控:通过URL:http://turbine-hostname:port/turbine.stream?cluster=[clusterName]开启,实现对clusterName集群的监控。

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

  Delay:控制服务器上轮询监控信息的延迟时间,默认为2000毫秒,可以通过配置该属性来降低客户端的网络和CPU消耗。

  Title:该参数可以展示合适的标题。

这个界面就可以看到你我的服务调用的成功和失败的情况了

 
源码地址:https://github.com/OnlyXingxing/SpringCloud

使用Hystrix实现断路器处理的更多相关文章

  1. spring cloud 入门系列四:使用Hystrix 实现断路器进行服务容错保护

    在微服务中,我们将系统拆分为很多个服务单元,各单元之间通过服务注册和订阅消费的方式进行相互依赖.但是如果有一些服务出现问题了会怎么样? 比如说有三个服务(ABC),A调用B,B调用C.由于网络延迟或C ...

  2. SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)

    前言 本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识. SpringCloud Hystrix Hystrix 介绍 Netfl ...

  3. spring cloud 2.x版本 Hystrix Dashboard断路器教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  4. SpringCould-------使用Hystrix 实现断路器进行服务容错保护

    消费: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.or ...

  5. springcloud的Hystrix turbine断路器聚合监控实现(基于springboot2.02版本)

    本文基于方志朋先生的博客实现:https://blog.csdn.net/forezp/article/details/70233227 一.准本工作 1.工具:Idea,JDK1.8,Maven3. ...

  6. SpringCloud2.0 Hystrix Dashboard 断路器指标看板

    原文:https://www.cnblogs.com/songlu/p/9973856.html 1.启动基础工程 1.1.启动[服务中心]集群,工程名称:springcloud-eureka-ser ...

  7. SpringCloud2.0 Hystrix Dashboard 断路器指标看板 基础教程(八)

    1.启动基础工程 1.1.启动[服务中心]集群,工程名称:springcloud-eureka-server 参考 SpringCloud2.0 Eureka Server 服务中心 基础教程(二) ...

  8. Spring Cloud学习 之 Spring Cloud Hystrix(断路器原理)

    断路器定义: public interface HystrixCircuitBreaker { // 每个Hystrix都通过它判断是否被执行 public boolean allowRequest( ...

  9. Eureka+Hystrix(断路器、熔断器)

    红圈是断路器的三种状态: 关闭:1.当consumer访问provider时,在网络超时访问内,访问成功: 2.有时互相调用会出现网络涌动,(比如北京访问广东的服务器要经过很多次路由才能达到并相应), ...

随机推荐

  1. Java 枚举(enum)详解

    概念: Java1.5发行版本中增加了新的引用类型--枚举类型(enum type).枚举类型是指由一组固定的常量组成合法值的类型.在Java虚拟机中,枚举类在进行编译时会转变成普通的Java类. 创 ...

  2. for循环(C语言型)举例

  3. C语言实现Windows下获取IP和MAC地址。

    C语言实现Windows下获取IP和MAC地址. #include <winsock2.h> #include <stdio.h> #include <stdlib.h& ...

  4. 理解Java主函数中的"String[] args"

    public class Understand_String_args { public static void main(String[] args) { System.out.printf(&qu ...

  5. gene network analysis

      基因表达分析包括3个层次[68], 首先是单基因水平, 即比较对照组与实验组的每个基因是否存在表达差异, 这主要指差异基因表达分析; 其次是多基因水平, 如按照基因的共同功能.相互作用.共同表达等 ...

  6. leetcode-165周赛-1277-统计全为1的正方形子矩阵

    题目描述: 自己的提交: class Solution: def countSquares(self, matrix: List[List[int]]) -> int: if not matri ...

  7. sigaction函数学习

    sigaction(查询或设置信号处理方式) 相关函数 signal,sigprocmask() ,sigpending,sigsuspend, sigemptyset 表头文件 #include&l ...

  8. python四种方法实现去除列表中的重复元素

    转载:https://blog.csdn.net/together_cz/article/details/76201975 def func1(one_list): ''''' 使用集合,个人最常用 ...

  9. python模块学习之HTMLTestRunner模块生成HTML测试报告

    #!/usr/bin/env python #-*- coding:utf-8 -*- from HTMLTestRunner import HTMLTestRunner import time im ...

  10. MySQL高级学习笔记(五):查询截取分析

    文章目录 慢查询日志 是什么 怎么玩 说明 查看是否开启及如何开启 默认 开启 那么开启了慢查询日志后,什么样的SQL才会记录到慢查询日志里面呢? Case 配置版 日志分析工具mysqldumpsl ...