Java Code Examples for com.alipay.sofa.rpc.context.RpcInternalContext#getAttachment()
The following examples show how to use
com.alipay.sofa.rpc.context.RpcInternalContext#getAttachment() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestTracerAdapter.java From sofa-rpc with Apache License 2.0 | 6 votes |
/** * 存入tracer信息 * * @param requestContext ClientRequestContext */ public static void beforeSend(ClientRequestContext requestContext) { // tracer信息放入request 发到服务端 SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext(); SofaTracerSpan clientSpan = sofaTraceContext.getCurrentSpan(); RpcInternalContext context = RpcInternalContext.getContext(); if (clientSpan != null) { requestContext.getHeaders().add(RemotingConstants.NEW_RPC_TRACE_NAME, clientSpan.getSofaTracerSpanContext().serializeSpanContext()); } // 客户端发送自己的应用名 String appName = (String) context.getAttachment(INTERNAL_KEY_APP_NAME); if (appName != null) { requestContext.getHeaders().add(RemotingConstants.HEAD_APP_NAME, appName); } RestBaggageItemsHandler.encodeBaggageItemToRequest(requestContext.getHeaders()); }
Example 2
Source File: AbstractInvokeCallbackTest.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Test public void testRecordClientElapseTime() { BoltInvokerCallback invokerCallback = new BoltInvokerCallback(null, null, null, null, null, null); invokerCallback.recordClientElapseTime(); Long elapse = (Long) RpcInternalContext.getContext().getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE); Assert.assertNull(elapse); RpcInternalContext context = RpcInternalContext.getContext(); invokerCallback = new BoltInvokerCallback(null, null, null, null, context, null); invokerCallback.recordClientElapseTime(); elapse = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE); Assert.assertNull(elapse); context.setAttachment(RpcConstants.INTERNAL_KEY_CLIENT_SEND_TIME, RpcRuntimeContext.now() - 1000); invokerCallback.recordClientElapseTime(); elapse = (Long) context.getAttachment(RpcConstants.INTERNAL_KEY_CLIENT_ELAPSE); Assert.assertNotNull(elapse); }
Example 3
Source File: DefaultClientProxyInvoker.java From sofa-rpc with Apache License 2.0 | 6 votes |
@Override protected void decorateResponse(SofaResponse response) { // 公共的设置 super.decorateResponse(response); // 上下文内转外 RpcInternalContext context = RpcInternalContext.getContext(); ResponseFuture future = context.getFuture(); RpcInvokeContext invokeCtx = null; if (future != null) { invokeCtx = RpcInvokeContext.getContext(); invokeCtx.setFuture(future); } if (RpcInvokeContext.isBaggageEnable()) { BaggageResolver.pickupFromResponse(invokeCtx, response, true); } // bad code if (RpcInternalContext.isAttachmentEnable()) { String resultCode = (String) context.getAttachment(INTERNAL_KEY_RESULT_CODE); if (resultCode != null) { if (invokeCtx == null) { invokeCtx = RpcInvokeContext.getContext(); } invokeCtx.put(RemotingConstants.INVOKE_CTX_RPC_RESULT_CODE, resultCode); } } }
Example 4
Source File: RpcSofaTracer.java From sofa-rpc with Apache License 2.0 | 5 votes |
@Override public void clientAsyncReceivedPrepare() { //新的线程 RpcInternalContext rpcInternalContext = RpcInternalContext.getContext(); SofaTracerSpan clientSpan = (SofaTracerSpan) rpcInternalContext.getAttachment(RpcConstants.INTERNAL_KEY_TRACER_SPAN); if (clientSpan == null) { return; } SofaTraceContext sofaTraceContext = SofaTraceContextHolder.getSofaTraceContext(); sofaTraceContext.push(clientSpan); }
Example 5
Source File: BoltSendableResponseCallback.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * 初始化数据 */ protected void init() { // 从ThreadLocal取出当前长连接,request等信息设置进去,需要接到请求时提前设置到ThreadLocal里 RpcInternalContext context = RpcInternalContext.getContext(); asyncContext = (AsyncContext) context.getAttachment(RpcConstants.HIDDEN_KEY_ASYNC_CONTEXT); request = (SofaRequest) context.getAttachment(RpcConstants.HIDDEN_KEY_ASYNC_REQUEST); }
Example 6
Source File: Router.java From sofa-rpc with Apache License 2.0 | 5 votes |
/** * 记录路由路径记录 * * @param routerName 路由名字 * @since 5.2.0 */ protected void recordRouterWay(String routerName) { if (RpcInternalContext.isAttachmentEnable()) { RpcInternalContext context = RpcInternalContext.getContext(); String record = (String) context.getAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD); record = record == null ? routerName : record + ">" + routerName; context.setAttachment(RpcConstants.INTERNAL_KEY_ROUTER_RECORD, record); } }
Example 7
Source File: AllConnectConnectionHolder.java From sofa-rpc with Apache License 2.0 | 4 votes |
@Override public ClientTransport getAvailableClientTransport(ProviderInfo providerInfo) { // 先去存活列表 ClientTransport transport = aliveConnections.get(providerInfo); if (transport != null) { return transport; } // 再去亚健康列表 transport = subHealthConnections.get(providerInfo); if (transport != null) { return transport; } // 最后看看是否第一次调用未初始化 transport = uninitializedConnections.get(providerInfo); if (transport != null) { // 未初始化则初始化 synchronized (this) { transport = uninitializedConnections.get(providerInfo); if (transport != null) { initClientTransport(consumerConfig.getInterfaceId(), providerInfo, transport); uninitializedConnections.remove(providerInfo); } return getAvailableClientTransport(providerInfo); } } if (createConnWhenAbsent) { RpcInternalContext context = RpcInternalContext.peekContext(); String targetIP = (context == null) ? null : (String) context .getAttachment(RpcConstants.HIDDEN_KEY_PINPOINT); /** * RpcInvokeContext.getContext().setTargetUrl() 设置了地址,初始化tcp连接 */ if (StringUtils.isNotBlank(targetIP)) { ClientTransportConfig transportConfig = providerToClientConfig(providerInfo); transport = ClientTransportFactory.getClientTransport(transportConfig); initClientTransport(consumerConfig.getInterfaceId(), providerInfo, transport); } } return transport; }
Example 8
Source File: RpcReferenceContextFilter.java From sofa-rpc with Apache License 2.0 | 4 votes |
@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; }