上一篇文章,我們簡單的看了一下diamond server的功能及所做的事情,本篇我們看一下diamond client 所做的事情。
首先我們說明的是,應用系統想要獲得diamond中的動態配置,需要設置監聽器,當監聽到diamond的數據發生改變的時候,就會通知監聽器接收改變后的數據。所以應用程序需要做的就是設置diamond的監聽器。
Diamond.java
static public void addListener(String dataId, String group, ManagerListener listener)
{
defaultEnv.addListeners(dataId, group, Arrays.asList(listener));
}
在使用這個方法時,會首先執行靜態代碼塊的內容
static {
try {
initLog();
checkSnapshotValidity();
}
catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException(e);
}
}
static private void checkSnapshotValidity()
{
List<String> localServerlist = LocalConfigInfoProcessor.readServerlist(defaultEnv);
List<String> apacheServerlist = defaultEnv.getServerUrls();
log.info("[apache-urls] " + apacheServerlist);
log.info("[cache-urls] " + localServerlist);
boolean isNotChange = apacheServerlist.equals(localServerlist); if (isNotChange) {
log.info(LogConstants.PREFFIX + "environment ok.");
} else {
log.warn(LogConstants.PREFFIX + "environment changed. clear cache.");
LocalConfigInfoProcessor.cleanAllSnapshot();
LocalConfigInfoProcessor.saveServerlist(defaultEnv, apacheServerlist);
}
}
在這里又靜態導入了defaultEnv,所以導致了defaultEnv的初始化。
static public final DiamondEnv defaultEnv = new DiamondEnv(new ServerListManager());
DiamondEnv 的初始化需要ServerListManager的實例。
protected DiamondEnv(ServerListManager serverListMgr) {
{
initServerManager(serverListMgr); //初始化ServerListManager
cacheMap = new AtomicReference<Map<String, CacheData>>(new HashMap<String, CacheData>()); //初始化放CacheData的容器
worker = new ClientWorker(this); //初始化ClientWorker
}
看一下如何初始化的ServerListManager
public void initServerManager(ServerListManager _serverMgr)
{
_serverMgr.setEnv(this); //為ServerListManager 設置env
serverMgr = _serverMgr; //為DiamondEnv設置ServerListManager
serverMgr.start(); //ServerListManager 啟動
agent = new ServerHttpAgent(serverMgr); //創建一個http的Client,用于同diamond server進行http的通信。
}
關鍵是serverMgr.start();
public synchronized void start()
{
if (isStarted || isFixed) { return; }
GetServerListTask getServersTask = new GetServerListTask(DiamondConfigure.singleton.getDiamondHttpUrl());
while (serverUrls.isEmpty()) {
getServersTask.run();
try {
Thread.sleep(1000L); }
catch (Exception e) {}
}
TimerService.scheduleWithFixedDelay(getServersTask, 0L, 30L,TimeUnit.SECONDS);
isStarted = true;
}
這里有一個定時的線程池每30s執行這個任務,這個任務就是去diamond服務器取服務器的ip列表。我們發現有一個DiamondConfigure.singleton.getDiamondHttpUrl(),所以我們需要在設置監聽前要設置這個diamond的http url。這個很重要,要不然client起不來。
根據服務器上獲取的ip列表與本地的進行比較,不一致的話
LocalConfigInfoProcessor.saveServerlist(name, serverUrls);
更新本地的ip列表,否則直接return。這是initServerManager所做的事情。我們再來看一下
worker = new ClientWorker(this);
這里面做的事情就比較多了。
ClientWorker(final DiamondEnv env) {
this.env = env; //ClientWorker中的env進行賦值
executor = Executors.newScheduledThreadPool(1, new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setName("com.taobao.diamond.client.Worker."+ env.serverMgr.name);
t.setDaemon(true);
return t;
}
});
executor.scheduleWithFixedDelay(new Runnable() {
public void run() {
try {
checkLocalConfigInfo();
checkServerConfigInfo();
} catch (Throwable e) {
log.error("[sub-error-rotate] rotate check error", e);
}
}
}, 1L, 1L, TimeUnit.MILLISECONDS);
}
創建了一個定時的線程池,每隔1毫秒,進行checkLocalConfigInfo和checkServerConfigInfo,checkLocalConfigInfo是本地容災文件的處理,
我們著重看一下checkServerConfigInfo。
static public void checkServerConfigInfo(DiamondEnv env) {
for (String groupKey : checkUpdateDataIds(env)) {
String dataId = GroupKey.parseKey(groupKey)[0];
String group = GroupKey.parseKey(groupKey)[1];
try {
String content = getServerConfig(env, dataId, group, 3000L);
CacheData cache = env.getCache(dataId, group);
cache.setContent(content);
log.info("[data-received] dataId=" + dataId + ", group=" + group + ", md5="
+ cache.getMd5() + ", content=" + ContentUtils.truncateContent(content));
} catch (IOException ioe) {
log.warn(ioe.toString(), ioe);
}
}
checkListenerMd5(env);
}
checkUpdateDataIds 是將本地緩存的CacheData中的group,dataId和md5作為字符串拼接起來,作為參數,post到服務器上進行處理,根據group和dataId,查詢到md5與傳遞過來的MD5進行比對,如果不一致,將不一致的group和dataId回傳回來。
/**
* 從DiamondServer獲取值變化了的DataID列表。返回的對象里只有dataId和group是有效的。 保證不返回NULL。
*/
static List<String> checkUpdateDataIds(DiamondEnv env) {
/*
if (MockServer.isTestMode()) {
List<String> updateList = new ArrayList<String>();
//與DiamondEnv的模擬數據源比較,得出數據變化列表
for(CacheData cacheData : env.getAllCacheDataSnapshot()){
CacheData mockServerData = env.getMockCache(cacheData.dataId, cacheData.group);
if(mockServerData == null || !mockServerData.getMd5().equals(cacheData.getMd5()))
updateList.add(GroupKey.getKey(cacheData.dataId, cacheData.group));
}
return updateList;
} */
if (MockServer.isTestMode()) {
// 避免 test mode cpu% 過高
try {
Thread.sleep(3000l);
} catch (InterruptedException e) {}
List<String> updateList = new ArrayList<String>();
for(CacheData cacheData : env.getAllCacheDataSnapshot()){
if(!CacheData.getMd5String(MockServer.getConfigInfo(cacheData.dataId, cacheData.group, env))
.equals(cacheData.getMd5())) {
updateList.add(GroupKey.getKey(cacheData.dataId, cacheData.group));
}
}
return updateList;
}
String probeUpdateString = getProbeUpdateString(env);
List<String> params = Arrays.asList(Constants.PROBE_MODIFY_REQUEST, probeUpdateString);
long timeout = TimeUnit.SECONDS.toMillis(30L);
List<String> headers = Arrays.asList("longPullingTimeout", "" + timeout);
if (StringUtils.isBlank(probeUpdateString)) {
return Collections.emptyList();
}
try {
HttpResult result = env.agent.httpPost("/config.co", headers, params, Constants.ENCODE,
timeout);
if (HttpURLConnection.HTTP_OK == result.code) {
return parseUpdateDataIdResponse(result.content);
} else {
log.warn("[check-update] get changed dataId error, HTTP State: " + result.code);
}
} catch (IOException e) {
log.warn("[check-update] get changed dataId exception, " + e.toString());
}
return Collections.emptyList();
}
那么回傳回來的group和dataId說明是有變化的,隨后再請求服務器中的數據(最新的數據),放入本地的snapshot文件再放入放入CacheData
String content = getServerConfig(env, dataId, group, 3000L);
CacheData cache = env.getCache(dataId, group);
cache.setContent(content);
/**
* 對于404響應碼,返回NULL.
*
* @throws IOException
*/
static String getServerConfig(DiamondEnv env, String dataId, String group, long readTimeout)
throws IOException {
if (StringUtils.isBlank(group)) {
group = Constants.DEFAULT_GROUP;
}
if (MockServer.isTestMode()) {
return MockServer.getConfigInfo(dataId, group, env);
}
HttpResult result = null;
try {
List<String> params = Arrays.asList("dataId", dataId, "group", group);
result = env.agent.httpGet("/config.co", null, params, Constants.ENCODE, readTimeout);
} catch (IOException e) {
log.warn("[sub-server] get server config exception, dataId=" + dataId + ", group="
+ group + ", " + e.toString());
throw e;
}
switch (result.code) {
case HttpURLConnection.HTTP_OK:
// if (env == defaultEnv) {
LocalConfigInfoProcessor.saveSnapshot(env, dataId, group, result.content);
// }
return result.content;
case HttpURLConnection.HTTP_NOT_FOUND:
// if (env == defaultEnv) {
LocalConfigInfoProcessor.saveSnapshot(env, dataId, group, null);
// }
return null;
case HttpURLConnection.HTTP_CONFLICT: {
log.warn("[sub-server-error] data being modified");
throw new IOException("data being modified");
}
default: {
log.warn("[sub-server-error] error code " + result.code);
throw new IOException("http code: " + result.code);
}
}
}
CacheData處理完了,在處理監聽器。
static void checkListenerMd5(DiamondEnv env) {
for (CacheData cacheData : env.getAllCacheDataSnapshot()) {
cacheData.checkListenerMd5();
}
}
void checkListenerMd5() {
for (ManagerListenerWrap wrap : listeners) {
if (!md5.equals(wrap.lastCallMd5)) {
safeNotifyListener(dataId, group, content, md5, wrap);
}
}
static void safeNotifyListener(final String dataId, final String group, final String content,
final String md5, ManagerListenerWrap listenerWrap) {
final ManagerListener listener = listenerWrap.listener;
listenerWrap.lastCallMd5 = md5;
Runnable job = new Runnable() {
public void run() {
try {
listener.receiveConfigInfo(content);
log.info("[notify-ok] " + dataId + ", " + group + ", md5=" + md5
+ ", listener=" + listener);
} catch (Throwable t) {
log.error("[notify-error] " + dataId + ", " + group + ", md5=" + md5
+ ", listener=" + listener.toString(), t);
}
}
};
try {
if (null != listener.getExecutor()) {
listener.getExecutor().execute(job);
} else {
job.run();
}
} catch (Throwable t) {
log.error("[notify-error] " + dataId + ", " + group + ", md5=" + md5 + ", listener="
+ listener.toString(), t);
}
}
每次調用safeNotifyListener時都會開啟一個線程(有線程池用線程池),將改變后的內容傳遞給監聽器進行處理。