spring cloud 学习(4) - hystrix 服务熔断处理
hystrix 是一个专用于服务熔断处理的开源项目,当依赖的服务方出现故障不可用时,hystrix有一个所谓的断路器,一但打开,就会直接拦截掉对故障服务的调用,从而防止故障进一步扩大(类似中电路中的跳闸,保护家用电器)。
使用步骤:(仍然在之前的示例代码上加以改造)
一、添加hystrix依赖
compile 'org.springframework.cloud:spring-cloud-starter-hystrix'
二、在需要熔断的方法上添加注解
package com.cnblogs.yjmyzz.spring.cloud.study.service.controller; import com.cnblogs.yjmyzz.spring.cloud.study.dto.UserDTO;
import com.cnblogs.yjmyzz.spring.cloud.study.service.client.UserFeignClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
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 java.util.Random; @RestController
public class OrderController { @Autowired
private UserFeignClient userFeignClient; private final Random rnd = new Random(System.currentTimeMillis()); @GetMapping("/order/{userId}/{orderNo}")
@HystrixCommand(fallbackMethod = "findOrderFallback", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
})
public String findOrder(@PathVariable Integer userId, @PathVariable String orderNo) throws InterruptedException {
Thread.sleep(rnd.nextInt(2000));
UserDTO user = userFeignClient.findUser(userId);
if (user != null) {
return user.getUserName() + " 的订单" + orderNo + " 找到啦!";
}
return "用户不存在!";
} public String findOrderFallback(Integer userId, String orderNo) {
return "订单查找失败!";
}
}
注意findOrder上添加的HystrixCommand注解,大概意思是,如果这个方法调用失败,会切换到备用方法findOrderFallback上,而且还设置了一个超时时间1000ms,即1秒。换句话说,如果findOrder方法没有在1s内返回结果,也算调用失败,同样会切换到备用方法findOrderFallback上。
注:为了方便演示,故意在findOrder上随机停了2秒内的一段时间,所以预期这个方法,应该会偶尔超时,偶尔正常。
关于HystrixProperty的更多属性,可参考github上的官方文档:https://github.com/Netflix/Hystrix/wiki/Configuration
三、main入口上启用hytrix熔断
@EnableDiscoveryClient
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker
public class ServiceConsumer { public static void main(String[] args) {
SpringApplication.run(ServiceConsumer.class, args);
}
}
启用后,访问http://localhost:8002/order/1/100 ,可以看到类似以下输出:
正常时,返回类似上图的输出,如果超时,将返回下图:
此外,spring-boot的acturator也提供了health端点来查看hystrix状态,查看http://localhost:8002/health
这个表示hystrix的断路器未打开,如果 http://localhost:8002/order/1/100 这个页面狂刷(默认要5秒内失败20次以上),或者干脆把service-provider停掉,这个状态会变成:
表明此时断路器是打开的。
四、hystrix监控
health端点只能看到断路器的整体状态,但是对于细节展示不够详细,默认情况下,只要启用了hystrix功能,还会暴露一个端点hystrix.stream
访问 http://localhost:8002/hystrix.stream 可以查看详细的数据
注:这个页面会实时不断输出新的内容(如果有的话),首次访问的话,如果看到一直转圈,但是没有任何内容,说明这时服务对应的方法没人调用,可以访问findOrder方法后,再来看这个页面。
显然,一堆密密麻麻的文字,没有人会喜欢看,spring-cloud早就想到这一点了,提供了一个hystrix-dashboard的功能,可以用图形化的界面来解读这些数据。
再起一个项目,名为hystrix-dashboard,build.gradle参考下面:
buildscript {
repositories {
maven {
url "http://maven.aliyun.com/nexus/content/groups/public/"
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.4.RELEASE")
}
} apply plugin: 'org.springframework.boot' dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Dalston.RELEASE"
}
} dependencies {
compile 'org.springframework.cloud:spring-cloud-starter-eureka'
compile 'org.springframework.cloud:spring-cloud-starter-hystrix-dashboard'
compile 'org.springframework.boot:spring-boot-starter-actuator'
}
main函数如下:
package com.cnblogs.yjmyzz.spring.cloud.study.hystrix; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; /**
* Created by yangjunming on 2017/6/30.
*/
@SpringBootApplication
@EnableHystrixDashboard
@EnableDiscoveryClient
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
当然,这也是一个微服务,也可以注册到eureka上,参考下面的配置:
spring:
application:
name: hystrix-dashboard
server:
port: 8035 eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://yjmyzz:123456@server1:8100/eureka,http://yjmyzz:123456@server2:8200/eureka,http://yjmyzz:123456@server3:8300/eureka
启用成功后,访问http://localhost:8035/hystrix,会出现类似下面这个界面:
第1个输入框里,填写要监控的hystrix.steam地址,然后title这里起一个名字即可,然后点击monitor steam,就能看到图表:
这显然比纯文字友好多了。还有一个问题,如果有多个hystrix.stream地址同时监控,或者把多个地址的数据汇总起来,该怎么弄?github上有一个turbine ,就是专门为解决这个问题的,大家可以自行研究下。
最后,附上文中示例代码地址:https://github.com/yjmyzz/spring-cloud-demo
spring cloud 学习(4) - hystrix 服务熔断处理的更多相关文章
- Spring Cloud第五篇 | 服务熔断Hystrix
本文是Spring Cloud专栏的第五篇文章,了解前四篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Clo ...
- spring cloud学习一--Eureka服务注册与发现
spring cloud Eureka是基于Netflix Eureka服务发现注册产品的二次封装,它提供了服务注册功能(Service Registry)和服务发现功能(Service Discov ...
- Spring Cloud 学习 (四) Hystrix & Hystrix Dashboard & Turbine
在复杂的分布式系统中,可能有几十个服务相互依赖,这些服务由于某些原因,例如机房的不可靠性.网络服务商的不可靠性等,导致某个服务不可用 . 如果系统不隔离该不可用的服务,可能会导致整个系统不可用.Hys ...
- spring cloud学习(二) 调用服务
spring-cloud调用服务有两种方式,一种是Ribbon+RestTemplate, 另外一种是Feign. Ribbon是一个基于HTTP和TCP客户端的负载均衡器,其实feign也使用了ri ...
- spring cloud 学习(9) - turbine stream无法在eureka注册的解决办法
turbine是啥就不多解释了,初次接触的可以移步spring cloud 学习(4) - hystrix 服务熔断处理 拉到最后看一下,turbine stream默认情况下启动成功后,eureka ...
- Spring Cloud第九篇 | 分布式服务跟踪Sleuth
本文是Spring Cloud专栏的第九篇文章,了解前八篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...
- spring cloud 学习之服务消费者(rest+ribbon)
学习自 http://blog.csdn.net/forezp/article/details/81040946 方志朋的博客 在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于h ...
- Spring Cloud学习(一):Eureka服务注册与发现
1.Eureka是什么 Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的. Eureka ...
- spring cloud + mybatis 分布式 微服务 b2b2c 多商户商城 全球部署方案
用java实施的电子商务平台太少了,使用spring cloud技术构建的b2b2c电子商务平台更少,大型企业分布式互联网电子商务平台,推出PC+微信+APP+云服务的云商平台系统,其中包括B2B.B ...
随机推荐
- UVALive 6467 Strahler Order
> 题目链接 题意:给定一个有向图,顶点代表水池,入度为零的定点代表水源,等级是1,他们延河道(有向边)冲撞,对于普通的水池来说,题目给定判断它等级的两个准则,问出度为零的那个点的等级是多少. ...
- 第11月第21天 php引用 codeigniter cakephp
1. class CI_Controller { private static $instance; /** * Constructor */ public function __construct( ...
- [Alg] 尺取法
尺取法是在线性结构中进行搜寻满足某一条件的区间的方法. 该方法保存两个索引--首索引begin.尾索引end.判断 [begin, end] 区间是否满足条件. 移动 [begin, end] 区间的 ...
- Nginx是什么,有什么优点?为什么选择Nginx做web服务器软件?(经典经典)
1.基础知识 代理服务器: 一般是指局域网内部的机器通过代理服务器发送请求到互联网上的服务器,代理服务器一般作用在客户端.应用比如:GoAgent,FQ神器. 一个完整的代理请求过程为:客 ...
- UFLDL 教程学习笔记(一)
ufdl的新教程,从基础学起.第一节讲的是线性回归.主要目的是熟悉目标函数,计算梯度和优化. 按着教程写完代码后,总是编译出错,一查是mex的原因,实在不想整了. 这位博主用的是向量,比较简洁:htt ...
- Java 组合
组合: 在新类中产生现有类的对象,由于新的类是由现有类的对象所组成,所以这种方法称为组合 组合和继承都允许在新的类中放置对象,组合时显示的这样做,而继承则是隐式的这样做 组合技术通常用于想在新类中使用 ...
- SP14932 【LCA - Lowest Common Ancestor】
专业跟队形 唯一一个有$\LaTeX$的 裸的$LCA$,我用的是$Tarjan~LCA$,注意两点相同特判 #include<iostream> #include<cstdio&g ...
- javaweb笔记五
JSP:java server page服务器脚本语言.(脚本===插件),是一种在html代码中,嵌入java代码的方式.解决servlet产生动态页面缺陷而产生的一门技术.js:客户端脚本语言js ...
- 在centos中修改yum源为阿里源
cd /etc/yum.repos.d 备份旧的配置文件:mv CentOS-Base.repo CentOS-Base.repo.bak 下载阿里源的文件: wget -O CentOS-Base. ...
- spring-cloud-sleuth+zipkin追踪服务实现(四)
1.前言 在上一篇spring-cloud-sleuth+zipkin追踪服务实现(三)的处理实现后,很多朋友告诉我,在zipkin server的管理页面无法看到项目依赖关系. 当时也没有多想,以为 ...