構建基於事件的消息推送系統
事件分為: 優惠券、預訂業務 系統業務場景 1 客服通過基於開源sphinx搭建的搜索系統1 搭建GearmanWork 異步通知銀行系統生成唯一訂單號
#PHP#!/usr/local/php/bin/php -q 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));}#PHP
2 程序端調用GearmanWork
#PHP addServer("127.0.0.1"); $newarray=serialize(array('platformid'=>'KLB00001','order_mobile'=>'130XXXXXXXX')); $client->doBackground("xdAction",$newarray,md5(time()));#/PHP
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服務 等待推送數據
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);});
附件:
[安裝 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