用 centrifugo 搭建 消息推送服務器 docker + rancher 搭建


關於消息推送服務器

目前有很多第三方的開放成熟的推送服務。鑒於項目需要 我們項目需要自己搭建 自己的推送服務。

我們的推送應用場景
  • 聊天消息

  • 項目內部消息提醒

  • 移動設備接受消息

應用到的相關軟件工具知識點

  • redis 數據庫 用於 centrifugo 啟用分布式 集群做數據支撐(如果單節點 可以不用數據庫 默認以內存作為緩存)

  • docker

  • centrifugo 消息推送服務軟件 https://github.com/centrifugal/centrifugo

  • rancher 編排工具

redis 集群

mkdir /home/www/redis/

wget https://raw.githubusercontent.com/antirez/redis/3.0/redis.conf -O /home/www/redis/redis.conf

cd /home/www/redis/

docker run --name redis-master -p 6379:6379 -d redis # 在rancher 中就不必這么原始來運行docker 鏡像了!!!

# 配置 redis.conf  設置主redis 容器的的名稱
sed -i 's/# slaveof <masterip> <masterport>/slaveof redis-master 6379/g' redis.conf   # redis-master 為redis 主 容器名稱 

啟動一批 redis 重節點

完整 配置 參考 下面 rancher 配置

docker rancher 編排參考

下面的配置運行后 redis 主從 集群 會運行起來 不過 centrifugo 如果要啟用 redis 作為引擎 必須指定 redis 哨兵模式的地址 ;我的方法是在 已經運行的集群redis從 容器中 同時啟用哨兵模式

哨兵模式配置

在 /home/www/redis/sentinel.conf 中添加如下配置內容; 並在開啟redis從容器的時候 把這個路勁映射進去
/home/www/redis/sentinel.conf:/sentinel.conf 哨兵模式配置文件
/home/www/redis/redis.conf:/usr/local/etc/redis/redis.conf 集群配置文件

# /home/www/redis/sentinel.conf:/sentinel.conf

sentinel monitor redis-master 10.42.81.33 6379 2

啟動哨兵模式 (目前為手動啟動哨兵 后面盡量優化為自動剛啟動哨兵
# 容器啟動后進容器內執行

redis-sentinel /sentinel.conf

啟動容器




redis-sentinel-26379-1:
  tty: true
  command:
  - redis-server
  - /usr/local/etc/redis/redis.conf
  image: redis:latest
  links:
  - redis-master:redis-master
  volumes:
  - /home/www/redis/sentinel-26379.conf:/sentinel.conf
  - /home/www/redis/redis.conf:/usr/local/etc/redis/redis.conf
  stdin_open: true
centtrifogo6:
  tty: true
  command:
  - centrifugo
  - -c
  - config.json
  - --web
  - --engine=redis
  - --redis_master_name=redis-master
  - --redis_sentinels=redis-slave3:26379
  image: hub.03in.com:5002/ranmufei/centrifugo:v1
  volumes:
  - /host/dir/with/config/file:/centrifugo
  stdin_open: true
redis-slave5:
  tty: true
  command:
  - redis-server
  - /usr/local/etc/redis/redis.conf
  image: redis:latest
  links:
  - 'redis-master:'
  volumes:
  - /home/www/redis/redis.conf:/usr/local/etc/redis/redis.conf
  - /home/www/redis/sentinel.conf:/sentinel.conf
  stdin_open: true
redis-slave1:
  tty: true
  command:
  - redis-server
  - /usr/local/etc/redis/redis.conf
  image: redis:latest
  links:
  - redis-master:redis-master
  volumes:
  - /home/www/redis/redis.conf:/usr/local/etc/redis/redis.conf
  - /home/www/redis/sentinel.conf:/sentinel.conf
  stdin_open: true
redis-slave2:
  tty: true
  command:
  - redis-server
  - /usr/local/etc/redis/redis.conf
  image: redis:latest
  links:
  - redis-master:redis-master
  volumes:
  - /home/www/redis/redis.conf:/usr/local/etc/redis/redis.conf
  - /home/www/redis/sentinel.conf:/sentinel.conf
  stdin_open: true
aaabbb:
  ports:
  - 8182:8000
  labels:
    io.rancher.loadbalancer.target.centtrifogo6: push.03in.com
  tty: true
  image: rancher/load-balancer-service
  links:
  - centtrifogo6:centtrifogo6
  stdin_open: true
redis-slave3:
  tty: true
  command:
  - redis-server
  - /usr/local/etc/redis/redis.conf
  image: redis:latest
  links:
  - redis-master:redis-master
  volumes:
  - /home/www/redis/redis.conf:/usr/local/etc/redis/redis.conf
  - /home/www/redis/sentinel.conf:/sentinel.conf
  stdin_open: true
redis-master:
  ports:
  - 6379:6379/tcp
  tty: true
  image: redis:latest
  stdin_open: true

參考資料


作者:鵲橋仙
出處:http://www.cnblogs.com/freefei/
關於作者:專注於Linux平台項目架構、管理和企業解決方案。基於linux 容器技術 微服務架構 雲計算領域有一點點經驗。如有問題或建議,請多多賜教!
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接。如有問題,可以郵件:ranmufei@qq.com  微博:鵲橋仙  聯系我,非常感謝。

        </p>
    </div>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM