spring cloud学习笔记四 熔断器Hystrix
我们知道分布式服务有这样一个特点,每一个微服务都有自己的业务,并且很多时候一个微服务的业务要依赖于其他微服务,如果这些相互关联的微服务中其中某个微服务请求失败时,就会导致其他调用它的微服务也会请求失败。为了避免这种情况,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的更多相关文章
- 【Spring Cloud学习之六】断路器-Hystrix
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 一.服务雪崩1.什么是服务雪崩分布式系统中经常会出现某个基础服务不可用造成整个系统不 ...
- spring cloud ----> RibbonClient设置的熔断器Hystrix不起作用
Ribbon spring.io 官网的简介: Ribbon is a client side load balancer which gives you a lot of control over ...
- Spring Cloud学习笔记--Spring Boot初次搭建
1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...
- Feign详细使用-Spring Cloud学习第四天(非原创)
文章大纲 一.Feign是什么二.Feign的基本实现三.Feign的继承特性四.Feign配置详解五.项目源码与参考资料下载六.参考文章 一.Feign是什么 前面几篇文章我们详细的介绍了Rib ...
- Spring Cloud 学习笔记(二)——Netflix
4 Spring Cloud Netflix Spring Cloud 通过自动配置和绑定到Spring环境和其他Spring编程模型惯例,为Spring Boot应用程序提供Netflix OSS集 ...
- Spring Cloud学习笔记-006
服务容错保护:Spring Cloud Hystrix 在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调 ...
- Spring Cloud学习笔记-007
声明式服务调用:Spring Cloud Feign Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两 ...
- Spring Cloud学习笔记【八】服务网关 Zuul(过滤器)
在上篇文章中我们了解了 Spring Cloud Zuul 作为网关所具备的最基本功能:路由(Router),下面我们将关注 Spring Cloud Zuul 的另一核心功能:过滤器(Filter) ...
- Spring Cloud学习笔记【一】Eureka服务注册与发现
Spring Cloud Eureka 是 Spring Cloud Netflix 微服务套件的一部分,基于 Netflix Eureka 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...
随机推荐
- vue框架搭建--axios使用
前后端数据交互作为项目最基础需求(静态的除外),同时也是项目中最重要的需求. 本文重点介绍axios如何配合vue搭建项目框架,而axios的详细使用介绍请移步使用说明 1.安装 cnpm insta ...
- [CF846C]Four Segments题解
我们暴力枚举一下\(delim_{1}\) 然后对于每个\(delim_{1}\),O(n)扫一遍+前缀和求出最大\(delim_{0}\)和\(delim_{2}\),然后记录一下它们的位置就行啦 ...
- 20180816-Java 多态
Java 多态 多态是同一个行为具有多个不同表现形式或形态的能力. 多态性是对象多种表现形式的体现. 比如我们说"宠物"这个对象,它就有很多不同的表达或实现,比如有小猫.小狗.蜥蜴 ...
- sqlserver连接-2
本地连接 方法1. 方法2. 远程连接 如果无法通过IP地址远程连接你的SQL Server 2008服务器,可以参考下面的内容进行设置. 在进行下述设置之前,应该确保你的网络已经安装设置完毕,服务器 ...
- 刚性方程 Stiff equation
In mathematics, a stiff equation is a differential equation for which certain numerical methods for ...
- Note:目录1
ylbtech-Note:目录1 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbtech ...
- mysql_备份_mysqldump
命令行执行mysqldump mysqldump -uuser -pPassword dbnametable [option] > xx.sql;mysqldump• Mysqldump常用参数 ...
- 【经验分享】 解决CentOS7 安装VMTools提示找不到Kernel头文件的方案
配置一个Linux的开发环境,用VM10+CentOS7(Kernel版本3.10.0-327.10.1.el7),之后发现VMTools功能不全,查证后发现需要卸载重装,于是开始折腾. 按照各种说明 ...
- 16/7/11_PHP-图形图像操作
GD库简介 GD指的是Graphic Device,PHP的GD库是用来处理图形的扩展库,通过GD库提供的一系列API,可以对图像进行处理或者直接生成新的图片. PHP除了能进行文本处理以外,通过GD ...
- MySQL replace into 与insert into
https://blog.csdn.net/helloxiaozhe/article/details/77427266 使用replace带来的问题 1.Replace into 操作在唯一键重复情况 ...