Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1885546
  • 博文数量: 1000
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 7921
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-20 09:23
个人简介

storage R&D guy.

文章分类

全部博文(1000)

文章存档

2019年(5)

2017年(47)

2016年(38)

2015年(539)

2014年(193)

2013年(178)

分类: 服务器与存储

2015-11-06 17:36:50

好久没有更新博客了。最近由于项目并发请求压力比较大,所以着手改进架构,引入消息中间件集群作为一个缓冲消息队列。

需求:

1、将大量的WebService请求报文发送到mq集群之中,并保持消息先后顺序

2、保证每个消息的可靠性

3、维护MQ服务器的可扩展性


综合考虑,决定使用Apache的 activemq。接触的时候,新的activemq 5.10已经出,所以,本项目使用的是activemq5.10。activemq5.10要求jdk6+,由于项目立项比较老,所以将项目整体迁移到jdk1.7进行部署,并引入activemq服务器。OK,废话不多说,开始。


首先,选三台服务器。

      之所以选三台,是因为zookeeper推荐最低三台配置,这样可以保持最大的可用服务器数,具体的这里不多说,可以查阅zookeeper的安装配置,以后有机会,详细讲解zookeeper。

我这里选择三台linux服务器,ip地址分别为:192.168.120.241,192.168.120.242,192.168.120.171。系统均使用centos6.4


1、创建账号

      useradd amqbroker;  //默认会创建amqbroker的群组,并且amqbroker会自动加入amqbroker群组。

      passwd amqbroker;修改密码

      cd /home/amqbroker  切换目录

2、下载zookeeper

wget 

      tar -xvf  zookeeper-3.3.6.tar.gz

修改配置,mv zoo_sample.cfg zoo.cfg

     vi zoo.cfg,内容如下:


[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1.  1 # The number of milliseconds of each tick  
  2.  2 tickTime=2000  
  3.  3 # The number of ticks that the initial   
  4.  4 # synchronization phase can take  
  5.  5 initLimit=10  
  6.  6 # The number of ticks that can pass between   
  7.  7 # sending a request and getting an acknowledgement  
  8.  8 syncLimit=5  
  9.  9 # the directory where the snapshot is stored.  
  10. 10 # do not use /tmp for storage, /tmp here is just   
  11. 11 # example sakes.  
  12. 12   
  13. 13 dataDir=/home/amqbroker/zkdir/data  
  14. 14 dataLogDir=/home/amqbroker/zkdir/log  
  15. 15 # the port at which the clients will connect  
  16. 16 clientPort=2181  
  17. 17 # the maximum number of client connections.  
  18. 18 # increase this if you need to handle more clients  
  19. 19 #maxClientCnxns=60  
  20. 20 #  
  21. 21 # Be sure to read the maintenance section of the   
  22. 22 # administrator guide before turning on autopurge.  
  23. 23 #  
  24. 24 # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance  
  25. 25 #  
  26. 26 # The number of snapshots to retain in dataDir  
  27. 27 #autopurge.snapRetainCount=3  
  28. 28 # Purge task interval in hours  
  29. 29 # Set to "0" to disable auto purge feature  
  30. 30 #autopurge.purgeInterval=1  
  31. 31   
  32. 32   
  33. 33 ##three servers of this cluster  
  34. 34 server.1=192.168.120.241:2888:3888  
  35. 35 server.2=192.168.120.242:2888:3888  
  36. 36 server.3=192.168.120.171:2888:3888  
 dataDir=/home/amqbroker/zkdir/data
 dataLogDir=/home/amqbroker/zkdir/log  这两行说明了,需要创建一个文件夹,分别存放data以及log,所以,在/home/amqbroker下创建文件夹zkdir,也可以创建在其他地方,只要配置中指定就好


在zkdir/data下,创建myid文件,并写入与ip地址相称的服务器编号,比如,192.168.120.241,写入 1;echo 1>myid;


其他两台机器配置相同,不过myid文件写入相应的id号

启动服务:

cd /home/amqbroker/zookeeper-3.3.6/bin

./zkServer.sh start

可以使用zookeeper自带的客户端测试:

./zkCli.sh -server 192.168.120.241:2181

其他两台一样配置



3、下载activemq-5.10

wget  

tar -xvf apache-activemq-5.10.1-bin.tar.gz

vi apache-activemq-5.10.1/conf/activemq.xml



[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. "code" class="java">  1   
  2.  17   
  3.  18 
  4.  19   xmlns=""  
  5.  20   xmlns:xsi=""  
  6.  21   xsi:schemaLocation="http://www.springframework.org/schema/beans /spring-beans.xsd  
  7.  22   http://activemq.apache.org/schema/core ">  
  8.  23   
  9.  24       
  10.  25     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
  11.  26         "locations">  
  12.  27             file:${activemq.conf}/credentials.properties  
  13.  28           
  14.  29       
  15.  30   
  16.  31      
  17.  32       
  18.  38       
  19.  41     "" brokerName="job-broker" dataDirectory="${activemq.data}">  
  20.  42   
  21.  43           
  22.  44               
  23.  45                 
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1.  46                
  2.  47                 "QUEUE.SH.LTE.IMPORT">  
  3.  48                       
  4.  49                               
  5.  50                         "SH.LTE.DLQ." useQueueForQueueMessages="true" />  
  6.  51                       
  7.  52                   
  8.  53                 "QUEUE.STTTTT">  
  9.  54                       
  10.  55                            
  11.  56                           "TTT.LTE.SH.DLQ." useQueueForQueueMessages="true" />  
  12.  57                         
  13.  58                     
  14.  59                 ">" >  
  15.  60                       
  16.  68                     
  17.  69                     "1000"/>  
  18.  70                     
  19.  71                   
  20.  72                 
  21.  73               
  22.  74           
  23.  75   
  24.  76   
  25.  77           
  26.  84           
  27.  85             "false"/>  
  28.  86           
  29.  87   
  30.  88           
  31.  95   
  32.  96        
  33. 101        
  34. 102         
  35. 103              directory="${activemq.data}/leveldb"  
  36. 104              replicas="3"  
  37. 105              bind="tcp://0.0.0.0:0"  
  38. 106              zkAddress="192.168.120.171:2181,192.168.120.241:2181,192.168.120.242:2181"  
  39. 107              zkPassword=""  
  40. 108              hostname="192.168.120.241"  
  41. 109              sync="local_disk"  
  42. 110              zkPath="/activemq/leveldb-stores"/>  
  43. 111        
  44. 112   
  45. 113             
  46. 118             
  47. 119               
  48. 120                   
  49. 121                     "70" />  
  50. 122                   
  51. 123                   
  52. 124                     "100 gb"/>  
  53. 125                   
  54. 126                   
  55. 127                     "50 gb"/>  
  56. 128                   
  57. 129               
  58. 130           
  59. 131   
  60. 132           
  61. 138           
  62. 139               
  63. 140             "openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104    857600"/>  
  64. 141             "amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=1048576    00"/>  
  65. 142             "stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=1048    57600"/>  
  66. 143             "mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=1048576    00"/>  
  67. 144             "ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"    />  
  68. 145           
  69. 146   
  70. 147           
  71. 148           
  72. 149             "" class="org.apache.activemq.hooks.SpringContextHook" />  
  73. 150           
  74. 151   
  75. 152       
  76. 153   
  77. 154       
  78. 160     <import resource="jetty.xml"/>  
  79. 161   
  80. 162   





以上是192.168.120.241的activemq的activemq.xml配置文件配置。因为使用的是zookeeper,所以注意persistenceAdapter这个节点配置,如果有zkpassword,则填写,否则不要填写。按照上边的配置,zkpassword是不需要写的。

还有broker配置中,有一些policyEntity,是定制的,比如DLQ的配置,是因为针对不同的队列,重发机制的最大次数重发完以后,如果还有异常,则进入该队列定制的DLQ之中,详细的一些参数设置,请参考activemq官方文档。

另外,配置中应该有安全配置,目前没有写入,后续在说


另外两台服务器的配置与该文档相似,唯一不同就是    hostname="192.168.120.241"这里,需要写入各自的ip地址


启动activemq服务器

cd /home/amqbroker/activemq***/bin

./activemq start


三台服务器启动类似


然后,在浏览器地址栏里输入:

因为使用zookeeper做负载均衡,三台只有一台是master,其他两台处于等待状态,所以只有其中一台提供服务,但一旦这台服务器宕机以后,会有另外一台顶替上来,所以其他几个ip地址是打不开的,只有一台能打开

在客户端使用的时候,使用 failover进行配置的,如下:

##使用replicaDB方式来搭建activemq,使用zookeeper集群进行负载均衡以及数据同步
mq.broker.url=failover:(tcp://192.168.120.241:61616,tcp://192.168.120.242:61616,tcp://192.168.120.171:61616)?initialReconnectDelay=1000

在下一节说spring配置的时候再讲

阅读(1318) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~