協定
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
部署
[擴展性協定任務](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
,允許使用者在不更改底層架構的情況下新增自訂邏輯。