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 做了二次封装,主要负责完成微服务架构中的服务治理功能,服务 ...
随机推荐
- Django的使用一
Django是一个由Python写成的Web应用框架,是 Python 社区的两大最受欢迎的 Web 框架之一(另一个是 Flask). Django的主要目的是简便.快速的开发数据库驱动的网站. 1 ...
- /usr,/usr/local/ 还是 /opt 目录区别
Linux 的软件安装目录是也是有讲究的,理解这一点,在对系统管理是有益的 /us(Unix Software Resource)r:系统级的目录,可以理解为C:/Windows/, /usr/lib ...
- JavaScript点击事件——美女合集
Js点击事件--美女合集 实例 效果如下图: 代码如下: <!DOCTYPE html> <html lang="en"> <head> < ...
- 数据可视化-D3js-展示古地理图和古地理坐标反算^_^gplates古地理坐标反算接口
在线演示 <!DOCTYPE html> <html> <head> <link type="image/png" rel="i ...
- 20160122 DataView RowFilter语法
原文出自:http://www.csharp-examples.net/dataview-rowfilter/ DataView RowFilter语法(c#) 这个例子描述了DataView.Row ...
- Vagrant 手册之网络 - 公共网络 public network
原文地址 Vagrantfile 配置文件中公共网络的标识符:public_network,例如: config.vm.network "public_network" Vagra ...
- poj2010 Moo University - Financial Aid 优先队列
Description Bessie noted that although humans have many universities they can attend, cows have none ...
- 机器学习实战笔记-11-Apriori与FP-Growth算法
Apriori算法 优点:易编码实现:缺点:大数据集上较慢:适用于:数值型或标称型数据. 关联分析:寻找频繁项集(经常出现在一起的物品的集合)或关联规则(两种物品之间的关联关系). 概念:支持度:数据 ...
- Easyui表格显示日期格式错误
问题 如下图: 解决办法: 以Sql Server 2008为例,在执行查询时,把“采购日期”字段由datetime转换为string,前端以string类型显示.
- 【转载】ROS系统整体架构
目录 1.从文件系统级理解 2.从计算图级理解 3.从开源社区级理解 由于ROS系统的组织架构比较复杂,简单从一个方面来说明很难说清楚.按照ROS官方的说法,我们可以从3个方面来理解ROS系统整体架构 ...