php-nsq 正在参加 2021 年度 OSC 中国开源项目评选,请投票支持!
php-nsq 在 2021 年度 OSC 中国开源项目评选 中已获得 {{ projectVoteCount }} 票,请投票支持!
2021 年度 OSC 中国开源项目评选 正在火热进行中,快来投票支持你喜欢的开源项目!
2021 年度 OSC 中国开源项目评选 >>> 中场回顾
php-nsq 获得 2021 年度 OSC 中国开源项目评选「最佳人气项目」 !
授权协议 未知
开发语言 C/C++ PHP
操作系统 跨平台
软件类型 开源软件
开源组织
地区 国产
投 递 者 吴振宇_yunnian
适用人群 未知
收录时间 2018-06-21

软件简介

php-nsq

php-nsq 是nsq的php客户端,采用c扩展编写,性能和稳定性。

安装 :

请提前安装libevent

Dependencies: libevent  (apt-get install libevent-dev ,yum install libevent-devel)

1. sudo phpize
2. ./configure 
3. make  
4. make install  

add in your php.ini:

extension = nsq.so;

 pub例子:

$nsqdAddr = array(
    "127.0.0.1:4150",
    "127.0.0.1:4154"
);

$nsq = new Nsq();
$isTrue = $nsq->connectNsqd($nsqdAddr);

for($i = 0; $i < 10000; $i++){
    $nsq->publish("test", "nihao");
}
$nsq->closeNsqdConnection();

// Deferred publish 
//function : deferredPublish(string topic,string message, int millisecond); 
//millisecond default : [0 < millisecond < 3600000]

$deferred = new Nsq();
$isTrue = $deferred->connectNsqd($nsqdAddr);
for($i = 0; $i < 20; $i++){
    $deferred->deferredPublish("test", "message daly", 3000); 
}
$deferred->closeNsqdConnection();

 sub例子:

<?php 

//sub

$nsq_lookupd = new NsqLookupd("127.0.0.1:4161"); //the nsqlookupd http addr
$nsq = new Nsq();
$config = array(
    "topic" => "test",
    "channel" => "struggle",
    "rdy" => 2,                //optional , default 1
    "connect_num" => 1,        //optional , default 1   
    "retry_delay_time" => 5000,  //optional, default 0 , if run callback failed, after 5000 msec, message will be retried
    "auto_finish" => true, //default true
);

$nsq->subscribe($nsq_lookupd, $config, function($msg,$bev){ 

    echo $msg->payload;
    echo $msg->attempts;
    echo $msg->message_id;
    echo $msg->timestamp;


});

Nsq 类方法:

  • connectNsqd($nsqdAddrArr) 
    pub的时候连接nsq,你也可以利用此函数做健康检查

  • closeNsqdConnection() 
    关闭nsq的连接

  • publish($topic,$msg) 
    消息发送

  • deferredPublish($topic,$msg,$msec) 
    延迟消息发送

  • subscribe($nsq_lookupd,$config,$callback) 
    消息订阅

Message 类方法与属性:

  • timestamp 
    消息时间戳

  • attempts 
    消息的重试次数,(从1开始)

  • message_id 
    消息id 

  • payload 
    消息内容

  • finish($bev,$msg->message_id) 
    主动的 ack消息方法

  • touch($bev,$msg->message_id) 
    如果你消息执行太长,可以利用次函数告知nsq 你还活着,一般用于执行频率比较规律的场景。

Tips :

1.如果callback内需要外部变量,可以采用以下use的写法: 

$nsq->subscribe($nsq_lookupd, $config, function($msg,$bev) use ($you_variable){ 

    echo $msg->payload;
    echo $msg->attempts;
    echo $msg->message_id;
    echo $msg->timestamp;


});

 2.消息重试,只要抛异常就可以,切记不要陷入死循环,超过自己觉得可以的次数 要return: 

subscribe($nsq_lookupd, $config, function($msg){ 
    try{
        echo $msg->payload . " " . "attempts:".$msg->attempts."\n";
        //do something
    }catch(Exception $e){

        if($msg->attempts < 3){
            //the message will be retried after you configure retry_delay_time 
            throw new Exception(""); 
        }else{
            echo $e->getMessage();
            return;
        }
    }

});

3.如果你想增加 客户端的心跳时间与消息的超时时间 :

 第一步 在nsqd启动时要加入相关参数,这个参数是最大的限制,比如--max-heartbeat-interval=1m30s 心跳时间最大不能超过1分30秒:

      nsqd --lookupd-tcp-address=127.0.0.1:4160 --max-heartbeat-interval=1m30s --msg-timeout=10m30s

第二步  因为第一步是指定最大时间,所以还需要第二步在客户端指定所需要的值 具体请看 example目录中的identify开头的文件例子。

4.如果你想增强消费能力,可以加大rdy参数

5.你可以用supervisor管理,但是因为是多进程消费,你需要在supervisor job的配置文件 添加: 

    stopasgroup=true
    killasgroup=true

Changes

  • 3.0

    • 修复因libevent 超过4096消息被截断问题

    • 增加identify指令功能,可以增加客户端心跳时间 与 消息超时时间

  • 2.4.0

    • 修复 pub bug

    • 修复 sub coredump

    • 修覆盖 touch bug

    • 增加等待,当刚初始化的topic没消息时

  • 2.3.1

    • pub支持域名

    • 修复 pub coredump

展开阅读全文

代码

的 Gitee 指数为
超过 的项目

评论

点击加入讨论🔥(7) 发布并加入讨论🔥
2019/09/19 14:59

php-nsq 3.4.3 线上稳定版发布

php-nsq 3.4.3 线上稳定版发布了。 3.4.3 版本修复了消费功能一些不稳定因素, 此版本经历美篇线上核心功能实践后发布,稳定运行,子进程无论是代码问题导致内存溢出挂掉,还是手动 kill 掉都能保证自动拉起,保障消费稳定运行,达到无人值守,希望大家更新尝试。 更新内容: 修复某些环境下子进程挂掉拉不起来问题 修复内存分配报错等问题 更新了使用文档

5
3
2018/10/21 16:51

php-nsq 3.3 发布,增加进程管理功能

3.3 版本主要增加了进程管理功能,使客户端更加强壮,另外之前版本主进程被kill时 ,子进程变为了孤儿进程被init接管,对这种情况也做了优化。 更新内容: 当一个子进程(与nsqd连接的消费进程)异常退出时,会重新拉起一个新的消费进程 当主进程被杀死时,让所有子进程也退出 增加docker file bug fix : 修复异常错误没有被打印到终端上,导致问题不好定位的case 修复延迟发布 当消息为整形时异常问题,原因是少了convert_to_...

0
7
2018/06/21 00:48

php-nsq 3.0 线上稳定版发布,NSQ 的 PHP 客户端

php-nsq 是nsq的php扩展版本的客户端,经过大半年的迭代,功能逐渐完善。此版本是线上稳定版本. 新版本3.0更新内容: 增加了identify 指令功能,可以用于增加客户端的心跳时间 与 消息超时时间等 . 修复因libevent 超过4096字节消息被截断问题。

7
7
没有更多内容
加载失败,请刷新页面
点击加载更多
加载中
下一页
发表了博客
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
发表了问答
{{o.pubDate | formatDate}}

{{formatAllHtml(o.title)}}

{{parseInt(o.replyCount) | bigNumberTransform}}
{{parseInt(o.viewCount) | bigNumberTransform}}
没有更多内容
暂无内容
暂无内容
7 评论
30 收藏
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部