协议
Dubbo 通过协议扩展实现了大量内置功能,并支持许多常用的协议。您可以在 org.apache.dubbo.rpc.Protocol
文件中查看所有自定义协议。例如,在 Dubbo 3 中,我们有
# Built-in functionalities implemented by Dubbo through protocol extension
filter=org.apache.dubbo.rpc.cluster.filter.ProtocolFilterWrapper
qos=org.apache.dubbo.qos.protocol.QosProtocolWrapper
registry=org.apache.dubbo.registry.integration.InterfaceCompatibleRegistryProtocol
service-discovery-registry=org.apache.dubbo.registry.integration.RegistryProtocol
listener=org.apache.dubbo.rpc.protocol.ProtocolListenerWrapper
mock=org.apache.dubbo.rpc.support.MockProtocol
serializationwrapper=org.apache.dubbo.rpc.protocol.ProtocolSerializationWrapper
securitywrapper=org.apache.dubbo.rpc.protocol.ProtocolSecurityWrapper
# Commonly used protocols supported by Dubbo
dubbo=org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol
injvm=org.apache.dubbo.rpc.protocol.injvm.InjvmProtocol
rest=org.apache.dubbo.rpc.protocol.rest.RestProtocol
grpc=org.apache.dubbo.rpc.protocol.grpc.GrpcProtocol
tri=org.apache.dubbo.rpc.protocol.tri.TripleProtocol
如您所见,Dubbo 通过协议扩展实现了一系列功能,如过滤、监控、服务发现、监听器、模拟、序列化和安全。它还支持 dubbo
、injvm
、rest
、grpc
和 tri
协议供外部使用。
自定义私有协议有两种方法。第一种是包装现有协议并添加一些特定的业务逻辑。另一种是完全自定义一个新协议。前者更简单,在 Dubbo 中应用广泛,例如 ProtocolFilterWrapper
、QosProtocolWrapper
、ProtocolListenerWrapper
等。后者更复杂,但 Dubbo 已经实现了大多数常用的协议,这些协议在生产环境中经过了充分的测试。
本文将演示如何基于现有协议实现自定义协议。
先决条件
两种部署和运行方法,选择其中一种
基于 Kubernetes
安装 Kubernetes 环境
修改 Provider 中的配置文件以启用 Kubernetes 中部署的 nacos 地址
# Specify the application name of Dubbo dubbo.application.name=extensibility-protocol-provider # Enable token verification for each invocation dubbo.provider.token=true # Specify the registry address # dubbo.registry.address=nacos://localhost:8848?username=nacos&password=nacos dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos # Custom protocol edubbo dubbo.provider.protocol=edubbo
修改 Consumer 中的配置文件以启用 Kubernetes 中部署的 nacos 地址
# Specify the application name of Dubbo dubbo.application.name=extensibility-protocol-consumer # Enable token verification for each invocation dubbo.provider.token=true # Specify the registry address # dubbo.registry.address=nacos://localhost:8848?username=nacos&password=nacos dubbo.registry.address=nacos://${nacos.address:localhost}:8848?username=nacos&password=nacos # Custom protocol edubbo dubbo.consumer.protocol=edubbo
部署
[Extensibility Protocol Task](https://github.com/apache/dubbo-samples/blob/master/10-task/dubbo-samples-extensibility/deploy/All.yml)
使用本地 IDE
- 部署 Nacos 版本 2.2.0
- 修改 Provider 中的配置文件以启用本地 nacos 地址
# Specify the application name of Dubbo dubbo.application.name=extensibility-protocol-provider # Enable token verification for each invocation dubbo.provider.token=true # Specify the registry address # Enable local nacos address dubbo.registry.address=nacos://localhost:8848?username=nacos&password=nacos # Custom protocol edubbo dubbo.provider.protocol=edubbo
- 修改 Consumer 中的配置文件以启用本地 nacos 地址
# Specify the application name of Dubbo dubbo.application.name=extensibility-protocol-consumer # Enable token verification for each invocation dubbo.provider.token=true # Specify the registry address # Enable local nacos address dubbo.registry.address=nacos://localhost:8848?username=nacos&password=nacos # Custom protocol edubbo dubbo.consumer.protocol=edubbo
任务详情
基于现有 dubbo
协议实现自定义协议 edubbo
。
实现方法
包装现有 dubbo
协议以实现 edubbo
协议。
代码结构
通用
src
|-main
|-java
|-org
|-apache
|-dubbo
|-samples
|-extensibility
|-protocol
|-common
|-EnhancedProtocol.java (Implement Protocol interface)
提供者
src
|-main
|-java
|-org
|-apache
|-dubbo
|-samples
|-extensibility
|-protocol
|-provider
|-ExtensibilityProtocolProviderApplication.java
|-ExtensibilityProtocolServiceImpl.java
|-resources
|-META-INF
|-application.properties (Dubbo Provider configuration file)
|-dubbo
|-org.apache.dubbo.rpc.Protocol (Plain text file)
消费者
src
|-main
|-java
|-org
|-apache
|-dubbo
|-samples
|-extensibility
|-protocol
|-consumer
|-ExtensibilityProtocolConsumerApplication.java
|-ExtensibilityProtocolConsumerTask.java
|-resources
|-META-INF
|-application.properties (Dubbo Consumer configuration file)
|-dubbo
|-org.apache.dubbo.rpc.Protocol (
Plain text file)
代码详情
package org.apache.dubbo.samples.extensibility.protocol.common;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.ProtocolServer;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.protocol.dubbo.DubboProtocol;
import java.util.List;
public class EnhancedProtocol implements Protocol {
public EnhancedProtocol(FrameworkModel frameworkModel) {
this.protocol = new DubboProtocol(frameworkModel);
}
private final Protocol protocol;
@Override
public int getDefaultPort() {
return this.protocol.getDefaultPort();
}
@Override
public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
// do something
return this.protocol.export(invoker);
}
@Override
public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
// do something
return this.protocol.refer(type, url);
}
@Override
public void destroy() {
this.protocol.destroy();
}
@Override
public List<ProtocolServer> getServers() {
return protocol.getServers();
}
}
SPI 配置
提供者
在 resources/META-INF/dubbo/org.apache.dubbo.rpc.Protocol
文件中添加以下配置
edubbo=org.apache.dubbo.samples.extensibility.protocol.common.EnhancedProtocol
消费者
在 resources/META-INF/dubbo/org.apache.dubbo.rpc.Protocol
文件中添加以下配置
edubbo=org.apache.dubbo.samples.extensibility.protocol.common.EnhancedProtocol
配置文件
提供者
在 resources/application.properties
文件中添加以下配置
# Custom protocol
dubbo.provider.protocol=edubbo
消费者
在 resources/application.properties
文件中添加以下配置
# Custom protocol
dubbo.consumer.protocol=edubbo
执行结果
使用 使用本地 IDE 方法运行任务,结果如下
注册协议
输出结果
总之,Dubbo 提供了强大灵活的协议扩展机制。本教程演示了如何基于现有 dubbo
协议创建自定义协议 edubbo
,允许用户添加自定义逻辑而无需更改底层架构。