ACL的问题,即权限的问题。客户端设置的时候解决这个问题。
是或的运算就是相加的运算。
加入三个权限。
上节课遗漏的地方?
第一步:echo -n luban:123456 | openssl dgst -binary -sha1 | openssl base64
2Rz3ZtRZEs5RILjmwuXW/wT13Tk=
第二步:setAcl /luban2/xiaoluban78 digest:luban:2Rz3ZtRZEs5RILjmwuXW/wT13Tk=:ra
第三步:addauth digest luban:123456
第四步:
代码:
public class Datatest {
ZooKeeper zooKeeper;
@Before
public void init() throws IOException {
String conn = "10.211.55.10:2181";
zooKeeper = new ZooKeeper(conn,4000,new Watcher(){
public void process(WatchedEvent event){
System.out.println(8888);
System.out.println(event.getPath());
}
});
}
@Test
public void getData() throws KeeperException, InterruptedException {
byte[] data = zooKeeper.getData("/luban2",false,null);
System.out.println(new String(data));
System.out.println(123);
}
@Test
public void getDataWatch() throws KeeperException, InterruptedException {
byte[] data = zooKeeper.getData("/luban2",true,null);
System.out.println(new String(data));
System.out.println(123);
Thread.sleep(Long.MAX_VALUE);
}
@Test
public void getData3() throws KeeperException, InterruptedException {
Stat stat = new Stat();
zooKeeper.getData("/luban2", new Watcher() {
public void process(WatchedEvent event) {
try {
zooKeeper.getData(event.getPath(),this,null);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(event.getPath());
}
},stat);
System.out.println(stat);
Thread.sleep(Long.MAX_VALUE);
}
@Test
public void getChildren() throws KeeperException, InterruptedException {
List<String> list = zooKeeper.getChildren("/luban2",false);
list.stream().forEach(System.out::println);
}
@Test
public void getChildrenWatch() throws KeeperException, InterruptedException {
List<String> list = zooKeeper.getChildren("/luban2",event->{
// 数据的修改是不会被触发的
System.out.println(event.getPath());
try {
zooKeeper.getChildren(event.getPath(),false);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
list.stream().forEach(System.out::println);
Thread.sleep(Long.MAX_VALUE);
}
@Test
public void getCallBack() throws KeeperException, InterruptedException {
zooKeeper.getData("/luban2", false, new AsyncCallback.DataCallback() {
@Override
public void processResult(int i, String s, Object o, byte[] bytes, Stat stat) {
// i是是不是成功的 s是 ctx是传的低下那个值 data是返回的结果
System.out.println(stat);
}
},"123");
Thread.sleep(Long.MAX_VALUE);
}
@Test
public void create() throws KeeperException, InterruptedException {
// ACL是必须填的
List<ACL> list = new ArrayList<>();
int perm = ZooDefs.Perms.ADMIN|ZooDefs.Perms.READ;
// 用数字位移的方式来编写
int c = 1<<0;
int D = 1<<1;
int Q = 1<<2;
ACL acl1 = new ACL(perm,new Id("world","anyone"));
ACL acl2 = new ACL(perm,new Id("ip","10.211.55.10"));
ACL acl3 = new ACL(perm,new Id("ip","127.0.0.1"));
list.add(acl1);
list.add(acl2);
list.add(acl3);
zooKeeper.create("/luban2/xiaoluban78","hello".getBytes(),list,CreateMode.PERSISTENT);
}
}
zkClient:
api
-------
下半节课
--------
我的集群在10.211.55.10
zookeeper集群。
伪集群的搭建:https://blog.csdn.net/vbirdbest/article/details/82688462
第一步:在datadir下创建myid文件夹
第二步:写配置文件
第三步:启动三个集群
zkServer.sh start /usr/local/apps/zookeeper/zookeeper02/zookeeper/conf/zoo.cfg
zkServer.sh start /usr/local/apps/zookeeper/zookeeper03/zookeeper/conf/zoo.cfg
zkServer.sh start /usr/local/apps/zookeeper/zookeeper/conf/zoo.cfg
第四步:查看状态
zkServer.sh status /usr/local/apps/zookeeper/zookeeper/conf/zoo.cfg
zkServer.sh status /usr/local/apps/zookeeper/zookeeper02/zookeeper/conf/zoo.cfg
zkServer.sh status /usr/local/apps/zookeeper/zookeeper03/zookeeper/conf/zoo.cfg
选举是如何触发的呢?
当集群中的服务器出现已下两种情况时会进行Leader的选举
- 服务节点初始化启动
- 半数以上的节点无法和Leader建立连接
当节点初始起动时会在集群中寻找Leader节点,如果找到则与Leader建立连接,其自身状态变化follower或observer。如果没有找到Leader,当前节点状态将变化LOOKING,进入选举流程。
在集群运行其间如果有follower或observer节点宕机只要不超过半数并不会影响整个集群服务的正常运行。但如果leader宕机,将暂停对外服务,所有follower将进入LOOKING 状态,进入选举流程。
zookeeper的选举机制:https://www.cnblogs.com/shuaiandjun/p/9383655.html
选举:https://blog.csdn.net/why15732625998/article/details/80867151
选举:https://www.jianshu.com/p/77f80f2b585a
---
加入四字运维命令。