use of com.alipay.sofa.rpc.config.ProviderConfig 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.ProviderConfig 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);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class RejectedTest method testAll.
@Test
public void testAll() {
ServerConfig serverConfig = new ServerConfig().setStopTimeout(0).setPort(22222).setQueues(0).setCoreThreads(1).setMaxThreads(2);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setServer(serverConfig).setRegister(false);
providerConfig.export();
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setTimeout(3000).setDirectUrl("bolt://127.0.0.1:22222").setRegister(false);
final HelloService helloService = consumerConfig.refer();
final AtomicInteger success = new AtomicInteger();
final AtomicInteger failure = new AtomicInteger();
int times = 3;
final CountDownLatch latch = new CountDownLatch(times);
for (int i = 0; i < times; i++) {
Thread thread1 = new Thread(new Runnable() {
@Override
public void run() {
try {
helloService.sayHello("xxx", 22);
success.incrementAndGet();
} catch (Exception e) {
if (e instanceof SofaRpcException) {
Assert.assertEquals(((SofaRpcException) e).getErrorType(), RpcErrorType.SERVER_BUSY);
}
failure.incrementAndGet();
} finally {
latch.countDown();
}
}
}, "T1");
thread1.start();
}
try {
latch.await(10000, TimeUnit.MILLISECONDS);
} catch (InterruptedException ignore) {
}
Assert.assertEquals(success.get(), 2);
Assert.assertEquals(failure.get(), 1);
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class Http1ServerTest method testHttp1Json.
@Test
public void testHttp1Json() throws Exception {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_HTTP).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setUniqueId("uuu").setRegister(false);
providerConfig.export();
HttpClient httpclient = HttpClientBuilder.create().build();
{
// POST jackson不存在的接口
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService/adasdad";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
ExampleObj obj = new ExampleObj();
obj.setId(1);
obj.setName("xxx");
byte[] bytes = mapper.writeValueAsBytes(obj);
ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// POST 不存在的方法
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/adasdad";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
ExampleObj obj = new ExampleObj();
obj.setId(1);
obj.setName("xxx");
byte[] bytes = mapper.writeValueAsBytes(obj);
ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(404, httpResponse.getStatusLine().getStatusCode());
Assert.assertNotNull(getStringContent(httpResponse));
}
{
// POST 不传 HEAD_SERIALIZE_TYPE
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/object";
HttpPost httpPost = new HttpPost(url);
ExampleObj obj = new ExampleObj();
obj.setId(1);
obj.setName("xxx");
byte[] bytes = mapper.writeValueAsBytes(obj);
ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
ExampleObj result = mapper.readValue(data, ExampleObj.class);
Assert.assertEquals("xxxxx", result.getName());
}
{
// POST 正常请求
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/object";
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "json");
ExampleObj obj = new ExampleObj();
obj.setId(1);
obj.setName("xxx");
byte[] bytes = mapper.writeValueAsBytes(obj);
ByteArrayEntity entity = new ByteArrayEntity(bytes, ContentType.create("application/json"));
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
ExampleObj result = mapper.readValue(data, ExampleObj.class);
Assert.assertEquals("xxxxx", result.getName());
}
}
use of com.alipay.sofa.rpc.config.ProviderConfig in project sofa-rpc by sofastack.
the class BootstrapsTest method from.
@Test
public void from() throws Exception {
ProviderConfig providerConfig = new ProviderConfig().setBootstrap("test");
ProviderBootstrap bootstrap = Bootstraps.from(providerConfig);
Assert.assertEquals(TestProviderBootstrap.class, bootstrap.getClass());
Assert.assertEquals(providerConfig, bootstrap.getProviderConfig());
// if not set bootstrap
providerConfig = new ProviderConfig();
bootstrap = Bootstraps.from(providerConfig);
Assert.assertEquals(TestProviderBootstrap.class, bootstrap.getClass());
Assert.assertEquals(providerConfig, bootstrap.getProviderConfig());
}
Aggregations