將Zookeeper集成到你的應用中

首先,咱們下載zookeeper-3.3.6.tar.gz,最好不要最新版本,新版本在jdk1.5下有問題。解壓後在java

zookeeper-3.3.6/contrib/fatjar目錄下有一個zookeeper-3.3.6-fatjar.jar文件,咱們用這個jar來寫。web

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.Properties;

public class ZKServerStart {
 /**
  * 啓動zookeeper服務
  * @param zoocfg zoo.cfg文件的物理路徑
  * @param zooDataDir  zookeeper的data路徑
  * @throws Exception
  */
 public static void Run(String zoocfg, String zooDataDir) throws Exception {
  //加載zoocfg配置文件
  Properties prop = loadProperties(zoocfg);
  //提取本機服務的server編號,這個my.id是默認的zoo.cfg裏沒有的,須要咱們後加上,
  //它的值就是當前節點的serverNum
  String serverNum = prop.getProperty("my.id");
  //提取zookeeper的客戶端IP和端口,把IP和端口提取出來,方便咱們的客戶端API使用
  Global.zkClientIp = prop.getProperty("server." + serverNum).split(":")[0];
  Global.zkClientPort = Integer.parseInt(prop.getProperty("clientPort"));
  //myid文件的路徑
  String dataDir = zooDataDir + "/ZooData";
  //寫入myid文件
  writeMyid(dataDir, serverNum);
  prop.setProperty("dataDir", dataDir);
  //將dataDir保存到zoo.cfg
  saveConfig(prop, zoocfg);
     String[] config = {zoocfg};
  Class<?> clazz = Class.forName("org.apache.zookeeper.server.quorum.QuorumPeerMain");
     Method main = clazz.getMethod("main", String[].class);
     //啓動zookeeper
     main.invoke(null, (Object)config);
 }
 /*
  * 保存zookeeper的配置文件
  */
 private static void saveConfig(Properties prop, String configFile) throws IOException {
  OutputStream out = new FileOutputStream(configFile);
  try{
   prop.store(out, null);
  } finally {
   if(out != null) out.close();
  }
 }
 /*
  * 將server的編號寫入myid文件
  */
 private static void writeMyid(String dataDir, String serverNum)
   throws IOException, FileNotFoundException {
  File dir = new File(dataDir);
  if(!dir.exists()) dir.mkdirs();
  File myid = new File(dataDir + "/myid");
  if(!myid.exists()) myid.createNewFile();
  OutputStream out = new FileOutputStream(myid);
  try{
   out.write(serverNum.getBytes());
  } finally {
   if(out != null) out.close();
  }
 }
 /*
  * 加載zoocfg配置
  */
 private static Properties loadProperties(String zoocfg) throws FileNotFoundException, IOException {
  Properties prop = new Properties();
  InputStream is = new FileInputStream(zoocfg);
  try{
   prop.load(is);
  } finally {
   if(is != null) is.close();
  }
  return prop;
 }
}

 注意,使用這個啓動類來啓動zookeeper的時候要放到線程中。例如,咱們在Servlet的init()方法中啓動:apache

import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class ZooServlet extends HttpServlet{
 @Override
 public void init() throws ServletException {
  String zooConfig = this.getInitParameter("ZooConfig");
  //找到WEB-INF的物理路徑
  final String webInfo = getWebInfPath();
  //找到zoo.cfg的物理路徑
  final String configPath = webInfo + zooConfig.substring(1, zooConfig.length());
  new Thread(new Runnable(){
   public void run() {
    //啓動zookeeper服務
    try {
     ZKServerStart.Run(configPath, webInfo);
    } catch (Exception e) {
     e.printStackTrace();
    }
   }}).start();
  
  super.init();
 }
 private String getWebInfPath() {
  URL url = this.getClass().getProtectionDomain().getCodeSource().getLocation();
  String path = url.toString();
  int index = path.indexOf("WEB-INF");
  if (index == -1) {
   index = path.indexOf("classes");
  }
  if (index == -1) {
   index = path.indexOf("bin");
  }
  path = path.substring(0, index);
  if (path.startsWith("zip")) {// 當class文件在war中時,此時返回zip:D:/...這樣的路徑
   path = path.substring(4);
  } else if (path.startsWith("file")) {// 當class文件在class文件中時,此時返回file:/D:/...這樣的路徑
   path = path.substring(6);
  } else if (path.startsWith("jar")) {// 當class文件在jar文件裏面時,此時返回jar:file:/D:/...這樣的路徑
   path = path.substring(10);
  }
  try {
   path = URLDecoder.decode(path, "UTF-8");
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  }
  return path;
 }
 
}

web.xml的配置:app

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
 xmlns="http://java.sun.com/xml/ns/j2ee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
  <servlet>
   <servlet-name>ZooServlet</servlet-name>
   <servlet-class>ZooServlet</servlet-class>
   <init-param>
    <param-name>ZooConfig</param-name>
    <param-value>/WEB-INF/conf/zoo.cfg</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
   <servlet-name>ZooServlet</servlet-name>
   <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

這樣,Servlet在初始化的時候就啓動了zookeeper,同時將zookeeper的dataDir目錄設置到WEB-INF/ZooData/下。同時咱們還提取了zookeeper的當前節點的IP和客戶端端口,方便在調用客戶端API的地方使用。jsp

最後看一下zoo.cfg配置ide

tickTime=2000
initLimit=10
syncLimit=5
dataDir=
clientPort=2181
server.1=192.168.1.1:2888:3888
server.2=192.168.1.2:2888:3888
server.3=192.168.1.3:2888:3888
my.id=1

這個my.id是後加的一個屬性,用於記錄當前節點的server編號,方便咱們寫入到myid文件中。this

相關文章
相關標籤/搜索