读取数据

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);//本地快照获取
}

diamond源码阅读-diamond-client的更多相关文章

  1. diamond源码阅读-获取服务器列表

    serverAddressProcessor public synchronized void start() { if (isRun) { return; } isRun = true; initH ...

  2. diamond源码阅读-diamond-server

    diamond-server 1 增加一条数据 /diamond-server/admin.do?method=postConfig 1.1 调用 this.configService.addConf ...

  3. diamond源码阅读-循环探测配置信息是否变化rotateCheckConfigInfo

    rotateCheckConfigInfo 这是一个定时任务,循环调用 /** * 循环探测配置信息是否变化,如果变化,则再次向DiamondServer请求获取对应的配置信息 */ private ...

  4. diamond源码阅读-目录监控

    PathNode(Path)StandardWatchEventKind(WatchEvent)Watchable(WatchKey WatchService WatchEvent)WatchKey( ...

  5. ZooKeeper源码阅读——client(二)

    原创技术文章,转载请注明:转自http://newliferen.github.io/ 如何连接ZooKeeper集群   要想了解ZooKeeper客户端实现原理,首先需要关注一下客户端的使用方式, ...

  6. 【原】AFNetworking源码阅读(一)

    [原]AFNetworking源码阅读(一) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 AFNetworking版本:3.0.4 由于我平常并没有经常使用AFNetw ...

  7. 如何阅读Java源码 阅读java的真实体会

    刚才在论坛不经意间,看到有关源码阅读的帖子.回想自己前几年,阅读源码那种兴奋和成就感(1),不禁又有一种激动. 源码阅读,我觉得最核心有三点:技术基础+强烈的求知欲+耐心.   说到技术基础,我打个比 ...

  8. CI框架源码阅读笔记3 全局函数Common.php

    从本篇开始,将深入CI框架的内部,一步步去探索这个框架的实现.结构和设计. Common.php文件定义了一系列的全局函数(一般来说,全局函数具有最高的加载优先权,因此大多数的框架中BootStrap ...

  9. 转-OpenJDK源码阅读导航跟编译

    OpenJDK源码阅读导航 OpenJDK源码阅读导航 博客分类: Virtual Machine HotSpot VM Java OpenJDK openjdk 这是链接帖.主体内容都在各链接中.  ...

随机推荐

  1. npm使用常见问题及注意事项

    1.npm.cnpm.yarn不要混用,一个项目只使用一个 2.NPM problem: npm ERR! extraneous 表明安装了很多不需要的三方包 使用命令 npm prune删除无用的包 ...

  2. The method Inflate() in android

    Inflate() method can find out a layout defined by xml,as like the findViewById() method,but there ha ...

  3. Python 获取图片文件大小并转换为base64编码

    import os import base64 fileSize = os.path.getsize(文件路径) with open(文件路径, 'rb') as f: data = base64.b ...

  4. web.xml文件中配置mime下载文件类型(转)

    转自:http://5aijava.iteye.com/blog/166600 TOMCAT在默认情况下下载.rar的文件是把文件当作text打开,以至于IE打开RAR文件为乱码,如果遇到这种情况时不 ...

  5. Java8 CompletableFuture组合式的编程(笔记)

    * 实现异步API public double getPrice(String product) { return calculatePrice(product); } /** * 同步计算商品价格的 ...

  6. Bat 获取本地代码的Svn Revision并保存到变量

    echo off & color 0A for /f "usebackq delims=" %%i in (`"svn info Server | findstr ...

  7. react-native + react-native-tab-navigator 实现 TabBar

    1.安装 react-native-tab-navigator yarn add react-native-tab-navigator 2.页面调用 /** * 主页面 */ import React ...

  8. grid 布局 属性示例

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...

  9. struts2 接口如何接收客户端提交的json数据

      struts2 接口如何接收客户端提交的json数据 CreationTime--2018年6月20日15点54分 Author:Marydon 1.情景还原 使用struts2写的接口(服务端) ...

  10. 简单模拟javaScript面向对象

    <html> <head> <script type="text/javascript"> if (!Object.create) { Obje ...