Search in sources :

Example 1 with ExtensionClass

use of com.alipay.sofa.rpc.ext.ExtensionClass in project sofa-rpc by sofastack.

the class DynamicConfigManagerFactory method getDynamicManager.

/**
 * 得到动态配置管理
 *
 * @param alias 别名
 * @return DynamicManager 实现
 */
public static synchronized DynamicConfigManager getDynamicManager(String appName, String alias) {
    if (ALL_DYNAMICS.size() > 3) {
        // 超过3次 是不是配错了?
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("Size of dynamic manager is greater than 3, Please check it!");
        }
    }
    try {
        // 注意:RegistryConfig重写了equals方法,如果多个RegistryConfig属性一样,则认为是一个对象
        DynamicConfigManager registry = ALL_DYNAMICS.get(alias);
        if (registry == null) {
            ExtensionClass<DynamicConfigManager> ext = ExtensionLoaderFactory.getExtensionLoader(DynamicConfigManager.class).getExtensionClass(alias);
            if (ext == null) {
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "DynamicConfigManager", alias));
            }
            registry = ext.getExtInstance(new Class[] { String.class }, new Object[] { appName });
            ALL_DYNAMICS.put(alias, registry);
        }
        return registry;
    } catch (Throwable e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "DynamicConfigManager", alias), e);
    }
}
Also used : SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ExtensionClass(com.alipay.sofa.rpc.ext.ExtensionClass)

Example 2 with ExtensionClass

use of com.alipay.sofa.rpc.ext.ExtensionClass in project sofa-rpc by sofastack.

the class RegistryFactory method getRegistry.

/**
 * 得到注册中心对象
 *
 * @param registryConfig RegistryConfig类
 * @return Registry实现
 */
public static synchronized Registry getRegistry(RegistryConfig registryConfig) {
    if (ALL_REGISTRIES.size() > 3) {
        // 超过3次 是不是配错了?
        if (LOGGER.isWarnEnabled()) {
            LOGGER.warn("Size of registry is greater than 3, Please check it!");
        }
    }
    String protocol = null;
    try {
        // 注意:RegistryConfig重写了equals方法,如果多个RegistryConfig属性一样,则认为是一个对象
        Registry registry = ALL_REGISTRIES.get(registryConfig);
        if (registry == null) {
            protocol = registryConfig.getProtocol();
            ExtensionClass<Registry> ext = ExtensionLoaderFactory.getExtensionLoader(Registry.class).getExtensionClass(protocol);
            if (ext == null) {
                throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "Registry", protocol));
            }
            registry = ext.getExtInstance(new Class[] { RegistryConfig.class }, new Object[] { registryConfig });
            ALL_REGISTRIES.put(registryConfig, registry);
        }
        return registry;
    } catch (SofaRpcRuntimeException e) {
        throw e;
    } catch (Throwable e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_LOAD_EXT, "Registry", protocol));
    }
}
Also used : RegistryConfig(com.alipay.sofa.rpc.config.RegistryConfig) SofaRpcRuntimeException(com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException) ExtensionClass(com.alipay.sofa.rpc.ext.ExtensionClass)

Example 3 with ExtensionClass

use of com.alipay.sofa.rpc.ext.ExtensionClass in project sofa-rpc by sofastack.

the class Bootstraps method from.

/**
 * 引用一个服务
 *
 * @param consumerConfig 服务消费者配置
 * @param <T>            接口类型
 * @return 引用启动类
 */
public static <T> ConsumerBootstrap<T> from(ConsumerConfig<T> consumerConfig) {
    String bootstrap = consumerConfig.getBootstrap();
    ConsumerBootstrap consumerBootstrap;
    if (StringUtils.isNotEmpty(bootstrap)) {
        consumerBootstrap = ExtensionLoaderFactory.getExtensionLoader(ConsumerBootstrap.class).getExtension(bootstrap, new Class[] { ConsumerConfig.class }, new Object[] { consumerConfig });
    } else {
        // default is same with protocol
        bootstrap = consumerConfig.getProtocol();
        ExtensionLoader extensionLoader = ExtensionLoaderFactory.getExtensionLoader(ConsumerBootstrap.class);
        ExtensionClass<ConsumerBootstrap> extensionClass = extensionLoader.getExtensionClass(bootstrap);
        if (extensionClass == null) {
            // if not exist, use default consumer bootstrap
            bootstrap = RpcConfigs.getStringValue(RpcOptions.DEFAULT_CONSUMER_BOOTSTRAP);
            consumerConfig.setBootstrap(bootstrap);
            consumerBootstrap = ExtensionLoaderFactory.getExtensionLoader(ConsumerBootstrap.class).getExtension(bootstrap, new Class[] { ConsumerConfig.class }, new Object[] { consumerConfig });
        } else {
            consumerConfig.setBootstrap(bootstrap);
            consumerBootstrap = extensionClass.getExtInstance(new Class[] { ConsumerConfig.class }, new Object[] { consumerConfig });
        }
    }
    return (ConsumerBootstrap<T>) consumerBootstrap;
}
Also used : ExtensionClass(com.alipay.sofa.rpc.ext.ExtensionClass) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ExtensionLoader(com.alipay.sofa.rpc.ext.ExtensionLoader)

Aggregations

ExtensionClass (com.alipay.sofa.rpc.ext.ExtensionClass)3 SofaRpcRuntimeException (com.alipay.sofa.rpc.core.exception.SofaRpcRuntimeException)2 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)1 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)1 ExtensionLoader (com.alipay.sofa.rpc.ext.ExtensionLoader)1