» Watcher 在 ZooKeeper 是一个核心功能,Watcher 可以监控目录节点的数据变化以及子目录的变化,一旦这些状态发生变化,服务器就会通知所有设置在这个目录节点上的 Watcher,从而每个客户端都很快知道它所关注的目录节点的状态发生变化,而做出相应的反应
» 可以设置观察的操作:exists,getChildren,getData
» 可以触发观察的操作:create,delete,setData
1、RMI解释
RMI:远程方法调用(Remote Method Invocation)。能够让在某个java虚拟机上的对象像调用本地对象一样调用另一个java 虚拟机中的对象上的方法。
2、RMI搭建步骤
2.1、创建远程方法接口,该接口必须继承自Remote接口
Remote 接口是一个标识接口,用于标识所包含的方法可以从非本地虚拟机上调用的接口,Remote接口本身不包含任何方法,这里我们创建一个HelloService接口。代码如下
package demo.zookeeper.remoting.common;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloService extends Remote {
String sayHello(String name) throws RemoteException;
}
2.2、创建远程方法接口实现类:
UnicastRemoteObject类的构造函数抛出了RemoteException,故其继承类不能使用默认构造函数,继承类的构造函数必须也抛出RemoteException,由于方法参数与返回值最终都将在网络上传输,故必须是可序列化的
package demo.zookeeper.remoting.server;
import demo.zookeeper.remoting.common.HelloService;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService {
private static final long serialVersionUID = 1L;
private static int a = -1;
protected HelloServiceImpl() throws RemoteException {
}
@Override
public String sayHello(String name) throws RemoteException {
a ++;
return String.format("Hello %s" + "[" + a + "] ", name);
}
}
2.3、指定url,注册并绑定服务
package demo.zookeeper.remoting.server;
import java.rmi.Naming;
import java.rmi.registry.LocateRegistry;
public class RmiServer {
public static void main(String[] args) throws Exception {
int port = 1099;
String ip = "192.168.1.7";
String url = "rmi://" + ip + ":" + port + "/test";
LocateRegistry.createRegistry(port);
Naming.rebind(url, new HelloServiceImpl());
}
}
2.4、客户端调用服务
package demo.zookeeper.remoting.client;
import demo.zookeeper.remoting.common.HelloService;
import java.rmi.Naming;
public class RmiClient {
public static void main(String[] args) throws Exception {
int port = 1099;
String ip = "192.168.1.7";
String url = "rmi://" + ip + ":" + port + "/test";
HelloService helloService = (HelloService) Naming.lookup(url);
String result = helloService.sayHello("Jack");
System.out.println(result);
}
}
2.5、启动服务端
执行RmiServer.java中main方法启动服务。
2.6、客户端调用服务端
执行RmiClient中main方法调用服务端,调用效果如下图:
三、使用Zookeeper实现RMI服务高可用
1、前言
前面我们知道RMI在服务端绑定了一个url,客户端可以通过这个url获取到服务端的服务对象,进而调用。显而易见当服务端挂掉之后客户端将无法正常调用,这不是高可用的。
为了实现高可用,我们有如下思路:
1.1、 开启多个RMI服务
1.2、将每个RMI的URL注册写入zookeeper路径/registry/provider中,其中路径类型选择EPHEMERAL_SEQUENTIAL,因为这样当服务端挂掉后,对应的zokeeper节点会自动删除。
1.3、客户端每一次调用,都在zookeeper的/registry/provider中遍获取可用url,再在其中随机拿一个url使用。
2、代码实现
2.1、Server01.java
package demo.zookeeper.remoting.server;
import demo.zookeeper.remoting.common.HelloService;
public class Server01 {
public static void main(String[] args) throws Exception {
String host = "192.168.1.7";
int port = Integer.parseInt("11231");
ServiceProvider provider = new ServiceProvider();
HelloService helloService = new HelloServiceImpl();
provider.publish(helloService, host, port);
Thread.sleep(Long.MAX_VALUE);
}
}
2.2、Server02.java
package demo.zookeeper.remoting.server;
import demo.zookeeper.remoting.common.HelloService;
public class Server02 {
public static void main(String[] args) throws Exception {
String host = "192.168.1.7";
int port = Integer.parseInt("11232");
ServiceProvider provider = new ServiceProvider();
HelloService helloService = new HelloServiceImpl();
provider.publish(helloService, host, port);
Thread.sleep(Long.MAX_VALUE);
}
}
2.3、Server03.java
package demo.zookeeper.remoting.server;
import demo.zookeeper.remoting.common.HelloService;
public class Server03 {
public static void main(String[] args) throws Exception {
String host = "192.168.1.7";
int port = Integer.parseInt("11233");
ServiceProvider provider = new ServiceProvider();
HelloService helloService = new HelloServiceImpl();
provider.publish(helloService, host, port);
Thread.sleep(Long.MAX_VALUE);
}
}
2.4、ServiceProvider.java
package demo.zookeeper.remoting.server;
import demo.zookeeper.remoting.common.Constant;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.util.concurrent.CountDownLatch;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ServiceProvider {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceProvider.class);
// 用于等待 SyncConnected 事件触发后继续执行当前线程
private CountDownLatch latch = new CountDownLatch(1);
// 发布 RMI 服务并注册 RMI 地址到 ZooKeeper 中
public void publish(Remote remote, String host, int port) {
String url = publishService(remote, host, port); // 发布 RMI 服务并返回 RMI 地址
if (url != null) {
ZooKeeper zk = connectServer(); // 连接 ZooKeeper 服务器并获取 ZooKeeper 对象
if (zk != null) {
createNode(zk, url); // 创建 ZNode 并将 RMI 地址放入 ZNode 上
}
}
}
// 发布 RMI 服务
private String publishService(Remote remote, String host, int port) {
String url = null;
try {
url = String.format("rmi://%s:%d/%s", host, port, remote.getClass().getName());
LocateRegistry.createRegistry(port);
Naming.rebind(url, remote);
LOGGER.debug("publish rmi service (url: {})", url);
} catch (RemoteException | MalformedURLException e) {
LOGGER.error("", e);
}
return url;
}
// 连接 ZooKeeper 服务器
private ZooKeeper connectServer() {
ZooKeeper zk = null;
try {
zk = new ZooKeeper(Constant.ZK_CONNECTION_STRING, Constant.ZK_SESSION_TIMEOUT, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.SyncConnected) {
latch.countDown(); // 唤醒当前正在执行的线程
}
}
});
latch.await(); // 使当前线程处于等待状态
} catch (IOException | InterruptedException e) {
LOGGER.error("", e);
}
return zk;
}
// 创建 ZNode
private void createNode(ZooKeeper zk, String url) {
try {
byte[] data = url.getBytes();
// path:创建节点路径,需保证父节点已存在
// data:节点数据
// acl:权限列表
// OPEN_ACL_UNSAFE:完全开放
// CREATOR_ALL_ACL:创建该znode的连接拥有所有权限
// READ_ACL_UNSAFE:所有的客户端都可读
// createMode:节点类型
// PERSISTENT:持久化节点
// PERSISTENT_SEQUENTIAL:持久化有序节点
// EPHEMERAL:临时节点(连接断开自动删除)
// EPHEMERAL_SEQUENTIAL:临时有序节点(连接断开自动删除)
String path = zk.create(Constant.ZK_PROVIDER_PATH, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); // 创建一个临时性且有序的 ZNode
LOGGER.debug("create zookeeper node ({} => {})", path, url);
} catch (KeeperException | InterruptedException e) {
LOGGER.error("", e);
}
}
}
2.5、HelloServiceImpl.java
package demo.zookeeper.remoting.server;
import demo.zookeeper.remoting.common.HelloService;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService {
private static final long serialVersionUID = 1L;
private static int a = -1;
protected HelloServiceImpl() throws RemoteException {
}
@Override
public String sayHello(String name) throws RemoteException {
a ++;
return String.format("Hello %s" + "[" + a + "] ", name);
}
}
2.6、HelloService.java
package demo.zookeeper.remoting.common;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface HelloService extends Remote {
String sayHello(String name) throws RemoteException;
}
2.7、Client.java
package demo.zookeeper.remoting.client;
import demo.zookeeper.remoting.common.HelloService;
public class Client {
public static void main(String[] args) throws Exception {
ServiceConsumer consumer = new ServiceConsumer();
while (true) {
HelloService helloService = consumer.lookup();
String result = helloService.sayHello("Jack");
System.out.println(result);
Thread.sleep(3000);
}
}
}
2.8、ServiceConsumer.java
package demo.zookeeper.remoting.client;
import demo.zookeeper.remoting.common.Constant;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.ConnectException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ServiceConsumer {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceConsumer.class);
// 用于等待 SyncConnected 事件触发后继续执行当前线程
private CountDownLatch latch = new CountDownLatch(1);
// 定义一个 volatile 成员变量,用于保存最新的 RMI 地址(考虑到该变量或许会被其它线程所修改,一旦修改后,该变量的值会影响到所有线程)
private volatile List urlList = new ArrayList<>();
// 构造器
public ServiceConsumer() {
ZooKeeper zk = connectServer(); // 连接 ZooKeeper 服务器并获取 ZooKeeper 对象
if (zk != null) {
watchNode(zk); // 观察 /registry 节点的所有子节点并更新 urlList 成员变量
}
}
// 查找 RMI 服务
public T lookup() {
T service = null;
int size = urlList.size();
if (size > 0) {
String url;
if (size == 1) {
url = urlList.get(0); // 若 urlList 中只有一个元素,则直接获取该元素
LOGGER.debug("using only url: {}", url);
} else {
url = urlList.get(ThreadLocalRandom.current().nextInt(size)); // 若 urlList 中存在多个元素,则随机获取一个元素
LOGGER.debug("using random url: {}", url);
}
service = lookupService(url); // 从 JNDI 中查找 RMI 服务
}
return service;
}
// 连接 ZooKeeper 服务器
private ZooKeeper connectServer() {
ZooKeeper zk = null;
try {
zk = new ZooKeeper(Constant.ZK_CONNECTION_STRING, Constant.ZK_SESSION_TIMEOUT, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getState() == Event.KeeperState.SyncConnected) {
latch.countDown(); // 唤醒当前正在执行的线程
}
}
});
latch.await(); // 使当前线程处于等待状态
} catch (IOException | InterruptedException e) {
LOGGER.error("", e);
}
return zk;
}
// 观察 /registry 节点下所有子节点是否有变化
private void watchNode(final ZooKeeper zk) {
try {
List nodeList = zk.getChildren(Constant.ZK_REGISTRY_PATH, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (event.getType() == Event.EventType.NodeChildrenChanged) {
watchNode(zk); // 若子节点有变化,则重新调用该方法(为了获取最新子节点中的数据)
}
}
});
List dataList = new ArrayList<>(); // 用于存放 /registry 所有子节点中的数据
for (String node : nodeList) {
byte[] data = zk.getData(Constant.ZK_REGISTRY_PATH + "/" + node, false, null); // 获取 /registry 的子节点中的数据
dataList.add(new String(data));
}
LOGGER.debug("node data: {}", dataList);
urlList = dataList; // 更新最新的 RMI 地址
} catch (KeeperException | InterruptedException e) {
LOGGER.error("", e);
}
}
// 在 JNDI 中查找 RMI 远程服务对象
@SuppressWarnings("unchecked")
private T lookupService(String url) {
T remote = null;
try {
remote = (T) Naming.lookup(url);
} catch (NotBoundException | MalformedURLException | RemoteException e) {
if (e instanceof ConnectException) {
// 若连接中断,则使用 urlList 中第一个 RMI 地址来查找(这是一种简单的重试方式,确保不会抛出异常)
LOGGER.error("ConnectException -> url: {}", url);
if (urlList.size() != 0) {
url = urlList.get(0);
return lookupService(url);
}
}
LOGGER.error("", e);
}
return remote;
}
}
3、执行代码
3.1、分别执行Server01、Server02、Server03重点main方法,启动三个RMI服务,并将其URL注册在zookeeper上。
启动前:
启动后:
3.2、调用Client中main方法
我们可以看出Jack后面的数字不是递增的,这就说明请求北分发到了不同的RMI服务中。