use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class CustomizeFilterTest method beforeClass.
@BeforeClass
public static void beforeClass() {
customizeContainerRequestTestFilter = new CustomizeContainerRequestTestFilter();
customizeContainerResponseTestFilter = new CustomizeContainerResponseTestFilter();
customizeClientRequestTestFilter = new CustomizeClientRequestTestFilter();
customizeClientResponseTestFilter = new CustomizeClientResponseTestFilter();
JAXRSProviderManager.registerCustomProviderInstance(customizeContainerRequestTestFilter);
JAXRSProviderManager.registerCustomProviderInstance(customizeContainerResponseTestFilter);
JAXRSProviderManager.registerCustomProviderInstance(customizeClientRequestTestFilter);
JAXRSProviderManager.registerCustomProviderInstance(customizeClientResponseTestFilter);
providerFilter = new CustomizeTestFilter();
List<Filter> providerFilters = new ArrayList<Filter>(2);
providerFilters.add(providerFilter);
ServerConfig restServer = new ServerConfig().setPort(8583).setProtocol(RpcConstants.PROTOCOL_TYPE_REST);
List<ServerConfig> servers = new ArrayList<ServerConfig>(2);
servers.add(restServer);
providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setRegister(false).setServer(servers).setFilterRef(providerFilters);
providerConfig.export();
// rest服务
clientFilter = new CustomizeTestFilter();
List<Filter> clientFilters = new ArrayList<Filter>(2);
clientFilters.add(clientFilter);
ConsumerConfig<RestService> consumerConfigRest = new ConsumerConfig<RestService>().setInterfaceId(RestService.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setDirectUrl("rest://127.0.0.1:8583").setTimeout(1000).setFilterRef(clientFilters).setApplication(new ApplicationConfig().setAppName("TestClientRest"));
filterRestService = consumerConfigRest.refer();
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class RestDirectUrlTest method testAll.
@Test
public void testAll() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setBootstrap("rest").setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
providerConfig.export();
final ConsumerConfig<RestService> consumerConfig = new ConsumerConfig<RestService>().setInterfaceId(RestService.class.getName()).setDirectUrl("rest://127.0.0.1:12300").setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setBootstrap("rest").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
RestService restService = consumerConfig.refer();
Assert.assertEquals(restService.query(11), "hello world !null");
serverConfig.getServer().stop();
// 关闭后再调用一个抛异常
try {
restService.query(11);
} catch (Exception e) {
// 应该抛出异常
Assert.assertTrue(e instanceof SofaRpcException);
}
Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return CommonUtils.isEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
}
}, true, 50, 40));
serverConfig.getServer().start();
// 等待客户端重连服务端
Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return CommonUtils.isNotEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
}
}, true, 50, 60));
Assert.assertEquals(restService.query(11), "hello world !null");
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class AllTypeServiceTest method testAll.
@Test
public void testAll() {
ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setDaemon(false);
ProviderConfig<AllTypeService> CProvider = new ProviderConfig<AllTypeService>().setInterfaceId(AllTypeService.class.getName()).setRef(new AllTypeServiceImpl()).setServer(serverConfig2);
CProvider.export();
ConsumerConfig<AllTypeService> BConsumer = new ConsumerConfig<AllTypeService>().setInterfaceId(AllTypeService.class.getName()).setTimeout(50000).setSerialization("hessian").setDirectUrl("bolt://127.0.0.1:22222");
AllTypeService helloService = BConsumer.refer();
// 基本类型和数组
helloService.echo();
Assert.assertEquals("a1", helloService.echo2("a", 1));
Assert.assertEquals(1, helloService.echoInt(1));
Assert.assertEquals(AllTypeEnum.TB, helloService.echoEnum(AllTypeEnum.TB));
Assert.assertEquals(AllTypeEnum2.TB, helloService.echoEnum2(AllTypeEnum2.TB));
Assert.assertArrayEquals(new String[] { "11", "22" }, helloService.echoStringArray(new String[] { "11", "22" }));
Assert.assertArrayEquals(new String[][] { { "aa", "bb" }, { "11", "22" } }, helloService.echoStringArray2(new String[][] { { "aa", "bb" }, { "11", "22" } }));
// 集合
List<String> list = new ArrayList<String>();
list.add("11");
list.add("22");
Assert.assertEquals(list, helloService.echoList(list));
list = new LinkedList<String>();
list.add("11");
list.add("22");
Assert.assertEquals(list, helloService.echoList(list));
list = new CopyOnWriteArrayList<String>();
list.add("11");
list.add("22");
Assert.assertEquals(list, helloService.echoList(list));
list = new LinkedList<String>();
list.add("11");
list.add("22");
Assert.assertEquals(list, helloService.echoList(list));
list = Arrays.asList("11", "22");
Assert.assertEquals(list, helloService.echoList(list));
MyList<String> myList = new MyList<String>();
myList.setListName("xxx");
myList.add("11");
myList.add("22");
MyList myList1 = helloService.echoMyList(myList);
// Assert.assertEquals(myList.getListName(), myList1.getListName()); // TODO 目前不支持自定义LIST的字段
// Assert.assertEquals(myList, myList1);
Assert.assertEquals(myList.get(0), myList1.get(0));
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("xx", 11);
map.put("yy", 22);
Assert.assertEquals(map, helloService.echoMap(map));
map = new LinkedHashMap<String, Integer>();
map.put("xx", 11);
map.put("yy", 22);
Assert.assertEquals(map, helloService.echoMap(map));
map = new TreeMap<String, Integer>();
map.put("xx", 11);
map.put("yy", 22);
Assert.assertEquals(map, helloService.echoMap(map));
map = new ConcurrentHashMap<String, Integer>();
map.put("xx", 11);
map.put("yy", 22);
Assert.assertEquals(map, helloService.echoMap(map));
map = Collections.singletonMap("xx", 11);
Assert.assertEquals(map, helloService.echoMap(map));
MyMap myMap = new MyMap();
myMap.setMapName("map");
myMap.put("xx", 11);
myMap.put("yy", 22);
MyMap myMap1 = helloService.echoMyMap(myMap);
// Assert.assertEquals(myMap.getMapName(), myMap1.getMapName()); // TODO 目前不支持自定义LIST的字段
// Assert.assertEquals(myMap, myMap1);
Assert.assertEquals(myMap.get("xx"), myMap1.get("xx"));
Set<String> set = new HashSet<String>();
set.add("11");
set.add("22");
Assert.assertEquals(set, helloService.echoSet(set));
set = new TreeSet<String>();
set.add("11");
set.add("22");
Assert.assertEquals(set, helloService.echoSet(set));
set = new LinkedHashSet<String>();
set.add("11");
set.add("22");
Assert.assertEquals(set, helloService.echoSet(set));
set = new ConcurrentHashSet<String>();
set.add("11");
set.add("22");
Assert.assertEquals(set, helloService.echoSet(set));
MySet<String> mySet = new MySet<String>();
mySet.setSetName("set");
myList.add("11");
myList.add("22");
MySet mySet1 = helloService.echoMySet(mySet);
// Assert.assertEquals(mySet.getSetName(), mySet1.getSetName()); // TODO 目前不支持自定义LIST的字段
// Assert.assertEquals(mySet, mySet1);
Assert.assertEquals(mySet.size(), mySet1.size());
// 常用类型
Date date = new Date();
Assert.assertEquals(date, helloService.echoDate(date));
Assert.assertEquals(new BigDecimal("6.6"), helloService.echoNum(new BigDecimal("2.2"), new BigInteger("3")));
Assert.assertEquals(new BigInteger("3"), helloService.echoBigInteger(new BigInteger("3")));
Assert.assertEquals(Currency.getInstance("CNY"), helloService.echoCurrency(Currency.getInstance("CNY")));
// 自定义类型
AllTypeObj obj = new AllTypeObj();
AllTypeSubObj subObj = new AllTypeSubObj();
Assert.assertEquals(obj, helloService.echoObj(obj));
Assert.assertEquals(subObj, helloService.echoObj(subObj));
Assert.assertEquals(obj, helloService.echoInterfaceObj(obj));
Assert.assertEquals(subObj, helloService.echoInterfaceObj(subObj));
Assert.assertEquals(obj, helloService.echoSubObj(obj));
Assert.assertEquals(subObj, helloService.echoSubObj(subObj));
{
ConsumerConfig<AllTypeService> aConsumer = new ConsumerConfig<AllTypeService>().setInterfaceId(AllTypeService.class.getName()).setTimeout(50000).setSerialization("hessian").setRepeatedReferLimit(-1).setProxy("javassist").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setDirectUrl("bolt://127.0.0.1:22222");
AllTypeService helloService2 = aConsumer.refer();
Assert.assertEquals(0, helloService2.echoInt(1));
boolean error = false;
Integer v = null;
try {
v = (Integer) RpcInvokeContext.getContext().getFuture().get();
} catch (Exception e) {
error = true;
}
Assert.assertFalse(error);
Assert.assertTrue(v == 1);
}
{
ConsumerConfig<AllTypeService> aConsumer = new ConsumerConfig<AllTypeService>().setInterfaceId(AllTypeService.class.getName()).setTimeout(50000).setSerialization("hessian").setRepeatedReferLimit(-1).setProxy("jdk").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setDirectUrl("bolt://127.0.0.1:22222");
AllTypeService helloService2 = aConsumer.refer();
Assert.assertEquals(0, helloService2.echoInt(1));
boolean error = false;
Integer v = null;
try {
v = (Integer) RpcInvokeContext.getContext().getFuture().get();
} catch (Exception e) {
error = true;
}
Assert.assertFalse(error);
Assert.assertTrue(v == 1);
}
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class MockTestRegistry method subscribe.
@Override
public List<ProviderGroup> subscribe(ConsumerConfig config) {
String key = buildKey(config);
Map<ConsumerConfig, ProviderInfoListener> listeners = notifyListeners.get(key);
if (listeners == null) {
listeners = new ConcurrentHashMap<ConsumerConfig, ProviderInfoListener>();
Map<ConsumerConfig, ProviderInfoListener> old = notifyListeners.putIfAbsent(key, listeners);
if (old != null) {
listeners = old;
}
}
final ProviderInfoListener listener = config.getProviderInfoListener();
listeners.put(config, listener);
ProviderGroup group = memoryCache.get(key);
List<ProviderGroup> groups = new ArrayList<ProviderGroup>();
if (group != null) {
groups.add(group);
}
return doReturn(listener, groups);
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class NotFoundInvokerTest method testAll.
@Test
public void testAll() {
ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setQueues(100).setCoreThreads(10).setMaxThreads(10);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1500)).setServer(serverConfig).setRegister(false);
providerConfig.export();
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(30000).setFilterRef(Collections.<Filter>singletonList(new Filter() {
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
// 造一个假方法
request.setMethodName(request.getMethodName() + "_unknown");
return invoker.invoke(request);
}
})).setRegister(false);
HelloService helloService = consumerConfig.refer();
boolean ok = true;
try {
// 找不到方法
helloService.sayHello("xxx", 22);
} catch (Exception e) {
if (e instanceof SofaRpcException) {
Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
} else if (e instanceof UndeclaredThrowableException) {
Assert.assertEquals(((SofaRpcException) e.getCause()).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
}
ok = false;
}
Assert.assertFalse(ok);
ConsumerConfig<EchoService> consumerConfig2 = new ConsumerConfig<EchoService>().setInterfaceId(EchoService.class.getName()).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(30000).setRegister(false);
EchoService echoService = consumerConfig2.refer();
ok = true;
try {
echoService.echoStr("xx");
} catch (Exception e) {
if (e instanceof SofaRpcException) {
Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
} else if (e instanceof UndeclaredThrowableException) {
Assert.assertEquals(((SofaRpcException) e.getCause()).getErrorType(), RpcErrorType.SERVER_UNDECLARED_ERROR);
}
ok = false;
}
Assert.assertFalse(ok);
}
Aggregations