协议扩展
扩展说明
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)