Parallel processing 如何在不使用Zookeeper的情况下实现分布式优先级队列?
我想在不使用Zookeeper的情况下实现分布式优先级队列?如果您知道如何在客户端和服务器之间进行通信(例如,使用TCP套接字),那么应该很简单。服务器包含优先级队列的线程安全实现,因此提供了一个接口。客户端连接到服务器并使用此接口 服务器 服务器必须提供优先级队列接口,即支持添加、查看、轮询等。。。。重要的是,这些方法必须是线程安全的!因此,我们将使用PriorityBlockingQueue,它是同步的,而不是PriorityQueue 此类正在侦听客户端发送的内容。 根据客户端发送的内容,服务器知道该做什么,因此有一个迷你协议。该协议是:当客户机想要调用分布式优先级队列的方法时,他发送一个整数,例如2=poll。服务器读取该整数并知道调用哪个方法 请注意,有时发送一个整数就足够了,请参见轮询示例,但并不总是这样。以add为例,它必须指定一个参数。服务器将从客户端接收1,即add,并将读取第二个整数或必须存储在分布式优先级队列中的任何其他对象 客户 根据协议,服务器向客户端提供一个接口,例如0=停止通信,1=添加。。。。客户端只需连接到服务器并发送有关procotol的消息!去吧 客户示例:Parallel processing 如何在不使用Zookeeper的情况下实现分布式优先级队列?,parallel-processing,distributed-computing,priority-queue,Parallel Processing,Distributed Computing,Priority Queue,我想在不使用Zookeeper的情况下实现分布式优先级队列?如果您知道如何在客户端和服务器之间进行通信(例如,使用TCP套接字),那么应该很简单。服务器包含优先级队列的线程安全实现,因此提供了一个接口。客户端连接到服务器并使用此接口 服务器 服务器必须提供优先级队列接口,即支持添加、查看、轮询等。。。。重要的是,这些方法必须是线程安全的!因此,我们将使用PriorityBlockingQueue,它是同步的,而不是PriorityQueue 此类正在侦听客户端发送的内容。 根据客户端发送的内容,
public class PQ_Client {
private static Socket skt;
private InputStream in;
private OutputStream out;
private final int _STOP_ = 0, _ADD_ = 1, _POLL_ = 2; // By convention (protocol)
PQ_Client(String ip, int port) {
try {
this.skt = new Socket(ip, port);
this.in = skt.getInputStream();
this.out = skt.getOutputStream();
System.out.println("Connected to distributed priority queue.");
} catch(IOException e) {
System.out.println("Could not connect with the distributed priority queue : " + e.getStackTrace());
}
}
// Sort of stub functions
public void stop() {
out.write(_STOP_);
out.flush();
out.close();
}
public void add(Integer el) {
out.write(_ADD_); // Send wanted operation
out.write(el); // Send argument element
// Real implementation would listen for result here
out.flush();
}
public int poll() {
out.write(_POLL_);
out.flush();
// Listen for answer
return in.read();
}
/*
* Rest of implementation
*/
}
注意,由于这些自制的存根函数,我们可以创建一个PQ_客户机对象,并将其作为优先级队列使用。客户机/服务器通信隐藏在存根后面
String ip = "...";
int port = 5555;
PQ_Client pq = new PQ_Client(ip , port);
pq.add(5);
pq.add(2);
pq.add(4);
int res = pq.poll();
注意,通过使用RPC远程过程调用,自动生成存根函数会更容易。。。。
事实上,我们上面实现的是一个类似RPC的机制,因为它不做任何其他事情,然后发送消息调用过程,例如在服务器上添加,序列化整数不需要的结果,将其发送回客户端。这是一个集中优先级队列,而不是分布式优先级队列。分布式优先级队列可能提供的优点是可伸缩性和高可用性。
public class PQ_Client {
private static Socket skt;
private InputStream in;
private OutputStream out;
private final int _STOP_ = 0, _ADD_ = 1, _POLL_ = 2; // By convention (protocol)
PQ_Client(String ip, int port) {
try {
this.skt = new Socket(ip, port);
this.in = skt.getInputStream();
this.out = skt.getOutputStream();
System.out.println("Connected to distributed priority queue.");
} catch(IOException e) {
System.out.println("Could not connect with the distributed priority queue : " + e.getStackTrace());
}
}
// Sort of stub functions
public void stop() {
out.write(_STOP_);
out.flush();
out.close();
}
public void add(Integer el) {
out.write(_ADD_); // Send wanted operation
out.write(el); // Send argument element
// Real implementation would listen for result here
out.flush();
}
public int poll() {
out.write(_POLL_);
out.flush();
// Listen for answer
return in.read();
}
/*
* Rest of implementation
*/
}
String ip = "...";
int port = 5555;
PQ_Client pq = new PQ_Client(ip , port);
pq.add(5);
pq.add(2);
pq.add(4);
int res = pq.poll();