我们知道分布式服务有这样一个特点,每一个微服务都有自己的业务,并且很多时候一个微服务的业务要依赖于其他微服务,如果这些相互关联的微服务中其中某个微服务请求失败时,就会导致其他调用它的微服务也会请求失败。为了避免这种情况,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. python学习笔记(十九)面向对象编程,类

    一.面向对象编程 面向对象,是一种程序设计思想. 编程范式:编程范式就是你按照什么方式去编程,去实现一个功能.不同的编程范式本质上代表对各种类型的任务采取的不同的解决问题的思路,两种最重要的编程范式分 ...

  2. Airtest断言方法

    1,第一种断言方式:验证UI界面 a.存在 b.不存在 2,断言第二种方式:验证数值 assert_equal:断言相等 assert_not_equal:断言不等 3,我发现Airtest一个bug ...

  3. shp文件导入数据库

    数据库服务器(引擎) sql server oracle nosql sql语句... 从数据库端导入:新建数据库,导入shp文件 发布地图服务 jdbc.sdk

  4. php7新特性的理解和比较

    1. null合并运算符(??) ??语法: 如果变量存在且值不为NULL,它就会返回自身的值,否则返回它的第二个操作数. //php7以前 if判断 if(empty($_GET['param']) ...

  5. scrapy抓取企业名录

    我们要用scrapy抓取企业名录网站的企业信息,并且保存在mysql数据库中,数据大概是22万条,我们用scrapy抓取. 第一步,现在item中定义好要抓取的字段 import scrapy cla ...

  6. 如何查看MySQL数据库的版本

    如何查看MySQL数据库的版本 一.总结 一句话总结: SQL语句:select version(); 命令行:mysql -V 或 mysql --version 二.三种方法查看MySQL数据库的 ...

  7. zabbix 接入钉钉机器人报警

    import requests import json import sys import os headers = {'Content-Type': 'application/json;charse ...

  8. 常用的Android关键词定位方法

    1字符串,特征字 根据程序运行中出现的特征字词进行搜索,从而获取定位到程序的相关位置之中.以前用 得比较多,不过现在一般难以找到想要的关键词.有时候需要对特征字进行拆分来进行搜索.才 能获得一点提示. ...

  9. day08—css布局解决方案之多列布局

    转行学开发,代码100天——2018-03-24 本文将记录CSS布局之垂直布局解决方案. 常见的多列布局包括以下: 1.定宽+自适应 2.两列定宽+一列自适应 3.不定宽+自适应 4.两列不定宽+一 ...

  10. kali 开启xdebug

    1.安装xdebug 参考https://xdebug.org/docs/install 2.配置 # vi /etc/php/7.3/mods-available/xdebug.inizend_ex ...