use of com.alipay.sofa.rpc.server.rest.RestService 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.server.rest.RestService in project sofa-rpc by sofastack.
the class MultiProtocolServerExpTest method testMultiProtocolExp.
@Test
public void testMultiProtocolExp() throws NoSuchFieldException {
try {
Field field = DefaultProviderBootstrap.class.getDeclaredField("EXPORTED_KEYS");
Map map = null;
field.setAccessible(true);
map = (ConcurrentMap<String, AtomicInteger>) field.get(null);
map.clear();
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
try {
// 只有2个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig).setRepeatedExportLimit(1).setRegister(false);
providerConfig.export();
ServerConfig serverConfig2 = new ServerConfig().setStopTimeout(0).setPort(22223).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setQueues(100).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<RestService> providerConfig2 = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig2).setRepeatedExportLimit(1).setRegister(false);
providerConfig2.export();
ProviderConfig<RestService> providerConfig3 = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig2).setRepeatedExportLimit(1).setRegister(false);
providerConfig3.export();
Assert.fail();
} catch (Throwable e) {
// reflect to fetch export key
ConcurrentMap<String, AtomicInteger> map = null;
Field field = DefaultProviderBootstrap.class.getDeclaredField("EXPORTED_KEYS");
try {
field.setAccessible(true);
map = (ConcurrentMap<String, AtomicInteger>) field.get(null);
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
// two providers publish done, the third will false, and revert counter, export value is 1
for (Map.Entry<String, AtomicInteger> entry : map.entrySet()) {
AtomicInteger atomicInteger = entry.getValue();
Assert.assertEquals(1, atomicInteger.get());
}
}
}
use of com.alipay.sofa.rpc.server.rest.RestService in project sofa-rpc by sofastack.
the class MultiProtocolServerTest method testMultiProtocol.
@Test
public void testMultiProtocol() {
try {
// 只有2个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setQueues(100).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig).setRepeatedExportLimit(1).setRegister(false);
providerConfig.export();
ServerConfig serverConfig2 = new ServerConfig().setStopTimeout(0).setPort(22223).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setQueues(100).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<RestService> providerConfig2 = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig2).setRepeatedExportLimit(1).setRegister(false);
providerConfig2.export();
} catch (Throwable e) {
Assert.fail();
}
}
Aggregations