Java Code Examples for com.alipay.sofa.rpc.common.RpcConfigs#getStringValue()

The following examples show how to use com.alipay.sofa.rpc.common.RpcConfigs#getStringValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ModuleFactory.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 加载全部模块
 */
public static void installModules() {
    ExtensionLoader<Module> loader = ExtensionLoaderFactory.getExtensionLoader(Module.class);
    String moduleLoadList = RpcConfigs.getStringValue(RpcOptions.MODULE_LOAD_LIST);
    for (Map.Entry<String, ExtensionClass<Module>> o : loader.getAllExtensions().entrySet()) {
        String moduleName = o.getKey();
        Module module = o.getValue().getExtInstance();
        // judge need load from rpc option
        if (needLoad(moduleLoadList, moduleName)) {
            // judge need load from implement
            if (module.needLoad()) {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("Install Module: {}", moduleName);
                }
                module.install();
                INSTALLED_MODULES.put(moduleName, module);
            } else {
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("The module " + moduleName + " does not need to be loaded.");
                }
            }
        } else {
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("The module " + moduleName + " is not in the module load list.");
            }
        }
    }
}
 
Example 2
Source File: Bootstraps.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 发布一个服务
 *
 * @param providerConfig 服务发布者配置
 * @param <T>            接口类型
 * @return 发布启动类
 */
public static <T> ProviderBootstrap<T> from(ProviderConfig<T> providerConfig) {
    String bootstrap = providerConfig.getBootstrap();
    if (StringUtils.isEmpty(bootstrap)) {
        // Use default provider bootstrap
        bootstrap = RpcConfigs.getStringValue(RpcOptions.DEFAULT_PROVIDER_BOOTSTRAP);
        providerConfig.setBootstrap(bootstrap);
    }
    ProviderBootstrap providerBootstrap = ExtensionLoaderFactory.getExtensionLoader(ProviderBootstrap.class)
        .getExtension(bootstrap, new Class[] { ProviderConfig.class }, new Object[] { providerConfig });
    return (ProviderBootstrap<T>) providerBootstrap;
}
 
Example 3
Source File: Bootstraps.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 引用一个服务
 *
 * @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;
}
 
Example 4
Source File: BootstrapsTest.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Before
public void before() {
    oldP = RpcConfigs.getStringValue(RpcOptions.DEFAULT_PROVIDER_BOOTSTRAP);
    oldC = RpcConfigs.getStringValue(RpcOptions.DEFAULT_CONSUMER_BOOTSTRAP);
    RpcConfigs.putValue(RpcOptions.DEFAULT_PROVIDER_BOOTSTRAP, "test");
    RpcConfigs.putValue(RpcOptions.DEFAULT_CONSUMER_BOOTSTRAP, "test");
}
 
Example 5
Source File: ModuleFactoryTest.java    From sofa-rpc with Apache License 2.0 4 votes vote down vote up
@Test
public void testAll() throws Exception {
    String old = RpcConfigs.getStringValue(RpcOptions.MODULE_LOAD_LIST);
    try {
        RpcConfigs.putValue(RpcOptions.MODULE_LOAD_LIST, "*,-test3");

        ModuleFactory.installModules();
        Assert.assertFalse(ModuleFactory.INSTALLED_MODULES.isEmpty());
        Assert.assertTrue(ModuleFactory.INSTALLED_MODULES.containsKey("test"));
        Assert.assertTrue(ModuleFactory.INSTALLED_MODULES.containsKey("test2"));
        Assert.assertFalse(ModuleFactory.INSTALLED_MODULES.containsKey("testNot"));
        Assert.assertEquals("testi", TestModules.test);
        Assert.assertEquals("test2i", TestModules.test2);
        Assert.assertNull(TestModules.testNot);

        TestModules.error = true;
        ModuleFactory.uninstallModule("test");
        Assert.assertTrue(ModuleFactory.INSTALLED_MODULES.containsKey("test"));
        TestModules.error = false;

        TestModules.error = false;
        ModuleFactory.uninstallModule("test");
        Assert.assertFalse(ModuleFactory.INSTALLED_MODULES.containsKey("test"));
        Assert.assertTrue(ModuleFactory.INSTALLED_MODULES.containsKey("test2"));
        Assert.assertEquals("testu", TestModules.test);
        Assert.assertEquals("test2i", TestModules.test2);
        Assert.assertNull(TestModules.testNot);

        TestModules.error = true;
        ModuleFactory.uninstallModules();
        Assert.assertTrue(ModuleFactory.INSTALLED_MODULES.containsKey("test2"));
        TestModules.error = false;

        ModuleFactory.uninstallModules();
        Assert.assertTrue(ModuleFactory.INSTALLED_MODULES.isEmpty());
        Assert.assertEquals("testu", TestModules.test);
        Assert.assertEquals("test2u", TestModules.test2);
        Assert.assertNull(TestModules.testNot);
    } finally {
        if (old != null) {
            RpcConfigs.putValue(RpcOptions.MODULE_LOAD_LIST, old);
        }
    }
}
 
Example 6
Source File: SofaTracerModule.java    From sofa-rpc with Apache License 2.0 2 votes vote down vote up
/**
 * 是否启动Tracer
 *
 * @return 是否开启
 */
public static boolean isEnable() {
    String traceName = RpcConfigs.getStringValue(RpcOptions.DEFAULT_TRACER);
    return "sofaTracer".equals(traceName);
}