4.dubbo源碼-發布服務

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()

ServiceConfigexport()方法部分源碼如下,如果<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()的作用:

  1. 檢查<dubbo:service>中是否配置了interface, 如果為空,那么拋出異常:
  2. 檢查xml配置中申明的interface的類型是否是java interface類型(interfaceClass.isInterface())
  3. 檢查xml配置中interface和ref是否匹配(interfaceClass.isInstance(ref))
  4. application&registry&protocol等有效性檢查;
  5. 有效性檢查通過后,調用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判斷服務的發布范圍:

  1. 如果配置scope=none, 那么不需要發布這個dubbo服務;
  2. 沒有配置scope=noe且配置的scope!=remote, 那么本地暴露 這個dubbo服務;
  3. 沒有配置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);
        }
    }
}

由這段源碼可知,如果發布dubbo服務到zookeeper上,invoker.getUrl()的值為:
registry://10.0.53.87:2188/com.alibaba.dubbo.registry.RegistryService?application=dubbo-test&dubbo=2.0.0&export=dubbo%3A%2F%2F10.52.16.218%3A20886%2Fcom.alibaba.dubbo.demo.DemoService%3Fanyhost%3Dtrue%26application%3Ddubbo-test%26dubbo%3D2.0.0%26interface%3Dcom.alibaba.dubbo.demo.DemoService%26loadbalance%3Droundrobin%26methods%3DsayHello%26owner%3Dafei%26pid%3D2380%26side%3Dprovider%26timestamp%3D1509953019382&owner=afei&pid=2380&registry=zookeeper&timestamp=1509953019349

且會有兩行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)

  1. 調用AbstractRegistry.register(URL),把這次需要注冊的URL加到Set<URL> registered中,即本地緩存新的注冊URL;
  2. 在ZookeeperRegistry.doRegister(URL)調用AbstractZookeeperClient.create(),toUrlPath將URL形式的地址轉換成zookeeper路徑,最終在AbstractZookeeperClient中把需要發布的服務的URL保存到zookeeper中;(依賴第三方jar包:org.I0Itec.zkclient)
  3. ZookeeperRegistry.doRegister(url)注冊服務如果失敗:
  • 如果開啟了啟動檢查check=true,那么直接拋出異常;
  • 如果沒有開啟啟動檢查,那么將失敗的注冊請求記錄到失敗列表,定時重試;

核心調用registry.subscribe(overrideSubscribeUrl, overrideSubscribeListener):

  1. 對發布的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);
}

監聽機制

  1. 訂閱并設置監聽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);
  1. 服務有變化時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(),

  1. 從Invoker中獲取URL:
    URL url = invoker.getUrl();
  2. 根據URL得到key, 由暴露的服務接口+端口組成,例如com.alibaba.dubbo.demo.DemoService:20886
    String key = serviceKey(url);
  3. 構造DubboExporter存到Map中local cache化:
    DubboExporter<T> exporter = new DubboExporter<T>(invoker, key, exporterMap);
    exporterMap.put(key, exporter);
  4. 調用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服務幾個重要的地方

  1. 構造ChannelPipeline時指定了編碼&解碼,其中編碼為NettyCodecAdapter.getEncoder(),解碼為NettyCodecAdapter.getDncoder();
  2. 指定了handler為final NettyHandler nettyHandler = new NettyHandler(getUrl(), this);處理請求;

附dubbo官方給出的暴露服務時序圖:
https://dubbo.gitbooks.io/dubbo-dev-book/design.html

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • dubbo暴露服務有兩種情況,一種是設置了延遲暴露(比如delay="5000"),另外一種是沒有設置延遲暴露或者...
    加大裝益達閱讀 21,334評論 5 36
  • 0 準備 安裝注冊中心:Zookeeper、Dubbox自帶的dubbo-registry-simple;安裝Du...
    七寸知架構閱讀 14,026評論 0 88
  • Dubbo是什么 Dubbo是Alibaba開源的分布式服務框架,它最大的特點是按照分層的方式來架構,使用這種方式...
    Coselding閱讀 17,287評論 3 196
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 喜氣洋洋的年, 一轉眼,就過去了。 雖然是國家規定的節假日,休息過后卻覺得無比的勞累。 與放假前對于節日的期盼形成...
    炅竹春筍閱讀 237評論 0 0