美文网首页
fescar源码分析-Configuration

fescar源码分析-Configuration

作者: do_young | 来源:发表于2019-01-28 11:24 被阅读199次

    Configuration

    在fescar项目中定义了一个Configuration接口,该接口用于读取配置信息。
    而之前文章已经介绍过fescar-config组件主要用于读取工程下的application.conf文件信息。
    但从接口来看,该组件不但提供了配置信息的读取功能,还实现了配置信息的修改功能,但通过代码分析来看这块功能还没有实现。

    Configuration.png
    我们先来分析一下接口的实现,再说明一下原因。
    ConfigurationDesign.png
    • ConfigurationFactory是一个工厂类,创建Configuration接口实例。
    • Configuration定义了配置信息读取的接口。
    • FileConfiguration是对Configuration接口的实现
      • FileConfiguration创建实现的时候,创建一个线程池实例。
        private ExecutorService configOperateExecutor;
        private static final int CORE_CONFIG_OPERATE_THREAD = 1;
        private static final int MAX_CONFIG_OPERATE_THREAD = 2;
        private static final long DEFAULT_CONFIG_TIMEOUT = 5 * 1000;
        /**
         * Instantiates a new File configuration.
         */
        public FileConfiguration() {
            configOperateExecutor = new ThreadPoolExecutor(CORE_CONFIG_OPERATE_THREAD, MAX_CONFIG_OPERATE_THREAD,
                Integer.MAX_VALUE, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(),
                new NamedThreadFactory("configOperate", MAX_CONFIG_OPERATE_THREAD, true));
        }
    
        private static final Config CONFIG = ConfigFactory.load();
    ... ... 
        String result = CONFIG.getString(configFuture.getDataId());
    
      • 对配置文件的属性读取通过线程池异步执行配置操作API的调用,将具体操作信息及操作结果信息封装为一个ConfigFuture对象,传递给线程使用。
        class ConfigOperateRunnable implements Runnable {
            private ConfigFuture configFuture;
            /**
             * Instantiates a new Config operate runnable.
             *
             * @param configFuture the config future
             */
            public ConfigOperateRunnable(ConfigFuture configFuture) {
                this.configFuture = configFuture;
            }
            @Override
            public void run() {
                if (null != configFuture) {
                    if (configFuture.isTimeout()) {
                        setFailResult(configFuture);
                    }
                    if (configFuture.getOperation() == ConfigOperation.GET) {
                        String result = CONFIG.getString(configFuture.getDataId());
                        configFuture.setResult(result == null ? configFuture.getContent() : result);
                    } else if (configFuture.getOperation() == ConfigOperation.PUT) {
                        //todo
                        configFuture.setResult(Boolean.TRUE);
                    } else if (configFuture.getOperation() == ConfigOperation.PUTIFABSENT) {
                        //todo
                        configFuture.setResult(Boolean.TRUE);
                    } else if (configFuture.getOperation() == ConfigOperation.REMOVE) {
                        //todo
                        configFuture.setResult(Boolean.TRUE);
                    }
                }
            }
    
      • 从上面这段代码中可以了解到fescar暂时并没有对配置文件的更新及删除操作实现具体的操作。个人分析原因如下:
        FileConfiguration是对Configuration的基本实现,不需要实现对配置进行运行时修改或删除的必要。
        Configuration接口上定义这类操作的原因是为了集成服务配置中心,比如:NACOS或spring-config等。这样当运行结点发起对配置信息更新的操作时,通过异步方式调用配置服务中心的接口,更新配置信息。
        同时也在结点中通过ConfigChangeListener实现对配置中心信息更新的监听,当发现有相应配置信息更新时,就将信息及时更新到结点中。
        (注:以上只是本人的推测)

    NettyBaseConfig

    NettyBaseConfig类就是通过使用ConfigurationFactory读取配置,设置客户端及服务端的Channel实现类:

    switch (TRANSPORT_SERVER_TYPE) {
                case NIO:
                    if (TRANSPORT_PROTOCOL_TYPE == TransportProtocolType.TCP) {
                        SERVER_CHANNEL_CLAZZ = NioServerSocketChannel.class;
                        CLIENT_CHANNEL_CLAZZ = NioSocketChannel.class;
                    } else {
                        raiseUnsupportedTransportError();
                        SERVER_CHANNEL_CLAZZ = null;
                        CLIENT_CHANNEL_CLAZZ = null;
                    }
                    break;
                case NATIVE:
                    if (PlatformDependent.isWindows()) {
                        throw new IllegalArgumentException("no native supporting for Windows.");
                    } else if (PlatformDependent.isOsx()) {
                        if (TRANSPORT_PROTOCOL_TYPE == TransportProtocolType.TCP) {
                            SERVER_CHANNEL_CLAZZ = KQueueServerSocketChannel.class;
                            CLIENT_CHANNEL_CLAZZ = KQueueSocketChannel.class;
                        } else if (TRANSPORT_PROTOCOL_TYPE == TransportProtocolType.UNIX_DOMAIN_SOCKET) {
                            SERVER_CHANNEL_CLAZZ = KQueueServerDomainSocketChannel.class;
                            CLIENT_CHANNEL_CLAZZ = KQueueDomainSocketChannel.class;
                        } else {
                            raiseUnsupportedTransportError();
                            SERVER_CHANNEL_CLAZZ = null;
                            CLIENT_CHANNEL_CLAZZ = null;
                        }
                    } else {
                        if (TRANSPORT_PROTOCOL_TYPE == TransportProtocolType.TCP) {
                            SERVER_CHANNEL_CLAZZ = EpollServerSocketChannel.class;
                            CLIENT_CHANNEL_CLAZZ = EpollSocketChannel.class;
                        } else if (TRANSPORT_PROTOCOL_TYPE == TransportProtocolType.UNIX_DOMAIN_SOCKET) {
                            SERVER_CHANNEL_CLAZZ = EpollServerDomainSocketChannel.class;
                            CLIENT_CHANNEL_CLAZZ = EpollDomainSocketChannel.class;
                        } else {
                            raiseUnsupportedTransportError();
                            SERVER_CHANNEL_CLAZZ = null;
                            CLIENT_CHANNEL_CLAZZ = null;
                        }
                    }
                    break;
                default:
                    throw new IllegalArgumentException("unsupported.");
            }
    

    等等......

    NettyClientConfig&NettyServerConfig

    NettyClientConfig&NettyServerConfig继承自NettyBaseConfig,主要是针对Netty客户端及服务端,初始化参数。

    相关文章

      网友评论

          本文标题:fescar源码分析-Configuration

          本文链接:https://www.haomeiwen.com/subject/pjqsjqtx.html