diamond源码阅读-diamond-client
生活随笔
收集整理的這篇文章主要介紹了
diamond源码阅读-diamond-client
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
讀取數(shù)據(jù)
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");//設(shè)置diamond-server服務(wù)的端口manager.getDiamondConfigure().setPort(8080);String availableConfigureInfomation = manager.getAvailableConfigureInfomation(5000);System.out.println("start config: " + availableConfigureInfomation);}1 初始化DefaultDiamondManager 并啟動(dòng)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();
/*** 啟動(dòng)DiamondSubscriber:<br>* 1.阻塞主動(dòng)獲取所有的DataId配置信息<br>* 2.啟動(dòng)定時(shí)線程定時(shí)獲取所有的DataId配置信息<br>*/public synchronized void start() {if (isRun) {return;}if (null == scheduledExecutor || scheduledExecutor.isTerminated()) {scheduledExecutor = Executors.newSingleThreadScheduledExecutor();}localConfigInfoProcessor.start(this.diamondConfigure.getFilePath() + "/" + DATA_DIR);//創(chuàng)建根目錄并監(jiān)控根目錄C:\Users\zhumenglong/diamond/dataserverAddressProcessor = new ServerAddressProcessor(this.diamondConfigure, this.scheduledExecutor);serverAddressProcessor.start();this.snapshotConfigInfoProcessor =new SnapshotConfigInfoProcessor(this.diamondConfigure.getFilePath() + "/" + SNAPSHOT_DIR);// 設(shè)置domainNamePos值randomDomainNamePos();initHttpClient();// 初始化完畢isRun = true;if (log.isInfoEnabled()) {log.info("當(dāng)前使用的域名有:" + this.diamondConfigure.getDomainNameList());}if (MockServer.isTestMode()) {bFirstCheck = false;}else {// 設(shè)置輪詢間隔時(shí)間this.diamondConfigure.setPollingIntervalTime(Constants.POLLING_INTERVAL_TIME);}// 輪詢r(jià)otateCheckConfigInfo();addShutdownHook();}1.1.1 http://www.cnblogs.com/clds/p/5997195.html
localConfigInfoProcessor.start(this.diamondConfigure.getFilePath() + "/" + DATA_DIR);//創(chuàng)建根目錄并監(jiān)控根目錄目錄獲取方式 System.getProperty("user.home") + "/diamond"; C:\Users\zhumenglong/diamond/data//監(jiān)控代碼分析見(jiàn) //核心思路起一個(gè)線程定時(shí)監(jiān)聽(tīng)文件夾及子文件,判斷文件是否增加刪除//修改, //保存到 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();//初始化HttpClientif (this.diamondConfigure.isLocalFirst()) {acquireServerAddressFromLocal();//如果是本地,從本地獲取服務(wù)器列表}else {synAcquireServerAddress();//如果不再異步每隔一段通過(guò)域名時(shí)間去取diamondIpList,注釋掉下面這行//asynAcquireServerAddress();asynAcquireServerAddress();}}1.1.3 randomDomainNamePos(); 隨機(jī)獲取服務(wù)器地址
private void randomDomainNamePos() {// 隨機(jī)化起始服務(wù)器地址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關(guān)閉鉤子
2 //設(shè)置diamond-server服務(wù)的端口
manager.getDiamondConfigure().setPort(8080);3 獲取數(shù)據(jù)String availableConfigureInfomation = manager.getAvailableConfigureInfomation(5000);
public String getAvailableConfigureInfomation(String dataId, String group, long timeout) {// 嘗試先從本地和網(wǎng)絡(luò)獲取配置信息try {String result = getConfigureInfomation(dataId, group, timeout);if (result != null && result.length() > 0) {return result;}}catch (Throwable t) {log.error(t.getMessage(), t);}// 測(cè)試模式不使用本地dumpif (MockServer.isTestMode()) {return null;}return getSnapshotConfiginfomation(dataId, group);//本地快照獲取}轉(zhuǎn)載于:https://my.oschina.net/sbcagf/blog/783026
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的diamond源码阅读-diamond-client的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: maven整合S2SH
- 下一篇: 检测IIS应用程序池对象 回收