From 04c0364f7384de0066685ffecf7a67122d21cf8a Mon Sep 17 00:00:00 2001 From: Calvin Date: Wed, 16 Oct 2013 13:11:18 +0800 Subject: [PATCH] add hystrix demo back, and improve the usage to follow the layers --- .../DependencyResourceController.java | 6 +- .../demos/hystrix/service/GetUserCommand.java | 56 ++----------------- modules/parent/pom.xml | 4 -- 3 files changed, 9 insertions(+), 57 deletions(-) diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/dependency/DependencyResourceController.java b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/dependency/DependencyResourceController.java index 2d628c53..6fd8a3b3 100644 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/dependency/DependencyResourceController.java +++ b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/dependency/DependencyResourceController.java @@ -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 diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/service/GetUserCommand.java b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/service/GetUserCommand.java index 0d53ab20..e555251b 100644 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/service/GetUserCommand.java +++ b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/service/GetUserCommand.java @@ -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 { 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 { 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; - } - } diff --git a/modules/parent/pom.xml b/modules/parent/pom.xml index faeda325..fa3a2290 100644 --- a/modules/parent/pom.xml +++ b/modules/parent/pom.xml @@ -25,12 +25,8 @@ 2.6.6 2.10.1 4.3.1.Final -<<<<<<< HEAD 1.1.4 -======= 1.3.7 - 1.1.3 ->>>>>>> add hystrix demo back. 2.2.3 1.7.5 1.0.13