Hystrix系列-4-Hystrix的动态配置

Hystrix默认使用Archaius来实现的动态配置,我们在上节中,使用了代码的方式来实现配置,这节,我们使用Hystrix的动态配置来实现。

1、实现一个Command,代码如下:

 

[java] view plain copy

  1. package com.example.demo.hystrix.command;  
  2.   
  3. import org.apache.http.HttpEntity;  
  4. import org.apache.http.client.methods.CloseableHttpResponse;  
  5. import org.apache.http.client.methods.HttpGet;  
  6. import org.apache.http.impl.client.CloseableHttpClient;  
  7. import org.apache.http.impl.client.HttpClients;  
  8. import org.apache.http.util.EntityUtils;  
  9.   
  10. import com.example.demo.utils.ObjectMapperInstance;  
  11. import com.example.demo.vo.User;  
  12. import com.fasterxml.jackson.databind.ObjectMapper;  
  13. import com.netflix.hystrix.HystrixCommand;  
  14. import com.netflix.hystrix.HystrixCommandGroupKey;  
  15. import com.netflix.hystrix.HystrixCommandKey;  
  16.   
  17. import lombok.Getter;  
  18.   
  19. /** 
  20.  * 只需要集成HystrixCommand即可,并覆写父类中的相应方法即可 
  21.  * @author Administrator 
  22.  * 
  23.  */  
  24. public class UserHystrixCommond extends HystrixCommand<User>{  
  25.       
  26.     @lombok.Setter @ Getter private String id;  
  27.       
  28.     public UserHystrixCommond(String id) {  
  29.         super(Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("UserCommandGroup"))  
  30.                 .andCommandKey(HystrixCommandKey.Factory.asKey("userCommand")));  
  31.         this.id = id;  
  32.     }  
  33.   
  34.   
  35.     /** 
  36.      * 覆写run方法,此处写业务逻辑 
  37.      */  
  38.     @Override  
  39.     protected User run() throws Exception {  
  40.         System.out.println("command user: "+Thread.currentThread().getName()+"  is running......");  
  41.         CloseableHttpClient client = HttpClients.createDefault();  
  42.         HttpGet get = new HttpGet("http://localhost:7901/user/"+id);  
  43.         CloseableHttpResponse response = client.execute(get);  
  44.         HttpEntity entity = response.getEntity();  
  45.         String body = EntityUtils.toString(entity);  
  46.         ObjectMapper mapper = ObjectMapperInstance.getInstance();  
  47.         return mapper.readValue(body, User.class);  
  48.     }  
  49.       
  50.     /** 
  51.      * 服务降级方法,当调用服务发生异常时,会调用该降级方法 
  52.      */  
  53.     @Override  
  54.     protected User getFallback() {  
  55.         System.out.println("进入fallback方法!");  
  56.         User u = new User();  
  57.         u.setUsername("刘先生");  
  58.         u.setId(1l);  
  59.           
  60.         return u;  
  61.     }  
  62. }  


在application.properties配置文件中加入如下配置:

 

 

[java] view plain copy

  1. 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支持的动态配置列表如下:

 

  1. Command Properties
    1. Execution
      1. execution.isolation.strategy
      2. execution.isolation.thread.timeoutInMilliseconds
      3. execution.timeout.enabled
      4. execution.isolation.thread.interruptOnTimeout
      5. execution.isolation.thread.interruptOnCancel
      6. execution.isolation.semaphore.maxConcurrentRequests
    2. Fallback
      1. fallback.isolation.semaphore.maxConcurrentRequests
      2. fallback.enabled
    3. Circuit Breaker
      1. circuitBreaker.enabled
      2. circuitBreaker.requestVolumeThreshold
      3. circuitBreaker.sleepWindowInMilliseconds
      4. circuitBreaker.errorThresholdPercentage
      5. circuitBreaker.forceOpen
      6. circuitBreaker.forceClosed
    4. Metrics
      1. metrics.rollingStats.timeInMilliseconds
      2. metrics.rollingStats.numBuckets
      3. metrics.rollingPercentile.enabled
      4. metrics.rollingPercentile.timeInMilliseconds
      5. metrics.rollingPercentile.numBuckets
      6. metrics.rollingPercentile.bucketSize
      7. metrics.healthSnapshot.intervalInMilliseconds
    5. Request Context
      1. requestCache.enabled
      2. requestLog.enabled
  2. Collapser Properties
    1. maxRequestsInBatch
    2. timerDelayInMilliseconds
    3. requestCache.enabled
  3. Thread Pool Properties
    1. coreSize
    2. maximumSize
    3. maxQueueSize
    4. queueSizeRejectionThreshold
    5. keepAliveTimeMinutes
    6. allowMaximumSizeToDivergeFromCoreSize
    7. metrics.rollingStats.timeInMilliseconds
    8. metrics.rollingStats.numBuckets
  4. http://blog.csdn.net/liuchuanhong1/article/details/73718483

 

转载于:https://my.oschina.net/xiaominmin/blog/1590726

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值