读取数据

DiamondManager manager = new DefaultDiamondManager("DEFAULT_GROUP", "zml", new ManagerListener() {
public void receiveConfigInfo(String configInfo) {
System.out.println("changed config: " + configInfo);
} public Executor getExecutor() {
return null;
}
}, "127.0.0.1");
//设置diamond-server服务的端口
manager.getDiamondConfigure().setPort(8080);
String availableConfigureInfomation = manager.getAvailableConfigureInfomation(5000);
System.out.println("start config: " + availableConfigureInfomation);
}

1 初始化DefaultDiamondManager 并启动diamondSubscriber

 public DefaultDiamondManager(String group, String dataId, ManagerListener managerListener,String diamondServer) {
this.dataId = dataId;
this.group = group; diamondSubscriber = DiamondClientFactory.getSingletonDiamondSubscriber(); this.managerListeners.add(managerListener);
((DefaultSubscriberListener) diamondSubscriber.getSubscriberListener()).addManagerListeners(this.dataId,
this.group, this.managerListeners);
String s[] = diamondServer.split(",");
if (s != null && s.length > 0) {
for (String o : s) {
if (o != null && !o.trim().equals(""))
diamondSubscriber.getDiamondConfigure().getDomainNameList().add(o.trim());
}
}
diamondSubscriber.addDataId(this.dataId, this.group);
diamondSubscriber.start();
}

1.1 diamondSubscriber.start();

 /**
* 启动DiamondSubscriber:<br>
* 1.阻塞主动获取所有的DataId配置信息<br>
* 2.启动定时线程定时获取所有的DataId配置信息<br>
*/
public synchronized void start() {
if (isRun) {
return;
} if (null == scheduledExecutor || scheduledExecutor.isTerminated()) {
scheduledExecutor = Executors.newSingleThreadScheduledExecutor();
} localConfigInfoProcessor.start(this.diamondConfigure.getFilePath() + "/" + DATA_DIR);//创建根目录并监控根目录C:\Users\zhumenglong/diamond/data
serverAddressProcessor = new ServerAddressProcessor(this.diamondConfigure, this.scheduledExecutor);
serverAddressProcessor.start(); this.snapshotConfigInfoProcessor =
new SnapshotConfigInfoProcessor(this.diamondConfigure.getFilePath() + "/" + SNAPSHOT_DIR);
// 设置domainNamePos值
randomDomainNamePos();
initHttpClient(); // 初始化完毕
isRun = true; if (log.isInfoEnabled()) {
log.info("当前使用的域名有:" + this.diamondConfigure.getDomainNameList());
} if (MockServer.isTestMode()) {
bFirstCheck = false;
}
else {
// 设置轮询间隔时间
this.diamondConfigure.setPollingIntervalTime(Constants.POLLING_INTERVAL_TIME);
}
// 轮询
rotateCheckConfigInfo(); addShutdownHook();
}

1.1.1 http://www.cnblogs.com/clds/p/5997195.html

 localConfigInfoProcessor.start(this.diamondConfigure.getFilePath() + "/" + DATA_DIR);//创建根目录并监控根目录
目录获取方式 System.getProperty("user.home") + "/diamond"; C:\Users\zhumenglong/diamond/data //监控代码分析见 //核心思路起一个线程定时监听文件夹及子文件,判断文件是否增加删除//修改,
//保存到 localConfigInfoProcessor 的existFiles Map<String/* filePath */, Long/* version */>

1.1.2 serverAddressProcessor.start(); http://www.cnblogs.com/clds/p/6001396.html

 public synchronized void start() {
if (isRun) {
return;
}
isRun = true;
initHttpClient();//初始化HttpClient
if (this.diamondConfigure.isLocalFirst()) {
acquireServerAddressFromLocal();//如果是本地,从本地获取服务器列表
}
else {
synAcquireServerAddress();
//如果不再异步每隔一段通过域名时间去取diamondIpList,注释掉下面这行
//asynAcquireServerAddress();
asynAcquireServerAddress();
} }

1.1.3 randomDomainNamePos(); 随机获取服务器地址

   private void randomDomainNamePos() {
// 随机化起始服务器地址
Random rand = new Random();
List<String> domainList = this.diamondConfigure.getDomainNameList();
if (!domainList.isEmpty()) {
this.domainNamePos.set(rand.nextInt(domainList.size()));
}
}

1.1.4 initHttpClient(); 初始化 httpClient 多线程

1.1.5 rotateCheckConfigInfo(); http://www.cnblogs.com/clds/p/6000771.html

1.1.6 addShutdownHook(); jvm关闭钩子

2 //设置diamond-server服务的端口

 manager.getDiamondConfigure().setPort(8080);

3 获取数据String availableConfigureInfomation = manager.getAvailableConfigureInfomation(5000);

  public String getAvailableConfigureInfomation(String dataId, String group, long timeout) {
// 尝试先从本地和网络获取配置信息
try {
String result = getConfigureInfomation(dataId, group, timeout);
if (result != null && result.length() > 0) {
return result;
}
}
catch (Throwable t) {
log.error(t.getMessage(), t);
} // 测试模式不使用本地dump
if (MockServer.isTestMode()) {
return null;
}
return getSnapshotConfiginfomation(dataId, group);//本地快照获取
}
05-23 16:18