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

  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

原创粉丝点击