袋鼠回头看了看文章,有些啰嗦,争取语音简练,不断提高表达力!袋鼠奋起直追!
注:此篇文章,暂时为了以后时间线排序的需要,暂时发表出来,可是仍然有许多地方需要改写。自己打算把服务端发布,客户端订阅都搞定后,再修改这部分。
接着上篇继续分析doExport()
。。。前面的部分就做了一件事,将相关配置信息转换成URL类。。。
下面的两行也就做了一件事,委托configHandler,生成exporter对象 1 ConfigHandler configHandler = ExtensionLoader.getExtensionLoader(ConfigHandler.class).getExtension(MotanConstants.DEFAULT_VALUE); exporters.add(configHandler.export(interfaceClass, ref, urls));
public <T> Exporter<T> export(Class<T> interfaceClass, T ref, List<URL> registryUrls) {
// embed的属性,embed则是保存了服务URL
String serviceStr = StringTools.urlDecode(registryUrls.get(0).getParameter(URLParamType.embed.getName()));
URL serviceUrl = URL.valueOf(serviceStr); // export service String protocolName = serviceUrl.getParameter(URLParamType.protocol.getName(), URLParamType.protocol.getValue());
// 查找协议,即DefaultRpcProtocol对象
Protocol orgProtocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension(protocolName);
// 创建提供service的provider
Provider<T> provider = getProvider(orgProtocol, ref, serviceUrl, interfaceClass);
// 利用protocol decorator来增加filter特性
Protocol protocol = new ProtocolFilterDecorator(orgProtocol);
// 暴露过程 它是委托给具体协议,调用协议类(即DefaultRpcProtocol对象)的export方法来进行暴露
Exporter<T> exporter = protocol.export(provider, serviceUrl); // register service 暴露之后注册服务,注意这里的先后顺序。为了保证,通知上线时,已经完成暴露的过程。
register(registryUrls, serviceUrl); return exporter;
}
我们进到第13行的方法
public <T> Exporter<T> export(Provider<T> provider, URL url) {
// decorateWithFilter(provider, url)的结果为参数provider本身,filter为null
return protocol.export(decorateWithFilter(provider, url), url);
}
public <T> Exporter<T> export(Provider<T> provider, URL url) {
if (url == null) {
throw new MotanFrameworkException(this.getClass().getSimpleName() + " export Error: url is null",
MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
} if (provider == null) {
throw new MotanFrameworkException(this.getClass().getSimpleName() + " export Error: provider is null, url=" + url,
MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
// 这里的url是服务url
String protocolKey = MotanFrameworkUtil.getProtocolKey(url); synchronized (exporterMap) {
Exporter<T> exporter = (Exporter<T>) exporterMap.get(protocolKey); if (exporter != null) {
throw new MotanFrameworkException(this.getClass().getSimpleName() + " export Error: service already exist, url=" + url,
MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
// 在DefaultRpcExporter的类初始化中,根据当前系统的SPI配置,找到EndpointFactory的实现类,并创建一个Server对象,
// 由Server对象的server.open()最终完成transport的网络层暴露。
// 网络层暴露的Netty部分,暂时略过
22 exporter = createExporter(provider, url);
23 exporter.init(); exporterMap.put(protocolKey, exporter); LoggerUtil.info(this.getClass().getSimpleName() + " export Success: url=" + url); return exporter;
} }
接下来,是具体的服务注册部分。
registryUrls是zookeeper的注册地址,serviceUrl则是提供需要的参数。
private void register(List<URL> registryUrls, URL serviceUrl) { for (URL url : registryUrls) {
// 根据check参数的设置,register失败可能会抛异常,上层应该知晓
// SPI生成ZookeeperRegistryFactory
RegistryFactory registryFactory = ExtensionLoader.getExtensionLoader(RegistryFactory.class).getExtension(url.getProtocol());
if (registryFactory == null) {
throw new MotanFrameworkException(new MotanErrorMsg(500, MotanErrorMsgConstant.FRAMEWORK_REGISTER_ERROR_CODE,
"register error! Could not find extension for registry protocol:" + url.getProtocol()
+ ", make sure registry module for " + url.getProtocol() + " is in classpath!"));
}
Registry registry = registryFactory.getRegistry(url);
registry.register(serviceUrl);
}
}
根据url得到注册地址的字符串,然后缓存中查询,没有的话,初始化生成ZookeeperRegistry的实例,并返回。
public Registry getRegistry(URL url) {
String registryUri = getRegistryUri(url);
try {
lock.lock();
Registry registry = registries.get(registryUri);
if (registry != null) {
return registry;
}
// zookeeper的相关代码封装在下面方法中
registry = createRegistry(url);
if (registry == null) {
throw new MotanFrameworkException("Create registry false for url:" + url, MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
}
registries.put(registryUri, registry);
return registry;
} catch (Exception e) {
throw new MotanFrameworkException("Create registry false for url:" + url, e, MotanErrorMsgConstant.FRAMEWORK_INIT_ERROR);
} finally {
lock.unlock();
}
}