Search in sources :

Example 6 with ReferenceConfig

use of com.alibaba.dubbo.config.ReferenceConfig 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 7 with ReferenceConfig

use of com.alibaba.dubbo.config.ReferenceConfig 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 8 with ReferenceConfig

use of com.alibaba.dubbo.config.ReferenceConfig in project tesla by linking12.

the class DynamicDubboClient method doRemoteCall.

@Override
public String doRemoteCall(final FilterRpcDO rpcDo, final String jsonInput) {
    try {
        final String serviceName = rpcDo.getServiceName();
        final String methodName = rpcDo.getMethodName();
        final String group = rpcDo.getServiceGroup();
        final String version = rpcDo.getServiceVersion();
        final String paramType = rpcDo.getInputParam();
        ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
        reference.setApplication(applicationConfig);
        reference.setRegistry(registryConfig);
        reference.setInterface(serviceName);
        reference.setGroup(group);
        reference.setGeneric(true);
        reference.setCheck(false);
        reference.setVersion(version);
        ReferenceConfigCache cache = ReferenceConfigCache.getCache();
        GenericService genericService = cache.get(reference);
        Pair<String[], Object[]> typeAndValue = paramTypeAndValue(paramType, jsonInput);
        Object response = genericService.$invoke(methodName, typeAndValue.getLeft(), typeAndValue.getRight());
        return JSON.toJSONString(response);
    } catch (Throwable e) {
        throw new IllegalArgumentException(String.format("service definition is wrong,please check the proto file you update,service is %s, method is %s", rpcDo.getServiceName(), rpcDo.getMethodName()), e);
    }
}
Also used : GenericService(com.alibaba.dubbo.rpc.service.GenericService) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) ReferenceConfigCache(com.alibaba.dubbo.config.utils.ReferenceConfigCache)

Aggregations

ReferenceConfig (com.alibaba.dubbo.config.ReferenceConfig)8 ApplicationConfig (com.alibaba.dubbo.config.ApplicationConfig)7 RegistryConfig (com.alibaba.dubbo.config.RegistryConfig)6 ProtocolConfig (com.alibaba.dubbo.config.ProtocolConfig)5 ServiceConfig (com.alibaba.dubbo.config.ServiceConfig)5 Test (org.junit.Test)4 Date (java.util.Date)3 GenericService (com.alibaba.dubbo.rpc.service.GenericService)2 MethodConfig (com.alibaba.dubbo.config.MethodConfig)1 DemoService (com.alibaba.dubbo.config.spring.api.DemoService)1 ReferenceConfigCache (com.alibaba.dubbo.config.utils.ReferenceConfigCache)1 RpcException (com.alibaba.dubbo.rpc.RpcException)1 GenericException (com.alibaba.dubbo.rpc.service.GenericException)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1