延遲隊列 Delayer 基於 Redis 使用 Golang 開發

Delayer

基於 Redis 的延遲消息隊列中間件,採用 Golang 開發,支持 PHP、Golang 等多種語言客戶端。

參考 有贊延遲隊列設計 中的部分設計,優化後實現。

項目鏈接:https://github.com/mixstart/delayer ,有需要的朋友加 Star 哦。

應用場景

  • 訂單超過30分鐘未支付,自動關閉訂單。
  • 訂單完成後, 如果用戶一直未評價, 5天后自動好評。
  • 會員到期前3天,短信通知續費。
  • 其他針對某個任務,延遲執行功能的需求。

實現原理

  • 客戶端:push 任務時,任務數據存入 hash 中,jobID 存入 zset 中,pop 時從指定的 list 中取準備好的數據。
  • 服務器端:定時使用連接池並行將 zset 中到期的 jobID 放入對應的 list 中,供客戶端 pop 取出。

核心特徵

  • 使用 Golang 開發,高性能。
  • 高可用,服務器端操作是原子的,且做了優雅停止,不會丟失數據。
  • 可通過配置文件控制執行性能參數。
  • 提供具體言語的 SDK,使用簡單快捷。

如何使用

delayer 分爲:

  • 服務器端:負責定時掃描到期的任務,並放入隊列,需在服務器上常駐執行。
  • 客戶端:在代碼中使用,以類庫的形式,提供 pushpopbPopremove 方法操作任務。

服務器端

https://github.com/mixstart/delayer/releases 中下載對應平臺的程序。

支持 windows、linux、mac 三種平臺

然後修改配置文件 delayer.conf

[delayer]
pid = /var/run/delayer.pid      ; 單例執行, 多實例執行留空, Win不支持
timer_interval = 1000           ; 計算間隔時間, 單位毫秒
access_log = logs/access.log    ; 存取日誌
error_log = logs/error.log      ; 錯誤日誌

[redis]
host = 127.0.0.1                ; 連接地址
port = 6379                     ; 連接端口
database = 0                    ; 數據庫編號
password =                      ; 密碼, 無需密碼留空
max_idle = 2                    ; 最大空閒連接數
max_active = 20                 ; 最大激活連接數
idle_timeout = 3600             ; 空閒連接超時時間, 單位秒
conn_max_lifetime = 3600        ; 連接生存時間, 單位秒

查看幫助:

[root@localhost bin]# ./delayer -h
Usage: delayer [options]

Options:
-d/--daemon run in the background
-c/--configuration FILENAME -- configuration file path (searches if not given)
-h/--help -- print this usage message and exit
-v/--version -- print version number and exit

啓動:

[root@localhost bin]# ./delayer
    ____       __
   / __ \___  / /___ ___  _____  _____
  / / / / _ \/ / __ `/ / / / _ \/ ___/
 / /_/ /  __/ / /_/ / /_/ /  __/ /
/_____/\___/_/\__,_/\__, /\___/_/
                   /____/
Service:		delayer
Version:		1.0.1
[info] 2018/10/19 11:24:24 Service started successfully, PID: 31023

客戶端

我們提供了以下幾種語言:

根據對應項目的說明使用

License

Apache License Version 2.0, http://www.apache.org/licenses/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章