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

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:

Last modified September 30, 2024: Translate (22d3d83a3b)