Spring Cloud Netflix Hystrix
目录
一、 Hystrix 简介
1 什么是灾难性雪崩效应
2 什么是 Hystrix
二、 服务降级
1 修改 application service 代码
2 application client POM 依赖
3 application client 容错处理代码
4 application client 配置文件 application.yml
5 application client 启动类
三、 服务熔断
1 application client POM 依赖
2 application client 容错处理代码
3 application client 配置文件 application.yml
4 application client 启动类
四、 请求缓存
1 修改 application service 代码
2 application client POM 依赖
3 application client 容错处理代码
4 application client 配置文件 application.yml
5 application client 启动类
五、 Openfeign 的雪崩处理
1 服务降级
1.1 POM 依赖
1.2 application client 接口
1.3 application client 接口实现
1.4 application client 配置文件 application.yml
1.5 applicaiton client 启动类
2 服务熔断
六、 可视化的数据监控 Hystrix-dashboard
1 POM 依赖
2 配置文件 application.yml
3 启动类
4 访问 Hystrix 监控数据
一、 Hystrix 简介
1 什么是灾难性雪崩效应
2 什么是 Hystrix
二、 服务降级
1 修改 application service 代码
@RestControllerpublic class ServiceController {@GetMappingpublic Object first(){try {// 用于模拟远程服务调用延时。Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return "测试 Spring Cloud Netflix Ribbon 开发服务提供者";}}
2 application client POM 依赖
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency>
3 application client 容错处理代码
@Servicepublic class ClientServiceImpl implements ClientService {@Autowiredprivate LoadBalancerClientloadBalancerClient;/*** @HystrixCommand - 开启 Hystrix 容错处理的注解。代表当前方法如果出现服务调用问题,使用 Hystrix 容错处理逻辑来处理*属性 fallbackMethod - 如果当前方法调用服务,远程服务出现问题的时候,调用本地的哪个方法得到托底数据。*/@HystrixCommand(fallbackMethod = "downgradeFallback")@Overridepublic String first() {ServiceInstance si = this.loadBalancerClient.choose("application-service");StringBuilder sb = new StringBuilder();sb.append("http://").append(si.getHost()).append(":").append(si.getPort()).append("/");System.out.println("本次访问的 service 是: " + sb.toString());RestTemplate rt = new RestTemplate();ParameterizedTypeReference<String> type = new ParameterizedTypeReference<String>(){};ResponseEntity<String> response =rt.exchange(sb.toString(), HttpMethod.GET, null, type);Stringresult = response.getBody();returnresult;}// 本地容错方法,只有远程服务调用过程出现问题的时候,才会调用此方法,获取托底数据。保证服务完整性。private String downgradeFallback(){return "服务降级方法返回托底数据";}}
4 application client 配置文件 application.yml
5 application client 启动类
三、 服务熔断
1 application client POM 依赖
2 application client 容错处理代码
@Servicepublic class ClientServiceImplimplements ClientService {@Autowiredprivate LoadBalancerClientloadBalancerClient;/*** @HystrixCommand - 开启 Hystrix 容错处理的注解。代表当前方法如果出现服务调用问题,使用 Hystrix 容错处理逻辑来处理*属性 fallbackMethod - 如果当前方法调用服务,远程服务出现问题的时候,调用本地的哪个方法得到托底数据。*/@HystrixCommand(fallbackMethod = "downgradeFallback",commandProperties = {// 默认 20 个;10s 内请求数大于 20 个时就启动熔断器,当请求符合熔断条件时将触发 fallback 逻辑@HystrixProperty(name =HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "10"),// 请求错误率大于 50%时就熔断,然后 for 循环发起请求,当请求符合熔断条件时将触发@HystrixProperty(name =HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "50"),// 默认 5 秒;熔断多少秒后去尝试请求@HystrixProperty(name =HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value ="5000")})@Overridepublic String first() {ServiceInstance si =this.loadBalancerClient.choose("application-service");StringBuilder sb = new StringBuilder();sb.append("http://").append(si.getHost()).append(":").append(si.getPort()).append("/");System.out.println("本次访问的 service 是: " + sb.toString());RestTemplate rt = new RestTemplate();ParameterizedTypeReference<String> type =new ParameterizedTypeReference<String>() {};ResponseEntity<String> response =rt.exchange(sb.toString(), HttpMethod.GET, null, type);Stringresult = response.getBody();returnresult;}// 本地容错方法,只有远程服务调用过程出现问题的时候,才会调用此方法,获取托底数据。保证服务完整性。private String downgradeFallback(){return "服务降级方法返回托底数据";}
3 application client 配置文件 application.yml
4 application client 启动类
四、 请求缓存
1 修改 application service 代码
@RestControllerpublic class ServiceController {@PostMapping("/testPost")public Object testPost(){System.out.println("testPost method run");return "写操作返回";}@GetMapping("/testGet")public Object testGet(){System.out.println("testGet method run");return "读操作返回";}@GetMappingpublic Object first(){System.out.println("run");try {// 用于模拟远程服务调用延时。Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return "测试 Spring Cloud Netflix Ribbon 开发服务提供者";}}
2 application client POM 依赖
3 application client 容错处理代码
/***在类上,增加@CacheConfig 注解,用来描述当前类型可能使用 cache 缓存。*如果使用缓存,则缓存数据的 key 的前缀是 cacheNames。*cacheNames 是用来定义一个缓存集的前缀命名的,相当于分组。*/@CacheConfig(cacheNames = "test.hystrix.caches")@Servicepublic class ClientServiceImpl implements ClientService {@Autowiredprivate LoadBalancerClient loadBalancerClient;/*** @HystrixCommand - 开启 Hystrix 容错处理的注解。代表当前方法如果出现服务调用问题,使用 Hystrix 容错处理逻辑来处理*属性 fallbackMethod - 如果当前方法调用服务,远程服务出现问题的时候,调用本地的哪个方法得到托底数据。*/@HystrixCommand(fallbackMethod = "downgradeFallback",commandProperties = {// 默认 20 个;10s 内请求数大于 20 个时就启动熔断器,当请求符合熔断条件时将触发 fallback 逻辑@HystrixProperty(name =HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "10"),// 请求错误率大于 50%时就熔断,然后 for 循环发起请求,当请求符合熔断条件时将触发@HystrixProperty(name =HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "50"),// 默认 5 秒;熔断多少秒后去尝试请求@HystrixProperty(name =HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS,value = "5000")})@Overridepublic String first() {ServiceInstance si = this.loadBalancerClient.choose("application-service");StringBuilder sb = new StringBuilder();sb.append("http://").append(si.getHost()).append(":").append(si.getPort()).append("/");System.out.println("本次访问的 service 是: " + sb.toString());RestTemplate rt = new RestTemplate();ParameterizedTypeReference<String> type =new ParameterizedTypeReference<String>() {};ResponseEntity<String> response =rt.exchange(sb.toString(), HttpMethod.GET, null, type);Stringresult = response.getBody();return result;}/***@Cacheable - 将当期方法的返回值缓存到 cache 中。*只要方法增加了@Cacheable 注解,每次调用当前方法的时候,spring cloud都会先访问 cache 获取数据,*如果 cache 中没有数据,则访问远程服务获取数据。远程服务返回数据,先保存在 cache 中,再返回给客户端。*如果 cache 中有数据,则直接返回 cache 中的数据,不会访问远程服务。*请求缓存会有缓存数据不一致的可能。缓存数据过期、失效、脏数据等情况。*一旦使用了请求缓存来处理幂等性请求操作。则在非幂等性请求操作中必须管理缓存。避免缓存数据的错误。* @return*/@Override@Cacheable("myCache")public String testGet() {ServiceInstance si = this.loadBalancerClient.choose("application-service");StringBuilder sb = new StringBuilder();sb.append("http://").append(si.getHost()).append(":").append(si.getPort()).append("/testGet");System.out.println("本次访问的 service 是: " + sb.toString());RestTemplate rt = new RestTemplate();ParameterizedTypeReference<String> type =new ParameterizedTypeReference<String>() { };ResponseEntity<String> response =rt.exchange(sb.toString(), HttpMethod.GET, null, type);String result = response.getBody();returnresult;}/***@CacheEvict - 管理缓存。根据参数 key 删除缓存中对应的缓存数据。*@return*/@Override@CacheEvict("myCache")public String testPost() {ServiceInstance si = this.loadBalancerClient.choose("application-service");StringBuilder sb = new StringBuilder();sb.append("http://").append(si.getHost()).append(":").append(si.getPort()).append("/testPost");System.out.println("本次访问的 service 是: " + sb.toString());RestTemplate rt = new RestTemplate();ParameterizedTypeReference<String> type =new ParameterizedTypeReference<String>() {};ResponseEntity<String> response =rt.exchange(sb.toString(), HttpMethod.POST, null, type);String result = response.getBody();return result;}// 本地容错方法,只有远程服务调用过程出现问题的时候,才会调用此方法,获取托底数据。保证服务完整性。private String downgradeFallback(){return "服务降级方法返回托底数据";}}
4 application client 配置文件 application.yml
5 application client 启动类
五、 Openfeign 的雪崩处理
1 服务降级
1.1 POM 依赖
1.2 application client 接口
/*** 注解属性 fallback - 当前接口远程调用服务发生问题时,使用哪一个对象中的对应方法用于实现容错逻辑。*Openfeign 技术中,容错处理是使用当前接口的实现类实现的。*实现类中的方法实现,就是对应的容错 fallback 处理逻辑。*/@FeignClient(name="openfeign-service", fallback =FirstClientServiceImpl.class)public interface FirstClientService extends FirstServiceAPI {}
1.3 application client 接口实现
/***当前类型的实例必须由 Spring 容器管理,需要使用@Component 注解描述。*每个实现方法对应远程服务调用的容错处理逻辑。*/@Componentpublic class FirstClientServiceImpl implements FirstClientService {@Overridepublic List<String> testFeign(){return Arrays.asList("Openfeign 返回托底数据");}@Overridepublic User getMultiParams(Integer id, String name) {User user = new User();user.setId(0);user.setUsername("托底数据");user.setRemark("getMultiParams");return user;}@Overridepublic User postMultiParams(Integer id, String name) {User user = new User();user.setId(0);user.setUsername("托底数据");user.setRemark("postMultiParams");return user;}@Overridepublic User postObjectParam(User pojo) {User user = new User();user.setId(0);user.setUsername("托底数据");user.setRemark("postObjectParam");return user;}}
1.4 application client 配置文件 application.yml
1.5 applicaiton client 启动类
2 服务熔断
六、 可视化的数据监控 Hystrix-dashboard
1 POM 依赖
2 配置文件 application.yml
3 启动类
4 访问 Hystrix 监控数据
进入后的监控视图界面具体含义如下:
Spring Cloud Netflix Hystrix相关推荐
- Spring Cloud Netflix Hystrix介绍和使用
前面我们搭建了具有服务降级功能的Hystrix客户端,现在我们来详细了解下Hystrix的一些功能. Hystrix的意思是豪猪,大家都知道,就是长满刺的猪...实际上,它表明了该框架的主要功能:自我 ...
- Spring Cloud Netflix五大组件简介
微服务与微服务架构 微服务的优缺点 优点 缺点 Dubbo与Spring Cloud Spring Cloud Netflix Eureka Eureka的自我保护机制 Eureka和ZooKeepe ...
- Spring Cloud Netfix Hystrix(断路器)
一.灾难性雪崩 造成灾难性雪崩效应的原因,可以简单归结为下述三种: 服务提供者(Application Service)不可用.如:硬件故障.程序BUG.缓存击穿.并发请求量过大等. 重试加大流量.如 ...
- Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失(续)
前言 上篇文章<Spring Cloud中Hystrix 线程隔离导致ThreadLocal数据丢失>我们对ThreadLocal数据丢失进行了详细的分析,并通过代码的方式复现了这个问题. ...
- Spring Cloud Netflix项目进入维护模式之我见
这两天看到一则新闻:https://spring.io/blog/2018/12/12/spring-cloud-greenwich-rc1-available-now#spring-cloud-ne ...
- SpringCloud学习笔记(1)- Spring Cloud Netflix
文章目录 SpringCloud学习笔记(1)- Spring Cloud Netflix 单体应用存在的问题 Spring Cloud Eureka Eureka Server代码实现 Eureka ...
- Spring Cloud Netflix之Eureka上篇
前言:Spring Cloud NetFlix这个项目对NetFlix中一些久经考验靠谱的服务发现,熔断,网关,智能路由,以及负载均衡等做了封装,并通过注解的或简单配置的方式提供给Spring Clo ...
- Spring Cloud Netflix中文文档翻译笔记
原文地址:http://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/1.2.2.RELEASE/ Spring Cloud Net ...
- Spring Cloud之Hystrix服务容错
Spring Cloud之Hystrix服务容错 Hystrix的概述 Hystrix的使用 相关依赖 Eureka注册中心 服务提供者 服务消费者 执行测试 @HystrixCommand详解 服务 ...
最新文章
- linux pureftp mysql_在Ubuntu下安装apache2+php5+mysql5+pureftp+ftp
- 批量 kill mysql 中运行时间长的sql
- linux下用c 开发web,用C一步步开发web服务器(2)
- python3 遍历列表得到序号索引和值
- python从零基础到项目实战怎么样-Python 3.x网络爬虫从零基础到项目实战
- MySQL 5.7.11 重置root密码
- 3-5比较和Fancy Indexing
- 容器的基础 XmlBeanFactory(下篇)
- how to reference the parent form from the WPF control(Control in ElementHost)
- C++函数的返回值是指针
- OpenCASCADE Texture Mapping
- java答题小程序代码_分享一个java技术开发的微信答题小程序仿头脑王者源码
- QTabWidget 删除tab
- 阿里云服务器安全组授权对象ip设置
- 人工智能设计------------意识可控与意识不可控(三)
- 计算机被限制压缩包打不开怎么办,【解决】压缩文件打不开,压缩文件损坏怎么修复?...
- android 手机壁纸制作教程,教程:让你的手机桌面瞬间高逼格!
- 谈 Scratch 版“植物大战僵尸”
- 服务器操作系统的特点,服务器操作系统的特点
- uni-app对接支付