Search in sources :

Example 11 with ApplicationConfig

use of com.alibaba.dubbo.config.ApplicationConfig in project dubbo by alibaba.

the class CacheTest method testCache.

@Test
public void testCache() throws Exception {
    ServiceConfig<CacheService> service = new ServiceConfig<CacheService>();
    service.setApplication(new ApplicationConfig("cache-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29582));
    service.setInterface(CacheService.class.getName());
    service.setRef(new CacheServiceImpl());
    service.export();
    try {
        ReferenceConfig<CacheService> reference = new ReferenceConfig<CacheService>();
        reference.setApplication(new ApplicationConfig("cache-consumer"));
        reference.setInterface(CacheService.class);
        reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&cache=true");
        CacheService cacheService = reference.get();
        try {
            // 测试缓存生效,多次调用返回同样的结果。(服务器端自增长返回值)
            String fix = null;
            for (int i = 0; i < 3; i++) {
                String result = cacheService.findCache("0");
                Assert.assertTrue(fix == null || fix.equals(result));
                fix = result;
                Thread.sleep(100);
            }
            // LRU的缺省cache.size为1000,执行1001次,应有溢出
            for (int n = 0; n < 1001; n++) {
                String pre = null;
                for (int i = 0; i < 10; i++) {
                    String result = cacheService.findCache(String.valueOf(n));
                    Assert.assertTrue(pre == null || pre.equals(result));
                    pre = result;
                }
            }
            // 测试LRU有移除最开始的一个缓存项
            String result = cacheService.findCache("0");
            Assert.assertFalse(fix == null || fix.equals(result));
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) Test(org.junit.Test)

Example 12 with ApplicationConfig

use of com.alibaba.dubbo.config.ApplicationConfig in project dubbo by alibaba.

the class ConfigTest method testGenericServiceConfig.

@Test
public void testGenericServiceConfig() throws Exception {
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setApplication(new ApplicationConfig("test"));
    service.setRegistry(new RegistryConfig("mock://localhost"));
    service.setInterface(DemoService.class.getName());
    service.setGeneric(Constants.GENERIC_SERIALIZATION_BEAN);
    service.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });
    try {
        service.export();
        Collection<Registry> collection = MockRegistryFactory.getCachedRegistry();
        MockRegistry registry = (MockRegistry) collection.iterator().next();
        URL url = registry.getRegistered().get(0);
        Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY));
    } finally {
        MockRegistryFactory.cleanCachedRegistry();
        service.unexport();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) GenericService(com.alibaba.dubbo.rpc.service.GenericService) MockRegistry(com.alibaba.dubbo.config.spring.registry.MockRegistry) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) Registry(com.alibaba.dubbo.registry.Registry) MockRegistry(com.alibaba.dubbo.config.spring.registry.MockRegistry) GenericException(com.alibaba.dubbo.rpc.service.GenericException) URL(com.alibaba.dubbo.common.URL) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) Test(org.junit.Test)

Example 13 with ApplicationConfig

use of com.alibaba.dubbo.config.ApplicationConfig in project dubbo by alibaba.

the class ConfigTest method testDubboProtocolPortOverride.

@Test
public void testDubboProtocolPortOverride() throws Exception {
    String dubboPort = System.getProperty("dubbo.protocol.dubbo.port");
    int port = 55555;
    System.setProperty("dubbo.protocol.dubbo.port", String.valueOf(port));
    ServiceConfig<DemoService> service = null;
    try {
        ApplicationConfig application = new ApplicationConfig();
        application.setName("dubbo-protocol-port-override");
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("N/A");
        ProtocolConfig protocol = new ProtocolConfig();
        service = new ServiceConfig<DemoService>();
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.setApplication(application);
        service.setRegistry(registry);
        service.setProtocol(protocol);
        service.export();
        Assert.assertEquals(port, service.getExportedUrls().get(0).getPort());
    } finally {
        if (StringUtils.isNotEmpty(dubboPort)) {
            System.setProperty("dubbo.protocol.dubbo.port", dubboPort);
        }
        if (service != null) {
            service.unexport();
        }
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) DemoServiceImpl(com.alibaba.dubbo.config.spring.impl.DemoServiceImpl) Test(org.junit.Test)

Example 14 with ApplicationConfig

use of com.alibaba.dubbo.config.ApplicationConfig in project dubbo by alibaba.

the class ConfigTest method testReferGenericExport.

@Test
public void testReferGenericExport() throws Exception {
    ApplicationConfig ac = new ApplicationConfig("test-refer-generic-export");
    RegistryConfig rc = new RegistryConfig();
    rc.setAddress(RegistryConfig.NO_AVAILABLE);
    ServiceConfig<GenericService> sc = new ServiceConfig<GenericService>();
    sc.setApplication(ac);
    sc.setRegistry(rc);
    sc.setInterface(DemoService.class.getName());
    sc.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });
    ReferenceConfig<DemoService> ref = new ReferenceConfig<DemoService>();
    ref.setApplication(ac);
    ref.setRegistry(rc);
    ref.setInterface(DemoService.class.getName());
    try {
        sc.export();
        ref.get();
        Assert.fail();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sc.unexport();
        ref.destroy();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) GenericService(com.alibaba.dubbo.rpc.service.GenericService) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) GenericException(com.alibaba.dubbo.rpc.service.GenericException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericException(com.alibaba.dubbo.rpc.service.GenericException) BeansException(org.springframework.beans.BeansException) RpcException(com.alibaba.dubbo.rpc.RpcException) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) Test(org.junit.Test)

Example 15 with ApplicationConfig

use of com.alibaba.dubbo.config.ApplicationConfig in project dubbo by alibaba.

the class ConfigTest method testSystemPropertyOverrideProperties.

@Test
public void testSystemPropertyOverrideProperties() throws Exception {
    String portString = System.getProperty("dubbo.protocol.port");
    System.clearProperty("dubbo.protocol.port");
    try {
        int port = 1234;
        System.setProperty("dubbo.protocol.port", String.valueOf(port));
        ApplicationConfig application = new ApplicationConfig();
        application.setName("aaa");
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("N/A");
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("rmi");
        ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.setApplication(application);
        service.setRegistry(registry);
        service.setProtocol(protocol);
        service.export();
        try {
            URL url = service.toUrls().get(0);
            // from api
            assertEquals("aaa", url.getParameter("application"));
            // from dubbo.properties
            assertEquals("world", url.getParameter("owner"));
            // from system property
            assertEquals(1234, url.getPort());
        } finally {
            service.unexport();
        }
    } finally {
        if (portString != null) {
            System.setProperty("dubbo.protocol.port", portString);
        }
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) URL(com.alibaba.dubbo.common.URL) DemoServiceImpl(com.alibaba.dubbo.config.spring.impl.DemoServiceImpl) Test(org.junit.Test)

Aggregations

ApplicationConfig (com.alibaba.dubbo.config.ApplicationConfig)18 RegistryConfig (com.alibaba.dubbo.config.RegistryConfig)17 Test (org.junit.Test)14 ProtocolConfig (com.alibaba.dubbo.config.ProtocolConfig)12 ReferenceConfig (com.alibaba.dubbo.config.ReferenceConfig)10 ServiceConfig (com.alibaba.dubbo.config.ServiceConfig)10 DemoService (com.alibaba.dubbo.config.spring.api.DemoService)10 DemoServiceImpl (com.alibaba.dubbo.config.spring.impl.DemoServiceImpl)6 URL (com.alibaba.dubbo.common.URL)5 JUnitMatchers.containsString (org.junit.matchers.JUnitMatchers.containsString)5 RpcException (com.alibaba.dubbo.rpc.RpcException)4 GenericService (com.alibaba.dubbo.rpc.service.GenericService)3 Date (java.util.Date)3 ConsumerConfig (com.alibaba.dubbo.config.ConsumerConfig)2 MethodConfig (com.alibaba.dubbo.config.MethodConfig)2 ProviderConfig (com.alibaba.dubbo.config.ProviderConfig)2 DemoService (com.alibaba.dubbo.config.api.DemoService)2 GenericException (com.alibaba.dubbo.rpc.service.GenericException)2 HelloService (com.alibaba.dubbo.config.spring.api.HelloService)1 HelloServiceImpl (com.alibaba.dubbo.config.spring.impl.HelloServiceImpl)1