fescar源码分析-Configuration

2019-01-28  本文已影响199人  do_young

Configuration

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

Configuration.png
我们先来分析一下接口的实现,再说明一下原因。
ConfigurationDesign.png
    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());
    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);
                }
            }
        }

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客户端及服务端,初始化参数。

上一篇 下一篇

猜你喜欢

热点阅读