Chinaunix首页 | 论坛 | 博客
  • 博客访问: 3964954
  • 博文数量: 366
  • 博客积分: 9916
  • 博客等级: 中将
  • 技术积分: 7195
  • 用 户 组: 普通用户
  • 注册时间: 2011-05-29 23:27
个人简介

简单!

文章分类

全部博文(366)

文章存档

2013年(51)

2012年(269)

2011年(46)

分类: LINUX

2013-03-23 13:23:29

一、获取、安装

      Mosquitto提供了Windows、Linux以及qnx系统的版本,安装文件可从获取。Windows系统下的安装过程非常简单,甚至可以把Mosquitto直接安装成为系统服务;但是,在实际应用中更倾向于使用Linux系统的服务器。在Linux系统上安装Mosquitto,建议使用源码安装模式,最新的源码可从获取。解压之后,可以在源码目录里面找到主要的配置文件config.mk,其中包含了所有Mosquitto的安装选项,详细的参数说明如下:

  1. # 是否支持tcpd/libwrap功能.
  2. #WITH_WRAP:=yes

  3. # 是否开启SSL/TLS支持
  4. #WITH_TLS:=yes

  5. # 是否开启TLS/PSK支持
  6. #WITH_TLS_PSK:=yes

  7. # Comment out to disable client client threading support.
  8. #WITH_THREADING:=yes

  9. # 是否使用严格的协议版本(老版本兼容会有点问题)
  10. #WITH_STRICT_PROTOCOL:=yes

  11. # 是否开启桥接模式
  12. #WITH_BRIDGE:=yes

  13. # 是否开启持久化功能
  14. #WITH_PERSISTENCE:=yes

  15. # 是否监控运行状态
  16. #WITH_MEMORY_TRACKING:=yes
      这里需要注意的是,默认情况下Mosquitto的安装需要OpenSSL的支持;如果不需要SSL,则需要关闭config.mk里面的某些与SSL功能有关的选项(WITH_TLS、WITH_TLS_PSK)。接着,就是运行make install进行安装,完成之后会在系统命令行里发现mosquitto、mosquitto_passwd、mosquitto_pub和 mosquitto_sub四个工具,分别用于启动代理、管理密码、发布消息和订阅消息。



二、配置、运行

       安装完成之后,所有配置文件会被放置于/etc/mosquitto/目录下,其中最重要的就是Mosquitto的配置文件,即mosquitto.conf,以下是详细的配置参数说明。

  1. # =================================================================
  2. # General configuration
  3. # =================================================================

  4. # 客户端心跳的间隔时间
  5. #retry_interval 20

  6. # 系统状态的刷新时间
  7. #sys_interval 10

  8. # 系统资源的回收时间,0表示尽快处理
  9. #store_clean_interval 10

  10. # 服务进程的PID
  11. #pid_file /var/run/mosquitto.pid

  12. # 服务进程的系统用户
  13. #user mosquitto

  14. # 客户端心跳消息的最大并发数
  15. #max_inflight_messages 10

  16. # 客户端心跳消息缓存队列
  17. #max_queued_messages 100

  18. # 用于设置客户端长连接的过期时间,默认永不过期
  19. #persistent_client_expiration

  20. # =================================================================
  21. # Default listener
  22. # =================================================================

  23. # 服务绑定的IP地址
  24. #bind_address

  25. # 服务绑定的端口号
  26. #port 1883

  27. # 允许的最大连接数,-1表示没有限制
  28. #max_connections -1

  29. # cafile:CA证书文件
  30. # capath:CA证书目录
  31. # certfile:PEM证书文件
  32. # keyfile:PEM密钥文件
  33. #cafile
  34. #capath
  35. #certfile
  36. #keyfile

  37. # 必须提供证书以保证数据安全性
  38. #require_certificate false

  39. # 若require_certificate值为true,use_identity_as_username也必须为true
  40. #use_identity_as_username false

  41. # 启用PSK(Pre-shared-key)支持
  42. #psk_hint

  43. # SSL/TSL加密算法,可以使用“openssl ciphers”命令获取
  44. # as the output of that command.
  45. #ciphers

  46. # =================================================================
  47. # Persistence
  48. # =================================================================

  49. # 消息自动保存的间隔时间
  50. #autosave_interval 1800

  51. # 消息自动保存功能的开关
  52. #autosave_on_changes false

  53. # 持久化功能的开关
  54. persistence true

  55. # 持久化DB文件
  56. #persistence_file mosquitto.db

  57. # 持久化DB文件目录
  58. #persistence_location /var/lib/mosquitto/

  59. # =================================================================
  60. # Logging
  61. # =================================================================

  62. # 4种日志模式:stdout、stderr、syslog、topic
  63. # none 则表示不记日志,此配置可以提升些许性能
  64. log_dest none

  65. # 选择日志的级别(可设置多项)
  66. #log_type error
  67. #log_type warning
  68. #log_type notice
  69. #log_type information

  70. # 是否记录客户端连接信息
  71. #connection_messages true

  72. # 是否记录日志时间
  73. #log_timestamp true

  74. # =================================================================
  75. # Security
  76. # =================================================================

  77. # 客户端ID的前缀限制,可用于保证安全性
  78. #clientid_prefixes

  79. # 允许匿名用户
  80. #allow_anonymous true

  81. # 用户/密码文件,默认格式:username:password
  82. #password_file

  83. # PSK格式密码文件,默认格式:identity:key
  84. #psk_file

  85. # pattern write sensor/%u/data
  86. # ACL权限配置,常用语法如下:
  87. # 用户限制:user <username>
  88. # 话题限制:topic [read|write] <topic>
  89. # 正则限制:pattern write sensor/%u/data
  90. #acl_file

  91. # =================================================================
  92. # Bridges
  93. # =================================================================

  94. # 允许服务之间使用“桥接”模式(可用于分布式部署)
  95. #connection <name>
  96. #address <host>[:<port>]
  97. #topic <topic> [[[out | in | both] qos-level] local-prefix remote-prefix]

  98. # 设置桥接的客户端ID
  99. #clientid

  100. # 桥接断开时,是否清除远程服务器中的消息
  101. #cleansession false

  102. # 是否发布桥接的状态信息
  103. #notifications true

  104. # 设置桥接模式下,消息将会发布到的话题地址
  105. # $SYS/broker/connection/<clientid>/state
  106. #notification_topic

  107. # 设置桥接的keepalive数值
  108. #keepalive_interval 60

  109. # 桥接模式,目前有三种:automatic、lazy、once
  110. #start_type automatic

  111. # 桥接模式automatic的超时时间
  112. #restart_timeout 30

  113. # 桥接模式lazy的超时时间
  114. #idle_timeout 60

  115. # 桥接客户端的用户名
  116. #username

  117. # 桥接客户端的密码
  118. #password

  119. # bridge_cafile:桥接客户端的CA证书文件
  120. # bridge_capath:桥接客户端的CA证书目录
  121. # bridge_certfile:桥接客户端的PEM证书文件
  122. # bridge_keyfile:桥接客户端的PEM密钥文件
  123. #bridge_cafile
  124. #bridge_capath
  125. #bridge_certfile
  126. #bridge_keyfile

  127. # 自己的配置可以放到以下目录中
  128. include_dir /etc/mosquitto/conf.d

       启动Mosquitto服务很简单,直接运行命令行“mosquitto -c /etc/mosquitto/mosquitto.conf -d”即可。另外,Mosquitto是个纯异步IO框架,经测试可以轻松处理20000个以上的客户端连接。当然,实际的最大承载量还和业务的复杂度有比较大的关系。测试的时候不要忘记调整系统的最大连接数和栈大小,比如Linux上可用ulimit -n20000 -s512命令设置你需要的系统参数。
阅读(32686) | 评论(2) | 转发(1) |
1

上一篇:HTTP/FTP客户端开发库

下一篇:指针与数组

给主人留下些什么吧!~~

txgc_wm2013-03-23 20:15:48

利用该代理的的一篇文章:http://blog.csdn.net/wingsu/article/details/7955804

txgc_wm2013-03-23 20:05:24

执行时出错:

    mosquitto_pub: error while loading shared libraries: libmosquitto.so.0: cannot open shared object file: No such file or directory

这是由于目录是生成到/usr/local/lib,应该拷贝到/usr/lib目录

    ln -s /usr/local/lib/libmosquitto.so.0 /usr/lib/libmosquitto.so.0