Search in sources :

Example 16 with ServiceConfig

use of org.apache.dubbo.config.ServiceConfig in project dubbo by alibaba.

the class GenericServiceTest method testGenericSerializationJava.

@Test
public void testGenericSerializationJava() throws Exception {
    ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
    service.setInterface(DemoService.class.getName());
    DemoServiceImpl ref = new DemoServiceImpl();
    service.setRef(ref);
    ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
    reference.setInterface(DemoService.class);
    reference.setUrl("dubbo://127.0.0.1:29581?scope=remote&timeout=3000");
    reference.setGeneric(GENERIC_SERIALIZATION_NATIVE_JAVA);
    DubboBootstrap bootstrap = DubboBootstrap.getInstance().application(new ApplicationConfig("generic-test")).registry(new RegistryConfig("N/A")).protocol(new ProtocolConfig("dubbo", 29581)).service(service).reference(reference);
    bootstrap.start();
    try {
        GenericService genericService = bootstrap.getCache().get(reference);
        String name = "kimi";
        ByteArrayOutputStream bos = new ByteArrayOutputStream(512);
        ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").serialize(null, bos).writeObject(name);
        byte[] arg = bos.toByteArray();
        Object obj = genericService.$invoke("sayName", new String[] { String.class.getName() }, new Object[] { arg });
        Assertions.assertTrue(obj instanceof byte[]);
        byte[] result = (byte[]) obj;
        Assertions.assertEquals(ref.sayName(name), ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").deserialize(null, new ByteArrayInputStream(result)).readObject().toString());
        // getUsers
        List<User> users = new ArrayList<User>();
        User user = new User();
        user.setName(name);
        users.add(user);
        bos = new ByteArrayOutputStream(512);
        ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").serialize(null, bos).writeObject(users);
        obj = genericService.$invoke("getUsers", new String[] { List.class.getName() }, new Object[] { bos.toByteArray() });
        Assertions.assertTrue(obj instanceof byte[]);
        result = (byte[]) obj;
        Assertions.assertEquals(users, ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").deserialize(null, new ByteArrayInputStream(result)).readObject());
        // echo(int)
        bos = new ByteArrayOutputStream(512);
        ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").serialize(null, bos).writeObject(Integer.MAX_VALUE);
        obj = genericService.$invoke("echo", new String[] { int.class.getName() }, new Object[] { bos.toByteArray() });
        Assertions.assertTrue(obj instanceof byte[]);
        Assertions.assertEquals(Integer.MAX_VALUE, ExtensionLoader.getExtensionLoader(Serialization.class).getExtension("nativejava").deserialize(null, new ByteArrayInputStream((byte[]) obj)).readObject());
    } finally {
        bootstrap.stop();
    }
}
Also used : RegistryConfig(org.apache.dubbo.config.RegistryConfig) GenericService(org.apache.dubbo.rpc.service.GenericService) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Serialization(org.apache.dubbo.common.serialize.Serialization) ServiceConfig(org.apache.dubbo.config.ServiceConfig) ApplicationConfig(org.apache.dubbo.config.ApplicationConfig) ByteArrayInputStream(java.io.ByteArrayInputStream) ReferenceConfig(org.apache.dubbo.config.ReferenceConfig) DubboBootstrap(org.apache.dubbo.config.bootstrap.DubboBootstrap) ProtocolConfig(org.apache.dubbo.config.ProtocolConfig) Test(org.junit.jupiter.api.Test)

Example 17 with ServiceConfig

use of org.apache.dubbo.config.ServiceConfig in project dubbo by alibaba.

the class CacheTest method testCache.

private void testCache(String type) throws Exception {
    ApplicationConfig applicationConfig = new ApplicationConfig("cache-test");
    RegistryConfig registryConfig = new RegistryConfig("N/A");
    ProtocolConfig protocolConfig = new ProtocolConfig("injvm");
    ServiceConfig<CacheService> service = new ServiceConfig<CacheService>();
    service.setApplication(applicationConfig);
    service.setRegistry(registryConfig);
    service.setProtocol(protocolConfig);
    service.setInterface(CacheService.class.getName());
    service.setRef(new CacheServiceImpl());
    service.export();
    try {
        ReferenceConfig<CacheService> reference = new ReferenceConfig<CacheService>();
        reference.setApplication(applicationConfig);
        reference.setInterface(CacheService.class);
        reference.setUrl("injvm://127.0.0.1?scope=remote&cache=true");
        MethodConfig method = new MethodConfig();
        method.setName("findCache");
        method.setCache(type);
        reference.setMethods(Arrays.asList(method));
        CacheService cacheService = reference.get();
        try {
            // verify cache, same result is returned for multiple invocations (in fact, the return value increases
            // on every invocation on the server side)
            String fix = null;
            cacheService.findCache("0");
            for (int i = 0; i < 3; i++) {
                String result = cacheService.findCache("0");
                assertTrue(fix == null || fix.equals(result));
                fix = result;
                Thread.sleep(100);
            }
            if ("lru".equals(type)) {
                // default cache.size is 1000 for LRU, should have cache expired if invoke more than 1001 times
                for (int n = 0; n < 1001; n++) {
                    String pre = null;
                    cacheService.findCache(String.valueOf(n));
                    for (int i = 0; i < 10; i++) {
                        String result = cacheService.findCache(String.valueOf(n));
                        assertTrue(pre == null || pre.equals(result));
                        pre = result;
                    }
                }
                // verify if the first cache item is expired in LRU cache
                String result = cacheService.findCache("0");
                assertFalse(fix == null || fix.equals(result));
            }
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : RegistryConfig(org.apache.dubbo.config.RegistryConfig) MethodConfig(org.apache.dubbo.config.MethodConfig) ApplicationConfig(org.apache.dubbo.config.ApplicationConfig) ServiceConfig(org.apache.dubbo.config.ServiceConfig) ReferenceConfig(org.apache.dubbo.config.ReferenceConfig) ProtocolConfig(org.apache.dubbo.config.ProtocolConfig)

Example 18 with ServiceConfig

use of org.apache.dubbo.config.ServiceConfig in project dubbo by alibaba.

the class PublishingServiceDefinitionListenerTest method testOnServiceConfigExportedEvent.

/**
 * Test {@link ServiceConfigExportedEvent} arising
 */
@Test
public void testOnServiceConfigExportedEvent() {
    ServiceConfig<EchoService> serviceConfig = new ServiceConfig<>();
    serviceConfig.setInterface(EchoService.class);
    serviceConfig.setRef(new EchoServiceImpl());
    serviceConfig.setRegistry(new RegistryConfig("N/A"));
    serviceConfig.setProtocol(new ProtocolConfig("dubbo", NetUtils.getAvailablePort(20880 + new Random().nextInt(10000))));
    serviceConfig.export();
    String serviceDefinition = writableMetadataService.getServiceDefinition(EchoService.class.getName());
    ServiceDefinition serviceDefinitionBuild = ServiceDefinitionBuilder.build(serviceConfig.getInterfaceClass());
    assertEquals(serviceDefinition, JSON.toJSONString(serviceDefinitionBuild));
    serviceConfig.unexport();
}
Also used : RegistryConfig(org.apache.dubbo.config.RegistryConfig) Random(java.util.Random) ServiceConfig(org.apache.dubbo.config.ServiceConfig) EchoService(org.apache.dubbo.config.bootstrap.EchoService) ProtocolConfig(org.apache.dubbo.config.ProtocolConfig) EchoServiceImpl(org.apache.dubbo.config.bootstrap.EchoServiceImpl) ServiceDefinition(org.apache.dubbo.metadata.definition.model.ServiceDefinition) Test(org.junit.jupiter.api.Test)

Example 19 with ServiceConfig

use of org.apache.dubbo.config.ServiceConfig in project dubbo by alibaba.

the class ConfigTest method testApiOverrideProperties.

@Test
public void testApiOverrideProperties() throws Exception {
    ApplicationConfig application = new ApplicationConfig();
    application.setName("api-override-properties");
    RegistryConfig registry = new RegistryConfig();
    registry.setAddress("N/A");
    ProtocolConfig protocol = new ProtocolConfig();
    protocol.setName("dubbo");
    protocol.setPort(13123);
    ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
    service.setInterface(DemoService.class);
    service.setRef(new DemoServiceImpl());
    service.setRegistry(registry);
    service.setProtocol(protocol);
    ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>();
    reference.setRegistry(new RegistryConfig(RegistryConfig.NO_AVAILABLE));
    reference.setInterface(DemoService.class);
    reference.setUrl("dubbo://127.0.0.1:13123");
    DubboBootstrap bootstrap = DubboBootstrap.getInstance().application(application).registry(registry).protocol(protocol).service(service).reference(reference).start();
    try {
        URL url = service.getExportedUrls().get(0);
        assertEquals("api-override-properties", url.getParameter("application"));
        assertEquals("world", url.getParameter("owner"));
        assertEquals(13123, url.getPort());
        url = reference.getExportedUrls().get(0);
        assertEquals("2000", url.getParameter("timeout"));
    } finally {
        bootstrap.stop();
    }
}
Also used : RegistryConfig(org.apache.dubbo.config.RegistryConfig) ApplicationConfig(org.apache.dubbo.config.ApplicationConfig) ServiceConfig(org.apache.dubbo.config.ServiceConfig) ReferenceConfig(org.apache.dubbo.config.ReferenceConfig) DemoService(org.apache.dubbo.config.spring.api.DemoService) DubboBootstrap(org.apache.dubbo.config.bootstrap.DubboBootstrap) ProtocolConfig(org.apache.dubbo.config.ProtocolConfig) URL(org.apache.dubbo.common.URL) DemoServiceImpl(org.apache.dubbo.config.spring.impl.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Example 20 with ServiceConfig

use of org.apache.dubbo.config.ServiceConfig in project dubbo by alibaba.

the class ConfigTest method testGenericServiceConfig.

@Test
public void testGenericServiceConfig() throws Exception {
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setRegistry(new RegistryConfig("mock://localhost"));
    service.setInterface(DemoService.class.getName());
    service.setGeneric(GENERIC_SERIALIZATION_BEAN);
    service.setRef((method, parameterTypes, args) -> null);
    DubboBootstrap bootstrap = DubboBootstrap.getInstance().application(new ApplicationConfig("test")).service(service).start();
    try {
        Collection<Registry> collection = MockRegistryFactory.getCachedRegistry();
        MockRegistry registry = (MockRegistry) collection.iterator().next();
        URL url = registry.getRegistered().get(0);
        Assertions.assertEquals(GENERIC_SERIALIZATION_BEAN, url.getParameter(GENERIC_KEY));
    } finally {
        MockRegistryFactory.cleanCachedRegistry();
        bootstrap.stop();
    }
}
Also used : RegistryConfig(org.apache.dubbo.config.RegistryConfig) GenericService(org.apache.dubbo.rpc.service.GenericService) ServiceConfig(org.apache.dubbo.config.ServiceConfig) ApplicationConfig(org.apache.dubbo.config.ApplicationConfig) MockRegistry(org.apache.dubbo.config.spring.registry.MockRegistry) DemoService(org.apache.dubbo.config.spring.api.DemoService) DubboBootstrap(org.apache.dubbo.config.bootstrap.DubboBootstrap) MockRegistry(org.apache.dubbo.config.spring.registry.MockRegistry) Registry(org.apache.dubbo.registry.Registry) URL(org.apache.dubbo.common.URL) Test(org.junit.jupiter.api.Test)

Aggregations

ServiceConfig (org.apache.dubbo.config.ServiceConfig)26 RegistryConfig (org.apache.dubbo.config.RegistryConfig)19 Test (org.junit.jupiter.api.Test)19 ApplicationConfig (org.apache.dubbo.config.ApplicationConfig)18 ProtocolConfig (org.apache.dubbo.config.ProtocolConfig)14 DubboBootstrap (org.apache.dubbo.config.bootstrap.DubboBootstrap)14 DemoService (org.apache.dubbo.config.spring.api.DemoService)11 ReferenceConfig (org.apache.dubbo.config.ReferenceConfig)9 URL (org.apache.dubbo.common.URL)7 GenericService (org.apache.dubbo.rpc.service.GenericService)7 DemoServiceImpl (org.apache.dubbo.config.spring.impl.DemoServiceImpl)6 ArrayList (java.util.ArrayList)5 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 List (java.util.List)3 JavaBeanDescriptor (org.apache.dubbo.common.beanutil.JavaBeanDescriptor)2 MethodConfig (org.apache.dubbo.config.MethodConfig)2 ProviderConfig (org.apache.dubbo.config.ProviderConfig)2 UserService (org.apache.dubbo.config.bootstrap.rest.UserService)2 UserServiceImpl (org.apache.dubbo.config.bootstrap.rest.UserServiceImpl)2 GenericException (org.apache.dubbo.rpc.service.GenericException)2