首先DUBBO本質(zhì)上是一款RPC框架,不可缺少的2個角色:服務(wù)提供者和服務(wù)消費者。
創(chuàng)新互聯(lián)公司是專業(yè)的興化網(wǎng)站建設(shè)公司,興化接單;提供成都做網(wǎng)站、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行興化網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!已經(jīng)剖析了服務(wù)端暴露端口過程。本文簡單說下注冊中心。
1.注冊中心是什么玩意
這是官網(wǎng)提供的圖例
通過圖例,可以看出消費者和提供者并不是直接通信的,中間有個第三者,就是注冊中心。這種結(jié)構(gòu),可以實現(xiàn)消費者和提供者兩者的依賴,和參數(shù)信息的集群化。所以這帶來了幾個問題。
服務(wù)注冊
服務(wù)發(fā)現(xiàn)
服務(wù)訂閱
服務(wù)通知
2. 服務(wù)暴露及服務(wù)注冊過程
《Dubbo點滴(4)之暴露服務(wù)解析》已經(jīng)剖析了dubbo協(xié)議具體打開網(wǎng)絡(luò)端口過程。本節(jié)內(nèi)容會隱去這部分內(nèi)容。因為一個完整的服務(wù)暴露,主要涉及2部分內(nèi)容,1)打開端口等待消費者連接;2)將服務(wù)信息登記到注冊中心,以告知消費者可以連接了。
有3點需要說明:
1)首先,根據(jù)條件判斷會暴露一個injvm本地服務(wù)(step 6);
InjvmProtocol協(xié)議完成,主要供同一JVM種的消費者調(diào)用,提供RPC效率。
2) 為服務(wù)暴露一個dubbo服務(wù)(step 12),一般為DubboProtocol完成
3)step 12提供的的服務(wù),注冊到注冊中心(step 13-step 23)。這一步是本文的剖析重點。
3.認識注冊中心
該圖是DUBBO的總體結(jié)構(gòu)圖。重點停留在Resistry層。比較重要的是幾個組件
ZookeeperRegistry :負責(zé)與zookeeper進行交互
RegistryProtocol :從注冊中心獲取可用服務(wù),或者將服務(wù)注冊到zookeeper,然后提供服務(wù)或者提供調(diào)用代理。
RegistryDirectory :維護著所有可用的遠程Invoker或者本地的Invoker。這個類實現(xiàn)了NotifyListner。
NotifyListener :負責(zé)RegistryDirectory和ZookeeperRegistry的通信。
FailbackRegistry:繼承自Registry,實現(xiàn)了失敗重試機制。
4. 注冊中心數(shù)據(jù)模型
流程說明:
服務(wù)提供者啟動時
向/dubbo/com.foo.BarService/providers目錄下寫入自己的URL地址。
服務(wù)消費者啟動時
訂閱/dubbo/com.foo.BarService/providers目錄下的提供者URL地址。
并向/dubbo/com.foo.BarService/consumers目錄下寫入自己的URL地址。
監(jiān)控中心啟動時
訂閱/dubbo/com.foo.BarService目錄下的所有提供者和消費者URL地址。
4.Registry 結(jié)構(gòu)樹
ZookeeperRegistry是常見的注冊中心實現(xiàn)方案,由ZookeeperRegistryFactory負責(zé)構(gòu)造。
AbstractRegistry這個類主要存儲的是已經(jīng)注冊的服務(wù)接口,已經(jīng)訂閱的服務(wù)接口和已經(jīng)收到通知的接口的URL,不能直接調(diào)用。
public abstract class AbstractRegistry implements Registry { // 本地磁盤緩存,其中特殊的key值.registies記錄注冊中心列表,其它均為notified服務(wù)提供者列表 private final Properties properties = new Properties(); // 文件緩存定時寫入 private final ExecutorService registryCacheExecutor = Executors.newFixedThreadPool(1, new NamedThreadFactory("DubboSaveRegistryCache", true)); //是否是同步保存文件 private final boolean syncSaveFile ; private final AtomicLong lastCacheChanged = new AtomicLong(); private final Set<URL> registered = new ConcurrentHashSet<URL>(); private final ConcurrentMap<URL, Set<NotifyListener>> subscribed = new ConcurrentHashMap<URL, Set<NotifyListener>>(); private final ConcurrentMap<URL, Map<String, List<URL>>> notified = new ConcurrentHashMap<URL, Map<String, List<URL>>>(); ... }
FailbackRegistry 繼承自AbstractRegistry ,實現(xiàn)了失敗重試機制。
public abstract class FailbackRegistry extends AbstractRegistry { // 定時任務(wù)執(zhí)行器 private final ScheduledExecutorService retryExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("DubboRegistryFailedRetryTimer", true)); // 失敗重試定時器,定時檢查是否有請求失敗,如有,無限次重試 private final ScheduledFuture<?> retryFuture; private final Set<URL> failedRegistered = new ConcurrentHashSet<URL>(); private final Set<URL> failedUnregistered = new ConcurrentHashSet<URL>(); private final ConcurrentMap<URL, Set<NotifyListener>> failedSubscribed = new ConcurrentHashMap<URL, Set<NotifyListener>>(); private final ConcurrentMap<URL, Set<NotifyListener>> failedUnsubscribed = new ConcurrentHashMap<URL, Set<NotifyListener>>(); private final ConcurrentMap<URL, Map<NotifyListener, List<URL>>> failedNotified = new ConcurrentHashMap<URL, Map<NotifyListener, List<URL>>>(); ... }
5.服務(wù)提供者啟動的注冊過程
服務(wù)提供者啟動時 向/dubbo/com.foo.BarService/providers目錄下寫入自己的URL地址。接下來,找尋下代碼執(zhí)行路徑。RegistryProtocol 作為注冊中心的核心組件,作為代碼的入口,還要明確一點,這是個注冊登記過程。
//入口 public class RegistryProtocol implements Protocol { final Registry registry = getRegistry(originInvoker); final URL registedProviderUrl = getRegistedProviderUrl(originInvoker); registry.register(registedProviderUrl); } public abstract class AbstractRegistry implements Registry { public void register(URL url) { if (url == null) { throw new IllegalArgumentException("register url == null"); } if (logger.isInfoEnabled()){ logger.info("Register: " + url); } registered.add(url); } ... } public abstract class FailbackRegistry extends AbstractRegistry { public void register(URL url) { super.register(url); failedRegistered.remove(url); failedUnregistered.remove(url); try { // 向服務(wù)器端發(fā)送注冊請求 doRegister(url); } catch (Exception e) { ... } // 將失敗的注冊請求記錄到失敗列表,定時重試 failedRegistered.add(url); } protected abstract void doRegister(URL url); protected abstract void doUnregister(URL url); protected abstract void doSubscribe(URL url, NotifyListener listener); protected abstract void doUnsubscribe(URL url, NotifyListener listener); ... } public class ZookeeperRegistry extends FailbackRegistry { public ZookeeperRegistry(URL url, ZookeeperTransporter zookeeperTransporter) { super(url); //如果provider的url是“0.0.0.0”或者在參數(shù)中帶anyHost=true則拋出異常注冊地址不存在 if (url.isAnyHost()) { throw new IllegalStateException("registry address == null"); } //服務(wù)分組(默認“dubbo”) String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT); if (! group.startsWith(Constants.PATH_SEPARATOR)) { group = Constants.PATH_SEPARATOR + group; } //服務(wù)分組根地址 this.root = group; zkClient = zookeeperTransporter.connect(url); //添加狀態(tài)監(jiān)聽器 zkClient.addStateListener(new StateListener() { public void stateChanged(int state) { if (state == RECONNECTED) { try { recover(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } }); } protected void doRegister(URL url) { try { //連接注冊中心注冊,在zk上創(chuàng)建節(jié)點 zkClient.create(toUrlPath(url), url.getParameter(Constants.DYNAMIC_KEY, true)); } catch (Throwable e) { throw new RpcException("Failed to register " + url + " to zookeeper " + getUrl() + ", cause: " + e.getMessage(), e); } } }
6. 注冊中心關(guān)系類圖
重要概念:
ZookeeperRegistry :負責(zé)與zookeeper進行交互
RegistryProtocol :從注冊中心獲取可用服務(wù),或者將服務(wù)注冊到zookeeper,然后提供服務(wù)或者提供調(diào)用代理。
RegistryDirectory :維護著所有可用的遠程Invoker或者本地的Invoker。這個類實現(xiàn)了NotifyListner。
NotifyListener :負責(zé)RegistryDirectory和ZookeeperRegistry的通信。
FailbackRegistry:繼承自Registry,實現(xiàn)了失敗重試機制。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
本文名稱:Dubbo點滴(5)之服務(wù)注冊中心-創(chuàng)新互聯(lián)
轉(zhuǎn)載源于:http://aaarwkj.com/article14/cdhjde.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、網(wǎng)站收錄、品牌網(wǎng)站設(shè)計、動態(tài)網(wǎng)站、外貿(mào)建站、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)