registry 這個模塊主要是 註冊中心
註冊中心+監聽者模式+工廠模式+模板模式 下面是核心的5個方法
RegistryFactory
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.registry;
import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.extension.Adaptive;
import com.alibaba.dubbo.common.extension.SPI;
/**
* RegistryFactory. (SPI, Singleton, ThreadSafe)
*
* @see com.alibaba.dubbo.registry.support.AbstractRegistryFactory
* @author william.liangf
*/
@SPI("dubbo")
public interface RegistryFactory {
/**
* 連接註冊中心.
*
* 連接註冊中心需處理契約:<br>
* 1. 當設置check=false時表示不檢查連接,否則在連接不上時拋出異常。<br>
* 2. 支持URL上的username:password權限認證。<br>
* 3. 支持backup=10.20.153.10備選註冊中心集羣地址。<br>
* 4. 支持file=registry.cache本地磁盤文件緩存。<br>
* 5. 支持timeout=1000請求超時設置。<br>
* 6. 支持session=60000會話超時或過期設置。<br>
*
* @param url 註冊中心地址,不允許爲空
* @return 註冊中心引用,總不返回空
*/
@Adaptive({"protocol"})
Registry getRegistry(URL url);
}
RegistryService
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.registry;
import java.util.List;
import com.alibaba.dubbo.common.URL;
/**
* RegistryService. (SPI, Prototype, ThreadSafe)
*
* @see com.alibaba.dubbo.registry.Registry
* @see com.alibaba.dubbo.registry.RegistryFactory#getRegistry(URL)
* @author william.liangf
*/
public interface RegistryService {
/**
* 註冊數據,比如:提供者地址,消費者地址,路由規則,覆蓋規則,等數據。
*
* 註冊需處理契約:<br>
* 1. 當URL設置了check=false時,註冊失敗後不報錯,在後臺定時重試,否則拋出異常。<br>
* 2. 當URL設置了dynamic=false參數,則需持久存儲,否則,當註冊者出現斷電等情況異常退出時,需自動刪除。<br>
* 3. 當URL設置了category=routers時,表示分類存儲,缺省類別爲providers,可按分類部分通知數據。<br>
* 4. 當註冊中心重啓,網絡抖動,不能丟失數據,包括斷線自動刪除數據。<br>
* 5. 允許URI相同但參數不同的URL並存,不能覆蓋。<br>
*
* @param url 註冊信息,不允許爲空,如:dubbo://10.20.153.10/com.alibaba.foo.BarService?version=1.0.0&application=kylin
*/
void register(URL url);
/**
* 取消註冊.
*
* 取消註冊需處理契約:<br>
* 1. 如果是dynamic=false的持久存儲數據,找不到註冊數據,則拋IllegalStateException,否則忽略。<br>
* 2. 按全URL匹配取消註冊。<br>
*
* @param url 註冊信息,不允許爲空,如:dubbo://10.20.153.10/com.alibaba.foo.BarService?version=1.0.0&application=kylin
*/
void unregister(URL url);
/**
* 訂閱符合條件的已註冊數據,當有註冊數據變更時自動推送.
*
* 訂閱需處理契約:<br>
* 1. 當URL設置了check=false時,訂閱失敗後不報錯,在後臺定時重試。<br>
* 2. 當URL設置了category=routers,只通知指定分類的數據,多個分類用逗號分隔,並允許星號通配,表示訂閱所有分類數據。<br>
* 3. 允許以interface,group,version,classifier作爲條件查詢,如:interface=com.alibaba.foo.BarService&version=1.0.0<br>
* 4. 並且查詢條件允許星號通配,訂閱所有接口的所有分組的所有版本,或:interface=*&group=*&version=*&classifier=*<br>
* 5. 當註冊中心重啓,網絡抖動,需自動恢復訂閱請求。<br>
* 6. 允許URI相同但參數不同的URL並存,不能覆蓋。<br>
* 7. 必須阻塞訂閱過程,等第一次通知完後再返回。<br>
*
* @param url 訂閱條件,不允許爲空,如:consumer://10.20.153.10/com.alibaba.foo.BarService?version=1.0.0&application=kylin
* @param listener 變更事件監聽器,不允許爲空
*/
void subscribe(URL url, NotifyListener listener);
/**
* 取消訂閱.
*
* 取消訂閱需處理契約:<br>
* 1. 如果沒有訂閱,直接忽略。<br>
* 2. 按全URL匹配取消訂閱。<br>
*
* @param url 訂閱條件,不允許爲空,如:consumer://10.20.153.10/com.alibaba.foo.BarService?version=1.0.0&application=kylin
* @param listener 變更事件監聽器,不允許爲空
*/
void unsubscribe(URL url, NotifyListener listener);
/**
* 查詢符合條件的已註冊數據,與訂閱的推模式相對應,這裏爲拉模式,只返回一次結果。
*
* @see com.alibaba.dubbo.registry.NotifyListener#notify(List)
* @param url 查詢條件,不允許爲空,如:consumer://10.20.153.10/com.alibaba.foo.BarService?version=1.0.0&application=kylin
* @return 已註冊信息列表,可能爲空,含義同{@link com.alibaba.dubbo.registry.NotifyListener#notify(List<URL>)}的參數。
*/
List<URL> lookup(URL url);
}
NotifyListener
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.dubbo.registry;
import java.util.List;
import com.alibaba.dubbo.common.URL;
/**
* NotifyListener. (API, Prototype, ThreadSafe)
*
* @see com.alibaba.dubbo.registry.RegistryService#subscribe(URL, NotifyListener)
* @author william.liangf
*/
public interface NotifyListener {
/**
* 當收到服務變更通知時觸發。
*
* 通知需處理契約:<br>
* 1. 總是以服務接口和數據類型爲維度全量通知,即不會通知一個服務的同類型的部分數據,用戶不需要對比上一次通知結果。<br>
* 2. 訂閱時的第一次通知,必須是一個服務的所有類型數據的全量通知。<br>
* 3. 中途變更時,允許不同類型的數據分開通知,比如:providers, consumers, routers, overrides,允許只通知其中一種類型,但該類型的數據必須是全量的,不是增量的。<br>
* 4. 如果一種類型的數據爲空,需通知一個empty協議並帶category參數的標識性URL數據。<br>
* 5. 通知者(即註冊中心實現)需保證通知的順序,比如:單線程推送,隊列串行化,帶版本對比。<br>
*
* @param urls 已註冊信息列表,總不爲空,含義同{@link com.alibaba.dubbo.registry.RegistryService#lookup(URL)}的返回值。
*/
void notify(List<URL> urls);
}
FailbackRegistry
// ==== 模板方法 ====
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);
ZookeeperRegistry
查詢url,走的zk path
public List<URL> lookup(URL url) {
if (url == null) {
throw new IllegalArgumentException("lookup url == null");
}
try {
List<String> providers = new ArrayList<String>();
for (String path : toCategoriesPath(url)) {
List<String> children = zkClient.getChildren(path);
if (children != null) {
providers.addAll(children);
}
}
return toUrlsWithoutEmpty(url, providers);
} catch (Throwable e) {
throw new RpcException("Failed to lookup " + url + " from zookeeper " + getUrl() + ", cause: " + e.getMessage(), e);
}
}