dubbo服務發布
dubbo服務發布只需在spring.xml中如下配置即可:
<dubbo:service interface="com.alibaba.dubbo.demo.DemoService" ref="demoService" />
export初始化
通過2-dubbo結合spring可知,<dubbo:service>
解析后封裝到ServiceBean中;ServiceBean定義如下,繼承了dubbo定義的類ServiceConfig,實現了5個spring的接口,為了融入spring容器的啟動過程中:
public class ServiceBean<T> extends ServiceConfig<T> implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener, BeanNameAware {
... ...
}
ServiceBean實現了ApplicationListener接口,當spring容器觸發了ContextRefreshedEvent事件時,就會調用ServiceConfig中的export()
方法發布<dubbo:service>
申明的dubbo服務,且在dubbo的info級別日志中有相應的日志:
public void onApplicationEvent(ApplicationEvent event) {
if (ContextRefreshedEvent.class.getName().equals(event.getClass().getName())) {
if (isDelay() && ! isExported() && ! isUnexported()) {
if (logger.isInfoEnabled()) {
logger.info("The service ready on spring started. service: " + getInterface());
}
export();
}
}
}
info日志示例:The service ready on spring started. service: com.alibaba.dubbo.demo.DemoService
ServiceConfig.export()
ServiceConfig中export()
方法部分源碼如下,如果<dubbo:service>
中申明了delay
(例如<dubbo:service interface="com.alibaba.dubbo.demo.TestService" ref="testService" delay="3000"/>
),那么延遲調用doExport()
發布這個服務,否則直接調用doExport()
發布服務:
public synchronized void export() {
... ...
if (delay != null && delay > 0) {
Thread thread = new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(delay);
} catch (Throwable e) {
}
doExport();
}
});
thread.setDaemon(true);
thread.setName("DelayExportServiceThread");
thread.start();
} else {
doExport();
}
}
ServiceConfig.doExport()
的作用:
- 檢查
<dubbo:service>
中是否配置了interface, 如果為空,那么拋出異常: - 檢查xml配置中申明的interface的類型是否是java interface類型(interfaceClass.isInterface())
- 檢查xml配置中interface和ref是否匹配(interfaceClass.isInstance(ref))
- application®istry&protocol等有效性檢查;
- 有效性檢查通過后,調用doExportUrls()發布dubbo服務;
ServiceConfig.doExportUrls()
通過調用loadRegistries(true)得到所有registry的url地址,例如在dubbo.properties
中通過配置dubbo.registry.address=zookeeper://127.0.0.1:2181
;protocols就是將要發布服務的協議集合(dubbo服務可以同時暴露多種協議),可以在dubbo.properties
中配置,以dubbo協議為例:
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
ServiceConfig.doExportUrls()
源碼如下:
private void doExportUrls() {
List<URL> registryURLs = loadRegistries(true);
// 一般只配置dubbo協議,那么protocols就是:<dubbo:protocol name="dubbo" port="20880" id="dubbo" />
for (ProtocolConfig protocolConfig : protocols) {
doExportUrlsFor1Protocol(protocolConfig, registryURLs);
}
}
ServiceConfig.doExportUrlsFor1Protocol()
先把所有相關屬性封裝到Map中,例如protocol=dubbo,host=10.0.0.1,port=20880,path=com.alibaba.dubbo.demo.TestService等,然后構造dubbo定義的統一數據模型URL:
URL url = new URL(name, host, port, (contextPath == null || contextPath.length() == 0 ? "" : contextPath + "/") + path, map);
得到的url如下所示(這個url非常重要,貫穿整個dubbo服務的發布和調用過程,可以在服務發布后在dubbo-monitor中看到):
ServiceConfig.doExportUrlsFor1Protocol()
中根據scope判斷服務的發布范圍:
- 如果配置scope=none, 那么不需要發布這個dubbo服務;
- 沒有配置scope=noe且配置的scope!=remote, 那么本地暴露 這個dubbo服務;
- 沒有配置scope=noe且配置的scope!=remote且配置的scope!=local,那么遠程暴露這個dubbo服務(例如遠程暴露這個服務到zk上,默認情況下scope沒有配置,就是在這里發布服務);
實現源碼如下:
//配置為none不暴露
if (! Constants.SCOPE_NONE.toString().equalsIgnoreCase(scope)) {
//配置不是remote的情況下做本地暴露 (配置為remote,則表示只暴露遠程服務)
if (!Constants.SCOPE_REMOTE.toString().equalsIgnoreCase(scope)) {
exportLocal(url);
}
//如果配置不是local則暴露為遠程服務.(配置為local,則表示只暴露遠程服務)
if (! Constants.SCOPE_LOCAL.toString().equalsIgnoreCase(scope) ){
if (logger.isInfoEnabled()) {
logger.info("Export dubbo service " + interfaceClass.getName() + " to url " + url);
}
// 如果注冊url地址存在,例如申明了注冊的zk地址
if (registryURLs != null && registryURLs.size() > 0
&& url.getParameter("register", true)) {
// 注冊的zk地址可能是集群,那么需要遍歷這些地址一一進行注冊
for (URL registryURL : registryURLs) {
url = url.addParameterIfAbsent("dynamic", registryURL.getParameter("dynamic"));
// 如果申明了dubbo-monitor,那么再url地址上append類似monitor=monitor全地址
URL monitorUrl = loadMonitor(registryURL);
if (monitorUrl != null) {
url = url.addParameterAndEncoded(Constants.MONITOR_KEY, monitorUrl.toFullString());
}
if (logger.isInfoEnabled()) {
logger.info("Register dubbo service " + interfaceClass.getName() + " url " + url + " to registry " + registryURL);
}
Invoker<?> invoker = proxyFactory.getInvoker(ref, (Class) interfaceClass, registryURL.addParameterAndEncoded(Constants.EXPORT_KEY, url.toFullString()));
// 默認都是dubbo協議,所以調用DubboProtol.export(Invoker)
Exporter<?> exporter = protocol.export(invoker);
exporters.add(exporter);
}
} else {
Invoker<?> invoker = proxyFactory.getInvoker(ref, (Class) interfaceClass, url);
Exporter<?> exporter = protocol.export(invoker);
exporters.add(exporter);
}
}
}
且會有兩行info級別的日志;
Export dubbo service com.alibaba.dubbo.demo.DemoService to url ... ...
Register dubbo service com.alibaba.dubbo.demo.DemoService url ... ...
Protocol.export()
com.alibaba.dubbo.rpc.Protocol中暴露服務接口申明:
/**
* 暴露遠程服務:<br>
* 1. 協議在接收請求時,應記錄請求來源方地址信息:RpcContext.getContext().setRemoteAddress();<br>
* 2. export()必須是冪等的,也就是暴露同一個URL的Invoker兩次,和暴露一次沒有區別。<br>
* 3. export()傳入的Invoker由框架實現并傳入,協議不需要關心。<br>
*
* @param <T> 服務的類型
* @param invoker 服務的執行體
* @return exporter 暴露服務的引用,用于取消暴露
* @throws RpcException 當暴露服務出錯時拋出,比如端口已占用
*/
@Adaptive
<T> Exporter<T> export(Invoker<T> invoker) throws RpcException;
RegistryProtocol.export()
源碼如下:
public <T> Exporter<T> export(final Invoker<T> originInvoker) throws RpcException {
//export invoker
final ExporterChangeableWrapper<T> exporter = doLocalExport(originInvoker);
//registry provider,根據發布的服務originInvoker得到Registry實例,由于一般都使用zookeeper為注冊中心,所以這里得到的是ZookeeperRegistry;
final Registry registry = getRegistry(originInvoker);
final URL registedProviderUrl = getRegistedProviderUrl(originInvoker);
// 所以這里調用ZookeeperRegistry.register(URL)把需要發布的服務注冊到zookeeper中
registry.register(registedProviderUrl);
// 訂閱override數據
// FIXME 提供者訂閱時,會影響同一JVM即暴露服務,又引用同一服務的的場景,因為subscribed以服務名為緩存的key,導致訂閱信息覆蓋。
final URL overrideSubscribeUrl = getSubscribedOverrideUrl(registedProviderUrl);
final OverrideListener overrideSubscribeListener = new OverrideListener(overrideSubscribeUrl);
overrideListeners.put(overrideSubscribeUrl, overrideSubscribeListener);
registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener);
//保證每次export都返回一個新的exporter實例
return new Exporter<T>() {
public Invoker<T> getInvoker() {
return exporter.getInvoker();
}
public void unexport() {
try {
exporter.unexport();
} catch (Throwable t) {
logger.warn(t.getMessage(), t);
}
try {
registry.unregister(registedProviderUrl);
} catch (Throwable t) {
logger.warn(t.getMessage(), t);
}
try {
overrideListeners.remove(overrideSubscribeUrl);
registry.unsubscribe(overrideSubscribeUrl, overrideSubscribeListener);
} catch (Throwable t) {
logger.warn(t.getMessage(), t);
}
}
};
}
核心調用registry.register(registedProviderUrl)
- 調用AbstractRegistry.register(URL),把這次需要注冊的URL加到Set<URL> registered中,即本地緩存新的注冊URL;
- 在ZookeeperRegistry.doRegister(URL)調用AbstractZookeeperClient.create(),toUrlPath將URL形式的地址轉換成zookeeper路徑,最終在AbstractZookeeperClient中把需要發布的服務的URL保存到zookeeper中;(依賴第三方jar包:org.I0Itec.zkclient)
- ZookeeperRegistry.doRegister(url)注冊服務如果失敗:
- 如果開啟了啟動檢查check=true,那么直接拋出異常;
- 如果沒有開啟啟動檢查,那么將失敗的注冊請求記錄到失敗列表,定時重試;
核心調用registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener)
:
- 對發布的dubbo服務的這個url進行監聽, 當服務變化有時通知重新暴露服務, 以zookeeper為例,暴露服務會在zookeeper生成一個節點,當節點發生變化的時候會觸發overrideSubscribeListener的notify方法重新暴露服務;
重試機制
注冊服務失敗后,會將url加入重試url集合中,failedRegistered.add(url);
重試任務在FailbackRegistry中實現:
public FailbackRegistry(URL url) {
super(url);
int retryPeriod = url.getParameter(Constants.REGISTRY_RETRY_PERIOD_KEY, Constants.DEFAULT_REGISTRY_RETRY_PERIOD);
// retryExecutor是一個單獨的線程池Executors.newScheduledThreadPool(1, new NamedThreadFactory("DubboRegistryFailedRetryTimer", true)); 默認重試周期是5s;
this.retryFuture = retryExecutor.scheduleWithFixedDelay(new Runnable() {
public void run() {
// 檢測并連接注冊中心
try {
retry();
} catch (Throwable t) { // 防御性容錯
logger.error("Unexpected error occur at failed retry, cause: " + t.getMessage(), t);
}
}
}, retryPeriod, retryPeriod, TimeUnit.MILLISECONDS);
}
監聽機制
- 訂閱并設置監聽registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener);
--> FailbackRegistry.subscribe(URL url, NotifyListener listener)
--> ZookeeperRegistry.doSubscribe(final URL url, final NotifyListener listener),部分實現源碼如下:
ChildListener zkListener = listeners.get(listener);
if (zkListener == null) {
listeners.putIfAbsent(listener, new ChildListener() {
public void childChanged(String parentPath, List<String> currentChilds) {
ZookeeperRegistry.this.notify(url, listener, toUrlsWithEmpty(url, parentPath, currentChilds));
}
});
zkListener = listeners.get(listener);
}
zkClient.create(path, false);
// 在準備監聽的path上添加ChildListener
List<String> children = zkClient.addChildListener(path, zkListener);
- 服務有變化時notify:
FailbackRegistry.notify(URL url, NotifyListener listener, List<URL> urls)
以path:/dubbo/com.alibaba.dubbo.demo.TestService/providers
為例,Consumer會監聽這個zk路徑;
- 假設在consumer啟動時只有1個provider:dubbo://10.52.17.98:20888... ;當再啟動一個provider:dubbo://10.52.17.98:20886后,path路徑/dubbo/com.alibaba.dubbo.demo.TestService/providers就會變化,結果就觸發notify(URL url, NotifyListener listener, List<URL> urls),此時List<URL> urls中有兩個provider,即dubbo://10.52.17.98:20886...和dubbo://10.52.17.98:20888...
- 或者在consumer啟動時有2個provider:dubbo://10.52.17.98:20886... 和 dubbo://10.52.17.98:20888... ;當關閉一個provider:dubbo://10.52.17.98:20886后,path路徑/dubbo/com.alibaba.dubbo.demo.TestService/providers也會變化,結果就觸發notify(URL url, NotifyListener listener, List<URL> urls),此時List<URL> urls中只有1個provider,即dubbo://10.52.17.98:20888...;
--> AbstractRegistry.notify(URL url, NotifyListener listener, List<URL> urls)
--> RegistryDirectory.notify(List<URL> urls)
--> RegistryDirectory.refreshInvoker(List<URL> invokerUrls),這里調用toMethodInvokers(Map<String, Invoker<T>> invokersMap)
的實現比較重要,將invokers列表轉成與方法的映射關系,且每個方法對應的List<Invoker>
需要通過Collections.sort(methodInvokers, InvokerComparator.getComparator());
排序,排序后,還要將其轉為unmodifiable的map:
for (String method : new HashSet<String>(newMethodInvokerMap.keySet())) {
List<Invoker<T>> methodInvokers = newMethodInvokerMap.get(method);
Collections.sort(methodInvokers, InvokerComparator.getComparator());
newMethodInvokerMap.put(method, Collections.unmodifiableList(methodInvokers));
}
return Collections.unmodifiableMap(newMethodInvokerMap);
其中InvokerComparator 的定義如下,即直接根據url進行比較排序:
private static class InvokerComparator implements Comparator<Invoker<?>> {
private static final InvokerComparator comparator = new InvokerComparator();
public static InvokerComparator getComparator() {
return comparator;
}
private InvokerComparator() {}
public int compare(Invoker<?> o1, Invoker<?> o2) {
return o1.getUrl().toString().compareTo(o2.getUrl().toString());
}
}
最后刷新本地緩存的方法和List<Invoker>關系:
this.methodInvokerMap = multiGroup ? toMergeMethodInvokerMap(newMethodInvokerMap) : newMethodInvokerMap;
DubboProtocol.export()
dubbo協議發布服務會調用DubboProtocol.export(),
- 從Invoker中獲取URL:
URL url = invoker.getUrl(); - 根據URL得到key, 由暴露的服務接口+端口組成,例如com.alibaba.dubbo.demo.DemoService:20886
String key = serviceKey(url); - 構造DubboExporter存到Map中local cache化:
DubboExporter<T> exporter = new DubboExporter<T>(invoker, key, exporterMap);
exporterMap.put(key, exporter); - 調用DubboProtocol.openServer()開啟netty(默認)服務保持通信,并設置requestHandler處理consumer對provider的調用請求;
DubboProtocol.openServer():
key的值就是IP:Port,例如10.52.17.167:20886,根據key從serverMap中如果取不到ExchangeServer,表示還沒綁定服務端口,需要調用createServer(url)-->Exchangers.bind(url, requestHandler)-->Transporters.getTransporter().bind(url, handler)(dubbo支持mina,netty,grizzly,默認實現是netty) --> NettyTransporter.bind(URL, ChannelHandler) --> NettyServer.open();
NettyServer.open()源碼如下:
@Override
rotected void doOpen() throws Throwable {
NettyHelper.setNettyLoggerFactory();
ExecutorService boss = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerBoss", true));
ExecutorService worker = Executors.newCachedThreadPool(new NamedThreadFactory("NettyServerWorker", true));
ChannelFactory channelFactory = new NioServerSocketChannelFactory(boss, worker, getUrl().getPositiveParameter(Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS));
bootstrap = new ServerBootstrap(channelFactory);
final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
channels = nettyHandler.getChannels();
// https://issues.jboss.org/browse/NETTY-365
// https://issues.jboss.org/browse/NETTY-379
// final Timer timer = new HashedWheelTimer(new NamedThreadFactory("NettyIdleTimer", true));
bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() {
NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec() ,getUrl(), NettyServer.this);
ChannelPipeline pipeline = Channels.pipeline();
/*int idleTimeout = getIdleTimeout();
if (idleTimeout > 10000) {
pipeline.addLast("timer", new IdleStateHandler(timer, idleTimeout / 1000, 0, 0));
}*/
pipeline.addLast("decoder", adapter.getDecoder());
pipeline.addLast("encoder", adapter.getEncoder());
pipeline.addLast("handler", nettyHandler);
return pipeline;
}
});
// bind
channel = bootstrap.bind(getBindAddress());
}
開啟Netty服務幾個重要的地方
- 構造ChannelPipeline時指定了編碼&解碼,其中編碼為NettyCodecAdapter.getEncoder(),解碼為NettyCodecAdapter.getDncoder();
- 指定了handler為
final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);
處理請求;
附dubbo官方給出的暴露服務時序圖:
https://dubbo.gitbooks.io/dubbo-dev-book/design.html