Protocol

This article explains how to provide a custom RPC protocol implementation by extending the org.apache.dubbo.rpc.Protocol SPI.

In the Communication Protocol chapter, we learned about several core RPC protocols built into Dubbo: dubbo, rest, and tri, and how to use them. This article explains how to provide a custom RPC protocol implementation by extending the org.apache.dubbo.rpc.Protocol SPI.

There are two ways to create a private protocol: the first is to wrap the existing protocol and add specific business logic. The second is to completely customize a protocol. The former is simpler to implement and is widely used in dubbo, such as ProtocolFilterWrapper, QosProtocolWrapper, and ProtocolListenerWrapper. The latter is relatively complex but offers maximum flexibility; for instance, the built-in protocols dubbo and triple in the Dubbo framework fall under this implementation style.

For the complete source code of this example, refer to dubbo-samples-extensibility. In addition to this example, many Protocol implementations in the core Dubbo repository apache/dubbo and the extension library apache/dubbo-spi-extensions can also serve as extension references:

  1. # Common protocols supported by Dubbo
  2. dubbo=org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol
  3. tri=org.apache.dubbo.rpc.protocol.tri.TripleProtocol

Task Details

Implement a custom protocol edubbo based on the existing dubbo protocol.

Implementation

The edubbo protocol is implemented by wrapping the dubbo protocol.

Code Structure

Common
  1. src
  2. |-main
  3. |-java
  4. |-org
  5. |-apache
  6. |-dubbo
  7. |-samples
  8. |-extensibility
  9. |-protocol
  10. |-common
  11. |-EnhancedProtocol.java (Implements Protocol interface)
Provider
  1. src
  2. |-main
  3. |-java
  4. |-org
  5. |-apache
  6. |-dubbo
  7. |-samples
  8. |-extensibility
  9. |-protocol
  10. |-provider
  11. |-ExtensibilityProtocolProviderApplication.java
  12. |-ExtensibilityProtocolServiceImpl.java
  13. |-resources
  14. |-META-INF
  15. |-application.properties (Dubbo Provider configuration file)
  16. |-dubbo
  17. |-org.apache.dubbo.rpc.Protocol (Plain text file)
Consumer
  1. src
  2. |-main
  3. |-java
  4. |-org
  5. |-apache
  6. |-dubbo
  7. |-samples
  8. |-extensibility
  9. |-protocol
  10. |-consumer
  11. |-ExtensibilityProtocolConsumerApplication.java
  12. |-ExtensibilityProtocolConsumerTask.java
  13. |-resources
  14. |-META-INF
  15. |-application.properties (Dubbo Consumer configuration file)
  16. |-dubbo
  17. |-org.apache.dubbo.rpc.Protocol (Plain text file)

Code Details

  1. package org.apache.dubbo.samples.extensibility.protocol.common;
  2. import org.apache.dubbo.common.URL;
  3. import org.apache.dubbo.rpc.Protocol;
  4. import org.apache.dubbo.rpc.Invoker;
  5. import org.apache.dubbo.rpc.Exporter;
  6. import org.apache.dubbo.rpc.ProtocolServer;
  7. import org.apache.dubbo.rpc.RpcException;
  8. import org.apache.dubbo.rpc.model.FrameworkModel;
  9. import org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol;
  10. import java.util.List;
  11. public class EnhancedProtocol implements Protocol {
  12. public EnhancedProtocol(FrameworkModel frameworkModel) {
  13. this.protocol = new DubboProtocol(frameworkModel);
  14. }
  15. private final Protocol protocol;
  16. @Override
  17. public int getDefaultPort() {
  18. return this.protocol.getDefaultPort();
  19. }
  20. @Override
  21. public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
  22. // do something
  23. return this.protocol.export(invoker);
  24. }
  25. @Override
  26. public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
  27. // do something
  28. return this.protocol.refer(type, url);
  29. }
  30. @Override
  31. public void destroy() {
  32. this.protocol.destroy();
  33. }
  34. @Override
  35. public List<ProtocolServer> getServers() {
  36. return protocol.getServers();
  37. }
  38. }

SPI Configuration

Provider

Add the following configuration in the resources/META-INF/dubbo/org.apache.dubbo.rpc.Protocol file:

  1. edubbo=org.apache.dubbo.samples.extensibility.protocol.common.EnhancedProtocol
Consumer

Add the following configuration in the resources/META-INF/dubbo/org.apache.dubbo.rpc.Protocol file:

  1. edubbo=org.apache.dubbo.samples.extensibility.protocol.common.EnhancedProtocol

Configuration File

Provider

Add the following configuration in the resources/application.properties file:

  1. # Custom protocol
  2. dubbo.provider.protocol=edubbo
Consumer

Add the following configuration in the resources/application.properties file:

  1. # Custom protocol
  2. dubbo.consumer.protocol=edubbo

Run Results

Run the task using local IDE, the results are as follows:

Registered Protocol

dubbo-samples-extensibility-protocol-output2.jpg

Output Result

dubbo-samples-extensibility-protocol-output1.png

Feedback

Was this page helpful?

Yes No

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