zookeeper_03:Java 客户端(原生API)

  • 环境配置
  1. 下载并ZooKeeper的发行版
  2. 新建Java project,并导入jar包
  • 创建会话

public class CreateSession implements Watcher {

private static ZooKeeper zookeeper;

public static void main(String[] args) throws IOException, InterruptedException {

  zookeeper = new ZooKeeper("192.168.1.105:2181",5000,new CreateSession());

  System.out.println(zookeeper.getState());

  Thread.sleep(Integer.MAX_VALUE);

  }

  private void doSomething(){

    System.out.println("do something");

  }

public void process(WatchedEvent event) {

  System.out.println("收到事件:"+event);

  if (event.getState()==KeeperState.SyncConnected){

    if (event.getType()==EventType.None && null==event.getPath()){

      doSomething();

    }

  }

  }

}

  • 同步创建节点

public class CreateNodeSync implements Watcher {

  private static ZooKeeper zookeeper;
  public static void main(String[] args) throws IOException, InterruptedException {
    zookeeper = new ZooKeeper("192.168.124.128:2181,192.168.124.128:2182,192.168.124.128:2183",5000,new CreateNodeSync());
    System.out.println(zookeeper.getState());

    Thread.sleep(Integer.MAX_VALUE);
  }

  private void doSomething(){
    try {
      String path = zookeeper.create("/test", "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
      System.out.println("return path:"+path);
    } catch (KeeperException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
      System.out.println("do something");
    }
  public void process(WatchedEvent event) {
    System.out.println("收到事件:"+event);
    if (event.getState()==KeeperState.SyncConnected){
      if (event.getType()==EventType.None && null==event.getPath()){
        doSomething();
      }
    }
  }
}

  • 异步创建节点

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs.Perms;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;

public class CreateNodeSyncAuth implements Watcher {

  private static ZooKeeper zookeeper;
  private static boolean somethingDone = false;

  public static void main(String[] args) throws IOException, InterruptedException {
    zookeeper = new ZooKeeper("192.168.124.128:2181,192.168.124.128:2182,192.168.124.128:2183",5000,new CreateNodeSyncAuth());
    System.out.println(zookeeper.getState());

    Thread.sleep(Integer.MAX_VALUE);
  }
  /*
  * 权限模式(scheme): ip, digest
  * 授权对象(ID)
  * ip权限模式: 具体的ip地址
  * digest权限模式: username:Base64(SHA-1(username:password))
  * 权限(permission): create(C), DELETE(D),READ(R), WRITE(W), ADMIN(A)
  * 注:单个权限,完全权限,复合权限
  *
  * 权限组合: scheme + ID + permission
  *
  *
  *
  * */
  private void doSomething(){
    try {

      ACL aclIp = new ACL(Perms.READ,new Id("ip","192.168.124.128"));
      ACL aclDigest = new ACL(Perms.READ|Perms.WRITE,new Id("digest",DigestAuthenticationProvider.generateDigest("zhangsan:123456")));
      ArrayList<ACL> acls = new ArrayList<ACL>();
      acls.add(aclDigest);
      acls.add(aclIp);
      //zookeeper.addAuthInfo("digest", "zhangsan:123456".getBytes());
      String path = zookeeper.create("/test", "test".getBytes(), acls, CreateMode.PERSISTENT);
      System.out.println("return path:"+path);

      somethingDone = true;

    } catch (KeeperException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  public void process(WatchedEvent event) {
    
    System.out.println("收到事件:"+event);
    if (event.getState()==KeeperState.SyncConnected){
      if (!somethingDone && event.getType()==EventType.None && null==event.getPath()){
        doSomething();
      }
    }
  }
}



StringCallback{

public void (int rc,String path,Object ctx,String name)

}

rc是result code 服务端响应结果码。客户端可以从这个结果码中识别出API的调用结果,常见的结果码有:

0(OK),接口调用成功

-4(ConnectionLoss),客户端和服务器连接断开

-110(NodeExists) 节点已存在

-112(SessionExpired)会话已过期

path: 接口调用传入的数据节点的节点路径

ctx: 接口调用传入的ctx参数

name: 实际在服务器端创建的节点名


  • 同步删除节点

import java.io.IOException;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;

public class DeleteNodeSync implements Watcher{

  private static ZooKeeper zooKeeper;
  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    zooKeeper = new ZooKeeper("192.168.1.105:2181",5000,new DeleteNodeSync());
    System.out.println(zooKeeper.getState().toString());

    Thread.sleep(Integer.MAX_VALUE);

  }

  private void doSomething(ZooKeeper zooKeeper){
    try {
      zooKeeper.delete("/node_7", -1);
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (KeeperException e) {
      e.printStackTrace();
    }
  }

  @Override
  public void process(WatchedEvent event) {
    if (event.getState()==KeeperState.SyncConnected){
      if (event.getType()==EventType.None && null==event.getPath()){
        doSomething(zooKeeper);
      }
    }
  }
}

  • 异步删除节点

import java.io.IOException;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;

public class DeleteNodeASync implements Watcher{

  private static ZooKeeper zooKeeper;
  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    zooKeeper = new ZooKeeper("192.168.1.105:2181",5000,new DeleteNodeASync());
    System.out.println(zooKeeper.getState().toString());
    Thread.sleep(Integer.MAX_VALUE);
  }

  private void doSomething(WatchedEvent event){
    zooKeeper.delete("/node_6", -1, new IVoidCallback(),null);
  }

  public void process(WatchedEvent event) {
    if (event.getState()==KeeperState.SyncConnected){
      if (event.getType()==EventType.None && null==event.getPath()){
        doSomething(event);
      }
    }
  }

  static class IVoidCallback implements AsyncCallback.VoidCallback{
    public void processResult(int rc, String path, Object ctx) {
      StringBuilder sb = new StringBuilder();
      sb.append("rc="+rc).append("\n");
      sb.append("path"+path).append("\n");
      sb.append("ctx="+ctx).append("\n");
      System.out.println(sb.toString());
    }
  }

}

  • 同步读取节点

import java.io.IOException;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class GetDataSync implements Watcher{
  private static ZooKeeper zooKeeper;
  private static Stat stat = new Stat();

  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    zooKeeper = new ZooKeeper("192.168.1.105:2181",5000,new GetDataSync());
    System.out.println(zooKeeper.getState().toString());
    Thread.sleep(Integer.MAX_VALUE);
  }

  private void doSomething(ZooKeeper zookeeper){
    zookeeper.addAuthInfo("digest", "jike:123456".getBytes());
    try {
      System.out.println(new String(zooKeeper.getData("/node_4", true, stat)));
    } catch (KeeperException e) {
      e.printStackTrace();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }

  public void process(WatchedEvent event) {
    if (event.getState()==KeeperState.SyncConnected){
      if (event.getType()==EventType.None && null==event.getPath()){
        doSomething(zooKeeper);
      }else{
        if (event.getType()==EventType.NodeDataChanged){
          try {
            System.out.println(new String(zooKeeper.getData(event.getPath(), true, stat)));
            System.out.println("stat:"+stat);
          } catch (KeeperException e) {
            e.printStackTrace();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }

    }
  }

}

  • 异步读取节点

import java.io.IOException;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class GetDataASync implements Watcher{
  private static ZooKeeper zooKeeper;
  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    zooKeeper = new ZooKeeper("192.168.1.105:2181",5000,new GetDataASync());
    System.out.println(zooKeeper.getState().toString());
    Thread.sleep(Integer.MAX_VALUE);
  }

  private void doSomething(ZooKeeper zookeeper){
    zooKeeper.getData("/node_1", true, new IDataCallback(), null);
  }

  public void process(WatchedEvent event) {
    if (event.getState()==KeeperState.SyncConnected){
      if (event.getType()==EventType.None && null==event.getPath()){
        doSomething(zooKeeper);
      }else{
        if (event.getType()==EventType.NodeDataChanged){
          try {
            zooKeeper.getData(event.getPath(), true, new IDataCallback(), null);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
  }
}

  static class IDataCallback implements AsyncCallback.DataCallback{
    public void processResult(int rc, String path, Object ctx, byte[] data,Stat stat) {
      try {
        System.out.println(new String(zooKeeper.getData(path, true, stat)));
        System.out.println("stat:"+stat);
      } catch (KeeperException e) {
        e.printStackTrace();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
}

  • 同步节点检测

import java.io.IOException;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class NodeExistsSync implements Watcher{

  private static ZooKeeper zooKeeper;
  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    zooKeeper = new ZooKeeper("192.168.1.105:2181",5000,new NodeExistsSync());
    System.out.println(zooKeeper.getState().toString());
    Thread.sleep(Integer.MAX_VALUE);
  }

  private void doSomething(ZooKeeper zooKeeper){
    try{
      Stat stat = zooKeeper.exists("/node_1", true);
      System.out.println(stat);
    }catch(Exception e){
    }
  }

  @Override
  public void process(WatchedEvent event) {
    if (event.getState()==KeeperState.SyncConnected){
      if (event.getType()==EventType.None && null==event.getPath()){
        doSomething(zooKeeper);
      }else{
        try {
          if (event.getType()==EventType.NodeCreated){
            System.out.println(event.getPath()+" created");
            System.out.println(zooKeeper.exists(event.getPath(), true));
          }
          else if (event.getType()==EventType.NodeDataChanged){
            System.out.println(event.getPath()+" updated");
            System.out.println(zooKeeper.exists(event.getPath(), true));
          }
          else if (event.getType()==EventType.NodeDeleted){
            System.out.println(event.getPath()+" deleted");
            System.out.println(zooKeeper.exists(event.getPath(), true));
          }

         } catch (Exception e) {
         }
      }
    }
  }
}

  • 异步检测节点

import java.io.IOException;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class NodeExistsASync implements Watcher{
  private static ZooKeeper zooKeeper;
  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
  zooKeeper = new ZooKeeper("192.168.1.105:2181",5000,new NodeExistsASync());
  System.out.println(zooKeeper.getState().toString());
  Thread.sleep(Integer.MAX_VALUE);
}

private void doSomething(ZooKeeper zookeeper){
  zooKeeper.exists("/node_1", true, new IStateCallback(), null);
}

public void process(WatchedEvent event) {

if (event.getState()==KeeperState.SyncConnected){
  if (event.getType()==EventType.None && null==event.getPath()){
    doSomething(zooKeeper);
  }else{
    try {
      if (event.getType()==EventType.NodeCreated){
        System.out.println(event.getPath()+" created");
        zooKeeper.exists(event.getPath(), true, new IStateCallback(), null);
      }
      else if (event.getType()==EventType.NodeDataChanged){
        System.out.println(event.getPath()+" updated");
        zooKeeper.exists(event.getPath(), true, new IStateCallback(), null);
      }
      else if (event.getType()==EventType.NodeDeleted){
        System.out.println(event.getPath()+" deleted");
        zooKeeper.exists(event.getPath(), true, new IStateCallback(), null);
      }
    } catch (Exception e) {
    }
  }
}

}


static class IStateCallback implements AsyncCallback.StatCallback{

    public void processResult(int rc, String path, Object ctx, Stat stat) {
      System.out.println("rc:"+rc);
    }
  }
}

  • 同步获取子节点

import java.io.IOException;
import java.util.List;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
public class GetChildrenSync implements Watcher{
  private static ZooKeeper zooKeeper;
  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    zooKeeper = new ZooKeeper("192.168.1.105:2181",5000,new GetChildrenSync());
    System.out.println(zooKeeper.getState().toString());
    Thread.sleep(Integer.MAX_VALUE);
  }

  private void doSomething(ZooKeeper zooKeeper){
    try {
      List<String> children = zooKeeper.getChildren("/", true);
      System.out.println(children);
    } catch (Exception e) {
    }
  }

  public void process(WatchedEvent event) {
    if (event.getState()==KeeperState.SyncConnected){
      if (event.getType()==EventType.None && null==event.getPath()){
        doSomething(zooKeeper);
      }else{
        if (event.getType()==EventType.NodeChildrenChanged){
          try {
            System.out.println(zooKeeper.getChildren(event.getPath(), true));
          } catch (KeeperException e) {
            e.printStackTrace();
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }
      }
    }
  }
}

  • 异步获取子节点

import java.io.IOException;
import java.util.List;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class GetChildrenASync implements Watcher{
  private static ZooKeeper zooKeeper;
  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    zooKeeper = new ZooKeeper("192.168.1.105:2181",5000,new GetChildrenASync());
    System.out.println(zooKeeper.getState().toString());
    Thread.sleep(Integer.MAX_VALUE);
  }

  private void doSomething(ZooKeeper zookeeper){
    try {
      zooKeeper.getChildren("/", true, new IChildren2Callback(), null);
    } catch (Exception e) {
    }
  }

  public void process(WatchedEvent event) {
    if (event.getState()==KeeperState.SyncConnected){
      if (event.getType()==EventType.None && null==event.getPath()){
        doSomething(zooKeeper);
      }else{
        if (event.getType()==EventType.NodeChildrenChanged){
          zooKeeper.getChildren(event.getPath(), true, new IChildren2Callback(), null);
        }
      }
    }
  }

  static class IChildren2Callback implements AsyncCallback.Children2Callback{
    public void processResult(int rc, String path, Object ctx,List<String> children, Stat stat) {
      StringBuilder sb = new StringBuilder();
      sb.append("rc="+rc).append("\n");
      sb.append("path="+path).append("\n");
      sb.append("ctx="+ctx).append("\n");
      sb.append("children="+children).append("\n");
      sb.append("stat="+stat).append("\n");
      System.out.println(sb.toString());
    }
  }
}

  • 同步更新

import java.io.IOException;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class UpdateNodeSync implements Watcher{
  private static ZooKeeper zooKeeper;
  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    zooKeeper = new ZooKeeper("192.168.1.105:2181",5000,new UpdateNodeSync());
    System.out.println(zooKeeper.getState().toString());
    Thread.sleep(Integer.MAX_VALUE);
  }
  private void doSomething(ZooKeeper zooKeeper){
    try {
      Stat stat = zooKeeper.setData("/node_6", "123".getBytes(), -1);
      System.out.println("stat:"+stat);
    } catch (InterruptedException e) {
      e.printStackTrace();
    } catch (KeeperException e) {
    e.printStackTrace();
    }
  }

  public void process(WatchedEvent event) {
    if (event.getState()==KeeperState.SyncConnected){
      if (event.getType()==EventType.None && null==event.getPath()){
        doSomething(zooKeeper);
      }
    }
  }
}

  • 异步更新

import java.io.IOException;

import org.apache.zookeeper.AsyncCallback;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

public class UpdateNodeASync implements Watcher{

  private static ZooKeeper zooKeeper;
  public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    zooKeeper = new ZooKeeper("192.168.1.105:2181",5000,new UpdateNodeASync());
    System.out.println(zooKeeper.getState().toString());
    Thread.sleep(Integer.MAX_VALUE);
  }

  private void doSomething(WatchedEvent event){
    zooKeeper.setData("/node_6", "234".getBytes(), -1, new IStatCallback(),null);
  }

  public void process(WatchedEvent event) {
    if (event.getState()==KeeperState.SyncConnected){
      if (event.getType()==EventType.None && null==event.getPath()){
        doSomething(event);
      }
    }
  }

  static class IStatCallback implements AsyncCallback.StatCallback{

    public void processResult(int rc, String path, Object ctx, Stat stat) {
      StringBuilder sb = new StringBuilder();
      sb.append("rc="+rc).append("\n");
      sb.append("path"+path).append("\n");
      sb.append("ctx="+ctx).append("\n");
      sb.append("Stat="+stat).append("\n");
      System.out.println(sb.toString());
    }
  }
}

时间: 2024-10-08 09:04:23

zookeeper_03:Java 客户端(原生API)的相关文章

ZooKeeper客户端原生API的使用以及ZkClient第三方API的使用

这两部分内容的介绍主要讲的是节点及节点内容和子节点的操作,并且讲解的节点的事件监听以及ACL授权 ZooKeeper客户端原生API的使用 百度网盘地址: http://pan.baidu.com/s/1jI3b8n8 ZkClient第三方API的使用 ZkClient是Github上一个开源的ZooKeeper客户端.ZkClient在ZooKeeper原生API之上进行了包装,是一个更加易用的ZooKeeper客户端.同时ZkClient在内部实现了诸如Session超时重连.Watche

Zookeeper学习(三) 客户端和原生API

前言 在这篇博客里我会主要总结下两个部分的操作: 在安装ZooKeeper的机器上利用ZKClient连接Zookeeper的集群,然后利用相应的命令做一些简单的操作.相信很多没有接触过Zookeeper的同学对第一篇简介里的哪些ZNode等等一些概念其实不是那么清楚,但是经过实际操作后会深入了解许多. 简单介绍下做的一个小demo,介绍了一下对Zookeeper原生API的使用.我们实际项目中用的是Curator的接口,但是原生API是根本.顺便也会提一下我们利用Zookeeper做的事-简单

hadoop系列二:HDFS文件系统的命令及JAVA客户端API

转载请在页首明显处注明作者与出处 http://www.cnblogs.com/zhuxiaojie/p/6391518.html 一:说明 此为大数据系列的一些博文,有空的话会陆续更新,包含大数据的一些内容,如hadoop,spark,storm,机器学习等. 当前使用的hadoop版本为2.6.4 上一篇:hadoop系列一:hadoop集群安装 二:HDFS的shell命令 上一章说完了安装HADOOP集群部分,这一张讲HDFS. 其实基本上操作都是通过JAVA API来操作,所以这里的s

Zookeeper系列三:Zookeeper客户端的使用(Zookeeper原生API如何进行调用、ZKClient、Curator)

一.Zookeeper原生API如何进行调用 准备工作: 首先在新建一个maven项目ZK-Demo,然后在pom.xml里面引入zk的依赖 <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.10</version> </dependency> 1. 连接zk并

(原) 2.1 Zookeeper原生API使用

本文为原创文章,未经允许不得转载 Zookeeper原生API使用 1.jar包引入,演示版本为3.4.6,非maven项目,可以下载jar包导入到项目中 <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> 2.

读书笔记-HBase in Action-第二部分Advanced concepts-(3)非Java客户端

HBase Shell HBase shell使用起来最方便,进入HBase shell控制台即可使用. $ $HBASE_HOME/bin/hbase shell 常见操作有create(创建表)/put(插入或更新数据)/get(根据rowkey查询)/scan(范围查询)/delete(删除列)/deleteAll(根据rowkey删除整行数据)/disable&drop(禁用表之后再删除). 基于数据库的项目,往往会在某个目录下存储专门的sql脚本,记录每次迭代数据库变更:同理,HBas

elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RESTful style API这一层,这种客户端的连接方式是RESTful风格的,使用http的方式进行连接 3.2 Transport 连接 端口 9300 这种连接方式对应于架构图中的Transport这一层,这种客户端连接方式是直接连接ES的节点,使用TCP的方式进行连接 4. ES提供了多种

RabbitMQ(3) Java客户端使用

RabbitMQ针对不同的开发语言(java,python,c/++,Go等等),提供了丰富对客户端,方便使用.就Java而言,可供使用的客户端有RabbitMQ Java client. RabbitMQ JMS client.apache的camel-rabbitmq.以及Banyan等.在Spring中,也可以使用Spring AMQP.Spring Cloud Data Flow方便对集成RabbitMQ. 实际开发使用中,RabbitMQ Java client和Spring AMQP

ZooKeeper系列(四)—— Java 客户端 Apache Curator

一.基本依赖 Curator 是 Netflix 公司开源的一个 Zookeeper 客户端,目前由 Apache 进行维护.与 Zookeeper 原生客户端相比,Curator 的抽象层次更高,功能也更加丰富,是目前 Zookeeper 使用范围最广的 Java 客户端.本篇文章主要讲解其基本使用,项目采用 Maven 构建,以单元测试的方法进行讲解,相关依赖如下: <dependencies> <!--Curator 相关依赖--> <dependency> &l

ZooKeeper实现配置中心的实例(原生API实现)(转)

说明:要实现配置中心的例子,可以选择的SDK有很多,原生自带的SDK也是不错的选择.比如使用I0Itec,Spring Boot集成等. 大型应用通常会按业务拆分成一个个业务子系统,这些大大小小的子应用,往往会使用一些公用的资源,比如:需要文件上传.下载时,各子应用都会访问公用的Ftp服务器.如果把Ftp Server的连接IP.端口号.用户名.密码等信息,配置在各子应用中,然后这些子应用再部署到服务器集群中的N台Server上,突然有一天,Ftp服务器要换IP或端口号,那么问题来了?),而是如