RPC 调用上下文
通过上下文存储当前调用过程中需要的环境信息
功能描述
上下文存储当前调用过程中需要的环境信息。所有配置信息将转换为 URL 参数,请参见 schema 配置参考手册 中的 **对应 URL 参数** 列。
RpcContext 是 ThreadLocal 的临时状态记录器。当接收到 RPC 请求或发起 RPC 请求时,RpcContext 的状态会发生变化。例如:A 调试 B,B 然后调试 C,那么在机器 B 上,在 B 调试 C 之前,RpcContext 记录了 A 调试 B 的信息,在 B 调试 C 之后,RpcContext 记录了 B 调试 C 的信息。
使用场景
全局链路追踪和隐藏参数。
使用方法
服务消费者
// remote call
xxxService. xxx();
// Whether this end is a consumer end, here will return true
boolean isConsumerSide = RpcContext.getServiceContext().isConsumerSide();
// Get the provider IP address of the last call
String serverIP = RpcContext.getServiceContext().getRemoteHost();
// Get the current service configuration information, all configuration information will be converted into URL parameters
String application = RpcContext.getServiceContext().getUrl().getParameter("application");
// Note: Every time an RPC call is initiated, the context state will change
yyyService.yyy();
服务提供者
public class XxxServiceImpl implements XxxService {
public void xxx() {
// Whether this end is the provider end, here will return true
boolean isProviderSide = RpcContext.getServiceContext().isProviderSide();
// Get the IP address of the caller
String clientIP = RpcContext.getServiceContext().getRemoteHost();
// Get the current service configuration information, all configuration information will be converted into URL parameters
String application = RpcContext.getServiceContext().getUrl().getParameter("application");
// Note: Every time an RPC call is initiated, the context state will change
yyyService.yyy();
// At this point, the local end becomes the consumer end, and false will be returned here
boolean isProviderSide = RpcContext.getServiceContext().isProviderSide();
}
}
上次修改时间:2023 年 1 月 2 日:增强 en 文档 (#1798) (95a9f4f6c1c)