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();
}
}
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();
}
}
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);
}
}
Aggregations