将zeromq套接字连接到redis服务器以进行数据传输?

时间:2014-09-21 16:28:53

标签: sockets redis client-server zeromq

我想将服务器(一个ZMQ_ROUTER套接字,处理多个客户端)上的消息传输到redis服务器以进行存储。我听说过,redis不会说ZMQ。所以没有桥梁就不可能实现。我对你的建议持开放态度。在哪里看?

// Load Balanced Multithreaded ed Server:

#include "zhelpers.hpp"
#include <queue>
#include "zmq.hpp"
#include <stdio.h>
#include <string>
#include <vector>
#include "datamsg.pb.h"
using namespace google::protobuf::io;
  bool verify(std::string str, std::vector<std::string> &s)
  {
  for(int q=0;q<s.size();q++)
  {    
   if(s.at(q)==str.substr(0,4)){ 
   s.push_back(str.substr(4,str.length()-1));

  return true;
  }
}
return false;
}
 // Basic request-reply client using REQ socket
 static void * worker_thread(void *arg) {
  zmq::context_t context(1);
  zmq::message_t worker_receive;
  datamsg worker_parsed;
  zmq::socket_t worker(context, ZMQ_REQ);
  s_set_id(worker); // Makes tracing easier
  worker.connect("ipc://backend.ipc");
   // Tell backend we're ready for work
   s_send(worker, "READY");

   while (1) {
   // Read and save all frames until we get an empty frame
   worker.recv(&worker_receive);
   worker_parsed.ParseFromArray(worker_receive.data(), worker_receive.size());
  // printing after parsing......... 
   s_sendmore (worker, worker_parsed.destination());
   s_sendmore (worker, "");  
   worker.send(worker_receive);// Here I sent the same structure back
 }
 return (NULL);
}

int main (int argc, char *argv[]) {

// Prepare our context and sockets
zmq::context_t context(1);
zmq::socket_t frontend (context, ZMQ_ROUTER);
zmq::socket_t backend (context, ZMQ_ROUTER);
zmq::socket_t verification (context, ZMQ_REP);
verification.bind("tcp://*:5557");
std::vector<std::string> s;
s.reserve(10);
s.push_back("cli4");
frontend.bind("tcp://*:5559");
backend.bind("ipc://backend.ipc");
zmq::message_t frontend_received; 
zmq::message_t front_get;
int worker_nbr;
 for(worker_nbr = 0; worker_nbr < 3; worker_nbr++) {
 pthread_t worker;
 pthread_create(&worker, NULL, worker_thread, NULL);
 }
 std::queue<std::string> worker_queue;

 while (1) {
  // Initialize poll set
   zmq::pollitem_t items[] = {
  // Always poll for worker activity on backend
  { backend, 0, ZMQ_POLLIN, 0 },
  // Poll front-end only if we have available workers
  { frontend, 0, ZMQ_POLLIN, 0 },
  //Poll for new customer for verification of client refrence ID
  {verification,0,ZMQ_POLLIN,0 }
}; zmq::poll (items, 3, -1);

if (items [0].revents & ZMQ_POLLIN) {   // Handle worker activity on backend
// Queue worker address for LoadBalanced routing
   worker_queue.push(s_recv (backend));

// Second frame is empty
 std::string empty = s_recv (backend);
 assert (empty.size() == 0);

// Third frame is READY or else a client reply address
 std::string client_addr = s_recv (backend);
// If client reply, send rest back to frontend
if(client_addr.compare("READY") != 0) {   
  std::string empty = s_recv (backend);
  assert (empty.size() == 0);
  backend.recv(&frontend_received);
  s_sendmore (frontend, client_addr);
  s_sendmore (frontend, "");
  frontend.send(frontend_received);
  //frontend.close();
  }
}
if (items [1].revents & ZMQ_POLLIN) {
// Client request is [address][request]

 std::string client_addr = s_recv (frontend);

 frontend.recv(&front_get);
 std::string worker_addr = worker_queue.front();
 worker_queue.pop();

  s_sendmore (backend, worker_addr);
  s_sendmore (backend, "");
  backend.send(front_get);

}
if (items [2].revents & ZMQ_POLLIN) {
 std::string refrence=s_recv(verification);
  if(verify(refrence,s)){
   s_send(verification,"OK");
   std::cout<<"ID:"<<refrence.substr(4,(refrence.length()-1))<<" Has been Registered"  <<std::endl;
 }
else s_send(verification,"Verification Failed!");
  }

}
sleep (1);
return 0;
}

1 个答案:

答案 0 :(得分:2)

那么,您有一个运行ZMQ ROUTER套接字的应用程序,并且您希望将这些消息归档到redis?除非您有一些未提及的约束,否则应直接从应用程序中连接到redis,而不是尝试通过ZMQ传递所有通信。 ZMQ套接字只能永远与其他ZMQ套接字通信(不需要或多或少地对ZMQ协议进行逆向工程,但这相当于建立一个你说你不想要的桥接器。)

Redis没有本机ZMQ连接选项。

或多或少你需要完成的事情将如下所示:

-------Application-------      ------------
|                       |      | External |
|           ZMQ socket-(|<----(|  Source  |
|            v          |      ------------
|            V          |
|     (Process Data)    |      ----------
|            V          |      |  Redis |
|      Redis connector--|)---->| Server |
|                       |      ----------
-------------------------

希望这是有道理的。如果您在原始问题中提供代码,我们可以直接解决。