spring cloud hystrix简介

hystrix通过服务隔离、熔断(也可以称为断路)、降级等手段控制依赖服务的延迟与失败。netflix hystrix

本篇中主要讲解对springcloud 对hystrix的集成,至于如何单独使用hystrix可以参考我分享的pdf.

spring cloud hystrix

引入依赖

-----------------------------------------------------------------

<!--hystrix-->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-hystrix</artifactId>

</dependency>

<!--hystrix-dashboard 监控-->

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>

</dependency>

---------------------------------------------

spring-cloud-starter-hystrix核心jar

spring-cloud-starter-hystrix-dashboard监控jar

EnableCircuitBreaker

使用EnableCircuitBreaker或者EnableHystrix表明Spring boot工程启用hystrix,两个注解是等价的.

Application.Java

--------------------------------------------------------------------

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);

}

}

----------------------------------------------------------------------

其中EnableHystrixDashboard注解表示启动对hystrix的监控,后面会用到

随后模拟一个调用三方依赖服务

controller->service->dependency service

---------------------------------------------------------------------

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.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;


/**

* 依赖服务

*/

@Service

public class HystrixService {


@Autowired

private CallDependencyService dependencyService;

public String callDependencyService() {

return dependencyService.mockGetUserInfo();

}

}

-----------------------------------------------------------------------

package com.lkl.springcloud.hystrix.service;


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.";

}

}

------------------------------------------------------------------------------

HystrixCommand表明该方法为hystrix包裹,可以对依赖服务进行隔离、降级、快速失败、快速重试等等hystrix相关功能
该注解属性较多,下面讲解其中几个

fallbackMethod 降级方法

commandProperties 普通配置属性,可以配置HystrixCommand对应属性,例如采用线程池还是信号量隔离、熔断器熔断规则等等

ignoreExceptions 忽略的异常,默认HystrixBadRequestException不计入失败

groupKey() 组名称,默认使用类名称

commandKey 命令名称,默认使用方法名

Dashboard

运行工程,可以访问http://localhost:9090/hystrix.stream获取dashboard信息,默认最大打开5个终端获取监控信息,可以增加delay参数指定获取监控数据间隔时间

直接访问hystrix.stream肯定是不明智的,官方提供监控hystrix-dashboard-#.#.#.war包,下载后放入tomcat中,得到如下界面

输入http://localhost:9090/hystrix.stream点击Monitor stream进入Dashboard界面

访问http://localhost:/9090/call观察进入Dashboard变化

ok ~ it’s work ! more about ishere


Hystrix如何解决依赖隔离

1:Hystrix使用命令模式HystrixCommand(Command)包装依赖调用逻辑,每个命令在单独线程中/信号授权下执行。

2:可配置依赖调用超时时间,超时时间一般设为比99.5%平均时间略高即可.当调用超时时,直接返回或执行fallback逻辑。

3:为每个依赖提供一个小的线程池(或信号),如果线程池已满调用将被立即拒绝,默认不采用排队.加速失败判定时间。

4:依赖调用结果分:成功,失败(抛出异常),超时,线程拒绝,短路。 请求失败(异常,拒绝,超时,短路)时执行fallback(降级)逻辑。

5:提供熔断器组件,可以自动运行或手动调用,停止当前依赖一段时间(10秒),熔断器默认错误率阈值为50%,超过将自动运行。

6:提供近实时依赖的统计和监控