传统的id生成策略包括数据库自增id和uuid。自增id策略,命名友好,但是不适合分布式。uuid可以用于分布式,但是命名友好。zookeeper的应用场景包括统一命名服务,如分布式id生成。
一、代码示例
1、分布式id生成代码
public class IdMaker {
private ZkClient client = null;
private final String server;
private final String root;
private final String nodeName;
private volatile boolean running = false;
private ExecutorService cleanExector = null;
public enum RemoveMethod{
NONE,IMMEDIATELY,DELAY
}
public IdMaker(String zkServer,String root,String nodeName){
this.root = root;
this.server = zkServer;
this.nodeName = nodeName;
}
//启动
public void start() throws Exception {
if (running)
throw new Exception("server has stated...");
running = true;
init();
}
public void stop() throws Exception {
if (!running)
throw new Exception("server has stopped...");
running = false;
freeResource();
}
private void init(){
//初始化zk客户端和线程池
client = new ZkClient(server,5000,5000,new BytesPushThroughSerializer());
cleanExector = Executors.newFixedThreadPool(10);
try{
client.createPersistent(root,true);
}catch (ZkNodeExistsException e){
//ignore;
}
}
private void freeResource(){
cleanExector.shutdown();
try{
cleanExector.awaitTermination(2, TimeUnit.SECONDS);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
cleanExector = null;
}
if (client!=null){
client.close();
client=null;
}
}
private void checkRunning() throws Exception {
if (!running)
throw new Exception("请先调用start");
}
private String ExtractId(String str){
int index = str.lastIndexOf(nodeName);
if (index >= 0){
index+=nodeName.length();
return index <= str.length()?str.substring(index):"";
}
return str;
}
public String generateId(RemoveMethod removeMethod) throws Exception{
checkRunning();
final String fullNodePath = root.concat("/").concat(nodeName);
final String ourPath = client.createPersistentSequential(fullNodePath, null);
if (removeMethod.equals(RemoveMethod.IMMEDIATELY)){
client.delete(ourPath);
}else if (removeMethod.equals(RemoveMethod.DELAY)){
cleanExector.execute(new Runnable() {
public void run() {
// TODO Auto-generated method stub
client.delete(ourPath);
}
});
}
//node-0000000000, node-0000000001
return ExtractId(ourPath);
}
}
2、测试类
public class TestIdMaker {
public static void main(String[] args) throws Exception {
IdMaker idMaker = new IdMaker("192.168.1.105:2181","/NameService/IdGen", "ID");
idMaker.start();
try {
for (int i = 0; i < 10; i++) {
String id = idMaker.generateId(RemoveMethod.DELAY);
System.out.println(id);
}
} finally {
idMaker.stop();
}
}
}
网友评论