nodejs+redis写的订阅分发(已抛弃…

本文介绍了一个使用Node.js监听Redis的发布/订阅机制的脚本案例,该脚本负责消息的复制、分发及通知接收者。尽管在运行过程中遇到了较高的资源消耗问题,但该实践对于理解Node.js与Redis的集成应用仍具有一定参考价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面是我前段时间用nodejs监听redis的pub/sub写的订阅脚本
后来由于持久化及系统资源耗费严重(cpu 50% 内存500m)抛弃掉了,放这里给大家围观下
如果有好办法请告诉我,谢过

设计思路如下
1.当有人在redis下发布一个消息的同时发布个广播
2.分发中转程序收到广播后将消息复制成多份扔到对应订阅者的接受队列内,并对各个队列发送订阅广播
3.redis下的订阅端就会接收到事件,并把事件用http发送一个通知接口让对方来取

中间分发用的开源的qdis后来想慢慢替换掉一直没来得及替换。

var redis = require("redis");
var subscription_client = redis.createClient();
var regular_client = redis.createClient();
var http=require("http");
//var querystring=require("querystring");
var url=require("url");

//current send process count
var currentSend=0;
var starttime = process.hrtime();
var configlistobj={
    'pub':{
        'sub1':'http://xxxx.com/test.php?p=123',
        'sub2':'http://xxxx.com/test.php?p=456'
    }
};
//time report
setInterval(function () {
var diff = process.hrtime(starttime);
console.log('benchmark took %d seconds and %d nanoseconds',diff[0], diff[1]);}, 1000);

//send http request
function httprequest(urlpath,dataString)
{
    currentSend++;
    //parse the url
    urlpath = url.parse(urlpath);
    //default port is 80
    if(urlpath["port"]=="")
    {
        urlpath["port"]=80;
    }

    var headers = {
        'content-type': 'application/x-www-form-urlencoded',
        'Content-Length': dataString.length
    };

    var options = {
        host: urlpath["hostname"],
        port: urlpath["port"],
        path: urlpath["path"],
        method: 'POST',
        headers: headers
    };

    //console.log(JSON.stringify(options));

    var req = http.request(options, function(res) {
        //console.log('STATUS: ' + res.statusCode);
        //console.log('HEADERS: ' + JSON.stringify(res.headers));
        if(res.statusCode!=200)
        {
            console.log("fuck,get error");
            return;
        }
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('BODY: ' + chunk);
            currentSend--;
            console.log('Children:'+currentSend);

        });
    });

    // error handle
    req.on('error',function(e){
        console.log('error');
    });
    ;
    req.setTimeout(1,function(socket){});
    req.write(dataString);
    //console.log(dataString);
    req.end();
}

function processOfflineData(channel)
{
    regular_client.rpop( channel, function(err,res){
        if(res != null)
        {
            httprequest(configlistobj["pub"]["sub1"],res);
            processOfflineData(channel);
        }
    });
}

subscription_client.on( 'message', function( channel, message ) {

    //the channel name is the same name as the list
    regular_client.rpop( channel, function(err,res){
        httprequest(configlistobj["pub"]["sub1"],res);
        console.log(currentSend);
    });

} );

console.log("sending the Offline info");
processOfflineData("sub");
//start process
subscription_client.subscribe('sub');

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值