Spring Cloud Hystrix项目实践和Hystrix监控
Posted on 2020-03-21 21:10 work hard work smart 阅读(270) 评论(0) 编辑 收藏 举报一、Hystrix项目实践
1、在Cinema影院工程中增加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2、配置属性
server: port: 8301 eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ management: endpoints: web: exposure: exclude: "*" spring: application: name: cinema-service datasource: url: jdbc:mysql://127.0.0.1:3306/springclouddeep?autoReconnect=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=CONVERT_TO_NULL&useSSL=false&serverTimezone=UTC username: root password: 123456 filters: log4j, wall, mergeState mybatis-plus: mapper-locations: classpath*:com/example/springclouddeep/**/xml/*Mapper.xml global-config: id-type: 0 db-column-underline: false refresh-mapper: true
3、增加注解
4、对方法配置Hystrix
ignoreExceptions的配置CommonServiceException,当抛出CommonServiceException,不做降级处理。
@HystrixCommand(fallbackMethod = "fallbackMethod", commandProperties = { @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD"), @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value= "1000"), @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "50") }, threadPoolProperties = { @HystrixProperty(name = "coreSize", value = "1"), @HystrixProperty(name = "maxQueueSize", value = "10"), @HystrixProperty(name = "keepAliveTimeMinutes", value = "1000"), @HystrixProperty(name = "queueSizeRejectionThreshold", value = "8"), @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"), @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1500") },ignoreExceptions = CommonServiceException.class) @RequestMapping(value = "",method = RequestMethod.GET) public BaseResponseVO describeCinemas(BasePageVO basePageVO) throws CommonServiceException { IPage<DescribeCinemasRespVO> describeCinemasRespVOIPage = cinemaServiceAPI.describeCinemas(basePageVO.getNowPage(), basePageVO.getPageSize()); if(basePageVO.getNowPage()>10000){ //throw new CommonServiceException(400,"nowPage太大了,不支持此处理"); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } // TODO 调用封装的分页返回方法 return BaseResponseVO.success(); }
对nowPage值大于10000时,线程休眠2秒,而配置的超时时间timeoutInMilliseconds为1秒 。从而触发熔断,调用fallbackMethod方法。
fallbackMethod方法如下,该方法的参数和返回值要与describeCinemas保持一致。
public BaseResponseVO fallbackMethod(BasePageVO basePageVO) throws CommonServiceException{ // 返回一定是成功,或者业务处理失败 Map<String,Object> result = Maps.newHashMap(); result.put("code", 500); result.put("message","请求处理降级返回"); return BaseResponseVO.success(result); }
5、调用请求
当nowPage小于等于10000时,返回成功
当nowPage大于10000时,触发熔断
二、Hystrix的Dashboard使用
1、打开http://localhost:8301/hystrix
添加监控http://localhost:8301/actuator/hystrix.stream
2、监控图
3、监控图数字含义
Hystrix很少作为监控使用,它一般只有2分钟左右的状态,不能查历史状态,比较难找原因。适合双十一,比如每分钟都要监控到。
作者:Work Hard Work Smart
出处:http://www.cnblogs.com/linlf03/
欢迎任何形式的转载,未经作者同意,请保留此段声明!