dubbo的服务发布流程 网上资料已经非常多 参照了网上资料 记录下调试学习过程
承接学习篇2中提到最终是由
public class ServiceBean<T> extends ServiceConfig<T> implements ApplicationListener<ContextRefreshedEvent>
public void onApplicationEvent(ContextRefreshedEvent event) {
if (this.isDelay() && !this.isExported() && !this.isUnexported()) {
if (logger.isInfoEnabled()) {
logger.info("The service ready on spring started. service: " + this.getInterface());
}
this.export();
}
}
这个位置开始发布 同时继承了ServiceConfig保存可配置属性。
之后主要是对注解配置参数进行解析处理等最终在ServiceConfig中调用
this.doExportUrls();
这里解析出最终的url 并根据配置参数进行实践发布 这里贴一个fullurlpath
dubbo://192.168.31.212:20880/org.boot.dubbo.api.service.HelloService?anyhost=true&application=springboot-dubbo-provider&bind.ip=192.168.31.212&bind.port=20880&dubbo=2.6.2&generic=false&interface=org.boot.dubbo.api.service.HelloService&methods=sayHello,sayHello1&owner=LuoLiang&pid=3520&revision=1.0.0&serialization=kryo&side=provider×tamp=1569831235376&version=1.0.0
其中主要的几句发布代码如下
Invoker<?> invoker = proxyFactory.getInvoker(this.ref, this.interfaceClass, registryURL.addParameterAndEncoded("export", url.toFullString()));
DelegateProviderMetaDataInvoker wrapperInvoker = new DelegateProviderMetaDataInvoker(invoker, this);
Exporter<?> exporter = protocol.export(wrapperInvoker);
this.exporters.add(exporter);
我们知道dubbo默认的protocol 为dubbo协议 下面的代码即将跳转到dubbo协议中执行
public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
URL url = invoker.getUrl();
String key = serviceKey(url);
DubboExporter<T> exporter = new DubboExporter(invoker, key, this.exporterMap);
this.exporterMap.put(key, exporter);
Boolean isStubSupportEvent = url.getParameter("dubbo.stub.event", false);
Boolean isCallbackservice = url.getParameter("is_callback_service", false);
if (isStubSupportEvent.booleanValue() && !isCallbackservice.booleanValue()) {
String stubServiceMethods = url.getParameter("dubbo.stub.event.methods");
if (stubServiceMethods != null && stubServiceMethods.length() != 0) {
this.stubServiceMethodsMap.put(url.getServiceKey(), stubServiceMethods);
} else if (this.logger.isWarnEnabled()) {
this.logger.warn(new IllegalStateException("consumer [" + url.getParameter("interface") + "], has set stubproxy support event ,but no stub methods founded."));
}
}
this.openServer(url);
this.optimizeSerialization(url);
return exporter;
}
url 即为上面生成的url 贴出key值为
org.boot.dubbo.api.service.HelloService:1.0.0:20880
DubboExporter<T> exporter = new DubboExporter(invoker, key, this.exporterMap);
this.exporterMap.put(key, exporter);
这两句 使用当前的invoker生成一个exporter并放入当前缓存map中
接下去就是openserver dubbo协议默认使用netty作为tcp服务
private void openServer(URL url) {
String key = url.getAddress();
boolean isServer = url.getParameter("isserver", true);
if (isServer) {
ExchangeServer server = (ExchangeServer)this.serverMap.get(key);
if (server == null) {
this.serverMap.put(key, this.createServer(url));
} else {
server.reset(url);
}
}
}
这里 1.存在相同端口ip的情况直接reset 2.如果没有才执行重新创建
ExchangeServer server;
try {
server = Exchangers.bind(url, requestHandler);
} catch (RemotingException e) {
throw new RpcException("Fail to start server(url: " + url + ") " + e.getMessage(), e);
}
通过dubbo spi找到
@Override
public ExchangeServer bind(URL url, ExchangeHandler handler) throws RemotingException {
return new HeaderExchangeServer(Transporters.bind(url, new DecodeHandler(new HeaderExchangeHandler(handler))));
}
@SPI("netty")
public interface Transporter {
/**
* Bind a server.
*
* @param url server url
* @param handler
* @return server
* @throws RemotingException
* @see org.apache.dubbo.remoting.Transporters#bind(URL, ChannelHandler...)
*/
@Adaptive({Constants.SERVER_KEY, Constants.TRANSPORTER_KEY})
Server bind(URL url, ChannelHandler handler) throws RemotingException;
/**
* Connect to a server.
*
* @param url server url
* @param handler
* @return client
* @throws RemotingException
* @see org.apache.dubbo.remoting.Transporters#connect(URL, ChannelHandler...)
*/
@Adaptive({Constants.CLIENT_KEY, Constants.TRANSPORTER_KEY})
Client connect(URL url, ChannelHandler handler) throws RemotingException;
}
这里是生成了适配器类 最终动态绑定到nettyserver上 同时注意到最初传入的requesthandler为在dubboprotocol中定义的,并被包装后传给了netty
private ExchangeHandler requestHandler = new ExchangeHandlerAdapter() {
public Object reply(ExchangeChannel channel, Object message) throws RemotingException {
if (!(message instanceof Invocation)) {
throw new RemotingException(channel, "Unsupported request: " + (message == null ? null : message.getClass().getName() + ": " + message) + ", channel: consumer: " + channel.getRemoteAddress() + " --> provider: " + channel.getLocalAddress());
} else {
Invocation inv = (Invocation)message;
Invoker<?> invoker = DubboProtocol.this.getInvoker(channel, inv);
if (Boolean.TRUE.toString().equals(inv.getAttachments().get("_isCallBackServiceInvoke"))) {
String methodsStr = (String)invoker.getUrl().getParameters().get("methods");
boolean hasMethod = false;
if (methodsStr != null && methodsStr.indexOf(",") != -1) {
String[] methods = methodsStr.split(",");
String[] arr$ = methods;
int len$ = methods.length;
for(int i$ = 0; i$ < len$; ++i$) {
String method = arr$[i$];
if (inv.getMethodName().equals(method)) {
hasMethod = true;
break;
}
}
} else {
hasMethod = inv.getMethodName().equals(methodsStr);
}
if (!hasMethod) {
DubboProtocol.this.logger.warn(new IllegalStateException("The methodName " + inv.getMethodName() + " not found in callback service interface ,invoke will be ignored." + " please update the api interface. url is:" + invoker.getUrl()) + " ,invocation is :" + inv);
return null;
}
}
RpcContext.getContext().setRemoteAddress(channel.getRemoteAddress());
return invoker.invoke(inv);
}
}
}
这里只截取最主要的处理回复消息的函数 从这里可以看到之前exporterMap存储的servicekey对应的invoker此处将会对应被取出来 并最终执行对应的invoke方法并通过netty将数据返回 。
中间涉及到netty的细节 包括encode 系列化 此节不再叙述,不过跟完整个流程后发现一个问题 并没有找到向zookeeper登记的代码,经过搜索一些资料找到一个比较靠谱的参考 其也同时描述了本文发布的过程,不过写的比本文有条理 大家可以参考其对登记过程的描述。
https://www.jianshu.com/p/7f3871492c71
完。