diff --git a/examples/showcase/pom.xml b/examples/showcase/pom.xml index 66096dec..bb44b463 100644 --- a/examples/showcase/pom.xml +++ b/examples/showcase/pom.xml @@ -188,18 +188,6 @@ jedis - - - com.netflix.hystrix - hystrix-core - - - - com.netflix.hystrix - hystrix-metrics-event-stream - ${hystrix.version} - - net.spy diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/resource/DependencyResourceController.java b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/resource/DependencyResourceController.java deleted file mode 100644 index ed157808..00000000 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/resource/DependencyResourceController.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.springside.examples.showcase.demos.hystrix.resource; - -import org.hibernate.service.spi.ServiceException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springside.examples.showcase.demos.hystrix.web.HystrixController; -import org.springside.examples.showcase.entity.User; -import org.springside.examples.showcase.service.AccountEffectiveService; -import org.springside.examples.showcase.webservice.rest.UserDTO; -import org.springside.modules.mapper.BeanMapper; -import org.springside.modules.utils.Threads; - -/** - * 被Service所依赖的Resource. - */ -@Controller -public class DependencyResourceController { - public static final int TIMEOUT = 10000; - - @Autowired - private AccountEffectiveService accountService; - - /** - * 根据控制器中的状态而演示不同的行为,如正常返回,20秒无返回或直接报错 - */ - @RequestMapping(value = "/hystrix/resource/{id}", method = RequestMethod.GET) - @ResponseBody - public UserDTO getUser(@PathVariable("id") Long id) { - if ("normal".equals(HystrixController.status)) { - return handleRequest(id); - } - - if ("timeout".equals(HystrixController.status)) { - Threads.sleep(TIMEOUT); - return handleRequest(id); - } - - if ("fail".equals(HystrixController.status)) { - throw new ServiceException("Server Exception"); - } - - return null; - } - - /** - * 备用的节点,永远返回正确结果。 - */ - @RequestMapping(value = "/hystrix/resource/standby/{id}", method = RequestMethod.GET) - @ResponseBody - public UserDTO getUserOnStandby(@PathVariable("id") Long id) { - return handleRequest(id); - } - - private UserDTO handleRequest(Long id) { - User user = accountService.getUser(id); - UserDTO dto = BeanMapper.map(user, UserDTO.class); - dto.setTeamId(user.getTeam().getId()); - return dto; - } -} 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 deleted file mode 100644 index e9f239f8..00000000 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/service/GetUserCommand.java +++ /dev/null @@ -1,78 +0,0 @@ -package org.springside.examples.showcase.demos.hystrix.service; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.web.client.RestTemplate; -import org.springside.examples.showcase.demos.hystrix.web.HystrixController; -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 Long id; - - private RestTemplate restTemplate = new RestTemplate(); - - protected GetUserCommand(Long id) { - - super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("ExampleGroup")) - .andCommandKey(HystrixCommandKey.Factory.asKey("GetUserCommand")) - .andCommandPropertiesDefaults(HystrixCommandProperties.Setter() - // 线程超时,默认为1秒,设为2秒方便演示 - .withExecutionIsolationThreadTimeoutInMilliseconds(2000) - // 至少多少请求在rolling window内发生,开始触发短路的计算,默认为20, 设为3方便演示。 - .withCircuitBreakerRequestVolumeThreshold(3) - // 多少百分比的fail在rolling windows内发生,计算为短路。默认为50%,无改变。 - .withCircuitBreakerErrorThresholdPercentage(50) - // 被置成短路后,多久才会重新尝试访问资源。默认为5秒,改为20秒方便演示。 - .withCircuitBreakerSleepWindowInMilliseconds(20000) - // rolling windows 长度,默认为20秒,改为60秒方便演示。 - .withMetricsRollingStatisticalWindowInMilliseconds(60000) - // rolling Windows的桶的数量,默认为10,无为20,因为rolling window增大,需相应增加以保持秒的精度。 - .withMetricsRollingStatisticalWindowBuckets(60) - // 计算短路的时间间隔,默认是500毫秒,无改变。 - .withMetricsHealthSnapshotIntervalInMilliseconds(500)) - // 线程池属性 - .andThreadPoolPropertiesDefaults(HystrixThreadPoolProperties.Setter() - // 线程池大小,默认为10,无改变。 - .withCoreSize(10) - // 待执行队列的大小,默认为5,无改变。 - .withQueueSizeRejectionThreshold(5))); - this.id = id; - } - - @Override - protected UserDTO run() throws Exception { - logger.info("Access restful resource"); - return restTemplate.getForObject("http://localhost:8080/showcase/hystrix/resource/{id}", UserDTO.class, id); - } - - @Override - protected UserDTO getFallback() { - if ("exception".equals(HystrixController.fallback)) { - throw new RuntimeException("HystrixException"); - } - - if ("dummy".equals(HystrixController.fallback)) { - UserDTO dummyUser = new UserDTO(); - dummyUser.setId(0L); - dummyUser.setLoginName("dummy"); - dummyUser.setName("Dummy"); - return dummyUser; - } - - if ("standby".equals(HystrixController.fallback)) { - return restTemplate.getForObject("http://localhost:8080/showcase/hystrix/resource/standby/{id}", - UserDTO.class, id); - } - - return null; - } -} \ No newline at end of file diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/service/HyStrixServiceController.java b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/service/HyStrixServiceController.java deleted file mode 100644 index c0ed598b..00000000 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/service/HyStrixServiceController.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.springside.examples.showcase.demos.hystrix.service; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springside.examples.showcase.webservice.rest.RestException; -import org.springside.examples.showcase.webservice.rest.UserDTO; - -/** - * 使用Hystrix访问资源的服务。 - */ -@Controller -public class HyStrixServiceController { - private static Logger logger = LoggerFactory.getLogger(HyStrixServiceController.class); - - @RequestMapping(value = "/hystrix/service/{id}", method = RequestMethod.GET) - @ResponseBody - public UserDTO getUser(@PathVariable("id") Long id) { - GetUserCommand command = new GetUserCommand(id); - - try { - return command.execute(); - } catch (Exception e) { - logger.error(e.getMessage(), e); - throw new RestException(HttpStatus.SERVICE_UNAVAILABLE); - } - } -} diff --git a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/web/HystrixController.java b/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/web/HystrixController.java deleted file mode 100644 index 7e0b31e8..00000000 --- a/examples/showcase/src/main/java/org/springside/examples/showcase/demos/hystrix/web/HystrixController.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.springside.examples.showcase.demos.hystrix.web; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; - -@Controller -public class HystrixController { - - public static String status = "normal"; - public static String fallback = "exception"; - - @RequestMapping(value = "/story/hystrix/status/{status}") - public String updateStatus(@PathVariable("status") String newStatus) { - HystrixController.status = newStatus; - return "redirect:/story/hystrix"; - } - - @RequestMapping(value = "/story/hystrix/fallback/{fallback}") - public String updateFallback(@PathVariable("fallback") String newFallback) { - HystrixController.fallback = newFallback; - return "redirect:/story/hystrix"; - } -} diff --git a/examples/showcase/src/main/webapp/WEB-INF/decorators.xml b/examples/showcase/src/main/webapp/WEB-INF/decorators.xml index bfff7358..d00e42a3 100644 --- a/examples/showcase/src/main/webapp/WEB-INF/decorators.xml +++ b/examples/showcase/src/main/webapp/WEB-INF/decorators.xml @@ -4,7 +4,6 @@ /static/* /cxf/* /api/* - /hystrix/* diff --git a/examples/showcase/src/main/webapp/WEB-INF/layouts/left.jsp b/examples/showcase/src/main/webapp/WEB-INF/layouts/left.jsp index 1dbc118a..20f4f9d0 100644 --- a/examples/showcase/src/main/webapp/WEB-INF/layouts/left.jsp +++ b/examples/showcase/src/main/webapp/WEB-INF/layouts/left.jsp @@ -11,7 +11,6 @@

演示说明

diff --git a/examples/showcase/src/main/webapp/WEB-INF/views/story/hystrix.jsp b/examples/showcase/src/main/webapp/WEB-INF/views/story/hystrix.jsp deleted file mode 100644 index 3339af03..00000000 --- a/examples/showcase/src/main/webapp/WEB-INF/views/story/hystrix.jsp +++ /dev/null @@ -1,63 +0,0 @@ -<%@ page contentType="text/html;charset=UTF-8" %> -<%@ page import="org.springside.examples.showcase.demos.hystrix.web.HystrixController,com.netflix.hystrix.*,com.netflix.hystrix.HystrixCommandMetrics.HealthCounts,com.netflix.hystrix.util.*" %> -<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%! %> - - - - - Web Service高级演示 - - - - -

Hystrix 演示

- -

Netflix Hystrix 是一个延迟与容错类库,通过独立访问远程系统、服务和第三方库的节点,在复杂的分布式系统里停止雪崩及提供恢复能力。

- -

演示操作

- -

主要用户故事

-
    -
  • 在默认的正常状态,访问Hystrix服务和依赖资源,均返回正常结果。
  • -
  • 将资源状态切换为"超时",访问依赖资源,需要在10秒后才返回结果。
  • -
  • 访问Hystrix服务,2秒后超时,返回503。
  • -
  • 三次超时后满足短路条件(60秒滚动窗口内起码有3个请求,50%失败),再次刷新服务,立即返回503。
  • -
  • 10秒短路保护期内,所有访问都立即返回503,不会访问依赖资源。
  • -
  • 保护期过后,会放行一个请求,如果还是超时,则继续保持短路状态。如果成功则重置所有计数器。
  • -
- -

其他用户故事

-
    -
  • 将资源状态切换为"失败",访问依赖资源,立即返回500错误,访问Hystrix服务,即时返回503错误。
  • -
  • 将异常处理方式切换为"返回默认用户",访问Hystrix服务,返回默认用户。
  • -
  • 将异常处理方式切换为"访问备用节点",访问Hystrix服务,返回正确结果。
  • -
- -

监控结果

-
- <% - HystrixCommandKey key = HystrixCommandKey.Factory.asKey("GetUserCommand"); - HystrixCommandMetrics metrics = HystrixCommandMetrics.getInstance(key); - if (metrics!=null){ - HealthCounts counts = metrics.getHealthCounts(); - HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory.getInstance(key); - - - %> -
    -
  • 是否短路:<%= circuitBreaker.isOpen() %>
  • -
  • 窗口内请求:<%= counts.getTotalRequests()%>,失败百分比:<%= counts.getErrorPercentage() %>% -
  • 窗口成功:<%= metrics.getRollingCount(HystrixRollingNumberEvent.SUCCESS) %>, 超时:<%= metrics.getRollingCount(HystrixRollingNumberEvent.TIMEOUT) %>,失败:<%= metrics.getRollingCount(HystrixRollingNumberEvent.FAILURE) %>,短路:<%= metrics.getRollingCount(HystrixRollingNumberEvent.SHORT_CIRCUITED) %>
  • -
  • 50%延时:<%= metrics.getTotalTimePercentile(50) +"ms"%>,90%延时:<%= metrics.getTotalTimePercentile(90) +"ms"%>,100%延时:<%= metrics.getTotalTimePercentile(100) +"ms"%>
  • -
- <%} %> - - \ No newline at end of file diff --git a/examples/showcase/src/main/webapp/WEB-INF/web.xml b/examples/showcase/src/main/webapp/WEB-INF/web.xml index feddb937..0534c2ec 100644 --- a/examples/showcase/src/main/webapp/WEB-INF/web.xml +++ b/examples/showcase/src/main/webapp/WEB-INF/web.xml @@ -79,16 +79,6 @@ RemoteContentServlet /remote-content - - - - HystrixMetricsStreamServlet - com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet - - - HystrixMetricsStreamServlet - /hystrix/hystrix.stream - diff --git a/modules/parent/pom.xml b/modules/parent/pom.xml index bd420d58..dfb72595 100644 --- a/modules/parent/pom.xml +++ b/modules/parent/pom.xml @@ -24,7 +24,6 @@ 2.1.0 2.6.6 2.9.1 - 1.2.17 4.3.1.Final 1.1.2 2.1.5 @@ -365,19 +364,6 @@ ${jedis.version}
- - - - com.netflix.hystrix - hystrix-core - ${hystrix.version} - - - commons-logging - commons-logging - - -