作者:zhaozao5757 来源:CSDN博客 时间:2018-03-13 21:17:54 我要评论

Hystrix的使用-隔离与缓存(三)

1、隔离策略

  • thread(线程,消耗可能大点,异步超时)
  • semaphore(信号量,不支持超时、异步)

1.1、编写命令

package com.atm.cloud;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

public class MyCommand extends HystrixCommand<String> {

    public int index;

    public MyCommand(int index) {
        super(Setter.withGroupKey(HystrixCommandGroupKey.Factory
                .asKey("ExampleGroup")));
        this.index = index;
    }

    @Override
    protected String run() throws Exception {
        Thread.sleep(500);
        System.out.println("run(),当前索引:" + index);
        return "success";
    }

    @Override
    protected String getFallback() {
        System.out.println("getFallback(),当前索引:" + index);
        return "fallback";
    }

}

1.2、执行main

  • 默认是使用线程隔离策略
package com.atm.cloud;

import com.netflix.config.ConfigurationManager;

public class HelloMain {

    public static void main(String[] args) throws Exception {
        // 设置线程池(超过3,认为是线程池满载)
        ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.threadpool.default.coreSize", 3);

        for (int i = 0; i < 6; i++) {
            MyCommand c = new MyCommand(i);
            c.queue();//异步执行
        }

        Thread.sleep(5000);
    }
}

  • 信号量隔离策略
package com.atm.cloud;

import com.netflix.config.ConfigurationManager;
import com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy;

public class SemaphoreMain {

    public static void main(String[] args) throws Exception {
        // 信号量策略,默认最大并发数10
        ConfigurationManager.getConfigInstance().setProperty(
                "hystrix.command.default.execution.isolation.strategy",
                ExecutionIsolationStrategy.SEMAPHORE);

        // 设置最大并发数为2
        ConfigurationManager
                .getConfigInstance()
                .setProperty(
                        "hystrix.command.default.execution.isolation.semaphore.maxConcurrentRequests",
                        2);

        for (int i = 0; i < 6; i++) {
            final int index = i;
            Thread t = new Thread() {
                public void run() {
                    MyCommand c = new MyCommand(index);
                    c.execute();
                }
            };
            t.start();
        }

        Thread.sleep(5000);
    }
}

2、缓存

  • 在一起请求中,多个地方调用同一个接口,可考虑使用缓存
  • 需要用到CommandKey
  • 要在一起请求中执行:要初始化上下文
package com.atm.cloud;

import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;

public class CacheMain {
    public static void main(String[] args) {
        // 初始化请求上下文,告知:这是一次请求
        HystrixRequestContext ctx = HystrixRequestContext.initializeContext();

        // 定义一个key
        String cacheKey = "cache-key";

        // 创建命令,开始执行
        CacheCommand cacheCommand1 = new CacheCommand(cacheKey);
        CacheCommand cacheCommand2 = new CacheCommand(cacheKey);
        CacheCommand cacheCommand3 = new CacheCommand(cacheKey);

        // 从结果可以发现,run只执行了一次
        cacheCommand1.execute();
        cacheCommand2.execute();
        cacheCommand3.execute();

        System.out.println("命令cacheCommand1是否读取缓存:"
                + cacheCommand1.isResponseFromCache());

        System.out.println("命令cacheCommand2是否读取缓存:"
                + cacheCommand2.isResponseFromCache());

        System.out.println("命令cacheCommand3是否读取缓存:"
                + cacheCommand3.isResponseFromCache());

        // 关闭
        ctx.shutdown();

    }

    static class CacheCommand extends HystrixCommand {

        public String cacheKey;

        public CacheCommand(String cacheKey) {
            super(Setter.withGroupKey(
                    HystrixCommandGroupKey.Factory.asKey("GroupKey"))
                    .andCommandKey(
                            HystrixCommandKey.Factory.asKey("MyCommoandKey")));
            this.cacheKey = cacheKey;
        }

        @Override
        protected String run() throws Exception {
            System.out.println("run()");
            return "success";
        }

        @Override
        protected String getFallback() {
            System.out.println("getFallback()");
            return "fallback";
        }

        @Override
        protected String getCacheKey() {
            return this.cacheKey;
        }

    }
}

// 清空缓存
HystrixRequestCache cache = HystrixRequestCache.getInstance(
    HystrixCommandKey.Factory.asKey("MyCommoandKey"),
    HystrixConcurrencyStrategyDefault.getInstance());
cache.clear(cacheKey);
作者:zhaozao5757 发表于2018/3/13 21:17:54 原文链接
阅读:1 评论:0 查看评论
24阅读 | 0评论
你的回应
写文章

联系我们