Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
easonjim committed Sep 16, 2017
1 parent 0f07f7f commit ee47314
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 196 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.jsoft.testzookeeper.test1;

import com.fasterxml.jackson.core.JsonProcessingException;

public class App {

public static void main(String[] args) throws JsonProcessingException, InterruptedException {
ConfigManager cfgManager = new ConfigManager();
ClientApp clientApp = new ClientApp();

// 模拟【配置管理中心】初始化时,从db加载配置初始参数
cfgManager.loadConfigFromDB();

// 然后将配置同步到ZK
cfgManager.syncFtpConfigToZk();

// 模拟客户端程序运行
clientApp.run();

// 模拟配置修改
cfgManager.updateFtpConfigToDB(23, "10.6.12.34", "newUser", "newPwd");

// 然后将配置同步到ZK
cfgManager.syncFtpConfigToZk();

//线程阻塞,让程序保持在后台运行
new Thread() {
@Override
public void run() {
try {
while (true) {
Thread.sleep(1);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;

import java.util.concurrent.TimeUnit;

public class ClientApp {

FtpConfig ftpConfig;
Expand All @@ -14,15 +12,16 @@ private FtpConfig getFtpConfig() {
// 首次获取时,连接zk取得配置,并监听配置变化
ZkClient zk = ZKUtil.getZkClient();
ftpConfig = (FtpConfig) zk.readData(ZKUtil.FTP_CONFIG_NODE_NAME);
System.out.println("ftpConfig => " + ftpConfig);

System.out.println("从ZK中获取配置数据");
System.out.println("ftpConfig=>"+ftpConfig);
System.out.println("注册ZK的回调事件");
zk.subscribeDataChanges(ZKUtil.FTP_CONFIG_NODE_NAME, new IZkDataListener() {

@Override
public void handleDataChange(String s, Object o) throws Exception {
System.out.println("ftpConfig is changed !");
System.out.println("ZK事件回调handleDataChange:ftpConfig is changed !");
System.out.println("node:" + s);
System.out.println("o:" + o.toString());
System.out.println("ftpConfig=>" + o.toString());
ftpConfig = (FtpConfig) o;// 重新加载FtpConfig
}

Expand All @@ -44,26 +43,7 @@ public void handleDataDeleted(String s) throws Exception {
* @throws InterruptedException
*/
public void run() throws InterruptedException {

getFtpConfig();

upload();

download();
System.out.println("程序运行");
getFtpConfig();
}

public void upload() throws InterruptedException {
System.out.println("正在上传文件...");
System.out.println(ftpConfig);
TimeUnit.SECONDS.sleep(10);
System.out.println("文件上传完成...");
}

public void download() throws InterruptedException {
System.out.println("正在下载文件...");
System.out.println(ftpConfig);
TimeUnit.SECONDS.sleep(10);
System.out.println("文件下载完成...");
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.jsoft.testzookeeper.test1;

import com.fasterxml.jackson.core.JsonProcessingException;

import java.util.Date;

import org.I0Itec.zkclient.ZkClient;

public class ConfigManager {
Expand All @@ -14,6 +17,8 @@ public void loadConfigFromDB() {
// query config from database
// TODO...
ftpConfig = new FtpConfig(21, "192.168.1.1", "test", "123456");
System.out.println("从db加载初始配置");
System.out.println("ftpConfig=>"+ftpConfig);
}

/**
Expand All @@ -34,6 +39,8 @@ public void updateFtpConfigToDB(int port, String host, String user, String passw
ftpConfig.setPassword(password);
// write to db...
// TODO...
System.out.println("更新DB中的配置");
System.out.println("ftpConfig=>"+ftpConfig);
}

/**
Expand All @@ -46,6 +53,7 @@ public void syncFtpConfigToZk() throws JsonProcessingException {
}
zk.writeData(ZKUtil.FTP_CONFIG_NODE_NAME, ftpConfig);
zk.close();
System.out.println("将配置同步到ZK");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import java.io.Serializable;

/**
* Created by jimmy on 15/6/27.
*/
public class FtpConfig implements Serializable {

/**
Expand Down
12 changes: 12 additions & 0 deletions zookeeper/test1/test1/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Configure logging for testing: optionally with log file
log4j.rootLogger=WARN, stdout
# log4j.rootLogger=WARN, stdout, logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n

log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/out.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.jsoft.testzookeeper.test1;

import com.fasterxml.jackson.core.JsonProcessingException;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
Expand Down Expand Up @@ -33,26 +31,4 @@ public static Test suite() {
public void testApp() {
assertTrue(true);
}

public void testZkConfig() throws JsonProcessingException, InterruptedException {

ConfigManager cfgManager = new ConfigManager();
ClientApp clientApp = new ClientApp();

// 模拟【配置管理中心】初始化时,从db加载配置初始参数
cfgManager.loadConfigFromDB();
// 然后将配置同步到ZK
cfgManager.syncFtpConfigToZk();

// 模拟客户端程序运行
clientApp.run();

// 模拟配置修改
cfgManager.updateFtpConfigToDB(23, "10.6.12.34", "newUser", "newPwd");
cfgManager.syncFtpConfigToZk();

// 模拟客户端自动感知配置变化
clientApp.run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,30 @@
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
public static void main(String[] args) throws InterruptedException {
System.out.println("程序启动,并初始化数据!");
MyClient client = new MyClient();

//保证在后台运行
new Thread() {
@Override
public void run() {
while (true) {
System.out.println("线程监听数据变化!");
System.out.println("url=" + client.getuRL());
System.out.println("userName=" + client.getUserName());
System.out.println("passwd=" + client.getPasswd());
try {
Thread.sleep(10 * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
}.start();

System.out.println("更新数据!");
client.changeValue();
}
}
Loading

0 comments on commit ee47314

Please sign in to comment.