构建基于redis+gearman+nodejs 的消息推送系统

9 篇文章 0 订阅
2 篇文章 0 订阅

转自:http://www.woyuw.com/?p=59

前言

系统的核心服务为优惠券获取、预订业务(酒店、机票等)。服务供应商有大型运营商、实体企业。随着PC、各种终端的涌现,人们对消息的即时性要求越来越高,同时也要求我们的运维第一时间能更好服务运营商、实体企业。这就要求我们的信息推送是多方的、快速的、有效的。

系统定位在:

构建基于事件的消息推送系统

事件分为:

优惠券、预订业务

系统业务场景

1 客服通过基于开源sphinx搭建的搜索系统

2 查找优惠券或进行预订业务

3 通过某银行系统获取唯一订单号

4 各种通道通知消费者

简要代码实现

1 搭建GearmanWork 异步通知银行系统生成唯一订单号

#!/usr/local/php/bin/php -q
<?php
set_time_limit(0);
$worker = new GearmanWorker();
$worker->addServer("127.0.0.1",4730);
$worker->addFunction("xdAction", "xdAction_fn");
while (1) {
$ret = $worker->work();
if ($worker->returnCode() != GEARMAN_SUCCESS) {
break;
}
}
function xdAction_fn($a) {
$devAuth = array(
'appID:100132695302118',
'APPKEY:22713038b5ddb849a6f9853e18b3dfd3'
);

$rw=unserialize($a->workload());
echo $json = kerHttpRest::connect( 'http://xxxxx')
->header($devAuth)
->post($rw);
$row = json_decode($json);

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$lname = 'xdQueue'; //下单队列
$arra = array(
'accessid' => 'xxxxxxxxxxxxx'
);
$redis->lpush($lname, json_encode($arra));
}
?>

2 程序端调用GearmanWork

<?php
$client = new GearmanClient();
$client->addServer("127.0.0.1");
$newarray=serialize(array('platformid'=>'KLB00001','order_mobile'=>'130XXXXXXXX'));
$client->doBackground("xdAction",$newarray,md5(time()));
?>

3 nodejs非阻塞调用redis的信息 推送到client

.
.
.
var redis = require("redis"),
client = redis.createClient(6379,'127.0.0.1');
client.on("error", function (err) {
console.log("Error " + err);
});
setInterval(function(){

client.rpop("xdQueue",function (err,value) {

if(value!=null){
console.log(" " + ": " + value);
io.sockets.emit("neworder",value);
}
});

};

},1000);

4 客户端连接node服务 等待推送数据

<script>
var socket = io.connect('http://127.0.0.1:1337');
socket.emit('login',{'socketId':"xxxxx", mobile:"xxxxxx",accessid: "96e79218965eb72c92a549dd5a330112"});
socket.on('neworder', function (data) {
var rw = eval('('+data+')');
$('.booking_order_count').show();
var v=$('.booking_order_count').html();
$('.booking_order_count').html(parseInt(v)+1);
console.log(rw.accessid);
});
</script>

附件:
[安装 gearmand]

rpm -ivh http://dl.iuscommunity.org/pub/ius/stable/Redhat/6/x86_64/epel-release-6-5.noarch.rpm
yum install -y gearmand
yum install -y libgearman-devel.x86_64
yum install -y re2c

[安装 gearman PHP扩展]

wget wget http://pecl.php.net/get/gearman-1.1.2.tgz
/software/php/bin/phpize
./configure --with-php-config=/alidata1/software/php/bin/php-config 
make 
make install

[安装node redis]

npm install redis

[安装 node forever] 用于node 进程的托管

npm install -g forever
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值