spring cloud hystrix
引入依赖
-----------------------------------------------------------------
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<!--hystrix-dashboard 监控-->
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
---------------------------------------------
<code>spring-cloud-starter-hystrix</code> 核心jar
<code>spring-cloud-starter-hystrix-dashboard</code> 监控jar
使用<code>EnableCircuitBreaker</code>或者 <code>EnableHystrix</code> 表明<code>Spring boot</code>工程启用hystrix,两个注解是等价的.
--------------------------------------------------------------------
package com.lkl.springcloud.hystrix;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableCircuitBreaker
@EnableHystrixDashboard
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
----------------------------------------------------------------------
其中<code>EnableHystrixDashboard</code>注解表示启动对hystrix的监控,后面会用到
随后模拟一个调用三方依赖服务
<code>controller</code>-> <code>service</code> -> <code>dependency service</code>
---------------------------------------------------------------------
package com.lkl.springcloud.hystrix.controller;
import com.lkl.springcloud.hystrix.service.HystrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* 模拟一个对外的接口
*/
@RestController
public class HystrixController {
@Autowired
private HystrixService service;
/**
* 调用依赖的服务
*/
@RequestMapping("/call")
public String callDependencyService(){
return service.callDependencyService();
-------------------------------------------------------------------
package com.lkl.springcloud.hystrix.service;
import org.springframework.stereotype.Service;
* 依赖服务
@Service
public class HystrixService {
private CallDependencyService dependencyService;
public String callDependencyService() {
return dependencyService.mockGetUserInfo();
-----------------------------------------------------------------------
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Component;
import java.util.Random;
* 调用依赖服务,通过hystrix包装调用服务
@Component
public class CallDependencyService {
private Random random = new Random();
* 模拟获取用户信息(通过网络调用)
* @return
@HystrixCommand(fallbackMethod = "fallback")
public String mockGetUserInfo(){
int randomInt= random.nextInt(10) ;
if(randomInt<8){ //模拟调用失败情况
throw new RuntimeException("call dependency service fail.");
}else{
return "UserName:liaokailin;number:"+randomInt;
}
public String fallback(){
return "some exception occur call fallback method.";
------------------------------------------------------------------------------
<code>HystrixCommand</code> 表明该方法为hystrix包裹,可以对依赖服务进行隔离、降级、快速失败、快速重试等等hystrix相关功能
该注解属性较多,下面讲解其中几个
fallbackMethod 降级方法
commandProperties 普通配置属性,可以配置HystrixCommand对应属性,例如采用线程池还是信号量隔离、熔断器熔断规则等等
ignoreExceptions 忽略的异常,默认<code>HystrixBadRequestException</code>不计入失败
groupKey() 组名称,默认使用类名称
commandKey 命令名称,默认使用方法名
<a href="https://s2.51cto.com/wyfs02/M01/06/E9/wKiom1nAht2RIg_wAAiopUwoKco221.png-wh_500x0-wm_3-wmp_4-s_1035124941.png" target="_blank"></a>
<a href="https://s5.51cto.com/wyfs02/M01/A5/99/wKioL1nAhr6zFMtMAAYKpJGISTQ964.png-wh_500x0-wm_3-wmp_4-s_1800811931.png" target="_blank"></a>
ok ~ it’s work ! more about is here
1:Hystrix使用命令模式HystrixCommand(Command)包装依赖调用逻辑,每个命令在单独线程中/信号授权下执行。
2:可配置依赖调用超时时间,超时时间一般设为比99.5%平均时间略高即可.当调用超时时,直接返回或执行fallback逻辑。
3:为每个依赖提供一个小的线程池(或信号),如果线程池已满调用将被立即拒绝,默认不采用排队.加速失败判定时间。
4:依赖调用结果分:成功,失败(抛出异常),超时,线程拒绝,短路。 请求失败(异常,拒绝,超时,短路)时执行fallback(降级)逻辑。
5:提供熔断器组件,可以自动运行或手动调用,停止当前依赖一段时间(10秒),熔断器默认错误率阈值为50%,超过将自动运行。
6:提供近实时依赖的统计和监控
本文转自 独孤环宇 51CTO博客,原文链接:http://blog.51cto.com/snowtiger/1966590