我们知道分布式服务有这样一个特点,每一个微服务都有自己的业务,并且很多时候一个微服务的业务要依赖于其他微服务,如果这些相互关联的微服务中其中某个微服务请求失败时,就会导致其他调用它的微服务也会请求失败。为了避免这种情况,spring cloud为我们提供了一个熔断器Hystrix来管理。

一、Hystrix的熔断

  Hystrix的熔断主要是为我们解决“雪崩效应”,它的作用主要是针对我们所调用的接口,如果接口请求异常,那么Hystrix会熔断到一个方法中。

  1.导入maven  

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

  2.在启动类中加入@EnableCircuitBreaker注解。 

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate; @SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public class DemoHystrixApplication { public static void main(String[] args) {
SpringApplication.run(DemoHystrixApplication .class, args);
} }

  3.创建controller层,controller层中对访问接口进行了熔断

@RestController
public class DemoHystrixController {   @Autowired
DemoHystrixService demoHystrixService ; //请求熔断注解,当服务出现问题时候会执行fallbackMetho属性的名为fallCode的方法
@HystrixCommand(fallbackMethod = "fallCode")
  @RequestMapping("/show")
public String hystrixMain(@PathVariable("num") int num) {
String str=demoHystrixService.show(num);
      if(str==null)//如果没有找的数据,就报一个异常,Hystrix会熔断调用fallCode方法
      throw new RuntimeException("数据不存在") ;
 }
} private String fallCode() {
return "数据不存在";
} }

二、服务降级

  在分布式项目中,某个服务的并发过高,导致资源不够用了,此时可以考虑关闭一些服务来提供资源。关闭的服务程序不会报异常,而是本地的调用操作,这就是服务降级。服务的降级处理是在客户端实现的,与你的服务器端没有关系。

  1.创建一个controller层 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import com.alibaba.fastjson.JSONObject;
import com.java.feign.service.HostService; @RestController
public class DowngradeController { @Autowired
DemoHystrixService demoHystrixService; @RequestMapping("/down")
public JSONObject getDown(@PathVariable("nums") int nums) { return demoHystrixService.showJsonData(nums);
}
}

  2.创建一个服务降级管理类,实现FallbackFactory接口,FallbackFactory中的泛型就是我们需要降级的controller 

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import feign.hystrix.FallbackFactory;

@Component
public class DowngradeFallbackFactory implements FallbackFactory<DowngradeController > { @Override
public DowngradeController create(Throwable cause) {
return new DowngradeController () { @Override
public JSONObject getDown(int nums) {
JSONObject json = new JSONObject();
json.put("num", num);
json.put("message", "服务的降级处理");
return json;
}
};
} }

 

spring cloud学习笔记四 熔断器Hystrix的更多相关文章

  1. 【Spring Cloud学习之六】断路器-Hystrix

    环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 一.服务雪崩1.什么是服务雪崩分布式系统中经常会出现某个基础服务不可用造成整个系统不 ...

  2. spring cloud ----> RibbonClient设置的熔断器Hystrix不起作用

    Ribbon spring.io 官网的简介: Ribbon is a client side load balancer which gives you a lot of control over ...

  3. Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...

  4. Feign详细使用-Spring Cloud学习第四天(非原创)

    文章大纲 一.Feign是什么二.Feign的基本实现三.Feign的继承特性四.Feign配置详解五.项目源码与参考资料下载六.参考文章   一.Feign是什么 前面几篇文章我们详细的介绍了Rib ...

  5. Spring Cloud 学习笔记(二)——Netflix

    4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...

  6. Spring Cloud学习笔记-006

    服务容错保护:Spring Cloud Hystrix 在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调 ...

  7. Spring Cloud学习笔记-007

    声明式服务调用:Spring Cloud Feign Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两 ...

  8. Spring Cloud学习笔记【八】服务网关 Zuul(过滤器)

    在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router),下面我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter) ...

  9. Spring Cloud学习笔记【一】Eureka服务注册与发现

    Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...

随机推荐

  1. kwargs - Key words arguments in python function

    This is a tutorial of how to use *args and **kwargs For defining the default value of arguments that ...

  2. linux运维、架构之路-正则表达式

    一.通配符的含义 符号 参数说明 其他说明 | 管道 把前一个命令结果通过管道传递给后面一个命令 ; 命令的分隔符 ll /oldboy/;cat oldboy.tx . 表示当前目录 * 匹配文本或 ...

  3. MySQL基础篇——安装、管理

    MySQL 安装 所有平台的 MySQL 下载地址为https://dev.mysql.com/downloads/mysql/ .挑选你需要的 MySQL Community Server 版本及对 ...

  4. (1)消灭初级程序员常用的多层if-else嵌套--------------【表驱动法】

    表驱动法 1.相信很多刚从事工作的程序员或一些初级程序员在写代码的时候会出现对一些逻辑判断写成多层if-else嵌套的经历,这种方式在一些简单的层次中运用起来确实可行,但对于一些大型项目逻辑判断比较多 ...

  5. [NOIP2016]蚯蚓 题解

    题目描述 本题中,我们将用符号[c]表示对c向下取整,例如:[3.0」= [3.1」= [3.9」=3.蛐蛐国最近蚯蚓成灾了!隔壁跳蚤国的跳蚤也拿蚯蚓们没办法,蛐蛐国王只好去请神刀手来帮他们消灭蚯蚓. ...

  6. HTML与CSS中的文本个人分享

    文本 标题元素 注意: 在一个HTML页面中最好只使用一个<h1>标题 因为浏览器只会抓取一个多了没用 示例代码: <body> <!-- 标题元素 - <h1&g ...

  7. GIS网站收藏

    igismap: https://www.igismap.com/ 高德WebAPI: https://lbs.amap.com/api/javascript-api/example/other-ga ...

  8. 【CDN+】 Spark 的入门学习与运行流程

    前言 上文已经介绍了与Spark 息息相关的MapReduce计算模型,那么相对的Spark的优势在哪,有哪些适合大数据的生态呢? Spark对比MapReduce,Hive引擎,Storm流式计算引 ...

  9. 【ABAP系列】SAP ABAP 工单增强

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 工单增强   ...

  10. linux操作系统的调度策略

    linux的进程分为两种 1.实时进程,优先级高,操作系统会优先执行这种进程 2.普通进程,大多数的进程都是这种进程 调度策略 unsigned int policy; 调度策略的定义 #define ...