淘先锋技术网

首页 1 2 3 4 5 6 7

1.zookeeper介绍

zookeeper是一个开源的分布式系统,我i分布式应用提供协调服务的Apache项目,多用作为集群提供服务的中间件。
zookeeper从设计模式角度来理解,是一个基于观察者模式设计的分布式服务管理框架,它负责存储和管理大家关心的数据,然后接受观察者的注册,一旦这些数据的状态发生变化,zookeeper就将负责通知已经在zookeeper上注册的那些观察者做出相应的反应,从而实现集群中类似Master/Slave管理模式。

zookeeper特点
1.一致性:zookeeper中的数据按照顺序分批入库,且最终一致性。
2.原子性:一次数据要么成功,要么失败。
3.单一视图:client无论连接到哪个节点,数据都是一致的。
4.可靠性: 每次对zk的操作状态都会保存到服务端,每个server保存一份相同的数据副本。

2安装zookeeper

将zookeeper安装包上传到h2机器上,然后解压到/opt/module下。

[hzhao@h2 ~]$ tar 0-zxvf /opt/soft/zookeeper-3.4.10.tar.gz -C /opt/module/

修改zookeeper的配置文件。

[hzhao@h2 ~]$ mkdir /opt/module/zookeeper-3.4.10/datas
[hzhao@h2 ~]$ cp -p /opt/module/zookeeper-3.4.10/conf/zoo_sample.cfg /opt/module/zookeeper-3.4.10/conf/zoo.cfg
[hzhao@h2 ~]$ vim /opt/module/zookeeper-3.4.10/conf/zoo.cfg

修改dataDir

dataDir=/opt/module/zookeeper-3.4.10/datas

单机启动zookeeper服务端

[hzhao@h2 ~]$ /opt/module/zookeeper-3.4.10/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[hzhao@h2 ~]$ /opt/module/zookeeper-3.4.10/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: standalone

启动zookeeper客户端

[hzhao@h2 ~]$ /opt/module/zookeeper-3.4.10/bin/zkCli.sh -server h2:2181 ls /

修改日志配置

[hzhao@h2 ~]$ mkdir /opt/module/zookeeper-3.4.10/logs
[hzhao@h2 ~]$ vim /opt/module/zookeeper-3.4.10/bin/zkEnv.sh

加入服务端日志文件输出

ZOO_LOG_DIR=/opt/module/zookeeper-3.4.10/logs

修改客户端日志级别

ZOO_LOG4J_PROP=“ERROR,CONSOLE”

3.四字命令

安装四字命令客户端

[hzhao@h2 ~]$ sudo yum -y install nc
[hzhao@h2 ~]$ nc h2 2181
conf

4.zookeeper集群搭建

修改集群配置文件

[hzhao@h2 conf]$ vim /opt/module/zookeeper-3.4.10/conf/zoo.cfg

文件中加入如下内容。server.id=hostname:2888:3888

server.1=h1:2888:3888
server.2=h2:2888:3888
server.3=h3:2888:3888

向机器h1和h3分发该文件

[hzhao@h2 conf]$ xsync /opt/module/zookeeper-3.4.10/conf/zoo.cfg

指定机器id

[hzhao@h2 conf]$ cd /opt/module/zookeeper-3.4.10/datas/
[hzhao@h2 datas]$ vim myid

文件内容为server后面对应的id,比如h2的机器id为2。同样在机器h1和h3中指定myid文件中的id。
启动zookeeper集群并查看集群状态

[hzhao@h2 ~]$ xcall /opt/module/zookeeper-3.4.10/bin/zkServer.sh start
[hzhao@h2 ~]$ xcall /opt/module/zookeeper-3.4.10/bin/zkServer.sh status
要执行的命令是/opt/module/zookeeper-3.4.10/bin/zkServer.sh status
----------------h1---------------
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
----------------h2---------------
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
----------------h3---------------
ZooKeeper JMX enabled by default
Using config: /opt/module/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower

5.java操作zookeeper集群

引入maven依赖

<dependency>
	<groupId>org.apache.zookeeper</groupId>
	<artifactId>zookeeper</artifactId>
	<version>3.3.6</version>
</dependency>

java操作zookeeper集群

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
public class TestZookeeper {
    private String connectString = "h1:2181,h2:2181";
    private  int sessionTimeout = 6000;
    private ZooKeeper zooKeeper;

    @Before
    public void init() throws IOException {
        //创建一个zk的客户端对象
        zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            //回调方法,一旦watcher观察的path触发了指定的事件,服务端会通知客户端,客户端收到通知后会自动调用process
            public void process(WatchedEvent watchedEvent) {

            }
        });
        System.out.println(zooKeeper);
    }
    @After
    public void close() throws InterruptedException {
        if(zooKeeper != null) {
            zooKeeper.close();
        }
    }
    @Test
    public void ls() throws KeeperException, InterruptedException, IOException {
        Stat stat = new Stat();
        List<String> children = zooKeeper.getChildren("/", null, stat);
        System.out.println(children);
        System.out.println(stat);
    }
    @Test
    public void create() throws KeeperException, InterruptedException {
        zooKeeper.create("/idea", "hello".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    @Test
    public void get() throws KeeperException, InterruptedException {
        byte[] data = zooKeeper.getData("/idea", null, null);
        System.out.println(new String(data));
    }
    @Test
    public void set() throws KeeperException, InterruptedException {
        zooKeeper.setData("/idea", "hi".getBytes(), -1);
    }
    @Test
    public void delete() throws KeeperException, InterruptedException {
        zooKeeper.delete("/idea", -1);
    }
}