Search in sources :

Example 36 with RpcInvokeContext

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

the class ClientHeaderClientInterceptor method interceptCall.

@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method, CallOptions callOptions, Channel next) {
    // 这里和下面不在一个线程
    if (RpcRunningState.isDebugMode()) {
        LOGGER.info("[1]header send from client:");
    }
    return new ForwardingClientCall.SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {

        @Override
        public void start(Listener<RespT> responseListener, Metadata requestHeader) {
            RpcInvokeContext context = RpcInvokeContext.getContext();
            SofaRequest sofaRequest = (SofaRequest) context.get(TripleContants.SOFA_REQUEST_KEY);
            ConsumerConfig consumerConfig = (ConsumerConfig) context.get(TripleContants.SOFA_CONSUMER_CONFIG_KEY);
            TripleTracerAdapter.beforeSend(sofaRequest, consumerConfig, requestHeader);
            if (RpcRunningState.isDebugMode()) {
                LOGGER.info("[2]prepare to send from client:{}", requestHeader);
            }
            super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {

                @Override
                public void onHeaders(Metadata responseHeader) {
                    // 客户端收到响应Header
                    if (RpcRunningState.isDebugMode()) {
                        LOGGER.info("[3]response header received from server:{}", responseHeader);
                    }
                    super.onHeaders(responseHeader);
                }

                @Override
                public void onMessage(RespT message) {
                    if (RpcRunningState.isDebugMode()) {
                        LOGGER.info("[4]response message received from server:{}", message);
                    }
                    super.onMessage(message);
                }

                @Override
                public void onClose(Status status, Metadata trailers) {
                    if (RpcRunningState.isDebugMode()) {
                        LOGGER.info("[5]response close received from server:{},trailers:{}", status, trailers);
                    }
                    super.onClose(status, trailers);
                }

                @Override
                public void onReady() {
                    if (RpcRunningState.isDebugMode()) {
                        LOGGER.info("[5]client is ready");
                    }
                    super.onReady();
                }
            }, requestHeader);
        }
    };
}
Also used : Status(io.grpc.Status) SimpleForwardingClientCallListener(io.grpc.ForwardingClientCallListener.SimpleForwardingClientCallListener) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) SofaRequest(com.alipay.sofa.rpc.core.request.SofaRequest) Metadata(io.grpc.Metadata) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig)

Example 37 with RpcInvokeContext

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

the class TripleTracerAdapter method beforeSend.

/**
 * 存入tracer信息
 *
 * @param sofaRequest   SofaRequest
 * @param requestHeader Metadata
 */
public static void beforeSend(SofaRequest sofaRequest, ConsumerConfig consumerConfig, Metadata requestHeader) {
    // 客户端设置请求服务端的Header
    // tracer信息放入request 发到服务端
    Map<String, String> header = new HashMap<String, String>();
    header.put(RemotingConstants.HEAD_METHOD_NAME, sofaRequest.getMethodName());
    header.put(RemotingConstants.HEAD_TARGET_SERVICE, sofaRequest.getTargetServiceUniqueName());
    header.put(RemotingConstants.HEAD_TARGET_APP, sofaRequest.getTargetAppName());
    // 客户端的启动
    SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext();
    // 获取并不弹出
    SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan();
    if (clientSpan == null) {
        SelfLog.warn("ClientSpan is null.Before call interface=" + sofaRequest.getInterfaceName() + ",method=" + sofaRequest.getMethodName());
    } else {
        SofaTracerSpanContext sofaTracerSpanContext = clientSpan.getSofaTracerSpanContext();
        header.put(TripleHeadKeys.HEAD_KEY_TRACE_ID.name(), sofaTracerSpanContext.getTraceId());
        header.put(TripleHeadKeys.HEAD_KEY_RPC_ID.name(), sofaTracerSpanContext.getSpanId());
        header.put(TripleHeadKeys.HEAD_KEY_OLD_TRACE_ID.name(), sofaTracerSpanContext.getTraceId());
        header.put(TripleHeadKeys.HEAD_KEY_OLD_RPC_ID.name(), sofaTracerSpanContext.getSpanId());
        header.put(TripleHeadKeys.HEAD_KEY_BIZ_BAGGAGE_TYPE.name(), sofaTracerSpanContext.getBizSerializedBaggage());
        header.put(TripleHeadKeys.HEAD_KEY_SYS_BAGGAGE_TYPE.name(), sofaTracerSpanContext.getSysSerializedBaggage());
    }
    // 获取 RPC 上下文
    RpcInvokeContext internalContext = RpcInvokeContext.getContext();
    String route = (String) internalContext.get(USERID_KEY);
    if (StringUtils.isNotBlank(route)) {
        Map<String, String> map = new HashMap<>();
        map.put(USERID_KEY, route);
        header.put(TripleHeadKeys.HEAD_KEY_UNIT_INFO.name(), JSONUtils.toJSONString(map));
    }
    if (StringUtils.isNotEmpty(consumerConfig.getUniqueId())) {
        header.put(TripleHeadKeys.HEAD_KEY_SERVICE_VERSION.name(), consumerConfig.getUniqueId());
    }
    header.put(TripleHeadKeys.HEAD_KEY_META_TYPE.name(), "rpc");
    header.put(TripleHeadKeys.HEAD_KEY_CURRENT_APP.name(), (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
    header.put(TripleHeadKeys.HEAD_KEY_CONSUMER_APP.name(), (String) sofaRequest.getRequestProp(HEAD_APP_NAME));
    header.put(TripleHeadKeys.HEAD_KEY_PROTOCOL_TYPE.name(), (String) sofaRequest.getRequestProp(RemotingConstants.HEAD_PROTOCOL));
    header.put(TripleHeadKeys.HEAD_KEY_INVOKE_TYPE.name(), (String) sofaRequest.getRequestProp(RemotingConstants.HEAD_INVOKE_TYPE));
    final String source = consumerConfig.getParameter("interworking.source");
    if (StringUtils.isNotBlank(source)) {
        header.put(TripleHeadKeys.HEAD_KEY_SOURCE_TENANTID.name(), source);
    }
    final String target = consumerConfig.getParameter("interworking.target");
    if (StringUtils.isNotBlank(target)) {
        header.put(TripleHeadKeys.HEAD_KEY_TARGET_TENANTID.name(), target);
    }
    for (Map.Entry<String, String> entry : header.entrySet()) {
        if (StringUtils.isNotBlank(entry.getValue())) {
            requestHeader.put(TripleHeadKeys.getKey(entry.getKey()), entry.getValue());
        }
    }
    // set custom headers
    try {
        Set<Map.Entry<String, String>> customHeader = RpcInvokeContext.getContext().getCustomHeader().entrySet();
        for (Map.Entry<String, String> entry : customHeader) {
            if (StringUtils.isNotBlank(entry.getValue())) {
                requestHeader.put(TripleHeadKeys.getKey(entry.getKey()), entry.getValue());
            }
        }
    } finally {
        RpcInvokeContext.getContext().clearCustomHeader();
    }
}
Also used : SofaTracerSpan(com.alipay.common.tracer.core.span.SofaTracerSpan) RpcInvokeContext(com.alipay.sofa.rpc.context.RpcInvokeContext) HashMap(java.util.HashMap) SofaTraceContext(com.alipay.common.tracer.core.context.trace.SofaTraceContext) SofaTracerSpanContext(com.alipay.common.tracer.core.context.span.SofaTracerSpanContext) HashMap(java.util.HashMap) Map(java.util.Map)

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