Dubbo3 網(wǎng)絡(luò)傳輸擴(kuò)展

2022-04-22 11:54 更新

擴(kuò)展說(shuō)明

遠(yuǎn)程通訊的服務(wù)器及客戶端傳輸實(shí)現(xiàn)。

擴(kuò)展接口

  • org.apache.dubbo.remoting.Transporter
  • org.apache.dubbo.remoting.Server
  • org.apache.dubbo.remoting.Client

擴(kuò)展配置

<!-- 服務(wù)器和客戶端使用相同的傳輸實(shí)現(xiàn) -->
<dubbo:protocol transporter="xxx" /> 
<!-- 服務(wù)器和客戶端使用不同的傳輸實(shí)現(xiàn) -->
<dubbo:protocol server="xxx" client="xxx" /> 
<!-- 缺省值設(shè)置,當(dāng)<dubbo:protocol>沒(méi)有配置transporter/server/client屬性時(shí),使用此配置 -->
<dubbo:provider transporter="xxx" server="xxx" client="xxx" />

已知擴(kuò)展

  • org.apache.dubbo.remoting.transport.transporter.netty.NettyTransporter
  • org.apache.dubbo.remoting.transport.transporter.mina.MinaTransporter
  • org.apache.dubbo.remoting.transport.transporter.grizzly.GrizzlyTransporter

擴(kuò)展示例

Maven 項(xiàng)目結(jié)構(gòu):

src
 |-main
    |-java
        |-com
            |-xxx
                |-XxxTransporter.java (實(shí)現(xiàn)Transporter接口)
                |-XxxServer.java (實(shí)現(xiàn)Server接口)
                |-XxxClient.java (實(shí)現(xiàn)Client接口)
    |-resources
        |-META-INF
            |-dubbo
                |-org.apache.dubbo.remoting.Transporter (純文本文件,內(nèi)容為:xxx=com.xxx.XxxTransporter)

XxxTransporter.java:

package com.xxx;
 
import org.apache.dubbo.remoting.Transporter;
 
public class XxxTransporter implements Transporter {
    public Server bind(URL url, ChannelHandler handler) throws RemotingException {
        return new XxxServer(url, handler);
    }
    public Client connect(URL url, ChannelHandler handler) throws RemotingException {
        return new XxxClient(url, handler);
    }
}

XxxServer.java:

package com.xxx;
 
import org.apache.dubbo.remoting.transport.transporter.AbstractServer;
 
public class XxxServer extends AbstractServer {
    public XxxServer(URL url, ChannelHandler handler) throws RemotingException{
        super(url, handler);
    }
    protected void doOpen() throws Throwable {
        // ...
    }
    protected void doClose() throws Throwable {
        // ...
    }
    public Collection<Channel> getChannels() {
        // ...
    }
    public Channel getChannel(InetSocketAddress remoteAddress) {
        // ...
    }
}

XxxClient.java:

package com.xxx;
 
import org.apache.dubbo.remoting.transport.transporter.AbstractClient;
 
public class XxxClient extends AbstractClient {
    public XxxServer(URL url, ChannelHandler handler) throws RemotingException{
        super(url, handler);
    }
    protected void doOpen() throws Throwable {
        // ...
    }
    protected void doClose() throws Throwable {
        // ...
    }
    protected void doConnect() throws Throwable {
        // ...
    }
    public Channel getChannel() {
        // ...
    }
}

META-INF/dubbo/org.apache.dubbo.remoting.Transporter:

xxx=com.xxx.XxxTransporter


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)