Search in sources :

Example 1 with ServiceConfig

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

the class CacheTest method testCache.

private void testCache(String type) 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");
        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;
            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;
                    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(com.alibaba.dubbo.config.RegistryConfig) MethodConfig(com.alibaba.dubbo.config.MethodConfig) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig)

Example 2 with ServiceConfig

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

the class ValidationTest method testGenericValidation.

@Test
public void testGenericValidation() {
    ServiceConfig<ValidationService> service = new ServiceConfig<ValidationService>();
    service.setApplication(new ApplicationConfig("validation-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29582));
    service.setInterface(ValidationService.class.getName());
    service.setRef(new ValidationServiceImpl());
    service.setValidation(String.valueOf(true));
    service.export();
    try {
        ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
        reference.setApplication(new ApplicationConfig("validation-consumer"));
        reference.setInterface(ValidationService.class.getName());
        reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&validation=true&timeout=9000000");
        reference.setGeneric(true);
        GenericService validationService = reference.get();
        try {
            // Save OK
            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("name", "liangfei");
            parameter.put("Email", "liangfei@liang.fei");
            parameter.put("Age", 50);
            parameter.put("LoginDate", new Date(System.currentTimeMillis() - 1000000));
            parameter.put("ExpiryDate", new Date(System.currentTimeMillis() + 1000000));
            validationService.$invoke("save", new String[] { ValidationParameter.class.getName() }, new Object[] { parameter });
            // Save Error
            try {
                parameter = new HashMap<String, Object>();
                validationService.$invoke("save", new String[] { ValidationParameter.class.getName() }, new Object[] { parameter });
                Assert.fail();
            } catch (GenericException e) {
                Assert.assertTrue(e.getMessage().contains("Failed to validate service"));
            }
            // Delete OK
            validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 2, "abc" });
            // Delete Error
            try {
                validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 0, "abc" });
                Assert.fail();
            } catch (GenericException e) {
                Assert.assertTrue(e.getMessage().contains("Failed to validate service"));
            }
            try {
                validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 2, null });
                Assert.fail();
            } catch (GenericException e) {
                Assert.assertTrue(e.getMessage().contains("Failed to validate service"));
            }
            try {
                validationService.$invoke("delete", new String[] { long.class.getName(), String.class.getName() }, new Object[] { 0, null });
                Assert.fail();
            } catch (GenericException e) {
                Assert.assertTrue(e.getMessage().contains("Failed to validate service"));
            }
        } catch (GenericException e) {
            Assert.assertTrue(e.getMessage().contains("Failed to validate service"));
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) GenericService(com.alibaba.dubbo.rpc.service.GenericService) HashMap(java.util.HashMap) GenericException(com.alibaba.dubbo.rpc.service.GenericException) Date(java.util.Date) 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 3 with ServiceConfig

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

the class ValidationTest method testValidation.

@Test
public void testValidation() {
    ServiceConfig<ValidationService> service = new ServiceConfig<ValidationService>();
    service.setApplication(new ApplicationConfig("validation-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29582));
    service.setInterface(ValidationService.class.getName());
    service.setRef(new ValidationServiceImpl());
    service.setValidation(String.valueOf(true));
    service.export();
    try {
        ReferenceConfig<ValidationService> reference = new ReferenceConfig<ValidationService>();
        reference.setApplication(new ApplicationConfig("validation-consumer"));
        reference.setInterface(ValidationService.class);
        reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&validation=true");
        ValidationService validationService = reference.get();
        try {
            // Save OK
            ValidationParameter parameter = new ValidationParameter();
            parameter.setName("liangfei");
            parameter.setEmail("liangfei@liang.fei");
            parameter.setAge(50);
            parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
            parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
            validationService.save(parameter);
            try {
                parameter = new ValidationParameter();
                parameter.setName("l");
                parameter.setEmail("liangfei@liang.fei");
                parameter.setAge(50);
                parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
                parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
                validationService.save(parameter);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }
            // verify save group, save error
            try {
                parameter = new ValidationParameter();
                parameter.setName("liangfei");
                parameter.setAge(50);
                parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
                parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
                validationService.save(parameter);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
            }
            // and Update
            try {
                parameter = new ValidationParameter();
                parameter.setName("liangfei");
                parameter.setAge(50);
                parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
                parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
                validationService.relatedQuery(parameter);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertEquals(violations.size(), 2);
            }
            // Save Error
            try {
                parameter = new ValidationParameter();
                validationService.save(parameter);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertTrue(violations.size() == 3);
                Assert.assertNotNull(violations);
            }
            // Delete OK
            validationService.delete(2, "abc");
            // Delete Error
            try {
                validationService.delete(2, "a");
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            // Delete Error
            try {
                validationService.delete(0, "abc");
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(2, null);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(1, violations.size());
            }
            try {
                validationService.delete(0, null);
                Assert.fail();
            } catch (ConstraintViolationException ve) {
                Set<ConstraintViolation<?>> violations = ve.getConstraintViolations();
                Assert.assertNotNull(violations);
                Assert.assertEquals(2, violations.size());
            }
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) Set(java.util.Set) Date(java.util.Date) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) ConstraintViolationException(javax.validation.ConstraintViolationException) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) Test(org.junit.Test)

Example 4 with ServiceConfig

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

the class ValidationTest method testProviderValidation.

@Test
public void testProviderValidation() {
    ServiceConfig<ValidationService> service = new ServiceConfig<ValidationService>();
    service.setApplication(new ApplicationConfig("validation-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29582));
    service.setInterface(ValidationService.class.getName());
    service.setRef(new ValidationServiceImpl());
    service.setValidation(String.valueOf(true));
    service.export();
    try {
        ReferenceConfig<ValidationService> reference = new ReferenceConfig<ValidationService>();
        reference.setApplication(new ApplicationConfig("validation-consumer"));
        reference.setInterface(ValidationService.class);
        reference.setUrl("dubbo://127.0.0.1:29582");
        ValidationService validationService = reference.get();
        try {
            // Save OK
            ValidationParameter parameter = new ValidationParameter();
            parameter.setName("liangfei");
            parameter.setEmail("liangfei@liang.fei");
            parameter.setAge(50);
            parameter.setLoginDate(new Date(System.currentTimeMillis() - 1000000));
            parameter.setExpiryDate(new Date(System.currentTimeMillis() + 1000000));
            validationService.save(parameter);
            // Save Error
            try {
                parameter = new ValidationParameter();
                validationService.save(parameter);
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
            // Delete OK
            validationService.delete(2, "abc");
            // Delete Error
            try {
                validationService.delete(0, "abc");
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
            try {
                validationService.delete(2, null);
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
            try {
                validationService.delete(0, null);
                Assert.fail();
            } catch (RpcException e) {
                Assert.assertTrue(e.getMessage().contains("ConstraintViolation"));
            }
        } 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) RpcException(com.alibaba.dubbo.rpc.RpcException) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) Date(java.util.Date) Test(org.junit.Test)

Example 5 with ServiceConfig

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

the class ConfigTest method testConfig.

@Test
public void testConfig() {
    com.alibaba.dubbo.config.ServiceConfig<DemoService> service = new ServiceConfig<>();
    service.setApplication(applicationConfig);
    service.setRegistry(registryConfig);
    service.setInterface(DemoService.class);
    service.setRef(new DemoServiceImpl());
    com.alibaba.dubbo.config.ReferenceConfig<DemoService> reference = new ReferenceConfig<>();
    reference.setApplication(applicationConfig);
    reference.setRegistry(registryConfig);
    reference.setInterface(DemoService.class);
    DubboBootstrap bootstrap = DubboBootstrap.getInstance().application(applicationConfig).registry(registryConfig).service(service).reference(reference).start();
    DemoService demoService = bootstrap.getCache().get(reference);
    String message = demoService.sayHello("dubbo");
    Assertions.assertEquals("hello dubbo", message);
}
Also used : ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) DemoService(org.apache.dubbo.service.DemoService) DubboBootstrap(org.apache.dubbo.config.bootstrap.DubboBootstrap) DemoServiceImpl(org.apache.dubbo.service.DemoServiceImpl) Test(org.junit.jupiter.api.Test)

Aggregations

ServiceConfig (com.alibaba.dubbo.config.ServiceConfig)7 ApplicationConfig (com.alibaba.dubbo.config.ApplicationConfig)6 ReferenceConfig (com.alibaba.dubbo.config.ReferenceConfig)6 RegistryConfig (com.alibaba.dubbo.config.RegistryConfig)6 ProtocolConfig (com.alibaba.dubbo.config.ProtocolConfig)5 Test (org.junit.Test)4 Date (java.util.Date)3 MethodConfig (com.alibaba.dubbo.config.MethodConfig)1 RpcException (com.alibaba.dubbo.rpc.RpcException)1 GenericException (com.alibaba.dubbo.rpc.service.GenericException)1 GenericService (com.alibaba.dubbo.rpc.service.GenericService)1 GreetingsService (com.baeldung.dubbo.remote.GreetingsService)1 GreetingsServiceImpl (com.baeldung.dubbo.remote.GreetingsServiceImpl)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 DubboBootstrap (org.apache.dubbo.config.bootstrap.DubboBootstrap)1 DemoService (org.apache.dubbo.service.DemoService)1 DemoServiceImpl (org.apache.dubbo.service.DemoServiceImpl)1 Before (org.junit.Before)1