SpringCloud Hystrix
⒈Hystrix是什么?
Hystrix使一个用于处理分布式系统的延迟和容错的开源库。在分布式系统里,许多依赖不可避免的因服务超时、服务异常等导致调用失败,Hystrix能够保证在一个依赖出现问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。
⒉断路器&服务熔断
“断路器”本身使一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似于熔断保险丝),向服务调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要的占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。
熔断机制是应对雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务不可用或者响应时间太长时,会进行服务的降级,进而熔断该节点微服务的调用,快速返回“错误”的响应信息,当检测到该节点的微服务调用响应正常后恢复调用链路。在SpringCloud框架里,熔断机制通过Hystrix实现,Hystrix会监控微服务间调用的状况,当失败的调用达到一定的阈值(默认是5秒内20次调用失败),就会启动熔断机制。
⒊示例
①在服务提供者项目中添加Hystrix starter依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
②在服务提供者项目中对控制器中的Action方法指定熔断调用方法
package cn.coreqi.controller; import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List; @RestController
public class UserController {
@Autowired
private UserService userService; @GetMapping("/users")
@HystrixCommand(fallbackMethod = "getUsersFallback") //一旦服务消费者远程调用该方法失败并抛出错误信息后,Hystrix会自动调用@HystrixCommand注解fallbackMethod属性中标注的方法返回
public List<User> getUsers(){
throw new NullPointerException();
//return userService.getList();
} public List<User> getUsersFallback(){
return null;
}
}
③在主程序启动类上添加@EnableCircuitBreaker注解
package cn.coreqi; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient //启用Eureka客户端功能
@EnableCircuitBreaker //对Hystrix熔断机制的支持
public class SpringbootcloudserviceproviderApplication { public static void main(String[] args) {
SpringApplication.run(SpringbootcloudserviceproviderApplication.class, args);
} }
⒋服务降级
服务器整体资源快不够了,将某些服务先关掉,待渡过难关再开启回来,服务降级处理是在服务消费者实现完成的,与服务提供者没有关系
⒌示例
①在服务消费者配置文件中开启
feign.hystrix.enabled=true
②编写回退方法
package cn.coreqi.fallbackfactory; import cn.coreqi.entities.User;
import cn.coreqi.service.UserService;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component; import java.util.ArrayList;
import java.util.List; @Component
public class UserServiceFallbackFactory implements FallbackFactory<UserService> {
@Override
public UserService create(Throwable throwable) {
return new UserService() {
@Override
public void addUser(User user) { } @Override
public void delById(Integer id) { } @Override
public void modifyUser(User user) { } @Override
public User getById(Integer id) {
return null;
} @Override
public List<User> getList() {
List<User> userList = new ArrayList<>();
userList.add(new User(0,"Error","Error",0));
return userList;
}
};
}
}
③在@FeignClient注解中指定fallbackFactory的属性值
package cn.coreqi.feign; import cn.coreqi.entities.User;
import cn.coreqi.fallbackfactory.UserServiceFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping; import java.util.List; @FeignClient(value = "USER-PROVIDER",fallbackFactory = UserServiceFallbackFactory.class) //指定微服务实例名称
public interface UserFeignClient {
@GetMapping("/users") //指定调用微服务的服务地址
public List<User> getList();
}
⒍服务监控-Hystrix Dashboard
Hystrix提供了准实时的调用监控(Hystrix Dashboard),Hystrix 会持续的记录所有通过Hystrix 发起请求的执行信息,并以统计报表和图形的形式展示给用户,Netfilx通过hystrix-metrics-event-stream项目实现了对以上指标的监控,Spring Cloud也提供了对Hystrix Dashboard的整合,对监控内容转化为可视化界面。
7示例
①新建监控项目添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
②配置文件中指定运行端口
server.port=9001
③主程序启动类添加@EnableHystrixDashboard注解
package cn.coreqi; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; @SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication { public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
} }
④对所有需要监控的服务提供者项目添加以下依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-actuator</artifactId>
</dependency>
⑤访问监控项目web管理地址
http://localhost:9001/hystrix
SpringCloud Hystrix的更多相关文章
- SpringCloud Hystrix熔断之线程池
服务熔断 雪崩效应:是一种因服务提供者的不可用导致服务调用者的不可用,并导致服务雪崩的过程. 服务熔断:当服务提供者无法调用时,会通过断路器向调用方直接返回一个错误响应,而不是长时间的等待,避免服务雪 ...
- SpringBoot + SpringCloud Hystrix 实现服务熔断
什么是Hystrix 在分布式系统中,每个服务都可能会调用很多其他服务,被调用的那些服务就是依赖服务,有的时候某些依赖服务出现故障也是很常见的. Hystrix是Netflix公司开源的一个项目,它提 ...
- springCloud Hystrix 断路由
第一步加入依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId& ...
- SpringCloud+Hystrix服务容错
Netflix Hystrix — 应对复杂分布式系统中的延时和故障容错 +应用场景 分布式系统中经常会出现某个基础服务不可用造成整个系统不可用的情况, 这种现象被称为服务雪崩效应. 为了应对服务雪崩 ...
- springcloud hystrix 部分参数整理
hystrix.command.default和hystrix.threadpool.default中的default为默认CommandKey Command Properties Executio ...
- springcloud Hystrix fallback无效
在使用feign调用服务的时候防止雪崩效应,因此需要添加熔断器.(基于springboot2.0) 一.在控制器的方法上添加 fallbackMethod ,写一个方法返回,无须在配置文件中配置,因 ...
- SpringCloud Hystrix 参数
hystrix.command.default和hystrix.threadpool.default中的default为默认CommandKey Command PropertiesExecution ...
- 关于springcloud hystrix 执行 hystrix.stream 跳转失败的问题
经过观看网友的总结:应该时版本的问题.某些版本没有对/hystrix.stream进行配置 所以解决方案(网友答案): 需要配置类配置下面 @Bean public ServletRegistrati ...
- Hippo4J v1.3.1 发布,增加 Netty 监控上报、SpringCloud Hystrix 线程池监控等特性
文章首发在公众号(龙台的技术笔记),之后同步到博客园和个人网站:xiaomage.info Hippo4J v1.3.1 正式发布,本次发布增加了 Netty 上传动态线程池监控数据.适配 Hystr ...
随机推荐
- M1-Flask-Day1
前情概要 1.flask的基本使用 - 配置 - 路由 - 视图 - 请求与响应相关 - 模板 2.flask基于装饰器实现的路由 - 基本操作 - functools - 带参数的装饰器 - 源码剖 ...
- Mysql数据约束 整理
数据约束 1.默认值: 作用: 当用户对使用默认值的字段不插入值的时候,就使用默认值. 注意: 1)对默认值字段插入null是可以的. 2)对默认值字段可以插入非null CREATE TABLE ...
- nGrinder windows agent / linux agent
s ngrinder部署 https://blog.csdn.net/yue530tomtom/article/details/82113558 Windows机器启动不了ngrinder-agent ...
- Linux CGroup
catalog . 引言 . Cgroup安装配置 . Cgroup使用方式 . CGroup的子系统 1. 引言 我们已经讨论了Linux下命名空间(Namespace)的基本知识,详情请参阅另一篇 ...
- python 管道 事件(Event) 信号量 进程池(map/同步/异步)回调函数
####################总结######################## 管道:是进程间通信的第二种方式,但是不推荐使用,因为管道会导致数据不安全的情况出现 事件:当我运行主进程的 ...
- 使用JMeter进行一次简单的带json数据的post请求测试,json可配置参数
配置: 1.新建一个线程组: 然后设置线程数.运行时间.重复次数. 2.新建Http请求: 设置服务器域名,路径,方法,编码格式,数据内容. 可以在函数助手中,编辑所需要的变量,比如本例中的随机生成电 ...
- 虚拟机 the image's hash and certificate are not allowed 解决方案
根据计划,需要在虚拟机上安装一个linux系统,用作web架构学习的服务器. 公司项目的服务器用的是linux系统,具体版本未知.虽然我们开发不用关注最后的部署,但多少也接触了一些,算是有一定的了解, ...
- vue加载本地json文件
背景:做地区跟行业级联下拉选择,因为想做成可以搜索的,所以必须一次加载数据,后台有做memcache缓存,但因为数据量大,还是比较费时间,所以做成本地文件,简单记录一下 准备数据,放到static下 ...
- Vertica系列: 表的分段和分区
Vertica 有两个数据分布的概念, segmentation 和 partition, 至少有下面几个区别: 1.目的方面:segmentation 解决各节点数据倾斜问题, 适用于木桶原理, 数 ...
- kudu系列: Java API使用和效率测试
Kudu+Impala很适合数据分析, 但直接使用Insert values语句往Kudu表插入数据, 效率实在不好, 测试下来insert的速度仅为80笔/秒. 原因也是显然的, Kudu本身写入效 ...