use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class ZookeeperRegistryTest method testProviderObserver.
/**
* 测试Zookeeper Provider Observer
*
* @throws Exception
*/
@Test
public void testProviderObserver() throws Exception {
int timeoutPerSub = 2000;
ServerConfig serverConfig = new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12200);
ProviderConfig<?> provider = new ProviderConfig();
provider.setInterfaceId(TEST_SERVICE_NAME).setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setRegister(true).setRegistry(registryConfig).setSerialization("hessian2").setServer(serverConfig).setWeight(222).setTimeout(3000);
// 注册
registry.register(provider);
ConsumerConfig<?> consumer = new ConsumerConfig();
consumer.setInterfaceId(TEST_SERVICE_NAME).setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
// 订阅
CountDownLatch latch = new CountDownLatch(1);
MockProviderInfoListener providerInfoListener = new MockProviderInfoListener();
providerInfoListener.setCountDownLatch(latch);
consumer.setProviderInfoListener(providerInfoListener);
List<ProviderGroup> all = registry.subscribe(consumer);
providerInfoListener.updateAllProviders(all);
Map<String, ProviderInfo> ps = providerInfoListener.getData();
Assert.assertEquals("after register: 1", 1, ps.size());
// 订阅 错误的uniqueId
ConsumerConfig<?> consumerNoUniqueId = new ConsumerConfig();
consumerNoUniqueId.setInterfaceId(TEST_SERVICE_NAME).setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
latch = new CountDownLatch(1);
MockProviderInfoListener providerInfoListener3 = new MockProviderInfoListener();
providerInfoListener3.setCountDownLatch(latch);
consumerNoUniqueId.setProviderInfoListener(providerInfoListener3);
all = registry.subscribe(consumerNoUniqueId);
providerInfoListener3.updateAllProviders(all);
Map<String, ProviderInfo> ps3 = providerInfoListener3.getData();
Assert.assertEquals("wrong uniqueId: 0", 0, ps3.size());
// 反注册
latch = new CountDownLatch(1);
providerInfoListener.setCountDownLatch(latch);
registry.unRegister(provider);
latch.await(timeoutPerSub, TimeUnit.MILLISECONDS);
Assert.assertEquals("after unregister: 0", 0, ps.size());
// 一次发2个端口的再次注册
latch = new CountDownLatch(2);
providerInfoListener.setCountDownLatch(latch);
provider.getServer().add(new ServerConfig().setProtocol("bolt").setHost("0.0.0.0").setPort(12201));
registry.register(provider);
latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
Assert.assertEquals("after register two servers: 2", 2, ps.size());
// 重复订阅
ConsumerConfig<?> consumer2 = new ConsumerConfig();
consumer2.setInterfaceId(TEST_SERVICE_NAME).setUniqueId("unique123Id").setApplication(new ApplicationConfig().setAppName("test-server")).setProxy("javassist").setSubscribe(true).setSerialization("java").setInvokeType("sync").setTimeout(4444);
CountDownLatch latch2 = new CountDownLatch(1);
MockProviderInfoListener providerInfoListener2 = new MockProviderInfoListener();
providerInfoListener2.setCountDownLatch(latch2);
consumer2.setProviderInfoListener(providerInfoListener2);
providerInfoListener2.updateAllProviders(registry.subscribe(consumer2));
latch2.await(timeoutPerSub, TimeUnit.MILLISECONDS);
Map<String, ProviderInfo> ps2 = providerInfoListener2.getData();
Assert.assertEquals("after register duplicate: 2", 2, ps2.size());
// 取消订阅者1
registry.unSubscribe(consumer);
// 批量反注册,判断订阅者2的数据
latch = new CountDownLatch(2);
providerInfoListener2.setCountDownLatch(latch);
List<ProviderConfig> providerConfigList = new ArrayList<ProviderConfig>();
providerConfigList.add(provider);
registry.batchUnRegister(providerConfigList);
latch.await(timeoutPerSub * 2, TimeUnit.MILLISECONDS);
Assert.assertEquals("after unregister: 0", 0, ps2.size());
// 批量取消订阅
List<ConsumerConfig> consumerConfigList = new ArrayList<ConsumerConfig>();
consumerConfigList.add(consumer2);
registry.batchUnSubscribe(consumerConfigList);
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class AbstractClusterTest method testDestroyWithDestroyHook.
@Test
public void testDestroyWithDestroyHook() {
ConsumerConfig consumerConfig = new ConsumerConfig().setProtocol("test").setBootstrap("test");
ConsumerBootstrap consumerBootstrap = new ConsumerBootstrap(consumerConfig) {
@Override
public Object refer() {
return null;
}
@Override
public void unRefer() {
}
@Override
public Object getProxyIns() {
return null;
}
@Override
public Cluster getCluster() {
return null;
}
@Override
public List<ProviderGroup> subscribe() {
return null;
}
@Override
public boolean isSubscribed() {
return false;
}
};
AbstractCluster abstractCluster = new AbstractCluster(consumerBootstrap) {
@Override
protected SofaResponse doInvoke(SofaRequest msg) throws SofaRpcException {
return null;
}
};
List<String> hookActionResult = new ArrayList<>(2);
Destroyable.DestroyHook destroyHook = new Destroyable.DestroyHook() {
@Override
public void preDestroy() {
hookActionResult.add("preDestroy");
}
@Override
public void postDestroy() {
hookActionResult.add("postDestroy");
}
};
abstractCluster.destroy(destroyHook);
Assert.assertEquals(2, hookActionResult.size());
Assert.assertEquals("preDestroy", hookActionResult.get(0));
Assert.assertEquals("postDestroy", hookActionResult.get(1));
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class RestProviderTest method testProvider.
@Test
public void testProvider() {
JAXRSProviderManager.registerCustomProviderInstance(new CustomerInjectorFactory());
JAXRSProviderManager.registerCustomProviderInstance(new ClientRequestTestFilter());
JAXRSProviderManager.registerCustomProviderInstance(new ClientRequestTestFilter2());
JAXRSProviderManager.registerCustomProviderInstance(new ContainerRequestTestFilter());
JAXRSProviderManager.registerCustomProviderInstance(new ContainerResponseTestFilter());
JAXRSProviderManager.registerCustomProviderInstance(new ClientResponseTestFilter());
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(8803).setProtocol(RpcConstants.PROTOCOL_TYPE_REST);
ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig).setBootstrap("rest").setRegister(false);
providerConfig.export();
ConsumerConfig<RestService> consumerConfig = new ConsumerConfig<RestService>().setInterfaceId(RestService.class.getName()).setDirectUrl("rest://127.0.0.1:8803").setProtocol("rest").setBootstrap("rest").setTimeout(30000).setRegister(false).setFilter(Arrays.asList("-*")).setApplication(new ApplicationConfig().setAppName("TestClient"));
RestService restService = consumerConfig.refer();
Assert.assertEquals("serverok", restService.get("ok"));
String nameA = ClientRequestTestFilter.getName();
String nameA2 = ClientRequestTestFilter2.getName();
String nameB = ContainerRequestTestFilter.getName();
String nameC = ContainerResponseTestFilter.getName();
String nameD = ClientResponseTestFilter.getName();
Assert.assertEquals("AtestInjecttestInject", nameA);
Assert.assertEquals("A2", nameA2);
Assert.assertEquals("B", nameB);
Assert.assertEquals("C", nameC);
Assert.assertEquals("D", nameD);
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class BoltDirectUrlTest method testAll.
// @Test
// FIXME 目前bolt的IO线程关闭时未释放,暂不支持本测试用例
public void testAll() {
// 只有2个线程 执行
ServerConfig serverConfig = new ServerConfig().setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setBootstrap("bolt").setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
providerConfig.export();
final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:12300").setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setBootstrap("bolt").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
HelloService helloService = consumerConfig.refer();
Assert.assertNotNull(helloService.sayHello("xx", 22));
serverConfig.getServer().stop();
// 关闭后再调用一个抛异常
try {
helloService.sayHello("xx", 22);
} 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.assertNotNull(helloService.sayHello("xx", 22));
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class H2cDirectUrlTest method testAll.
@Test
public void testAll() throws InterruptedException {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setBootstrap("h2c").setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
providerConfig.export();
final ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setDirectUrl("h2c://127.0.0.1:12300").setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setBootstrap("h2c").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
HttpService httpService = consumerConfig.refer();
ExampleObj request = new ExampleObj();
request.setId(200);
request.setName("yyy");
ExampleObj response = httpService.object(request);
Assert.assertEquals(200, response.getId());
Assert.assertEquals("yyyxx", response.getName());
serverConfig.getServer().stop();
// 关闭后再调用一个抛异常
try {
httpService.object(request);
} 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));
response = httpService.object(request);
Assert.assertEquals(200, response.getId());
Assert.assertEquals("yyyxx", response.getName());
}
Aggregations