SpringCloud之服务降级
1.Hystrix(断路器)
1.1定义
扇出:多个微服务调用的时候,假设微服务A调用微服务B和C,微服务B和C又调用其他的服务。
服务雪崩:如果扇出的链路上某个微服务的调用时间过长或不可用,对微服务A的调用就会越来多的系统资源,进而引起系统崩溃。
Hystrix是一个用于处理分布式系统的延迟和容错的开源库,能够保证在一个依赖出现问题的情况下,不会导致整体服务失败,从而提高分布式系统的弹性。它的功能有服务降级、服务熔断、服务限流等。
断路器:一种开关装置,当某个服务单元发生故障后,通过断路器的故障监控向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或抛出异常。
服务熔断:当扇出链路的某个微服务不可用火响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回错误的响应信息。当检测到该节点微服务调用响应正常后恢复调用链路。
服务降级:在客户端实现的
1.2项目开发
源代码:https://github.com/zhongyushi-git/cloud-hystrix.git
1.2.1服务提供者进行服务降级
1)参考feign的搭建来搭建hystrix的项目,只需要eureka集群、api和consumer80。
2)按照provider8001创建服务提供者cloud-provider-hystrix-8001,再导入hystrix依赖
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
3)对发生异常的方法进行处理
在服务提供者的UserController中的get方法添加结果为空的异常处理
@GetMapping("/get/{id}")
@HystrixCommand(fallbackMethod = "hystrix_get")
public User getUser(@PathVariable("id")long id){
User user=userService.getUser(id);
//数据为空就抛出异常
if(user==null){
throw new RuntimeException("未查询到数据");
}
return user;
}
//服务熔断
public User hystrix_get(@PathVariable("id")long id){
User user=new User();
user.setId(id);
user.setName("未查询到数据");
user.setPhone(new Date().toString());
return user;
}
4)在服务提供者启动类添加注解@EnableCircuitBreaker
@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
public class ProviderMain8001 {
public static void main(String[] args) {
SpringApplication.run(ProviderMain8001.class, args);
}
}
5)启动测试
先启动eureka集群,然后启动8001。分别访问http://localhost:8001/user/get/1和http://localhost:8001/user/get/123,get/1能获取正常数据,get/123不能获取正常数据而显示的错误信息。
1.2.2服务消费者进行服务降级
1)把服务提供者进行异常处理的地方给注释掉
@GetMapping("/get/{id}")
@HystrixCommand(fallbackMethod = "hystrix_get")
public User getUser(@PathVariable("id")long id){
User user=userService.getUser(id);
//数据为空就抛出异常
// if(user==null){
// throw new RuntimeException("未查询到数据");
// }
return user;
}
2)在cloud-feign-consumer80导入hystrix依赖
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
3)修改cloud-feign-consumer80的yml
feign:
hystrix:
enabled: true
4)创建service接口,用于feign获取服务
package com.zys.cloud.serivce; import com.zys.cloud.entity.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; //添加注解,指定微服务名称
@FeignClient(value="CLOUD-PROVIDER")
public interface UserClientService { @GetMapping("/user/get/{id}")
public User getUser(@PathVariable("id")long id); @PostMapping("/user/add")
int addUser(User user);
}
5)创建controller
package com.zys.cloud.controller; import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zys.cloud.entity.User;
import com.zys.cloud.serivce.UserClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import javax.annotation.Resource;
import java.util.Date; @RestController
@RequestMapping("/consumer")
public class UserController { @Resource
private UserClientService userClientService; @GetMapping("/get/{id}")
// @HystrixCommand(fallbackMethod = "hystrix_get")
public User getUser(@PathVariable("id") long id){
User user=userClientService.getUser(id);
//数据为空就抛出异常
if(user==null){
throw new RuntimeException("抱歉,未查询到数据");
}
return user;
}
//服务熔断
public User hystrix_get(@PathVariable("id")long id){
User user=new User();
user.setId(id);
user.setName("抱歉,未查询到数据");
user.setPhone(new Date().toString());
return user;
} @PostMapping("/add")
public int addUser(User user){
return userClientService.addUser(user);
} }
6)在服务消费者启动类添加注解@EnableHystrix
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class ConsumerFeignMain80 {
public static void main(String[] args) {
SpringApplication.run(ConsumerFeignMain80.class, args);
}
}
7)启动测试
先启动eureka集群,然后启动8001,最后80启动。分别访问http://localhost/consumer/get/1和http://localhost/consumer/get/123,get/1能获取正常数据,get/123不能获取正常数据而显示的错误信息。然后关闭8001,再次访问http://localhost/consumer/get/1也是显示错误信息。
1.2.3服务降级优化
在上面的服务提供者和消费者中,对服务进行降级,都有一些问题。就是要对每一个方法进行降级,降级的方法又混合在controller中,显示很繁琐,配置起来也很麻烦。不过是可以进行全局配置的。
(1)DefaultProperties设置默认的处理方法
在80的controller中加入注解进行配置
package com.zys.cloud.controller; import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zys.cloud.entity.User;
import com.zys.cloud.serivce.UserClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import javax.annotation.Resource;
import java.util.Date; @RestController
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback = "globalFallBack")
public class UserController { @Resource
private UserClientService userClientService; @GetMapping("/get/{id}")
// @HystrixCommand(fallbackMethod = "hystrix_get")
//未指定fallback,就使用默认的
@HystrixCommand
public User getUser(@PathVariable("id") long id){
User user=userClientService.getUser(id);
//数据为空就抛出异常
if(user==null){
throw new RuntimeException("抱歉,未查询到数据");
}
return user;
}
//服务熔断
public User hystrix_get(@PathVariable("id")long id){
User user=new User();
user.setId(id);
user.setName("抱歉,未查询到数据");
user.setPhone(new Date().toString());
return user;
} @PostMapping("/add")
public int addUser(User user){
return userClientService.addUser(user);
} //设置全局的fallback
public User globalFallBack(){
User user=new User();
user.setName("服务器维护中,请稍后再试!");
user.setPhone(new Date().toString());
return user;
} }
当没有指定fallback时,就会去使用默认的fallback。
(2)代码解耦
在service中新建一个类UserClientServiceFallBackFactory,实现FallbackFactory
package com.zys.cloud.serivce; import com.zys.cloud.entity.User;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component; @Component
public class UserClientServiceFallBackFactory implements FallbackFactory<UserClientService> {
@Override
public UserClientService create(Throwable throwable) {
return new UserClientService() {
@Override
public User getUser(long id) {
User user=new User();
user.setId(id);
user.setName("未查询到数据,服务降级-停止服务");
return user;
} @Override
public int addUser(User user) {
return 0;
}
};
}
}
在UserClientService中给FeignClient注解加fallbackFactory属性
@FeignClient(value="CLOUD-PROVIDER",fallbackFactory = UserClientServiceFallBackFactory.class)
1.3服务监控
对于微服务,也是需要进行监控的。Hystrix也提供了准实时的服务监控(Hystrix Dashboard),它会持续的记录所有通过Hystrix发起的请求的执行信息,并以统计报表和图形的形式展现给用户。在1.2的基础上继续开发。
1)新建cloud-feign-hystrix-dashboard-consumer9001的maven工程
2)在pom中添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
3)配置yml文件
server:
port: 9001
4)在包com.zys.cloud下创建启动类并添加注解@EnableHystrixDashboard
package com.zys.cloud; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @SpringBootApplication
@EnableHystrixDashboard
public class ConsumerFeignHystrixDashboardMain9001 {
public static void main(String[] args) {
SpringApplication.run(ConsumerFeignHystrixDashboardMain9001.class, args);
}
}
5)在8001的启动类加配置方法
//指定监控路径
@Bean
public ServletRegistrationBean getServlet(){
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
6)启动测试
先启动9001,然后启动eureka集群,然后启动8001。然后在浏览器输入http://localhost:9001/hystrix,可以看到下面的界面,说明配置成功。

在第一个输入框输入http://localhost:8001/hystrix.stream,点击下面的按钮,可以进入服务的监控页面。
再输入http://localhost:8001/user/get/1,快速的刷新几次,再回到监控页面看到有图形在实时变化,这就是实时的监控效果。

SpringCloud之服务降级的更多相关文章
- SpringCloud服务降级案列
一.什么是服务降级 所有的RPC技术里面服务降级是一个最为重要的话题,所谓的降级指的是当服务的提供方不可使用的时候,程序不会出现异常,而会出现本地的操作 二.服务降级案例 1.目录展示 2.导入依赖 ...
- SpringCloud断路器(Hystrix)和服务降级案列
断路器(Hystrix) 为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自 ...
- SpringCloud学习之Hystrix请求熔断与服务降级(六)
我们知道大量请求会阻塞在Tomcat服务器上,影响其它整个服务.在复杂的分布式架构的应用程序有很多的依赖,都会不可避免地在某些时候失败.高并发的依赖失败时如果没有隔离措施,当前应用服务就有被拖垮的风险 ...
- SpringCloud之Hystrix服务降级入门全攻略
理论知识 Hystrix是什么? Hystrix是由Netflix开源的一个服务隔离组件,通过服务隔离来避免由于依赖延迟.异常,引起资源耗尽导致系统不可用的解决方案.这说的有点儿太官方了,它的功能主要 ...
- SpringCloud微服务:Sentinel哨兵组件,管理服务限流和降级
源码地址:GitHub·点这里||GitEE·点这里 一.基本简介 1.概念描述 Sentinel 以流量为切入点,从流量控制.熔断降级.系统负载保护等多个维度保护服务的稳定性.包括核心的独立类库,监 ...
- SpringCloud Netflix (五) : Hystrix 服务熔断和服务降级
什么是Hystrix 在分布式环境中,许多服务依赖项中的一些服务依赖不可避免地会失败.Hystrix是一个库,通过添加延迟容忍和容错逻辑,帮助您控制这些分布式服务之间的交互.Hystrix通过隔离服务 ...
- 学习一下 SpringCloud (四)-- 服务降级、熔断 Hystrix、Sentinel
(1) 相关博文地址: 学习一下 SpringCloud (一)-- 从单体架构到微服务架构.代码拆分(maven 聚合): https://www.cnblogs.com/l-y-h/p/14105 ...
- 五. SpringCloud服务降级和熔断
1. Hystrix断路器概述 1.1 分布式系统面临的问题 复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地失败.这就造成有可能会发生服务雪崩.那么什么是服务雪崩呢 ...
- 【springcloud】服务熔断与降级(Hystrix)
转自:https://blog.csdn.net/pengjunlee/article/details/86688858 服务熔断 服务熔断的作用类似于我们家用的保险丝,当某服务出现不可用或响应超时的 ...
随机推荐
- python爬虫selenium相关
首先上很好用的selenium中文文档,基本上所有问题都能通过阅读此文档解决.可惜好像没找到翻译者名称. https://python-selenium-zh.readthedocs.io/zh_CN ...
- Kwp2000协议的应用(程序原理篇)
作者:良知犹存 转载授权以及围观:欢迎添加微信:becom_me 总述 接上篇文章Kwp2000协议的应用(硬件原理使用篇),本篇针对kwp2000协议标准的服务ID详细介绍,以及针对程序实现 ...
- CF-1328 E. Tree Queries
E. Tree Queries 题目链接 题意 给定一个树,每次询问一组点,问是否存在一条从根到某点的路径,使得该组点到该路径的最短距离不超过1 分析 从根到达某点的路径,如果覆盖到了某个点,那么一定 ...
- 南阳ccpc C题 The Battle of Chibi && hdu5542 The Battle of Chibi (树状数组优化+dp)
题意: 给你一个长度为n的数组,你需要从中找一个长度为m的严格上升子序列 问你最多能找到多少个 题解: 我们先对原序列从小到大排序,排序之后的序列就是一个上升序列 这里如果两个数相等的话,那么因为题目 ...
- NCD 2019 M. NCD Salary
题意 :给你两个指数类型的数\(A^m\)和\(B^n\),比较他们的大小.保证底数和指数中最多只有一个为0. 题解 :题目数据非常大,肯定不能直接比较.由换底公式和\(Log\)函数的性质我们知道: ...
- 踏上Revit二次开发之路 0 序
0 序 近来,由于工作上的需要,开始自学Revit二次开发. Revit由欧特克公司专为BIM构建,是建筑业体系中使用最广泛的软件之一.借助欧特克公司在我国市场占有率方面的绝对优势,甚至给不少人带来& ...
- java调用http接口的几种方式总结
本文参考: https://blog.csdn.net/fightingXia/article/details/71775516 https://www.cnblogs.com/jeffen/p/69 ...
- markdown嵌入图片
这里嵌入指不会因为本地文件丢失而丢失. 参考:https://blog.csdn.net/testcs_dn/article/details/78952358 https://blog.csdn.ne ...
- 51nod 1073约瑟夫环 递归公式法
约瑟夫环问题的原来描述为,设有编号为1,2,--,n的n(n>0)个人围成一个圈,从第1个人开始报数,报到m时停止报数,报m的人出圈,再从他的下一个人起重新报数,报到m时停止报数,报m的出圈,- ...
- freeRTOS V10.0.1移植到STM32F407标准库 - 环境Keil5
最近因为工作需要用到FreeRTOS,其实开始本人内心是拒绝的因为自己只学习过UCOSIII还没实际上过什么大又复杂的工程,但是谁让FreeRTOS他是Free的呢公司成本考虑肯定是不会选择USOS的 ...