① zookeeper 用什麼開發的
由於zookeeper的client只有zookeeper一個對象,使用也比較簡單,所以就不許要文字說明了,在代碼中注釋下就ok 了。
1、測試用的main方法
package ClientExample;
public class TestMain {
public static void main(String[] args) {
/*
* 測試流程
* 1、創建sever1的連接client1,並且創建一個永久性的/test節點
* 2、創建一個針對server1的臨時節點
* 3、創建server2的連接client21,並創建一個針對server2的臨時節點
* 4、創建server3的連接client3,並創建一個針對server3的臨時節點
* 5、分別查看client1、client2、client3的三個節點的位元組點數量,確定是否同步成功
* 6、修改client1的臨時節點內容,然後在在client2和client3中查看
* 7、kill掉client3的線程,然後檢查是watcher是否有通知給client1和client2
*/
Thread t1= new ClientThread("127.0.0.1:2181","server1",false);
Thread t2= new ClientThread("127.0.0.1:2182","server2",false);
Thread t3= new ClientThread("127.0.0.1:2183","server3",false);
Thread t4= new ClientThread("127.0.0.1:2181","server4",false);
t1.start();
t2.start();
t3.start();
t4.start();
ControlThread c = new ControlThread(t1, t2, t3, t4);
c.start();
int i=0;
while(true)
{
i++;
i--;
}
/*
* 測試控制台輸出:
* connectIP:server4,path:null,state:SyncConnected,type:None
* connectIP:server3,path:/test,state:SyncConnected,type:NodeChildrenChanged
* connectIP:server4,path:/test/server4,state:SyncConnected,type:NodeCreated
* 。。。。。。。。。。。
*
* connectIP:server2,path:null,state:Disconnected,type:None
server2exception,KeeperErrorCode = ConnectionLoss for /test
connectIP:newServer1,path:null,state:SyncConnected,type:None
connectIP:server1,path:/test,state:SyncConnected,type:NodeChildrenChanged
connectIP:server4,path:/test/server2,state:SyncConnected,type:NodeDeleted
connectIP:server4,path:/test,state:SyncConnected,type:NodeChildrenChanged
connectIP:newServer1,path:/test,state:SyncConnected,type:NodeChildrenChanged
connectIP:server3,path:/test/server2,state:SyncConnected,type:NodeDeleted
connectIP:server3,path:/test,state:SyncConnected,type:NodeChildrenChanged
*/
}
}
2、zookeeper封裝的介面:
package ClientExample;
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.KeeperException;
/**
* zookeeper的操作封裝介面,實現了常用的操作
* 創建、銷毀、寫入、修改、查詢等。
* @author ransom
*
*/
public interface ServerOperation {
void init(String address,String serverName) throws IOException;
void destroy() throws InterruptedException;
List<String> getChilds(String path) throws KeeperException,
InterruptedException;
String getData(String path) throws KeeperException, InterruptedException;
void changeData(String path, String data) throws KeeperException,
InterruptedException;
void delData(String path) throws KeeperException, InterruptedException;
void apendTempNode(String path, String data) throws KeeperException,
InterruptedException;
void apendPresistentNode(String path, String data) throws KeeperException,
InterruptedException;
void delNode(String path) throws KeeperException, InterruptedException;
boolean exist(String path) throws KeeperException, InterruptedException;
}
3、介面的實現:
package ClientExample;
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
public class ServerConnector implements ServerOperation {
// 創建一個Zookeeper實例,第一個參數為目標伺服器地址和埠,第二個參數為Session超時時間,第三個為節點變化時的回調方法
private ZooKeeper zk = null;
public void init(String address,String serverName) throws IOException {
zk = new ZooKeeper(address, 500000,
new MultiWatcher(serverName));
}
@Override
public void destroy() throws InterruptedException {
// TODO Auto-generated method stub
if (zk != null) {
zk.close();
}
}
@Override
public List<String> getChilds(String path) throws KeeperException, InterruptedException {
// TODO Auto-generated method stub
if (zk != null) {
return zk.getChildren(path, true);
}
return null;
}
@Override
public String getData(String path) throws KeeperException, InterruptedException {
// TODO Auto-generated method stub
if (zk != null) {
// 取得/root/childone節點下的數據,返回byte[]
byte[] b = zk.getData(path, true, null);
return new String(b);
}
return null;
}
@Override
public void changeData(String path,String data) throws KeeperException, InterruptedException {
// TODO Auto-generated method stub
if (zk != null) {
// 修改節點/root/childone下的數據,第三個參數為版本,如果是-1,那會無視被修改的數據版本,直接改掉
zk.setData(path, data.getBytes(),-1);
}
}
@Override
public void delData(String path) throws InterruptedException, KeeperException {
// TODO Auto-generated method stub
if (zk != null) {
// 刪除/root/childone這個節點,第二個參數為版本,-1的話直接刪除,無視版本
zk.delete(path, -1);
}
}
@Override
public void delNode(String path) throws InterruptedException, KeeperException {
// TODO Auto-generated method stub
if (zk != null) {
zk.delete(path, -1);
}
}
@Override
public boolean exist(String path) throws KeeperException,
InterruptedException {
// TODO Auto-generated method stub
if (zk != null) {
return zk.exists(path, true)!=null;
}
return false;
}
@Override
public void apendTempNode(String path, String data) throws KeeperException,
InterruptedException {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (zk != null)
{
// 創建一個節點root,數據是mydata,不進行ACL許可權控制,節點為永久性的(即客戶端shutdown了也不會消失)
/*
* 創建一個給定的目錄節點 path, 並給它設置數據,
* CreateMode 標識有四種形式的目錄節點,分別是
* PERSISTENT:持久化目錄節點,這個目錄節點存儲的數據不會丟失;
* PERSISTENT_SEQUENTIAL:順序自動編號的目錄節點,這種目錄節點會根據當前已近存在的節點數自動加 1,然後返回給客戶端已經成功創建的目錄節點名;
* EPHEMERAL:臨時目錄節點,一旦創建這個節點的客戶端與伺服器埠也就是 session 超時,這種節點會被自動刪除;
* EPHEMERAL_SEQUENTIAL:臨時自動編號節點
*/
zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
}
}
@Override
public void apendPresistentNode(String path, String data)
throws KeeperException, InterruptedException {
// TODO Auto-generated method stub
if (zk != null)
{
// 創建一個節點root,數據是mydata,不進行ACL許可權控制,節點為永久性的(即客戶端shutdown了也不會消失)
/*
* 創建一個給定的目錄節點 path, 並給它設置數據,
* CreateMode 標識有四種形式的目錄節點,分別是
* PERSISTENT:持久化目錄節點,這個目錄節點存儲的數據不會丟失;
* PERSISTENT_SEQUENTIAL:順序自動編號的目錄節點,這種目錄節點會根據當前已近存在的節點數自動加 1,然後返回給客戶端已經成功創建的目錄節點名;
* EPHEMERAL:臨時目錄節點,一旦創建這個節點的客戶端與伺服器埠也就是 session 超時,這種節點會被自動刪除;
* EPHEMERAL_SEQUENTIAL:臨時自動編號節點
*/
zk.create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
}
} 4、一個控制的線程,主要用來強制kill掉連接的線程
package ClientExample;
public class ControlThread extends Thread{
public ControlThread(Thread t1,Thread t2,Thread t3,Thread t4)
{
list[0]=t1;
list[1]=t2;
list[2]=t4;
list[3]=t4;
}
private Thread[] list = new Thread[4];
private int num=0;
public void run()
{
while(true)
{
if(num==7)
{
list[2].stop();
System.out.println("kill server3");
}
if(num==15)
{
list[3].stop();
System.out.println("kill server4");
}
try {
sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
5、watcher
的實現:
package ClientExample;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
/**
* 提供給多個client使用的watcher
* @author ransom
*
*/
public class MultiWatcher implements Watcher{
public MultiWatcher(String address)
{
connectAddress=address;
}
private String connectAddress=null;
@Override
public void process(WatchedEvent event) {
// TODO Auto-generated method stub
String outputStr="";
if(connectAddress!=null){
outputStr+="connectIP:"+connectAddress;
}
outputStr+=",path:"+event.getPath();
outputStr+=",state:"+event.getState();
outputStr+=",type:"+event.getType();
System.out.println(outputStr);
}
}
轉載
② storm nimbus server怎麼理解
Nimbus啟動時候,運行了一個Thrift Server。它會在topology提交之前做以下四個工作。
(1) 清理一些中斷了的topology(nimbus目錄下/storm.local.dir/stormdist下存在,zk中 storms/topologyid中不存在的topology): 刪除ZK上相關信息(清理tasks/topologyid; storms/topologyid; assignments/topologyid這個三個目錄)。
(2) 將storms/下所有的topology設置為啟動狀態: 能轉換成startup狀態的兩種狀態分別是:killed和rebalancing。nimbus的狀態轉換是很有意思的事情,killed狀態的topology在nimbus啟動的時候會被幹掉;rebalancing狀態的topology在nimbus啟動的時候會重新分發任務,狀態會變成rebalancing的上一個狀態。
舉例: 當某個topology提交的時候,會被設置成active狀態,假設storm集群增加機器了,你希望重新分發任務,可以將狀態轉換成rebalance狀態,轉換成這個狀態要做這幾件事:
首先,啟動一個延遲TOPOLOGY-MESSAGE-TIMEOUT-SECS秒執行的事件,將當前狀態轉換成do-rebalance狀態,在這之前會將當前topology的狀態設置成rebalancing狀態(注意設置和轉換的區別,設置就是指將ZK上存儲的topology的狀態進行重新設置)。
然後,將rebalancing的狀態轉換成do-rebalance, 也就是將任務重新分發。
③ ZK.BINisnotexistinSDCard是什麼意思
ZK.BINisnotexistinSDCard意思是sd卡中不存在zk.bin。
Android從某個版本開始(具體哪個忘了),會虛擬出一個sdcard出來,當你使用Environment.getExternalStorageDirectory()獲取的目錄,其實是位於手機內存存儲的空間,也就是ROM里的空間,並不是用戶插入的sdcard,要獲取用戶的sdcard目錄,可以使用反射來實現。
④ 如何查看zookeeper中存放的hbase的
尋找RegionServer
ZooKeeper--> -ROOT-(單Region)--> .META.--> 用戶表
想這個存儲在了zookeeper file中,也就是znode,信息肯定在這裡面的。
所以樓主在配置的時候,找到znode裡面的信息即可。-ROOT-表,則通過zk節點root-region-server獲取-ROOT-表所在的Location
⑤ zookeeper是要安裝在哪個目錄
然後每個文件夾裡面解壓一個zookeeper的下載包,並且還建了幾個文件夾,總體結構如下,最後那個是下載過來壓縮包的解壓文件
data dataLog logs zookeeper-3.3.2
那麼首先進入data目錄,創建一個myid的文件,裡面寫入一個數字,比如我這個是server1,那麼就寫一個1,server2對應myid文件就寫入2,server3對應myid文件就寫個3
然後進入zookeeper-3.3.2/conf目錄,那麼如果是剛下過來,會有3個文件,configuration.xml, log4j.properties,zoo_sample.cfg,這3個文件我們首先要做的就是在這個目錄創建一個zoo.cfg的配置文件,當然你可以把zoo_sample.cfg文件改成zoo.cfg,配置的內容如下所示:
tickTime=2000
initLimit=5
syncLimit=2
dataDir=xxxx/zookeeper/server1/data
dataLogDir=xxx/zookeeper/server1/dataLog
clientPort=2181
server.1=127.0.0.1:2888:3888
server.2=127.0.0.1:2889:3889
server.3=127.0.0.1:2890:3890
標紅的幾個配置應該官網講得很清楚了,只是需要注意的是clientPort這個埠如果你是在1台機器上部署多個server,那麼每台機器都要不同的clientPort,比如我server1是2181,server2是2182,server3是2183,dataDir和dataLogDir也需要區分下。
最後幾行唯一需要注意的地方就是 server.X 這個數字就是對應 data/myid中的數字。你在3個server的myid文件中分別寫入了1,2,3,那麼每個server中的zoo.cfg都配server.1,server.2,server.3就OK了。因為在同一台機器上,後面連著的2個埠3個server都不要一樣,否則埠沖突,其中第一個埠用來集群成員的信息交換,第二個埠是在leader掛掉時專門用來進行選舉leader所用。
進入zookeeper-3.3.2/bin 目錄中,./zkServer.sh start啟動一個server,這時會報大量錯誤?其實沒什麼關系,因為現在集群只起了1台server,zookeeper伺服器端起來會根據zoo.cfg的伺服器列表發起選舉leader的請求,因為連不上其他機器而報錯,那麼當我們起第二個zookeeper實例後,leader將會被選出,從而一致性服務開始可以使用,這是因為3台機器只要有2台可用就可以選出leader並且對外提供服務(2n+1台機器,可以容n台機器掛掉)。
⑥ zkparking資料庫配置程序
摘要 這邊給您查詢分析到每台機器的應用程序都需要連接資料庫,而資料庫的配置信息(連接信息),這時候放在機器本地的話不方面(機器多,需要一個個改配置信息),這就用到Zookeeper,把資料庫的配置信息放到配置中心,利用Zookeeper節點可以存儲數據的特性,然後各台機器可以使用JavaAPI去獲取Zookeeper中資料庫的配置信息。每一個應用都在Zookeeper節點注冊監聽器,一旦節點信息改變,各台機器就獲取信息,使用最新的信息連接資料庫,這樣優點一是方便了管理(只放置一份數據在配置中心,沒必要放到多個機器上去),二是一旦配置改了,就做一個發布的動作即可。
⑦ 如何提高zookeeper每個結點所能存儲的數據大小
今天發現一個問題,zookeeper默認對每個結點的最大數據量有一個上限是1M,如果你要設置的配置數據大於這個上限將無法寫法,在網上查了一圈發現有一個解決方案如下,增加-Djute.maxbuffer=10240000參數
最終提供一個完整的修改後的zkServer.sh文件如下
#!/bin/sh
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding right ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# If this scripted is run out of /usr/bin or some other system bin directory
# it should be linked to and not copied. Things like java jar files are found
# relative to the canonical path of this script.
#
# See the following page for extensive details on setting
# up the JVM to accept JMX remote management:
# http://java.sun.com/javase/6/docs/technotes/guides/management/agent.html
# by default we allow local JMX connections
ZOO_USER_CFG="-Djute.maxbuffer=10240000"
if [ "x$JMXLOCALONLY" = "x" ]
then
JMXLOCALONLY=false
fi
if [ "x$JMXDISABLE" = "x" ]
then
echo "JMX enabled by default" >&2
# for some reason these two options are necessary on jdk6 on Ubuntu
# accord to the docs they are not necessary, but otw jconsole cannot
# do a local attach
ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY org.apache.zookeeper.server.quorum.QuorumPeerMain"
else
echo "JMX disabled by user request" >&2
ZOOMAIN="org.apache.zookeeper.server.quorum.QuorumPeerMain"
fi
# Only follow symlinks if readlink supports it
if readlink -f "$0" > /dev/null 2>&1
then
ZOOBIN=`readlink -f "$0"`
else
ZOOBIN="$0"
fi
ZOOBINDIR=`dirname "$ZOOBIN"`
. "$ZOOBINDIR"/zkEnv.sh
if [ "x$SERVER_JVMFLAGS" ]
then
JVMFLAGS="$SERVER_JVMFLAGS $JVMFLAGS"
fi
if [ "x$2" != "x" ]
then
ZOOCFG="$ZOOCFGDIR/$2"
fi
# if we give a more complicated path to the config, don't screw around in $ZOOCFGDIR
if [ "x`dirname $ZOOCFG`" != "x$ZOOCFGDIR" ]
then
ZOOCFG="$2"
echo "Using config:$2" >&2
fi
if $cygwin
then
ZOOCFG=`cygpath -wp "$ZOOCFG"`
# cygwin has a "kill" in the shell itself, gets confused
KILL=/bin/kill
else
KILL=kill
fi
echo "Using config: $ZOOCFG" >&2
if [ -z $ZOOPIDFILE ]
then ZOOPIDFILE=$(grep dataDir "$ZOOCFG" | sed -e 's/.*=//')/zookeeper_server.pid
fi
_ZOO_DAEMON_OUT="$ZOO_LOG_DIR/zookeeper.out"
case $1 in
start)
echo -n "Starting zookeeper ... "
if [ -f $ZOOPIDFILE ]; then
if kill -0 `cat $ZOOPIDFILE` > /dev/null 2>&1; then
echo $command already running as process `cat $ZOOPIDFILE`.
exit 0
fi
fi
nohup $JAVA "$ZOO_USER_CFG" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG" > "$_ZOO_DAEMON_OUT" 2>&1 < /dev/null &
if [ $? -eq 0 ]
then
if /bin/echo -n $! > "$ZOOPIDFILE"
then
sleep 1
echo STARTED
else
echo FAILED TO WRITE PID
exit 1
fi
else
echo SERVER DID NOT START
exit 1
fi
;;
start-foreground)
ZOO_CMD="exec $JAVA"
if [ "${ZOO_NOEXEC}" != "" ]; then
ZOO_CMD="$JAVA"
fi
$ZOO_CMD "$ZOO_USER_CFG" "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS $ZOOMAIN "$ZOOCFG"
;;
print-cmd)
echo "$JAVA -Dzookeeper.log.dir=\"${ZOO_LOG_DIR}\" -Dzookeeper.root.logger=\"${ZOO_LOG4J_PROP}\" -cp \"$CLASSPATH\" $JVMFLAGS $ZOOMAIN \"$ZOOCFG\" > \"$_ZOO_DAEMON_OUT\" 2>&1 < /dev/null"
;;
stop)
echo -n "Stopping zookeeper ... "
if [ ! -f "$ZOOPIDFILE" ]
then
echo "no zookeeper to stop (could not find file $ZOOPIDFILE)"
else
$KILL -9 $(cat "$ZOOPIDFILE")
rm "$ZOOPIDFILE"
echo STOPPED
fi
;;
upgrade)
shift
echo "upgrading the servers to 3.*"
$JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.server.upgrade.UpgradeMain ${@}
echo "Upgrading ... "
;;
restart)
shift
"$0" stop ${@}
sleep 3
"$0" start ${@}
;;
status)
# -q is necessary on some versions of linux where nc returns too quickly, and no stat result is output
STAT=`$JAVA "-Dzookeeper.log.dir=${ZOO_LOG_DIR}" "-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}" \
-cp "$CLASSPATH" $JVMFLAGS org.apache.zookeeper.client.FourLetterWordMain localhost \
$(grep "^[[:space:]]*clientPort" "$ZOOCFG" | sed -e 's/.*=//') srvr 2> /dev/null \
| grep Mode`
if [ "x$STAT" = "x" ]
then
echo "Error contacting service. It is probably not running."
exit 1
else
echo $STAT
exit 0
fi
;;
*)
echo "Usage: $0 {start|start-foreground|stop|restart|status|upgrade|print-cmd}" >&2
esac
⑧ apache zookeeper是干什麼的
簡介ZooKeeper是Hadoop的正式子項目,它是一個針對大型分布式系統的可靠協調系統,提供的功能包括:配置維護、名字服務、分布式同步、組服務等。ZooKeeper的目標就是封裝好復雜易出錯的關鍵服務,將簡單易用的介面和性能高效、功能穩定的系統提供給用戶。
Zookeeper是Google的Chubby一個開源的實現.是高有效和可靠的協同工作系統.Zookeeper能夠用來leader選舉,配置信息維護等.在一個分布式的環境中,我們需要一個Master實例或存儲一些配置信息,確保文件寫入的一致性等.[1]
ZooKeeper是一個分布式的,開放源碼的分布式應用程序協調服務,包含一個簡單的原語集,是Hadoop和Hbase的重要組件。
說白了是hadoop的組件之一,用來管理hadoop。
⑨ zk是內存存儲還是磁碟
據我了解是磁碟存儲,一級緩存在內存