天天看點

Spring Cloud_18_Hystrix的使用-隔離與緩存(三)Hystrix的使用-隔離與緩存(三)

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();
        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", );

        for (int i = ; i < ; i++) {
            MyCommand c = new MyCommand(i);
            c.queue();//異步執行
        }

        Thread.sleep();
    }
}
           
Spring Cloud_18_Hystrix的使用-隔離與緩存(三)Hystrix的使用-隔離與緩存(三)
  • 信号量隔離政策
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",
                        );

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

        Thread.sleep();
    }
}
           
Spring Cloud_18_Hystrix的使用-隔離與緩存(三)Hystrix的使用-隔離與緩存(三)

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<String> {

        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;
        }

    }
}
           
Spring Cloud_18_Hystrix的使用-隔離與緩存(三)Hystrix的使用-隔離與緩存(三)
// 清空緩存
HystrixRequestCache cache = HystrixRequestCache.getInstance(
    HystrixCommandKey.Factory.asKey("MyCommoandKey"),
    HystrixConcurrencyStrategyDefault.getInstance());
cache.clear(cacheKey);
           

繼續閱讀