Dubbo源码-08-Exchanger
一 接口声明
@SPI(HeaderExchanger.NAME)
public interface Exchanger {
/**
* bind.
*
* @param url
* @param handler
* @return message server
*/
@Adaptive({Constants.EXCHANGER_KEY})
ExchangeServer bind(URL url, ExchangeHandler handler) throws RemotingException;
/**
* connect.
*
* @param url
* @param handler
* @return message channel
*/
@Adaptive({Constants.EXCHANGER_KEY})
ExchangeClient connect(URL url, ExchangeHandler handler) throws RemotingException;
}
默认实现使用header,HeaderExchanger,该实现没有标识@Adaptive
二 code生成
package com.alibaba.dubbo.remoting.exchange;
import com.alibaba.dubbo.common.extension.ExtensionLoader;
public class Exchanger$Adaptive implements com.alibaba.dubbo.remoting.exchange.Exchanger {
public com.alibaba.dubbo.remoting.exchange.ExchangeClient connect(com.alibaba.dubbo.common.URL arg0, com.alibaba.dubbo.remoting.exchange.ExchangeHandler arg1) throws com.alibaba.dubbo.remoting.RemotingException {
if (arg0 == null) throw new IllegalArgumentException("url == null");
com.alibaba.dubbo.common.URL url = arg0;
String extName = url.getParameter("exchanger", "header");
if (extName == null)
throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.remoting.exchange.Exchanger) name from url(" + url.toString() + ") use keys([exchanger])");
com.alibaba.dubbo.remoting.exchange.Exchanger extension = (com.alibaba.dubbo.remoting.exchange.Exchanger) ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.remoting.exchange.Exchanger.class).getExtension(extName);
return extension.connect(arg0, arg1);
}
public com.alibaba.dubbo.remoting.exchange.ExchangeServer bind(com.alibaba.dubbo.common.URL arg0, com.alibaba.dubbo.remoting.exchange.ExchangeHandler arg1) throws com.alibaba.dubbo.remoting.RemotingException {
if (arg0 == null) throw new IllegalArgumentException("url == null");
com.alibaba.dubbo.common.URL url = arg0;
String extName = url.getParameter("exchanger", "header");
if (extName == null)
throw new IllegalStateException("Fail to get extension(com.alibaba.dubbo.remoting.exchange.Exchanger) name from url(" + url.toString() + ") use keys([exchanger])");
com.alibaba.dubbo.remoting.exchange.Exchanger extension = (com.alibaba.dubbo.remoting.exchange.Exchanger) ExtensionLoader.getExtensionLoader(com.alibaba.dubbo.remoting.exchange.Exchanger.class).getExtension(extName);
return extension.bind(arg0, arg1);
}
}
三 实现
HeaderExchanger
1 生产者导出服务
@Override
public ExchangeServer bind(URL url, ExchangeHandler handler) throws RemotingException {
/**
* url
* - dubbo://192.168.0.11:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=native-provider&bind.ip=192.168.0.11&bind.port=20880&channel.readonly.sent=true&codec=dubbo&dubbo=2.0.2&generic=false&heartbeat=60000&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=35700&qos.port=22222&side=provider×tamp=1669554921457
*/
return new HeaderExchangeServer(Transporters.bind(url, new DecodeHandler(new HeaderExchangeHandler(handler))));
}
public static Server bind(URL url, ChannelHandler... handlers) throws RemotingException {
if (url == null) {
throw new IllegalArgumentException("url == null");
}
if (handlers == null || handlers.length == 0) {
throw new IllegalArgumentException("handlers == null");
}
ChannelHandler handler;
if (handlers.length == 1) {
handler = handlers[0];
} else {
handler = new ChannelHandlerDispatcher(handlers);
}
/**
* url
* - dubbo://192.168.0.11:20880/com.alibaba.dubbo.demo.DemoService?anyhost=true&application=native-provider&bind.ip=192.168.0.11&bind.port=20880&channel.readonly.sent=true&codec=dubbo&dubbo=2.0.2&generic=false&heartbeat=60000&interface=com.alibaba.dubbo.demo.DemoService&methods=sayHello&pid=36435&qos.port=22222&side=provider×tamp=1669555941396
* 自适应
* - @SPI指定默认实现是netty
* - 自适应扩展点优先级
* - URL中配置server
* - URL中配置transporter
* 默认实现是NettyTransporter
*/
return getTransporter().bind(url, handler);
}
public static Transporter getTransporter() {
return ExtensionLoader.getExtensionLoader(Transporter.class).getAdaptiveExtension();
}
2 消费者引用服务
// HeaderExchanger.java
@Override
public ExchangeClient connect(URL url, ExchangeHandler handler) throws RemotingException {
return new HeaderExchangeClient(Transporters.connect(url, new DecodeHandler(new HeaderExchangeHandler(handler))), true);
}
// Transporters.java
public static Client connect(URL url, ChannelHandler... handlers) throws RemotingException {
if (url == null) {
throw new IllegalArgumentException("url == null");
}
ChannelHandler handler;
if (handlers == null || handlers.length == 0) {
handler = new ChannelHandlerAdapter();
} else if (handlers.length == 1) {
handler = handlers[0];
} else {
handler = new ChannelHandlerDispatcher(handlers);
}
return getTransporter().connect(url, handler);
}
// Transporters.java
public static Transporter getTransporter() {
return ExtensionLoader.getExtensionLoader(Transporter.class).getAdaptiveExtension();
}