Basic Steps to Customize SPI Extensions

Description of the SPI plugin extension mechanism of Dubbo, explaining the basic steps to customize SPI extensions.

Taking the RPC Protocol Plugin as an example, this section demonstrates how to use the SPI plugin provided by Dubbo to provide a custom RPC protocol implementation. If you want to understand how the SPI mechanism works and the list of built-in SPI extension points in the framework, please refer to the Reference Manual - SPI Extension.

1. Provide SPI Plugin Implementation Class

Provide a Java class that implements the org.apache.dubbo.rpc.Protocol interface.

  1. package com.spi.demo;
  2. import org.apache.dubbo.rpc.Protocol;
  3. @Activate
  4. public class CustomizedProtocol implements Protocol {
  5. // ...
  6. }

2. Configure Implementation Class in the Specified File

Add a org.apache.dubbo.rpc.Protocol file in the application’s resources/META-INF/services/ directory, and include the following configuration in the file:

  1. customized=com.spi.demo.CustomizedProtocol

Configuration Notes

  • The file name must be the full package path defined by the SPI plugin, depending on the SPI definition you want to extend, such as resources/META-INF/services/org.apache.dubbo.rpc.Protocol in this example.
  • The content of the file must be in the key=value format, where key can be defined arbitrarily, but it is recommended to add a specific prefix to avoid name collisions with Dubbo’s built-in implementations; value must be the full path name of the implementation class.

3. Enable Custom Protocol Implementation Through Configuration

Modify the protocol configuration in the application to tell the Dubbo framework to use the custom protocol:

  1. # For Spring Boot, you can modify application.yml or application.properties
  2. dubbo
  3. protocol
  4. name: customized

or

  1. ProtocolConfig protocol = new ProtocolConfig();
  2. protocol.setName("cutomized");

4. More Examples

If you want to see more complete examples, please refer to other examples in this directory:

Feedback

Was this page helpful?

Yes No

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