Hystrix系列-4-Hystrix的动态配置
来源:互联网 发布:国家行政学院博士 知乎 编辑:程序博客网 时间:2024/06/05 03:25
Hystrix默认使用Archaius来实现的动态配置,我们在上节中,使用了代码的方式来实现配置,这节,我们使用Hystrix的动态配置来实现。
1、实现一个Command,代码如下:
package com.example.demo.hystrix.command;import org.apache.http.HttpEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.util.EntityUtils;import com.example.demo.utils.ObjectMapperInstance;import com.example.demo.vo.User;import com.fasterxml.jackson.databind.ObjectMapper;import com.netflix.hystrix.HystrixCommand;import com.netflix.hystrix.HystrixCommandGroupKey;import com.netflix.hystrix.HystrixCommandKey;import lombok.Getter;/** * 只需要集成HystrixCommand即可,并覆写父类中的相应方法即可 * @author Administrator * */public class UserHystrixCommond extends HystrixCommand<User>{@lombok.Setter @ Getter private String id;public UserHystrixCommond(String id) {super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("UserCommandGroup")).andCommandKey(HystrixCommandKey.Factory.asKey("userCommand")));this.id = id;}/** * 覆写run方法,此处写业务逻辑 */@Overrideprotected User run() throws Exception {System.out.println("command user: "+Thread.currentThread().getName()+" is running......");CloseableHttpClient client = HttpClients.createDefault();HttpGet get = new HttpGet("http://localhost:7901/user/"+id);CloseableHttpResponse response = client.execute(get);HttpEntity entity = response.getEntity();String body = EntityUtils.toString(entity);ObjectMapper mapper = ObjectMapperInstance.getInstance();return mapper.readValue(body, User.class);}/** * 服务降级方法,当调用服务发生异常时,会调用该降级方法 */@Overrideprotected User getFallback() {System.out.println("进入fallback方法!");User u = new User();u.setUsername("刘先生");u.setId(1l);return u;}}
在application.properties配置文件中加入如下配置:
hystrix.command.userCommand.execution.isolation.strategy=SEMAPHORE //其中userCommand是我们在代码中设置的commandKey
将隔离策略由默认改为的THREAD改为SEMAPHORE,然后跑下测试,我们发现,配置没有启作用,原因如下:
Archaius 默认支持两种方式来加载本地的配置文件:
1、默认情况下,Archaius默认会加载classpath下的config.properties文件
2、在程序启动的时候,加如下的启动参数
-Darchaius.configurationSource.additionalUrls=file:///apps/myapp/application.properties
下面,我们就来测试一下:
方式一:在src/main/resources下新建config.properties文件,并加入上面的配置
方式二:在启动程序的时候,添加如下的启动参数
再次测试就会发现,我们的动态配置生效了。
Hystrix支持的动态配置列表如下:
- Command Properties
- Execution
- execution.isolation.strategy
- execution.isolation.thread.timeoutInMilliseconds
- execution.timeout.enabled
- execution.isolation.thread.interruptOnTimeout
- execution.isolation.thread.interruptOnCancel
- execution.isolation.semaphore.maxConcurrentRequests
- Fallback
- fallback.isolation.semaphore.maxConcurrentRequests
- fallback.enabled
- Circuit Breaker
- circuitBreaker.enabled
- circuitBreaker.requestVolumeThreshold
- circuitBreaker.sleepWindowInMilliseconds
- circuitBreaker.errorThresholdPercentage
- circuitBreaker.forceOpen
- circuitBreaker.forceClosed
- Metrics
- metrics.rollingStats.timeInMilliseconds
- metrics.rollingStats.numBuckets
- metrics.rollingPercentile.enabled
- metrics.rollingPercentile.timeInMilliseconds
- metrics.rollingPercentile.numBuckets
- metrics.rollingPercentile.bucketSize
- metrics.healthSnapshot.intervalInMilliseconds
- Request Context
- requestCache.enabled
- requestLog.enabled
- Execution
- Collapser Properties
- maxRequestsInBatch
- timerDelayInMilliseconds
- requestCache.enabled
- Thread Pool Properties
- coreSize
- maximumSize
- maxQueueSize
- queueSizeRejectionThreshold
- keepAliveTimeMinutes
- allowMaximumSizeToDivergeFromCoreSize
- metrics.rollingStats.timeInMilliseconds
- metrics.rollingStats.numBuckets
阅读全文
0 0
- Hystrix系列-4-Hystrix的动态配置
- Hystrix系列-3-Hystrix的配置-通过代码构造配置
- Hystrix系列之属性配置
- Hystrix
- Hystrix
- Hystrix系列-5-Hystrix的资源隔离策略
- Hystrix监控的配置详解
- 【Hystrix系列】一、配置加载过程分析
- Hystrix系列-2-HystrixObservableCommand的使用
- Hystrix系列之组件间的关系
- Hystrix系列之熔断器
- Hystrix配置介绍
- hystrix配置属性说明
- Spring cloud系列九 Hystrix的配置属性优先级和详解
- Spring cloud系列十一 @Feign集成的Hystrix进行个性化配置及集成原理
- Hystrix的简单介绍
- hystrix的超时含义
- Hystrix的笔记
- Apache Kafka源码分析-模块简介
- [JZOJ5163] 【NOIP2017模拟6.25】PS的烦恼
- Spring Boot 集成MyBatis
- jsp两种不同的include
- 剑指offer--合并两个排序的链表
- Hystrix系列-4-Hystrix的动态配置
- 给 TI BLE 添加自己UUID
- 浅析c++中virtual关键字
- 手机的存储结构
- (目前WA)hihocoder1264-神奇字符串-JAVA版
- hdu 6025 gcd+前缀和+后缀和
- Storm原理
- Mybatis自定义TypeHandler
- Spring Boot 集成Shiro和CAS