forked from llohellohe/zookeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
50dd202
commit 1f52cfe
Showing
2 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
src/main/java/yangqi/zookeeper/example/masterworker/Master.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/* | ||
* Copyright 1999-2010 Alibaba.com All right reserved. This software is the | ||
* confidential and proprietary information of Alibaba.com ("Confidential | ||
* Information"). You shall not disclose such Confidential Information and shall | ||
* use it only in accordance with the terms of the license agreement you entered | ||
* into with Alibaba.com. | ||
*/ | ||
package yangqi.zookeeper.example.masterworker; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import org.apache.zookeeper.CreateMode; | ||
import org.apache.zookeeper.KeeperException; | ||
import org.apache.zookeeper.WatchedEvent; | ||
import org.apache.zookeeper.Watcher; | ||
import org.apache.zookeeper.ZooDefs; | ||
import org.apache.zookeeper.ZooKeeper; | ||
import org.apache.zookeeper.data.Stat; | ||
|
||
/** | ||
* 类Master.java的实现描述:TODO 类实现描述 | ||
* @author yangqi Jan 1, 2014 1:37:01 PM | ||
*/ | ||
public class Master implements Watcher, Runnable { | ||
|
||
private ZooKeeper zk; | ||
|
||
private String connectString; | ||
|
||
private String serverId; | ||
|
||
private static final String MASTER_PATH = "/master"; | ||
|
||
public Master(String connectString,String serverId) { | ||
this.connectString = connectString; | ||
this.serverId = serverId; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent) | ||
*/ | ||
@Override | ||
public void process(WatchedEvent event) { | ||
System.out.println(event); | ||
} | ||
|
||
public void startZK() { | ||
try { | ||
zk = new ZooKeeper(connectString, 2000, this); | ||
} catch (IOException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void stopZK() { | ||
try { | ||
zk.close(); | ||
} catch (InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
public void createMaterNode(){ | ||
String response = null; | ||
try { | ||
response = zk.create(MASTER_PATH, serverId.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, | ||
CreateMode.EPHEMERAL); | ||
|
||
} catch (KeeperException | InterruptedException e) { | ||
e.printStackTrace(); | ||
} | ||
System.out.println(serverId + " response is " + response); | ||
} | ||
|
||
public boolean checkForMaster() { | ||
|
||
Stat stat=new Stat(); | ||
byte[] data = null; | ||
try { | ||
data = zk.getData("/master", false, stat); | ||
System.out.println(serverId + " stat return " + new String(data)); | ||
return serverId.equals(new String(data)); | ||
} catch (KeeperException | InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
|
||
return Boolean.FALSE; | ||
} | ||
|
||
public boolean registerForMaster() { | ||
int count = 1; | ||
boolean isLeader = false; | ||
while (true) { | ||
System.out.println(serverId + " start to check count " + count++); | ||
if (!checkForMaster()) { | ||
createMaterNode(); | ||
try { | ||
Thread.sleep(5000); | ||
} catch (InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
} else { | ||
isLeader = true; | ||
System.out.println(serverId + " master registered with " + serverId); | ||
break; | ||
} | ||
} | ||
return isLeader; | ||
} | ||
|
||
public static void main(String[] args) throws InterruptedException { | ||
int masterCount = 3; | ||
ExecutorService service = Executors.newFixedThreadPool(masterCount); | ||
for (int i = 0; i < masterCount; i++) { | ||
Master master = new Master("localhost:2181", "o2-" + i); | ||
service.submit(master); | ||
} | ||
|
||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* @see java.lang.Runnable#run() | ||
*/ | ||
@Override | ||
public void run() { | ||
try { | ||
Thread.sleep(1000 * 1); | ||
} catch (InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
startZK(); | ||
|
||
boolean isLeader = registerForMaster(); | ||
if (isLeader) { | ||
stopZK(); | ||
} else { | ||
try { | ||
Thread.sleep(1000 * 10); | ||
} catch (InterruptedException e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
} | ||
stopZK(); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Define some default values that can be overridden by system properties | ||
zookeeper.root.logger=INFO, CONSOLE | ||
zookeeper.console.threshold=INFO | ||
zookeeper.log.dir=. | ||
zookeeper.log.file=zookeeper.log | ||
zookeeper.log.threshold=DEBUG | ||
zookeeper.tracelog.dir=. | ||
zookeeper.tracelog.file=zookeeper_trace.log | ||
|
||
# | ||
# ZooKeeper Logging Configuration | ||
# | ||
|
||
# Format is "<default threshold> (, <appender>)+ | ||
|
||
# DEFAULT: console appender only | ||
log4j.rootLogger=${zookeeper.root.logger} | ||
|
||
# Example with rolling log file | ||
#log4j.rootLogger=DEBUG, CONSOLE, ROLLINGFILE | ||
|
||
# Example with rolling log file and tracing | ||
#log4j.rootLogger=TRACE, CONSOLE, ROLLINGFILE, TRACEFILE | ||
|
||
# | ||
# Log INFO level and above messages to the console | ||
# | ||
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender | ||
log4j.appender.CONSOLE.Threshold=${zookeeper.console.threshold} | ||
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout | ||
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n | ||
|
||
# | ||
# Add ROLLINGFILE to rootLogger to get log file output | ||
# Log DEBUG level and above messages to a log file | ||
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender | ||
log4j.appender.ROLLINGFILE.Threshold=${zookeeper.log.threshold} | ||
log4j.appender.ROLLINGFILE.File=${zookeeper.log.dir}/${zookeeper.log.file} | ||
|
||
# Max log file size of 10MB | ||
log4j.appender.ROLLINGFILE.MaxFileSize=10MB | ||
# uncomment the next line to limit number of backup files | ||
#log4j.appender.ROLLINGFILE.MaxBackupIndex=10 | ||
|
||
log4j.appender.ROLLINGFILE.layout=org.apache.log4j.PatternLayout | ||
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n | ||
|
||
|
||
# | ||
# Add TRACEFILE to rootLogger to get log file output | ||
# Log DEBUG level and above messages to a log file | ||
log4j.appender.TRACEFILE=org.apache.log4j.FileAppender | ||
log4j.appender.TRACEFILE.Threshold=TRACE | ||
log4j.appender.TRACEFILE.File=${zookeeper.tracelog.dir}/${zookeeper.tracelog.file} | ||
|
||
log4j.appender.TRACEFILE.layout=org.apache.log4j.PatternLayout | ||
### Notice we are including log4j's NDC here (%x) | ||
log4j.appender.TRACEFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L][%x] - %m%n |