/ 中存储网

告诉你Ubuntu Server 12.10上安装Node.js,ZeroMQ的方法及命令

2015-04-20 12:15:57 来源:中存储网

Node.js是一个基于google v8+javascript的服务端编程框架。对于搞云计算,用Node.js,来做系统管理Web站点是非常合适的。

理由是:

1)足够简单。
2)足够快。
3)足够小巧。
4)前后端一致性。
好,接下来是安装步骤:

首先确保系统安装了python, gcc, g++,一旦没有则安装:
$ sudo apt-get install python
$ sudo apt-get install build-essential
$ sudo apt-get install gcc
$ sudo apt-get install g++

下载最新版源代码包:node-v0.8.14.tar.gz

解压:
$ sudo tar -zxf node-v0.8.14.tar.gz
$ cd node-v0.8.14

(一) 默认方式安装:

$ ./configure
$ make
$ sudo make install

(二) 选择目录方式安装:
$ ./configure --prefix=/opt/node
$ make -j 3    # 3=CPU核数+1
$ sudo make install

安装结束后, 用接下来的Linux命令检查安装的版本:
$ node --version
v0.8.14

zeromq是一个基于c的消息队列编程框架。zeromq用处很多, 它具有:

1)足够简单。
2)足够快。
3)足够小巧。
等优点. 接下来是安装步骤:

下载: zero-3.2.1
http://www.zeromq.org/

$ tar -xzf zeromq-3.2.1.tar.gz
$ ./configure
$ make
$ sudo make install
$ sudo ldconfig

接下来写一个客户端和一个服务端测试:

mqclient.c

// 
// Hello World client 
// Connects REQ socket to tcp://localhost:5555 
// Sends "Hello" to server, expects "World" back 
// 
#include <zmq.h> 
#include <string.h> 
#include <stdio.h> 
#include <unistd.h> 
int main (void)
{
void *context = zmq_ctx_new ();
// Socket to talk to server 
printf ("Connecting to hello world server…n");
void *requester = zmq_socket (context, ZMQ_REQ);
zmq_connect (requester, "tcp://localhost:5555");
int request_nbr;
for (request_nbr = 0; request_nbr != 10; request_nbr++) {
zmq_msg_t request;
zmq_msg_init_data (&request, "Hello", 6, 0, 0);
printf ("Sending Hello %d…n", request_nbr);
zmq_msg_send (&request, requester, 0);
zmq_msg_close (&request);
printf ("prepare recv messagen");
zmq_msg_t reply;
zmq_msg_init (&reply);
if (-1==zmq_msg_recv (&reply, requester, 0)) {
printf("recv data error.n");
}
printf ("Received World %dn", request_nbr);
zmq_msg_close (&reply);
}
zmq_close (requester);
zmq_ctx_destroy (context);
return 0;
}

mqserver.c

// 
// Hello World server 
// Binds REP socket to tcp://*:5555 
// Expects "Hello" from client, replies with "World" 
// 
#include <zmq.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <string.h> 
int main (void)
{
int ret;
char buf[5]="world";
buf[5] = 0;
void *context = zmq_ctx_new ();
// Socket to talk to clients 
void *responder = zmq_socket (context, ZMQ_REP);
ret = zmq_bind (responder, "tcp://*:5555");
if (ret==0) {
printf("zmq_bind successn");
} else {
printf("zmq_bind error=%d:%sn", ret, strerror(errno));
exit(ret);
}
while (1==1) {
// Wait for next request from client 
printf("wait for clients...n");
zmq_msg_t request;
zmq_msg_init (&request);
ret = zmq_msg_recv (&request, responder, 0);
printf ("Received=%dn", ret);
zmq_msg_close (&request);
// Do some 'work' 
sleep (1);
// Send reply back to client 
zmq_msg_t reply;
zmq_msg_init_data (&reply, buf, 6, 0, 0);
ret = zmq_msg_send (&reply, responder, 0);
printf("zmq_msg_send ret=%dn", ret);
zmq_msg_close (&reply);
}
// We never get here but if we did, this would be how we end 
zmq_close (responder);
zmq_ctx_destroy (context);
return 0;
}

接下来是编译命令:
$ gcc mqclient.c -o mqclnt -lzmq
$ gcc mqserver.c -o mqsrvr -lzmq

运行:
$ ./mqsrvr &
$ ./mqclnt

结束!