add hystrix demo back, and improve the usage to follow the layers

This commit is contained in:
Calvin
2013-10-16 13:11:18 +08:00
parent 0e1b825699
commit 04c0364f73
3 changed files with 9 additions and 57 deletions
@@ -16,11 +16,11 @@ import org.springside.modules.mapper.BeanMapper;
import org.springside.modules.utils.Threads;
/**
* 模拟被Service所依赖的远程Resource.
* 模拟被Service所依赖的Resource.
*/
@Controller
public class DependencyResourceController {
public static final int TIMEOUT = 10000;
public static final int TIMEOUT = 15000;
private String status = "normal";
@@ -28,7 +28,7 @@ public class DependencyResourceController {
private AccountEffectiveService accountService;
/**
* 根据控制器中的状态而演示不同的行为,如正常返回,10秒后返回或直接报错
* 根据控制器中的状态而演示不同的行为,如正常返回,15秒后返回或直接报错
*/
@RequestMapping(value = "/hystrix/resource/{id}", method = RequestMethod.GET)
@ResponseBody
@@ -6,22 +6,20 @@ import org.springframework.web.client.RestTemplate;
import org.springside.examples.showcase.webservice.rest.UserDTO;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixThreadPoolProperties;
public class GetUserCommand extends HystrixCommand<UserDTO> {
private static Logger logger = LoggerFactory.getLogger(GetUserCommand.class);
private RestTemplate restTemplate;
private Long id;
private RestTemplate restTemplate = new RestTemplate();
protected GetUserCommand(Long id, boolean runInNewThread) {
protected GetUserCommand(Setter config, RestTemplate restTemplate, Long id) {
// 配置Command
super(configCommand(runInNewThread));
super(config);
// 传入
this.restTemplate = restTemplate;
// 传入请求参数
this.id = id;
}
@@ -34,46 +32,4 @@ public class GetUserCommand extends HystrixCommand<UserDTO> {
logger.info("Access restful resource");
return restTemplate.getForObject("http://localhost:8080/showcase/hystrix/resource/{id}", UserDTO.class, id);
}
/**
* 演示Command的各种配置项.
*/
protected static Setter configCommand(boolean runInNewThread) {
// 设置名称 //
Setter config = Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")).andCommandKey(
HystrixCommandKey.Factory.asKey("GetUserCommand"));
// 设置短路计算规则 //
config.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
// 至少多少请求在rolling window内发生,开始触发短路的计算,默认为20, 设为3方便演示。
.withCircuitBreakerRequestVolumeThreshold(3)
// 多少百分比的fail在rolling windows内发生,计算为短路。默认为50%,无改变。
.withCircuitBreakerErrorThresholdPercentage(50)
// rolling windows 长度,默认为20秒,改为60秒方便演示。
.withMetricsRollingStatisticalWindowInMilliseconds(60000)
// rolling windows里桶的数量,默认为为20,因为rolling window增大,需相应增加让短路计算保持秒的精度。
.withMetricsRollingStatisticalWindowBuckets(60)
// 计算短路的时间间隔,默认是500毫秒,无改变。
.withMetricsHealthSnapshotIntervalInMilliseconds(500));
// 设置短路后的保护时间 //
config.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
.withCircuitBreakerSleepWindowInMilliseconds(20000));
// 使用Hystrix线程池的异步执行方式
if (runInNewThread) {
config.andCommandPropertiesDefaults(HystrixCommandProperties.Setter()
// 线程超时,默认为1秒,设为2秒方便演示
.withExecutionIsolationThreadTimeoutInMilliseconds(2000))
// 线程池属性, 线程池大小,默认为10,无改变。待执行队列的大小,默认为5,无改变。
.andThreadPoolPropertiesDefaults(
HystrixThreadPoolProperties.Setter().withCoreSize(10).withQueueSizeRejectionThreshold(5));
} else {
// 使用Invoker原有线程的方式
}
return config;
}
}
-4
View File
@@ -25,12 +25,8 @@
<ehcache.version>2.6.6</ehcache.version>
<spymemcached.version>2.10.1</spymemcached.version>
<hibernate-validator.version>4.3.1.Final</hibernate-validator.version>
<<<<<<< HEAD
<jolokia.version>1.1.4</jolokia.version>
=======
<hystrix.version>1.3.7</hystrix.version>
<jolokia.version>1.1.3</jolokia.version>
>>>>>>> add hystrix demo back.
<jackson.version>2.2.3</jackson.version>
<slf4j.version>1.7.5</slf4j.version>
<logback.version>1.0.13</logback.version>