Protocol Extension

Protocol Extension

Extension Description

RPC protocol extension, encapsulating remote call details.

Contract:

  • When the user calls the invoke() method of the Invoker object returned by refer(), the protocol should execute the invoke() method of the Invoker object passed in by remote export() with the same URL.
  • The Invoker returned by refer() is implemented by the protocol, which usually sends a remote request within this Invoker. The Invoker passed in by export() is implemented and provided by the framework, which the protocol does not need to be concerned about.

Note

  • The protocol does not concern itself with the transparent proxying of business interfaces; it is Invoker centric, with the outer layer converting Invoker to the business interface.
  • The protocol does not necessarily have to be TCP network communication, such as through shared files, IPC inter-process communication, etc.

Extension Interface

  • org.apache.dubbo.rpc.Protocol
  • org.apache.dubbo.rpc.Exporter
  • org.apache.dubbo.rpc.Invoker
  1. public interface Protocol {
  2. /**
  3. * Expose remote service:<br>
  4. * 1. The protocol, upon receiving requests, should record the request source address information: RpcContext.getContext().setRemoteAddress();<br>
  5. * 2. export() must be idempotent, meaning exposing the same URL's Invoker twice is the same as exposing it once.<br>
  6. * 3. The Invoker passed to export() is provided by the framework, and the protocol does not need to be concerned.<br>
  7. *
  8. * @param <T> Service type
  9. * @param invoker Service execution body
  10. * @return exporter Reference to exposed service for cancellation
  11. * @throws RpcException Thrown when an error occurs in exposing the service, such as port occupied.
  12. */
  13. <T> Exporter<T> export(Invoker<T> invoker) throws RpcException;
  14. /**
  15. * Reference a remote service:<br>
  16. * 1. When the user calls invoke() on the Invoker object returned by refer(), the protocol should execute the invoke() method of the Invoker object passed in by remote export() with the same URL.<br>
  17. * 2. The Invoker returned by refer() is implemented by the protocol, typically sending the remote request within this Invoker.<br>
  18. * 3. When check=false is set in the URL, connection failures should not throw exceptions but should automatically recover internally.<br>
  19. *
  20. * @param <T> Service type
  21. * @param type Service type
  22. * @param url Remote service's URL
  23. * @return invoker Local proxy of the service
  24. * @throws RpcException Thrown when connection to the service provider fails.
  25. */
  26. <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException;
  27. }

Extension Configuration

  1. <!-- Declare protocol, if no id is configured, name will be used as id -->
  2. <dubbo:protocol id="xxx1" name="xxx" />
  3. <!-- Reference protocol, if no protocol attribute is configured, it will automatically scan protocol configuration in ApplicationContext -->
  4. <dubbo:service protocol="xxx1" />
  5. <!-- Default value for referenced protocol, used when <dubbo:service> does not configure protocol attribute -->
  6. <dubbo:provider protocol="xxx1" />

Known Extensions

  • 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

Extension Example

Maven project structure:

  1. src
  2. |-main
  3. |-java
  4. |-com
  5. |-xxx
  6. |-XxxProtocol.java (implements Protocol interface)
  7. |-XxxExporter.java (implements Exporter interface)
  8. |-XxxInvoker.java (implements Invoker interface)
  9. |-resources
  10. |-META-INF
  11. |-dubbo
  12. |-org.apache.dubbo.rpc.Protocol (plain text file, content: xxx=com.xxx.XxxProtocol)

XxxProtocol.java:

  1. package com.xxx;
  2. import org.apache.dubbo.rpc.Protocol;
  3. public class XxxProtocol implements Protocol {
  4. public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
  5. return new XxxExporter(invoker);
  6. }
  7. public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
  8. return new XxxInvoker(type, url);
  9. }
  10. }

XxxExporter.java:

  1. package com.xxx;
  2. import org.apache.dubbo.rpc.support.AbstractExporter;
  3. public class XxxExporter<T> extends AbstractExporter<T> {
  4. public XxxExporter(Invoker<T> invoker) throws RemotingException{
  5. super(invoker);
  6. // ...
  7. }
  8. public void unexport() {
  9. super.unexport();
  10. // ...
  11. }
  12. }

XxxInvoker.java:

  1. package com.xxx;
  2. import org.apache.dubbo.rpc.support.AbstractInvoker;
  3. public class XxxInvoker<T> extends AbstractInvoker<T> {
  4. public XxxInvoker(Class<T> type, URL url) throws RemotingException{
  5. super(type, url);
  6. }
  7. @Override
  8. protected Result doInvoke(Invocation invocation) throws Throwable {
  9. // ...
  10. }
  11. }

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

  1. xxx=com.xxx.XxxProtocol

Feedback

Was this page helpful?

Yes No

Last modified September 30, 2024: Update & Translate Overview Docs (#3040) (d37ebceaea7)