我的mqtt协议和emqttd开源项目个人理解(10) - 建立高可用集群,负载均衡haproxy - Go语言中文社区

我的mqtt协议和emqttd开源项目个人理解(10) - 建立高可用集群,负载均衡haproxy


方案1:Nginx+Keepalived+emq+MySQL双机热备,MySQL用于数据持久化

方案2(推荐):HAProxy+Keepalived+emq+MySQL双机热备

方案3:HAProxy+confd+emq+etcd

问:我还有一个疑问,使用了nginx或者haproxy真的能提升性能吗?那nginx或haproxy本身服务器岂不是压力山大,所有数据都要经过它转发。
答:它不需要处理,仅仅是转发。但是nginx不行,性能比较好的软件负载均衡是LVS。如果软件性能那么好,F5就不会卖那么贵了。还有,4层负载,DR模式回包不用经过负载,这在下行流量大的业务场景里性能不错了。

 

haproxy参考配置1:https://github.com/lelylan/haproxy-mqtt

https://github.com/lelylan/haproxy-mqtt/blob/master/haproxy.cfg

global
  ulimit-n 99999
  maxconn 99999
  maxpipes 99999
  tune.maxaccept 500
  log 127.0.0.1 local0
  log 127.0.0.1 local1 notice
  chroot /var/lib/haproxy
  user haproxy
  group haproxy

defaults
  log global
  mode http
  option dontlognull
  timeout connect 5000ms
  timeout client 50000ms
  timeout server 50000ms
  errorfile 400 /etc/haproxy/errors/400.http
  errorfile 403 /etc/haproxy/errors/403.http
  errorfile 408 /etc/haproxy/errors/408.http
  errorfile 500 /etc/haproxy/errors/500.http
  errorfile 502 /etc/haproxy/errors/502.http
  errorfile 503 /etc/haproxy/errors/503.http
  errorfile 504 /etc/haproxy/errors/504.http

listen stats :80
  stats enable
  stats uri / # must be present to see the logs
  stats auth admin:admin

listen mqtt
  bind *:1883
  bind *:8883 ssl crt /certs/lelylan-mqtt.pem
  mode tcp
  #Use this to avoid the connection loss when client subscribed for a topic and its idle for sometime
  option clitcpka # For TCP keep-alive
  timeout client 3h #By default TCP keep-alive interval is 2hours in OS kernal, 'cat /proc/sys/net/ipv4/tcp_keepalive_time'
  timeout server 3h #By default TCP keep-alive interval is 2hours in OS kernal
  option tcplog
  balance leastconn
  server mosca_1 178.62.122.204:1883 check
  server mosca_2 178.62.104.172:1883 check

 

参考配置2:

global //全局配置
      tune.ssl.default-dh-param 2048
defaults //默认初始配置,listen没有指定情况下使用该值,不然会被listen覆盖
       log 127.0.0.1 local3
       mode http                       #所处理的类别 (#7层 http;4层tcp  )
       maxconn 10000                   #最大连接数
       option dontlognull              #不记录健康检查的日志信息
       option redispatch               #serverId对应的服务器挂掉后,强制定向到其他健康的服务器
       #stats refresh 30                #统计页面刷新间隔
       retries 3                       #3次连接失败就认为服务不可用,也可以通过后面设置
       balance roundrobin              #默认的负载均衡的方式,轮询方式
      #balance source                  #默认的负载均衡的方式,类似nginx的ip_hash
      #balance leastconn               #默认的负载均衡的方式,最小连接
       timeout connect 5000                 #连接超时
       timeout client 50000                #客户端超时
       timeout server 50000                #服务器超时
       timeout check 2000              #心跳检测超时
listen mqtt-ssl
    #bind *:1883
    bind *:8883 ssl crt /etc/ssl/emqttd/emq.pem no-sslv3
    mode tcp
    option clitcpka
    maxconn 50000
    timeout check 5000
    balance leastconn
    server emq1 192.168.0.1:1883 check inter 10000 fall 2 rise 5 weight 1
    server emq2 192.168.0.2:1883 check inter 10000 fall 2 rise 5 weight 1

 

以下是emq的官方文档介绍:

EMQ 2.2正式支持MQTT协议多{敏感词}配置,支持HAProxy的Proxy Protocol V1/V2。新增Web Hook插件(emq-web-hook)、Lua Hook插件(emq-lua-hook)。

MQTT协议{敏感词}配置

 

一个EMQ节点可配置多个MQTT协议监听端口,例如下述配置external, internal{敏感词},分别用于设备连接与内部通信:

                         -------
-- External TCP 1883 --> |     |
                         | EMQ | -- Internal TCP 2883 --> Service
-- External SSL 8883-->  |     |
                         -------

 

EMQ 2.2 版本etc/emq.conf{敏感词}配置方式:

listener.tcp.${name}= 127.0.0.1:2883

listener.tcp.${name}.acceptors = 16

listener.tcp.${name}.max_clients = 102400

 

Proxy Protocol V1/2支持

EMQ 集群通常部署在负载均衡器(LB)后面,典型架构:

              -----
              |   |
              | L | --TCP 1883--> EMQ
--SSL 8883--> |   |                |
              | B | --TCP 1883--> EMQ
              |   |
              -----

HAProxy、NGINX等常用的负载均衡器(LB),一般通过Proxy Protocol协议传递TCP连接源地址、源端口给EMQ。

EMQ 2.2 版本的{敏感词}开启Proxy Protocol支持:

## Proxy Protocol V1/2
## listener.tcp.${name}.proxy_protocol = on
## listener.tcp.${name}.proxy_protocol_timeout = 3s

Web Hook插件

新增WebHook插件: emq-web-hook ,支持在MQTT客户端上下线、消息发布订阅时触发WebHook回调。

 

Lua Hook插件

新增Lua Hook插件: emq-lua-hook ,支持Lua脚本注册EMQ扩展钩子来开发插件。

 

改进认证链设计

EMQ 2.2 版本改进认证链设计,当前认证模块返回ignore(例如用户名不存在等情况下),认证请求将继续转发后面认证模块:

           -------------           ------------           -------------
Client --> | Redis认证 | -ignore-> | HTTP认证 | -ignore-> | MySQL认证 |
           -------------           ------------           -------------
                 |                       |                       |
                |/                     |/                     |/
           allow | deny            allow | deny            allow | deny

 

支持bcrypt密码Hash

EMQ 2.2 版本支持bcrypt密码Hash方式,例如Redis认证插件配置:

auth.redis.password_hash = bcrypt

etc/emq.conf配置变更

'mqtt.queue.*' 配置变更为 'mqtt.mqueue.*'

 

emq-dashboard

WebSocket页面支持Unsubscribe

 

---

 

https://www.haproxy.com/

http://nginx.org/

http://www.linuxvirtualserver.org/

http://www.keepalived.org/

物联网架构成长之路(10)-Nginx负载均衡

物联网架构成长之路(9)-双机热备Keepalived了解
物联网架构成长之路(8)-EMQ-Hook了解、连接Kafka发送消息

http://www.cnblogs.com/wunaozai/tag/%E7%89%A9%E8%81%94%E7%BD%91/ 物联网架构之路总目录

Confd+etcd实现高可用自动发现

MQTT haproxy 负载均衡代理服务

emqtt集群 ,使用haproxy做tcp负载均衡服务器

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/libaineu2004/article/details/79062736
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2019-09-05 19:58:07
  • 阅读 ( 1609 )
  • 分类:Go开源项目

0 条评论

请先 登录 后评论

官方社群

GO教程

推荐文章

猜你喜欢