首页 > 技术文章 > 用 centrifugo 搭建 消息推送服务器 docker + rancher 搭建

freefei 2017-11-12 17:19 原文

关于消息推送服务器

目前有很多第三方的开放成熟的推送服务。鉴于项目需要 我们项目需要自己搭建 自己的推送服务。

我们的推送应用场景
  • 聊天消息

  • 项目内部消息提醒

  • 移动设备接受消息

应用到的相关软件工具知识点

  • 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>

推荐阅读