0 Star 4 Fork 5

SunStrider/all-simple-demo-in-work-1

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

注册插件的方式

  1. 直接调用
HystrixPlugins.getInstance().registerConcurrencyStrategy(new MyHystrixConcurrencyStrategy());
  1. 通过本案例中的jdk的spi方式注册 在resources下,META-INF/services中注册了com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy的实现类

具体的spi实现,可以查看hystrix的实现代码:

public HystrixConcurrencyStrategy getConcurrencyStrategy() {
        if (concurrencyStrategy.get() == null) {
            // check for an implementation from Archaius first
            // 1
            Object impl = getPluginImplementation(HystrixConcurrencyStrategy.class);
            if (impl == null) {
                // nothing set via Archaius so initialize with default
                concurrencyStrategy.compareAndSet(null, HystrixConcurrencyStrategyDefault.getInstance());
                // we don't return from here but call get() again in case of thread-race so the winner will always get returned
            } else {
                // we received an implementation from Archaius so use it
                concurrencyStrategy.compareAndSet(null, (HystrixConcurrencyStrategy) impl);
            }
        }
        return concurrencyStrategy.get();
    }

上图,1处,会去查找实现类:

    private <T> T getPluginImplementation(Class<T> pluginClass) {
        // 1
        T p = getPluginImplementationViaProperties(pluginClass, dynamicProperties);
        if (p != null) return p;        
        // 2
        return findService(pluginClass, classLoader);
    }

如上,1处,会去动态属性中获取,这个动态属性,怎么动态呢?具体的了解不多,如果集成了Netflix Archaius组件,Netflix Archaius 可以动态修改属性,然后这里就能感知到。 如果1处查找失败,会去2处查找,2处就是jdk的spi机制,也是我们demo中使用的方式。

关于插件相关的东西,更多可以查看: https://github.com/Netflix/Hystrix/wiki/Plugins

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dumpcao/all-simple-demo-in-work-1.git
git@gitee.com:dumpcao/all-simple-demo-in-work-1.git
dumpcao
all-simple-demo-in-work-1
all-simple-demo-in-work-1
master

搜索帮助