協定擴充套件

擴充說明

RPC 協議擴充,封裝遠端呼叫細節。

契約

  • 當用戶端呼叫 refer() 返回的 Invoker 物件的 invoke() 方法時,協議需要執行 URL 遠端 export() 傳入的 Invoker 物件的 invoke() 方法。
  • 其中,refer() 返回的 Invoker 由協議實現,協議通常需要在該 Invoker 中發送遠端請求,而 export() 傳入的 Invoker 由框架實現並傳入,協議無需關心。

注意事項

  • 協議不關心業務介面的透明代理,以 Invoker 為中心,外層將 Invoker 轉換為業務介面。
  • 協議不一定是 TCP 網絡通訊,例如可以透過共享檔案、IPC 進程間通訊等方式實現。

擴充點

  • org.apache.dubbo.rpc.Protocol
  • org.apache.dubbo.rpc.Exporter
  • org.apache.dubbo.rpc.Invoker
public interface Protocol {
    /**
     * Expose remote services:<br>
     * 1. When receiving a request, the protocol should record the request source address information: RpcContext.getContext().setRemoteAddress();<br>
     * 2. export() must be idempotent, that is, exposing the Invoker of the same URL twice is no different from exposing it once. <br>
     * 3. The Invoker passed in by export() is implemented and passed in by the framework, and the protocol does not need to care. <br>
     *
     * @param <T> type of service
     * @param invoker service execution body
     * @return exporter The reference of the exposed service, used to cancel the exposure
     * @throws RpcException Throws when there is an error in the exposed service, such as the port is already occupied
     */
    <T> Exporter<T> export(Invoker<T> invoker) throws RpcException;
 
    /**
     * Quoting remote services:<br>
     * 1. When the user calls the invoke() method of the Invoker object returned by refer(), the protocol needs to correspondingly execute the invoke() method of the Invoker object passed in from the URL remote export(). <br>
     * 2. The Invoker returned by refer() is implemented by the protocol, and the protocol usually needs to send a remote request in this Invoker. <br>
     * 3. When check=false is set in the url, an exception cannot be thrown if the connection fails, and internal automatic recovery is required. <br>
     *
     * @param <T> type of service
     * @param type service type
     * @param url URL address of the remote service
     * @return The local proxy for the invoker service
     * @throws RpcException thrown when the connection to the service provider fails
     */
    <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException;
 
}

擴充配置

<!-- declare protocol, if no id is configured, name will be used as id -->
<dubbo:protocol id="xxx1" name="xxx" />
<!-- Reference protocol, if the protocol attribute is not configured, the protocol configuration will be automatically scanned in the ApplicationContext -->
<dubbo:service protocol="xxx1" />
<!-- The reference protocol default value, when <dubbo:service> does not configure the protocol attribute, use this configuration -->
<dubbo:provider protocol="xxx1" />

已知擴充

  • org.apache.dubbo.rpc.protocol.injvm.InjvmProtocol
  • org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol
  • org.apache.dubbo.rpc.protocol.rmi.RmiProtocol
  • org.apache.dubbo.rpc.protocol.http.HttpProtocol
  • org.apache.dubbo.rpc.protocol.http.hessian.HessianProtocol
  • org.apache.dubbo.rpc.support.MockProtocol

擴充範例

Maven 專案結構

src
 |-main
    |-java
        |-com
            |-xxx
                |-XxxProtocol.java (implement the Protocol interface)
                |-XxxExporter.java (implements the Exporter interface)
                |-XxxInvoker.java (implements the Invoker interface)
    |-resources
        |-META-INF
            |-dubbo
                |-org.apache.dubbo.rpc.Protocol (plain text file, content: xxx=com.xxx.XxxProtocol)

XxxProtocol.java

package com.xxx;
 
import org.apache.dubbo.rpc.Protocol;
 
public class XxxProtocol implements Protocol {
    public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
        return new XxxExporter(invoker);
    }
    public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
        return new XxxInvoker(type, url);
    }
}

XxxExporter.java

package com.xxx;
 
import org.apache.dubbo.rpc.support.AbstractExporter;
 
public class XxxExporter<T> extends AbstractExporter<T> {
    public XxxExporter(Invoker<T> invoker) throws RemotingException{
        super(invoker);
        //...
    }
    public void unexport() {
        super. unexport();
        //...
    }
}

XxxInvoker.java

package com.xxx;
 
import org.apache.dubbo.rpc.support.AbstractInvoker;
 
public class XxxInvoker<T> extends AbstractInvoker<T> {
    public XxxInvoker(Class<T> type, URL url) throws RemotingException{
        super(type, url);
    }
    
    @Override
    protected Result doInvoke(Invocation invocation) throws Throwable {
        //...
    }
}

META-INF/dubbo/org.apache.dubbo.rpc.Protocol

xxx=com.xxx.XxxProtocol

最後修改日期:2023 年 1 月 2 日:強化英文文件 (#1798) (95a9f4f6c1c)