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 日:增強英文文件 (#1798) (95a9f4f6c1c)