use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class Http2ClearTextPriorKnowledgeTest method testProtobuf.
@Test
public void testProtobuf() {
// 只有1个线程 执行
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setRef(new HttpServiceImpl()).setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
providerConfig.export();
{
ConsumerConfig<HttpService> consumerConfig = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C);
HttpService httpService = consumerConfig.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse response = httpService.echoPb(request);
Assert.assertEquals("helloxxx", response.getMessage());
Assert.assertEquals(200, response.getCode());
}
{
ConsumerConfig<HttpService> consumerConfig2 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_ONEWAY).setRepeatedReferLimit(-1);
HttpService httpService2 = consumerConfig2.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
try {
httpService2.echoPb(request);
// NOT SUPPORTED NOW, If want support this, need add key to head.
Assert.fail();
} catch (Exception e) {
}
}
{
ConsumerConfig<HttpService> consumerConfig3 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_FUTURE).setRepeatedReferLimit(-1);
HttpService httpService3 = consumerConfig3.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse response = httpService3.echoPb(request);
Assert.assertNull(response);
ResponseFuture future = RpcInvokeContext.getContext().getFuture();
try {
response = (EchoResponse) future.get();
Assert.assertEquals("helloxxx", response.getMessage());
Assert.assertEquals(200, response.getCode());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
{
final EchoResponse[] result = new EchoResponse[1];
final CountDownLatch latch = new CountDownLatch(1);
ConsumerConfig<HttpService> consumerConfig4 = new ConsumerConfig<HttpService>().setInterfaceId(HttpService.class.getName()).setSerialization(RpcConstants.SERIALIZE_PROTOBUF).setDirectUrl("h2c://127.0.0.1:12300").setApplication(new ApplicationConfig().setAppName("clientApp")).setProtocol(RpcConstants.PROTOCOL_TYPE_H2C).setInvokeType(RpcConstants.INVOKER_TYPE_CALLBACK).setOnReturn(new SofaResponseCallback() {
@Override
public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
result[0] = (EchoResponse) appResponse;
latch.countDown();
}
@Override
public void onAppException(Throwable throwable, String methodName, RequestBase request) {
latch.countDown();
}
@Override
public void onSofaException(SofaRpcException sofaException, String methodName, RequestBase request) {
latch.countDown();
}
}).setRepeatedReferLimit(-1);
HttpService httpService4 = consumerConfig4.refer();
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("xxx").build();
EchoResponse response = httpService4.echoPb(request);
Assert.assertNull(response);
try {
latch.await(2000, TimeUnit.MILLISECONDS);
response = result[0];
Assert.assertNotNull(response);
Assert.assertEquals("helloxxx", response.getMessage());
Assert.assertEquals(200, response.getCode());
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class TripleHessianInvokeTest method testInvoke.
@Test
public void testInvoke() throws InterruptedException {
RpcRunningState.setDebugMode(true);
ApplicationConfig clientApp = new ApplicationConfig().setAppName("triple-client");
ApplicationConfig serverApp = new ApplicationConfig().setAppName("triple-server");
int port = getPort();
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
TripleHessianInterfaceImpl ref = new TripleHessianInterfaceImpl();
ProviderConfig<TripleHessianInterface> providerConfig = getProviderConfig().setApplication(serverApp).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(TripleHessianInterface.class.getName()).setRef(ref).setServer(serverConfig).setRegister(false);
providerConfig.export();
ConsumerConfig<TripleHessianInterface> consumerConfig = new ConsumerConfig<TripleHessianInterface>();
consumerConfig.setInterfaceId(TripleHessianInterface.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("localhost:" + port).setRegister(false).setApplication(clientApp);
TripleHessianInterface helloService = consumerConfig.refer();
LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
helloService.call();
Assert.assertEquals("call", ref.getFlag());
// test Pressure Mark
boolean isLoadTest = helloService.testPressureMark("name");
Assert.assertFalse(isLoadTest);
SofaTracerSpan currentSpan = SofaTraceContextHolder.getSofaTraceContext().getCurrentSpan();
Map<String, String> bizBaggage = currentSpan.getSofaTracerSpanContext().getBizBaggage();
bizBaggage.put("mark", "T");
Assert.assertTrue(TracerUtils.isLoadTest(currentSpan));
isLoadTest = helloService.testPressureMark("name");
Assert.assertTrue(isLoadTest);
String s = helloService.call1();
Assert.assertEquals("call1", ref.getFlag());
Assert.assertEquals("call1", s);
Request request = new Request();
int age = RandomUtils.nextInt();
request.setAge(age);
String call2 = "call2";
request.setFlag(call2);
Response response = helloService.call2(request);
Assert.assertEquals(age, response.getAge());
Assert.assertEquals(call2, response.getFlag());
Response response1 = helloService.call2(null);
Assert.assertNull(response1);
providerConfig.unExport();
serverConfig.destroy();
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class TripleHessianInvokeTest method testInvokeWithUniqueId.
@Test
public void testInvokeWithUniqueId() throws InterruptedException {
String uniqueId = "uniqueId1";
RpcRunningState.setDebugMode(true);
ApplicationConfig clientApp = new ApplicationConfig().setAppName("triple-client");
ApplicationConfig serverApp = new ApplicationConfig().setAppName("triple-server");
int port = getPort();
ServerConfig serverConfig = new ServerConfig().setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setPort(port);
TripleHessianInterfaceImpl ref = new TripleHessianInterfaceImpl();
ProviderConfig<TripleHessianInterface> providerConfig = getProviderConfig().setApplication(serverApp).setUniqueId(uniqueId).setBootstrap(RpcConstants.PROTOCOL_TYPE_TRIPLE).setInterfaceId(TripleHessianInterface.class.getName()).setRef(ref).setServer(serverConfig).setRegister(false);
providerConfig.export();
ConsumerConfig<TripleHessianInterface> consumerConfig = new ConsumerConfig<TripleHessianInterface>();
consumerConfig.setInterfaceId(TripleHessianInterface.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("localhost:" + port).setUniqueId(uniqueId).setRegister(false).setApplication(clientApp);
TripleHessianInterface helloService = consumerConfig.refer();
LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
helloService.call();
Assert.assertEquals("call", ref.getFlag());
String s = helloService.call1();
Assert.assertEquals("call1", ref.getFlag());
Assert.assertEquals("call1", s);
Request request = new Request();
int age = RandomUtils.nextInt();
request.setAge(age);
String call2 = "call2";
request.setFlag(call2);
Response response = helloService.call2(request);
Assert.assertEquals(age, response.getAge());
Assert.assertEquals(call2, response.getFlag());
Response response1 = helloService.call2(null);
Assert.assertNull(response1);
// 测试没有设置 uniqueId 的情况,也能访问
consumerConfig = new ConsumerConfig<TripleHessianInterface>();
consumerConfig.setInterfaceId(TripleHessianInterface.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_TRIPLE).setDirectUrl("localhost:" + port).setRegister(false).setApplication(clientApp);
helloService = consumerConfig.refer();
LOGGER.info("Grpc stub bean successful: {}", helloService.getClass().getName());
helloService.call();
Assert.assertEquals("call", ref.getFlag());
s = helloService.call1();
Assert.assertEquals("call1", ref.getFlag());
Assert.assertEquals("call1", s);
request = new Request();
age = RandomUtils.nextInt();
request.setAge(age);
call2 = "call2";
request.setFlag(call2);
response = helloService.call2(request);
Assert.assertEquals(age, response.getAge());
Assert.assertEquals(call2, response.getFlag());
response1 = helloService.call2(null);
Assert.assertNull(response1);
providerConfig.unExport();
serverConfig.destroy();
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class RpcReferenceContextFilter method invoke.
@Override
public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
ConsumerConfig config = (ConsumerConfig) invoker.getConfig();
RpcReferenceContext referenceCtx = new RpcReferenceContext();
referenceCtx.setGeneric(config.isGeneric());
referenceCtx.setInterfaceName(config.getInterfaceId());
referenceCtx.setUniqueId(config.getUniqueId());
referenceCtx.setServiceName(request.getTargetServiceUniqueName());
referenceCtx.setMethodName(request.getMethodName());
RpcInternalContext context = RpcInternalContext.getContext();
ProviderInfo providerInfo = context.getProviderInfo();
if (providerInfo != null) {
referenceCtx.setTargetAppName(providerInfo.getStaticAttr(ProviderInfoAttrs.ATTR_APP_NAME));
referenceCtx.setTargetUrl(providerInfo.getHost() + ":" + providerInfo.getPort());
}
referenceCtx.setProtocol(config.getProtocol());
referenceCtx.setInvokeType(request.getInvokeType());
referenceCtx.setRouteRecord((String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD));
RpcInvokeContext.getContext().put(RemotingConstants.INVOKE_CTX_RPC_REF_CTX, referenceCtx);
SofaResponse response = invoker.invoke(request);
// 调用后
InetSocketAddress local = context.getLocalAddress();
if (local != null) {
referenceCtx.setClientIP(NetUtils.toIpString(local));
referenceCtx.setClientPort(local.getPort());
}
Long ct = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CONN_CREATE_TIME);
if (ct != null) {
referenceCtx.setConnEstablishedSpan(ct);
}
Integer qs = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_REQ_SIZE);
if (qs != null) {
referenceCtx.setRequestSize(qs.longValue());
}
Integer ps = (Integer) context.getAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE);
if (ps != null) {
referenceCtx.setResponseSize(ps.longValue());
}
referenceCtx.setTraceId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_TRACE_ID));
referenceCtx.setRpcId((String) context.getAttachment(RpcConstants.INTERNAL_KEY_SPAN_ID));
Long ce = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE);
if (ce != null) {
referenceCtx.setCostTime(ce);
}
return response;
}
use of com.alipay.sofa.rpc.config.ConsumerConfig in project sofa-rpc by sofastack.
the class Http2ClientMultipleMain method main.
public static void main(String[] args) {
ApplicationConfig application = new ApplicationConfig().setAppName("test-client");
ConsumerConfig<ProtoService> consumerConfig = new ConsumerConfig<ProtoService>().setApplication(application).setInterfaceId(ProtoService.class.getName()).setProtocol("h2c").setDirectUrl("h2c://127.0.0.1:12300").setSerialization("protobuf").setTimeout(3000);
final ProtoService protoService = consumerConfig.refer();
LOGGER.warn("started at pid {}", RpcRuntimeContext.PID);
final int threads = 50;
final AtomicLong cnt = new AtomicLong(0);
final ThreadPoolExecutor service1 = new ThreadPoolExecutor(threads, threads, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), // 无队列
new NamedThreadFactory("client-"));
for (int i = 0; i < threads; i++) {
service1.execute(new Runnable() {
@Override
public void run() {
while (true) {
try {
EchoRequest request = EchoRequest.newBuilder().setGroup(Group.A).setName("12345678").build();
EchoResponse s = protoService.echoObj(request);
cnt.incrementAndGet();
} catch (Throwable e) {
LOGGER.error("", e);
}
}
}
});
}
Thread thread = new Thread(new Runnable() {
private long last = 0;
@Override
public void run() {
while (true) {
long count = cnt.get();
long tps = count - last;
LOGGER.error("last 1s invoke: {}, queue: {}", tps, service1.getQueue().size());
last = count;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}, "Print-tps-THREAD");
thread.start();
}
Aggregations