use of com.alipay.sofa.rpc.config.ProviderConfig 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.ProviderConfig in project sofa-rpc by sofastack.
the class WarmUpTest method testWarmUp.
@Test
public void testWarmUp() throws InterruptedException {
RegistryConfig registryConfig = new RegistryConfig().setProtocol(RpcConstants.REGISTRY_PROTOCOL_ZK).setAddress("127.0.0.1:2181");
ServerConfig serverConfig = new ServerConfig().setPort(22222).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
ProviderConfig<WarmUpService> providerConfig = new ProviderConfig<WarmUpService>().setInterfaceId(WarmUpService.class.getName()).setRef(new WarmUpServiceImpl(22222)).setServer(serverConfig).setRegistry(registryConfig).setParameter(ProviderInfoAttrs.ATTR_WARMUP_TIME, "2000").setParameter(ProviderInfoAttrs.ATTR_WARMUP_WEIGHT, "100").setWeight(0);
ServerConfig serverConfig2 = new ServerConfig().setPort(22111).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
ProviderConfig<WarmUpService> providerConfig2 = new ProviderConfig<WarmUpService>().setInterfaceId(WarmUpService.class.getName()).setRef(new WarmUpServiceImpl(22111)).setServer(serverConfig2).setRegistry(registryConfig).setRepeatedExportLimit(-1).setWeight(0);
providerConfig.export();
providerConfig2.export();
long startTime = System.currentTimeMillis();
ConsumerConfig<WarmUpService> consumerConfig = new ConsumerConfig<WarmUpService>().setInterfaceId(WarmUpService.class.getName()).setRegistry(registryConfig).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT);
WarmUpService warmUpService = consumerConfig.refer();
// Before the 2000 ms, all the traffic goes to 22222.
for (int i = 0; i < 10; i++) {
Assert.assertEquals(22222, warmUpService.getPort());
}
long elapsed = System.currentTimeMillis() - startTime;
LOGGER.info("elapsed " + elapsed + "ms");
long sleepTime = 2100 - elapsed;
if (sleepTime >= 0) {
Thread.sleep(sleepTime);
}
// After 2000 ms, all the traffic goes to 22222 && 22111.
int cnt = 0;
for (int i = 0; i < 100; i++) {
if (warmUpService.getPort() == 22111) {
cnt++;
}
}
Assert.assertTrue(cnt > 0);
}
use of com.alipay.sofa.rpc.config.ProviderConfig 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.ProviderConfig 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.ProviderConfig 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