Search in sources :

Example 26 with RpcInvokeContext

use of com.alipay.sofa.rpc.context.RpcInvokeContext 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");
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 27 with RpcInvokeContext

use of com.alipay.sofa.rpc.context.RpcInvokeContext 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");
}
Also used : EchoResponse(com.alipay.sofa.rpc.server.bolt.pb.EchoResponse) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) EchoRequest(com.alipay.sofa.rpc.server.bolt.pb.EchoRequest) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 28 with RpcInvokeContext

use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.

the class AsyncHelloServiceImpl method sayHello.

@Override
public String sayHello(String name, int age) {
    LOGGER.info("[2]----B get req :{}, {}", name, age);
    // 模拟A-->B-->C场景
    RpcInvokeContext context = RpcInvokeContext.getContext();
    context.setTimeout(2000);
    context.setResponseCallback(new BoltSendableResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            // 此时C-异步返回->B
            LOGGER.info("[5]----B get result: {}", appResponse);
            // 调这个方法B-异步返回->A
            sendAppResponse(appResponse);
        // 如果A是异步调用,则拿到这个appResponse返回值
        }
    });
    // B-异步调用->C
    String c0 = helloService.sayHello(name, age);
    if (c0 != null) {
        LOGGER.error("--------c0 is not null");
    }
    // 如果设置了AsyncProxyResponseCallback,则此处返回其实是无效。
    return "hello async无效返回";
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) BoltSendableResponseCallback(com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase)

Example 29 with RpcInvokeContext

use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.

the class AsyncHelloServiceImpl method rpcException.

@Override
public String rpcException(String name) {
    RpcInvokeContext context = RpcInvokeContext.getContext();
    context.setTimeout(2000);
    context.setResponseCallback(new BoltSendableResponseCallback() {

        @Override
        public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
            sendSofaException(new SofaRpcException(RpcErrorType.SERVER_BUSY, "bbb"));
        }
    });
    // B-异步调用->C
    helloService.sayHello(name, 1);
    return null;
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) BoltSendableResponseCallback(com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException)

Example 30 with RpcInvokeContext

use of com.alipay.sofa.rpc.context.RpcInvokeContext in project sofa-rpc by sofastack.

the class BAsyncChainSampleServiceImpl method hello.

@Override
public String hello() {
    RpcInvokeContext context = RpcInvokeContext.getContext();
    LOGGER.info("--b1---:" + context);
    // 读取一定要在这里读取
    reqBaggage = context.getRequestBaggage("reqBaggageB");
    context.putResponseBaggage("respBaggageB_useful1", "在返A之前写入有用");
    final CountDownLatch latch = new CountDownLatch(1);
    try {
        RpcInvokeContext.getContext().setResponseCallback(new BoltSendableResponseCallback() {

            @Override
            public void onAppResponse(Object appResponse, String methodName, RequestBase request) {
                // 返回一定要写在这里
                RpcInvokeContext context = RpcInvokeContext.getContext();
                LOGGER.info("--b3---:" + context);
                if (reqBaggage != null) {
                    context.putResponseBaggage("respBaggageB", "b2aaa");
                } else {
                    context.putResponseBaggage("respBaggageB_force", "b2aaaff");
                }
                String s1 = (String) appResponse;
                // 这里已经取不到值了
                String reqBaggageD = context.getRequestBaggage("reqBaggageD");
                LOGGER.info("----reqBaggageD---:" + reqBaggageD);
                String s2 = sampleServiceD.hello();
                sendAppResponse(s1 + s2);
                LOGGER.info("--b4---:" + RpcInvokeContext.getContext());
                // 返回写在这里可能没用
                context.putResponseBaggage("respBaggageB_useless2", "在返A之前写后没用");
                latch.countDown();
            }
        });
        sampleServiceC.hello();
        context.putResponseBaggage("respBaggageB_useful2", "在返A之前写入有用");
        LOGGER.info("--b2---:" + RpcInvokeContext.getContext());
        // 模拟Callback更早回来的行为
        latch.await(5000, TimeUnit.MILLISECONDS);
        // 返回写在这里可能没用
        context.putResponseBaggage("respBaggageB_useless2", "在返A之前写后没用");
        LOGGER.info("--b5---:" + RpcInvokeContext.getContext());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) BoltSendableResponseCallback(com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback) CountDownLatch(java.util.concurrent.CountDownLatch) RequestBase(com.alipay.sofa.rpc.core.request.RequestBase)

Aggregations

RpcInvokeContext (com.alipay.sofa.rpc.context.RpcInvokeContext)37 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)12 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)10 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)7 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)7 RpcInternalContext (com.alipay.sofa.rpc.context.RpcInternalContext)7 ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)6 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)6 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)6 CountDownLatch (java.util.concurrent.CountDownLatch)6 BoltSendableResponseCallback (com.alipay.sofa.rpc.message.bolt.BoltSendableResponseCallback)5 EchoResponse (com.alipay.sofa.rpc.server.bolt.pb.EchoResponse)5 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)4 Future (java.util.concurrent.Future)4 SofaTraceContext (com.alipay.common.tracer.core.context.trace.SofaTraceContext)3 SofaTracerSpan (com.alipay.common.tracer.core.span.SofaTracerSpan)3 HashMap (java.util.HashMap)3 SofaResponseFuture (com.alipay.sofa.rpc.api.future.SofaResponseFuture)2 SofaTimeOutException (com.alipay.sofa.rpc.core.exception.SofaTimeOutException)2 SofaResponse (com.alipay.sofa.rpc.core.response.SofaResponse)2