菜鸟玩云计算之六:Ubuntu Server 12.10 上安装 nodejs, zeromq

Ubuntu Server 12.10 上安装 python, nodejs, zeromq


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

a.足够简单。
b.足够快。
c.足够小巧。
d.前后端一致性。

下面是安装步骤:

1 首先确保系统安装了python, gcc, g++,如果没有则安装:

$ sudo apt-get install python
$ sudo apt-get install build-essential
$ sudo apt-get install gcc
$ sudo apt-get install g++

2 安装 python bindings


2.1 安装python自动安装工具: easy_install

$ sudo apt-get install python-setuptools

2.2 安装python的编译环境

$ sudo apt-get install python-dev

2.3 安装 pythong zmq 的 binding:

$ sudo easy_install pyzmq

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


3.1 解压:

$ sudo tar -zxf node-v0.8.14.tar.gz
$ cd node-v0.8.14

3.2 默认方式安装:


$ ./configure
$ make
$ sudo make install

3.3 选择目录方式安装:

$ ./configure --prefix=/opt/node
$ make -j 3     # 3=CPU核数+1
$ sudo make install

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

3.4 接下来参考下面的文章开始你的nodejs编程之旅吧:


http://www.nodebeginner.org/index-zh-cn.html#a-full-blown-web-application-with-nodejs

4 zeromq

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


a.足够简单。
b.足够快。
c.足够小巧。

等优点. 下面是安装步骤:

4.1 下载: zero-3.2.1

http://www.zeromq.org/

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

4.2 下面写一个c的客户端mqclient.c和一个服务端mqserver.c测试:


//
// 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 message\n");

        zmq_msg_t reply;
        zmq_msg_init (&reply);
        if (-1==zmq_msg_recv (&reply, requester, 0)) {
            printf("recv data error.\n");
        }
        printf ("Received World %d\n", 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 success\n");
    } else {
        printf("zmq_bind error=%d:%s\n", 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=%d\n", 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=%d\n", 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

5 使用python来编写zeromq程序

参考:  http://zguide.zeromq.org/py:all


这是一个发布(PUB)和订阅(SUB)模型。发布者发布信息,订阅者接收并处理和自己相关的信息。一个发布者对应多个订阅者。

               |----->SUB a
PUB------>|----->SUB b
               |----->SUB c

5.1 发布者:mqpub.py


#############################################################
# mqpub.py
# Weather update server
# Binds PUB socket to tcp://*:5556
# Publishes random weather updates
#############################################################

import zmq
import random

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:5556")

while True:
    zipcode = random.randrange(1,100000)
    temperature = random.randrange(1,215) - 80
    relhumidity = random.randrange(1,50) + 10

socket.send("%d %d %d" % (zipcode, temperature, relhumidity))

5.2 订阅者:mqsub.py

#############################################################
# mqsub.py
# Weather update client
# Connects SUB socket to tcp://localhost:5556
# Collects weather updates and finds avg temp in zipcode
#############################################################

import sys
import zmq

# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)

print "Collecting updates from weather server"
socket.connect ("tcp://localhost:5556")

# Subscribe to zipcode, default is NYC, 10001
zip_filter = sys.argv[1] if len(sys.argv) > 1 else "10001"
socket.setsockopt(zmq.SUBSCRIBE, zip_filter)

# Process 5 updates
total_temp = 0
for update_nbr in range (5):
    string = socket.recv()
    zipcode, temperature, relhumidity = string.split()
    total_temp += int(temperature)

print "Average temperature for zipcode '%s' was %dF" % (zip_filter, total_temp / update_nbr)

5.3 打开2个终端,分别运行:

$ python ./mqpub.py

$ python ./mqsub.py



你可能感兴趣的:(菜鸟玩云计算之六:Ubuntu Server 12.10 上安装 nodejs, zeromq)