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 ...
随机推荐
- Ajax的请求规范(二)
第一种方式:send()不带参数 function doAjax(url,fnSucc,fnFaild) { //1.创建Ajax对象 if (window.XMLHttpRequest) {//判断 ...
- 【知名的移动APP和网站设计工具】Sketch for Mac 54.1
以上图片来源于互联网分享,如涉及版权问题请联系作者删除. 文章素材来源:风云社区(www.scoee.com) 下载地址:风云社区(www.scoee.com) [简介] Sketch 是一款适用 ...
- mysql执行update报错 Err] 1055 - 'information_schema.PROFILING.SEQ' isn't in GROUP BY
mysql执行update报错 Err] 1055 - 'information_schema.PROFILING.SEQ' isn't in GROUP BY 今天开发的同事发来如下错误信息,最最简 ...
- chrome截图全网页
1.F12 2.ctrl+shift+p 3.输入:capture 4.选择Capture full size screenshot
- 反汇编Dis解析
目录 反汇编dis解析 COMM段BSS段 注释段 Bl指令 title: 反汇编Dis解析 tags: ARM date: 2018-10-21 18:02:58 --- 反汇编dis解析 关于段, ...
- ResourceBundle读取properties配置文件
package cn.rocker.readProperties; import java.util.ResourceBundle; import org.junit.Test; /** * @Cla ...
- python jquery初识
jQuery的介绍 jQuery是一个快速,小巧,功能丰富的JavaScript库.它通过易于使用的API在大量浏览器中运行,使得HTML文档遍历和操作, 事件处理动画和Ajax更加简单.通过多功能 ...
- redis做session会话共享
项目中需要两个不同的web项目互相访问,用户对象为同一个User.决定用Redis来存储用户对象信息...ok,环境搭建开始: 1.pom.xml引入Redis依赖的jar: <!-- jedi ...
- Hadoop记录-hadoop2.x常用端口及定义方法
Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以及HTTP访问.而随着Hadoop周边组件的增多,完全记不住哪个端口对应哪个应用,特收集记录如 ...
- C#设计模式(8)——外观模式
1.外观模式介绍 外观模式也被叫做门面模式,这种模式的作用是:隐藏系统的复杂性,并向客户端提供了一个可以访问系统的统一接口,这个统一的接口组合了子系统的多个接口.使用统一的接口使得子系统更容易被访问或 ...