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);
}
};
}
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();
}
}
Aggregations