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.
package com.spi.demo;
import org.apache.dubbo.rpc.Protocol;
@Activate
public class CustomizedProtocol implements Protocol {
// ...
}
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:
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, wherekey
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:
# For Spring Boot, you can modify application.yml or application.properties
dubbo
protocol
name: customized
or
ProtocolConfig protocol = new ProtocolConfig();
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)