一. 简介

引用其他文章:

Apache Apollo是一个代理服务器,其是在ActiveMQ基础上发展而来的,可以支持STOMP, AMQP, MQTT, Openwire, SSL, and WebSockets 等多种协议。

Apollo允许客户端通过开放的MQTT协议连接。该协议主要是用在资源有限的驱动上,以及网络不稳定的情况下使用,是一个订阅、发布模型。这种驱动通常不适用类似http,stomp这类基于文本,或者类似openfire,AMQP等传统二进制协议。MQTT是一个简介的二进制协议,适用这类驱动资源受限,而且是不稳定的网络条件下。之前的稳定发布版本中,MQTT是作为一个Apollo的一个插件提供的。但是现在,这个插件已经变为开发项目的一部分。MQTT在Apollo中已经不需要其他配置文件或者是第三方插件支持了。
MQTT是一个线路层的协议,任何实现该协议的客户端都可以连接到Apollo。当然也可以整合其他MQTT兼容的消息代理中。


二. Mac下apollo安装

1.下载

apollo下载以及源代码下载


2.解压


3. 添加环境变量

APOLLO_HOME=解压目录


4. 新建broker目录,最好不要跟APOLLO_HOME一致,便于以后升级


5. 创建broker

1.cdbroker目录2.${APOLLO_HOME}/bin/apollocreatemybroker

注意windows下命令可能不同,应该可以直接把apollo.bat拖过来执行。

执行完之后,应该会提示怎么运行:

Youcannowstartthebrokerbyexecuting:"/Users/huangrongwei/Documents/1_work_project/apache_apollo/apollo_broker/mybroker/bin/apollo-broker"runOryoucanrunthebrokerinthebackgroundusing:"/Users/huangrongwei/Documents/1_work_project/apache_apollo/apollo_broker/mybroker/bin/apollo-broker-service"start


6. 运行broker

按照上面的提示,选择一个运行。第一个是前台运行,就是会占用你的命令行,不会退出。

下面一个命令是后台运行。


7.查看apollo进程

ps-elf|grepapollo

结果如下;

50154185361400e03105048532125212-S+0ttys0062:00.45apollo-ea-serv1:27下午


8. 浏览器查看结果

http://127.0.0.1:61680/或者https://127.0.0.1:61681/

第一个是http的,后面一个是https的。

默认登录的用户名是admin/password,这样就可以看到apollo的管理界面。


三. 客户端测试apollo

在apollo的解压文件夹里面有个example文件夹,里面有个mqtt->java

用eclipse导入这个maven工程(eclipse->file->import->maven project),注意可能需要安装maven eclipse插件


然后里面有个Listener.java和Publisher.java,可以订阅和发布消息。直接右击->Run as java application启动起来。

然后看输出结果。

Listener.java最后会输入下面的消息。表示1万条消息接受花费了0.93秒

Received0messages.Received1000messages.Received2000messages.Received3000messages.Received4000messages.Received5000messages.Received6000messages.Received7000messages.Received8000messages.Received9000messages.Received10000in0.93seconds


四. 其他

1. curl访问apollo

curl-u"admin:password"http://localhost:61680/broker.json

结果{"id":"default","state":"STARTED","state_since":1499318856654,"version":"1.7.1","home_location":"/Users/huangrongwei/Documents/1_work_project/apache_apollo/apache-apollo-1.7.1","base_location":"/Users/huangrongwei/Documents/1_work_project/apache_apollo/apollo_broker/mybroker","jvm_metrics":{"heap_memory":{"used":23072848,"alloc":79691776,"max":954728448},"non_heap_memory":{"used":71784880,"alloc":74203136,"max":-1},"classes_loaded":7893,"classes_unloaded":0,"threads_current":27,"threads_peak":32,"os_arch":"x86_64","os_name":"Mac OS X 10.12.4","os_memory_total":8589934592,"os_memory_free":60743680,"os_swap_total":0,"os_swap_free":1073741824,"os_fd_open":135,"os_fd_max":6250,"os_load_average":2.90869140625,"os_cpu_time":128597245000,"os_processors":4,"runtime_name":"5418@huangrongweideMacBook-Pro.local","jvm_name":"Java HotSpot(TM) 64-Bit Server VM 1.8.0_131 (Oracle Corporation)","uptime":14200613,"start_time":1499318843487},"current_time":1499333764968,"virtual_hosts":["mybroker"],"connectors":["tcp","tls","ws","wss"],"connection_counter":16,"connected":0,"messages_sent":80008,"messages_received":70007,"read_counter":19251413,"write_counter":22040503}



2. mqtt协议内容

http://public.dhe.ibm.com/software/dw/webservices/ws-mqtt/mqtt-v3r1.html#fixed-header