使用Apollo 的订阅/发布 在 WebSockets 上轻松实现web页消息推送

最近要用到实时消息推送到web页面,从ajax轮询,长链接,到第三方的workman的webMsgSender都试了试。考虑到我们系统用到了apache apollo来进行消息的处理。而且apollo也支持websocket通讯,所以开始研了下,并测试运用成功!

开始之前,先了解下apollo的用户及安全策略

Authorization

因为要在前端使用用户名和密码,所以必须要做好安全策略。


1
2
3
4
5
6
<broker>
 <access_rule allow="admins" action="*"/>
 <access_rule allow="users" action="connect,receive"/>
 <access_rule allow="*" action="connect" kind="connector"/>
</broker>
这里面增加规则设置users组,当然前提是在users.properties和groups.properties里增加了组users和用户user,并给予action只connect,receive.

Continue reading

安装使用 ActiveMQ Apollo

下载


1
2
3
4
5
6
7
<span class="token function">wget</span> http<span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token function">ftp</span><span class="token punctuation">.</span>kddilabs<span class="token punctuation">.</span>jp<span class="token operator">/</span>infosystems<span class="token operator">/</span>apache<span class="token operator">/</span>activemq<span class="token operator">/</span>activemq<span class="token operator">-</span>apollo<span class="token operator">/</span><span class="token number">1.7</span><span class="token punctuation">.</span>1<span class="token operator">/</span>apache<span class="token operator">-</span>apollo<span class="token operator">-</span><span class="token number">1.7</span><span class="token punctuation">.</span>1<span class="token operator">-</span>unix<span class="token operator">-</span>distro<span class="token punctuation">.</span><span class="token function">tar</span><span class="token punctuation">.</span>gz

<span class="token comment" spellcheck="true"># 将其解压到 /usr/local/activemq-apollo 目录
</span>
<span class="token comment" spellcheck="true"># 创建目录
</span><span class="token function">mkdir</span> <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>activemq<span class="token operator">-</span>apollo
<span class="token function">tar</span> xvzf apache<span class="token operator">-</span>apollo<span class="token operator">-</span><span class="token number">1.7</span><span class="token punctuation">.</span>1<span class="token operator">-</span>unix<span class="token operator">-</span>distro<span class="token punctuation">.</span><span class="token function">tar</span><span class="token punctuation">.</span>gz  <span class="token operator">-</span>C <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>activemq<span class="token operator">-</span>apollo <span class="token operator">--</span>strip<span class="token operator">-</span>components<span class="token operator">=</span><span class="token number">1</span>

创建 broker


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<span class="token comment" spellcheck="true"># 先创建broker存储目录
</span><span class="token function">mkdir</span> <span class="token operator">/</span>data<span class="token operator">/</span>apollo<span class="token operator">/</span>

<span class="token comment" spellcheck="true"># 创建broker
</span>$ <span class="token function">cd</span> <span class="token operator">/</span>usr<span class="token operator">/</span>local<span class="token operator">/</span>activemq<span class="token operator">-</span>apollo
$ bin<span class="token operator">/</span>apollo create <span class="token operator">/</span>data<span class="token operator">/</span>apollo<span class="token operator">/</span>broker
Creating apollo instance at<span class="token punctuation">:</span> <span class="token operator">/</span>data<span class="token operator">/</span>apollo<span class="token operator">/</span>broker
Generating ssl keystore<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>

You can now start the broker by executing<span class="token punctuation">:</span>

   <span class="token string">"/data/apollo/broker/bin/apollo-broker"</span> run

Or you can setup the broker as system <span class="token function">service</span> and run it <span class="token keyword">in</span> the background<span class="token punctuation">:</span>

   <span class="token function">sudo</span> <span class="token function">ln</span> <span class="token operator">-</span>s <span class="token string">"/data/apollo/broker/bin/apollo-broker-service"</span> <span class="token operator">/</span>etc<span class="token operator">/</span>init<span class="token punctuation">.</span>d<span class="token operator">/</span>
   <span class="token operator">/</span>etc<span class="token operator">/</span>init<span class="token punctuation">.</span>d<span class="token operator">/</span>apollo<span class="token operator">-</span>broker<span class="token operator">-</span><span class="token function">service</span> start

配置web admin


1
<span class="token function">sed</span> <span class="token operator">-</span>i <span class="token string">'s/127.0.0.1:61680/192.168.2.11:61680/'</span> <span class="token operator">/</span>data<span class="token operator">/</span>apollo<span class="token operator">/</span>broker<span class="token operator">/</span>etc<span class="token operator">/</span>apollo<span class="token punctuation">.</span>xml

启动服务


1
2
<span class="token function">ln</span> <span class="token operator">-</span>s <span class="token string">"/data/apollo/broker/bin/apollo-broker-service"</span> <span class="token operator">/</span>etc<span class="token operator">/</span>init<span class="token punctuation">.</span>d<span class="token operator">/</span>
<span class="token function">service</span>  apollo<span class="token operator">-</span>broker<span class="token operator">-</span><span class="token function">service</span> start