use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class Http1ServerTest method testHttp1Protobuf.
@Test
public void testHttp1Protobuf() 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 不存在的接口
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, "protobuf");
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
ByteArrayEntity entity = new ByteArrayEntity(request.toByteArray(), ContentType.create("application/protobuf"));
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, "protobuf");
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
ByteArrayEntity entity = new ByteArrayEntity(request.toByteArray(), ContentType.create("application/protobuf"));
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/echoPb";
HttpPost httpPost = new HttpPost(url);
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
ByteArrayEntity entity = new ByteArrayEntity(request.toByteArray(), ContentType.create("application/protobuf"));
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
EchoResponse response = EchoResponse.parseFrom(data);
Assert.assertEquals("helloxxx", response.getMessage());
}
{
// POST 正常请求
String url = "http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/echoPb";
HttpPost httpPost = new HttpPost(url);
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE, "protobuf");
ByteArrayEntity entity = new ByteArrayEntity(request.toByteArray(), ContentType.create("application/protobuf"));
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
byte[] data = EntityUtils.toByteArray(httpResponse.getEntity());
EchoResponse response = EchoResponse.parseFrom(data);
Assert.assertEquals("helloxxx", response.getMessage());
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class SyncFilterTest method test.
@Test
public void test() {
ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
// ProviderConfig
TestSyncFilter filter1 = new TestSyncFilter();
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl(1000)).setFilterRef(Arrays.asList((Filter) filter1)).setApplication(new ApplicationConfig().setAppName("sss")).setServer(serverConfig2);
providerConfig.export();
// ConsumerConfig
TestSyncFilter filter0 = new TestSyncFilter();
ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setInvokeType(RpcConstants.INVOKER_TYPE_SYNC).setApplication(new ApplicationConfig().setAppName("ccc")).setTimeout(5000).setFilterRef(Arrays.asList((Filter) filter0)).setDirectUrl("bolt://127.0.0.1:22222?appName=sss");
HelloService helloService = consumerConfig.refer();
try {
helloService.sayHello("xxx", 12);
// Consumer side
Assert.assertEquals("xxx", filter0.args[0]);
Assert.assertEquals(12, filter0.args[1]);
Assert.assertEquals(HelloService.class.getName(), filter0.interfaceName);
Assert.assertEquals("com.alipay.sofa.rpc.test.HelloService:1.0", filter0.targetServiceUniqueName);
Assert.assertEquals("sayHello", filter0.methodName);
Assert.assertEquals(RpcConstants.INVOKER_TYPE_SYNC, filter0.invokeType);
Assert.assertNull(filter0.targetAppName);
// Provider side
Assert.assertEquals("xxx", filter1.args[0]);
Assert.assertEquals(12, filter1.args[1]);
Assert.assertEquals(HelloService.class.getName(), filter1.interfaceName);
Assert.assertEquals("com.alipay.sofa.rpc.test.HelloService:1.0", filter1.targetServiceUniqueName);
Assert.assertEquals("sayHello", filter1.methodName);
Assert.assertEquals("sss", filter1.targetAppName);
} catch (Exception e) {
}
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class BaggageFutureTest method doTest.
@Override
public void doTest() throws Exception {
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299);
// C服务的服务端
CSampleServiceImpl refC = new CSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>();
serviceBeanC.setInterfaceId(SampleService.class.getName());
serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC"));
serviceBeanC.setUniqueId("C2");
serviceBeanC.setRef(refC);
serviceBeanC.setServer(serverConfig);
serviceBeanC.setRegister(false);
serviceBeanC.export();
// D服务的服务端
DSampleServiceImpl refD = new DSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>();
serviceBeanD.setInterfaceId(SampleService.class.getName());
serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD"));
serviceBeanD.setUniqueId("D2");
serviceBeanD.setRef(refD);
serviceBeanD.setServer(serverConfig);
serviceBeanD.setRegister(false);
serviceBeanD.export();
// B服务里的C服务客户端
ConsumerConfig referenceBeanC = new ConsumerConfig();
referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanC.setInterfaceId(SampleService.class.getName());
referenceBeanC.setUniqueId("C2");
referenceBeanC.setDirectUrl("localhost:12299");
referenceBeanC.setTimeout(1000);
MethodConfig methodConfigC = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
referenceBeanC.setMethods(Collections.singletonList(methodConfigC));
SampleService sampleServiceC = (SampleService) referenceBeanC.refer();
// B服务里的D服务客户端
ConsumerConfig referenceBeanD = new ConsumerConfig();
referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanD.setInterfaceId(SampleService.class.getName());
referenceBeanD.setUniqueId("D2");
referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0");
referenceBeanD.setTimeout(1000);
MethodConfig methodConfigD = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
referenceBeanD.setMethods(Collections.singletonList(methodConfigD));
SampleService sampleServiceD = (SampleService) referenceBeanD.refer();
// B服务的服务端
BFutureSampleServiceImpl refB = new BFutureSampleServiceImpl(sampleServiceC, sampleServiceD);
ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>();
ServiceBeanB.setInterfaceId(SampleService.class.getName());
ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB"));
ServiceBeanB.setUniqueId("B2");
ServiceBeanB.setRef(refB);
ServiceBeanB.setServer(serverConfig);
ServiceBeanB.setRegister(false);
ServiceBeanB.export();
// A 服务
ConsumerConfig referenceBeanA = new ConsumerConfig();
referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA"));
referenceBeanA.setUniqueId("B2");
referenceBeanA.setInterfaceId(SampleService.class.getName());
referenceBeanA.setDirectUrl("localhost:12299");
referenceBeanA.setTimeout(3000);
MethodConfig methodConfigA = new MethodConfig().setName("hello").setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE);
referenceBeanA.setMethods(Collections.singletonList(methodConfigA));
SampleService service = (SampleService) referenceBeanA.refer();
// 开始测试
RpcInvokeContext context = RpcInvokeContext.getContext();
context.putRequestBaggage("reqBaggageB", "a2bbb");
context.putRequestBaggage("reqBaggageC", "a2ccc");
context.putRequestBaggage("reqBaggageD", "a2ddd");
String ret = service.hello();
Assert.assertEquals(ret, null);
ret = (String) SofaResponseFuture.getResponse(5000, false);
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertEquals(refB.getReqBaggage(), "a2bbb");
Assert.assertEquals(refC.getReqBaggage(), "a2ccc");
Assert.assertEquals(refD.getReqBaggage(), "a2ddd");
Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa");
Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
RpcInvokeContext.removeContext();
context = RpcInvokeContext.getContext();
ret = null;
ret = service.hello();
Assert.assertEquals(ret, null);
ret = (String) SofaResponseFuture.getResponse(5000, false);
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertNull(refB.getReqBaggage());
Assert.assertNull(refC.getReqBaggage());
Assert.assertNull(refD.getReqBaggage());
Assert.assertNull(context.getResponseBaggage("respBaggageB"));
Assert.assertNull(context.getResponseBaggage("respBaggageC"));
Assert.assertNull(context.getResponseBaggage("respBaggageD"));
Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff");
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class BaggageSyncTest method doTest.
@Override
void doTest() {
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299);
// C服务的服务端
CSampleServiceImpl refC = new CSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>();
serviceBeanC.setInterfaceId(SampleService.class.getName());
serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC"));
serviceBeanC.setUniqueId("C1");
serviceBeanC.setRef(refC);
serviceBeanC.setServer(serverConfig);
serviceBeanC.setRegister(false);
serviceBeanC.export();
// D服务的服务端
DSampleServiceImpl refD = new DSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>();
serviceBeanD.setInterfaceId(SampleService.class.getName());
serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD"));
serviceBeanD.setUniqueId("D1");
serviceBeanD.setRef(refD);
serviceBeanD.setServer(serverConfig);
serviceBeanD.setRegister(false);
serviceBeanD.export();
// B服务里的C服务客户端
ConsumerConfig referenceBeanC = new ConsumerConfig();
referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanC.setInterfaceId(SampleService.class.getName());
referenceBeanC.setUniqueId("C1");
referenceBeanC.setDirectUrl("localhost:12299");
referenceBeanC.setTimeout(1000);
SampleService sampleServiceC = (SampleService) referenceBeanC.refer();
// B服务里的D服务客户端
ConsumerConfig referenceBeanD = new ConsumerConfig();
referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanD.setInterfaceId(SampleService.class.getName());
referenceBeanD.setUniqueId("D1");
referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0");
referenceBeanD.setTimeout(1000);
SampleService sampleServiceD = (SampleService) referenceBeanD.refer();
// B服务的服务端
BSampleServiceImpl refB = new BSampleServiceImpl(sampleServiceC, sampleServiceD);
ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>();
ServiceBeanB.setInterfaceId(SampleService.class.getName());
ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB"));
ServiceBeanB.setUniqueId("B1");
ServiceBeanB.setRef(refB);
ServiceBeanB.setServer(serverConfig);
ServiceBeanB.setRegister(false);
ServiceBeanB.export();
// A 服务
ConsumerConfig referenceBeanA = new ConsumerConfig();
referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA"));
referenceBeanA.setUniqueId("B1");
referenceBeanA.setInterfaceId(SampleService.class.getName());
referenceBeanA.setDirectUrl("localhost:12299");
referenceBeanA.setTimeout(3000);
SampleService service = (SampleService) referenceBeanA.refer();
// 开始测试
RpcInvokeContext context = RpcInvokeContext.getContext();
context.putRequestBaggage("reqBaggageB", "a2bbb");
context.putRequestBaggage("reqBaggageC", "a2ccc");
context.putRequestBaggage("reqBaggageD", "a2ddd");
String ret = service.hello();
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertEquals(refB.getReqBaggage(), "a2bbb");
Assert.assertEquals(refC.getReqBaggage(), "a2ccc");
Assert.assertEquals(refD.getReqBaggage(), "a2ddd");
Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa");
Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
RpcInvokeContext.removeContext();
context = RpcInvokeContext.getContext();
ret = service.hello();
Assert.assertEquals(ret, "hello world chello world d");
Assert.assertNull(refB.getReqBaggage());
Assert.assertNull(refC.getReqBaggage());
Assert.assertNull(refD.getReqBaggage());
Assert.assertNull(context.getResponseBaggage("respBaggageB"));
Assert.assertNull(context.getResponseBaggage("respBaggageC"));
Assert.assertNull(context.getResponseBaggage("respBaggageD"));
Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff");
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class BoltProtobufBaggageSyncTest method doTest.
@Override
void doTest() {
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setPort(12299);
// C服务的服务端
CSampleServiceImpl refC = new CSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanC = new ProviderConfig<SampleService>();
serviceBeanC.setInterfaceId(SampleService.class.getName());
serviceBeanC.setApplication(new ApplicationConfig().setAppName("CCC"));
serviceBeanC.setUniqueId("C1");
serviceBeanC.setRef(refC);
serviceBeanC.setServer(serverConfig);
serviceBeanC.setRegister(false);
serviceBeanC.export();
// D服务的服务端
DSampleServiceImpl refD = new DSampleServiceImpl();
ProviderConfig<SampleService> serviceBeanD = new ProviderConfig<SampleService>();
serviceBeanD.setInterfaceId(SampleService.class.getName());
serviceBeanD.setApplication(new ApplicationConfig().setAppName("DDD"));
serviceBeanD.setUniqueId("D1");
serviceBeanD.setRef(refD);
serviceBeanD.setServer(serverConfig);
serviceBeanD.setRegister(false);
serviceBeanD.export();
// B服务里的C服务客户端
ConsumerConfig referenceBeanC = new ConsumerConfig();
referenceBeanC.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanC.setInterfaceId(SampleService.class.getName());
referenceBeanC.setUniqueId("C1");
referenceBeanC.setDirectUrl("localhost:12299");
referenceBeanC.setSerialization("protobuf");
referenceBeanC.setTimeout(1000);
SampleService sampleServiceC = (SampleService) referenceBeanC.refer();
// B服务里的D服务客户端
ConsumerConfig referenceBeanD = new ConsumerConfig();
referenceBeanD.setApplication(new ApplicationConfig().setAppName("BBB"));
referenceBeanD.setInterfaceId(SampleService.class.getName());
referenceBeanD.setUniqueId("D1");
referenceBeanD.setDirectUrl("localhost:12299?p=1&v=4.0");
referenceBeanD.setSerialization("protobuf");
referenceBeanD.setTimeout(1000);
SampleService sampleServiceD = (SampleService) referenceBeanD.refer();
// B服务的服务端
BSampleServiceImpl refB = new BSampleServiceImpl(sampleServiceC, sampleServiceD);
ProviderConfig<SampleService> ServiceBeanB = new ProviderConfig<SampleService>();
ServiceBeanB.setInterfaceId(SampleService.class.getName());
ServiceBeanB.setApplication(new ApplicationConfig().setAppName("BBB"));
ServiceBeanB.setUniqueId("B1");
ServiceBeanB.setRef(refB);
ServiceBeanB.setServer(serverConfig);
ServiceBeanB.setRegister(false);
ServiceBeanB.export();
// A 服务
ConsumerConfig referenceBeanA = new ConsumerConfig();
referenceBeanA.setApplication(new ApplicationConfig().setAppName("AAA"));
referenceBeanA.setUniqueId("B1");
referenceBeanA.setInterfaceId(SampleService.class.getName());
referenceBeanA.setDirectUrl("localhost:12299");
referenceBeanA.setSerialization("protobuf");
referenceBeanA.setTimeout(3000);
SampleService service = (SampleService) referenceBeanA.refer();
// 开始测试
RpcInvokeContext context = RpcInvokeContext.getContext();
context.putRequestBaggage("reqBaggageB", "a2bbb");
context.putRequestBaggage("reqBaggageC", "a2ccc");
context.putRequestBaggage("reqBaggageD", "a2ddd");
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse ret = service.echoObj(request);
Assert.assertEquals(ret.getMessage(), "hello world chello world d");
Assert.assertEquals(refB.getReqBaggage(), "a2bbb");
Assert.assertEquals(refC.getReqBaggage(), "a2ccc");
Assert.assertEquals(refD.getReqBaggage(), "a2ddd");
Assert.assertEquals(context.getResponseBaggage("respBaggageB"), "b2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageC"), "c2aaa");
Assert.assertEquals(context.getResponseBaggage("respBaggageD"), "d2aaa");
Assert.assertNull(context.getResponseBaggage("respBaggageB_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageC_force"));
Assert.assertNull(context.getResponseBaggage("respBaggageD_force"));
RpcInvokeContext.removeContext();
context = RpcInvokeContext.getContext();
ret = service.echoObj(request);
Assert.assertEquals(ret.getMessage(), "hello world chello world d");
Assert.assertNull(refB.getReqBaggage());
Assert.assertNull(refC.getReqBaggage());
Assert.assertNull(refD.getReqBaggage());
Assert.assertNull(context.getResponseBaggage("respBaggageB"));
Assert.assertNull(context.getResponseBaggage("respBaggageC"));
Assert.assertNull(context.getResponseBaggage("respBaggageD"));
Assert.assertEquals(context.getResponseBaggage("respBaggageB_force"), "b2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageC_force"), "c2aaaff");
Assert.assertEquals(context.getResponseBaggage("respBaggageD_force"), "d2aaaff");
}
Aggregations