基本概念

  • 生产者 - 发送消息的客户
  • 消费者 - 接受消息的客户
  • 配置文件 - activemq.xml

基本操作

初始化MQ

$/usr/local/apache-activemq-5.10.0/bin/activemq setup /etc/default/activemq

debug模式

$ /usr/local/apache-activemq-5.10.0/bin/activemq console

启动MQ

$ nohup /usr/local/apache-activemq-5.10.0/bin/activemq start /etc/default/activemq > /tmp/smlog.1 2>&1 &

关闭MQ

/usr/local/apache-activemq-5.10.0/bin/activemq stop

主备集群

有三种组成形式

  • 共享存储,数据存储为KahaDB(嵌入式数据库
  • JDBC,数据存储为关系数据
  • Replication HA,数据存储为LevelDB,分布式部署,依赖组件为Zookeeper

注意:集群服务涉及端口较多,为方便调试,建议先关闭iptables。

版本选择

ActiveMQ在5.9之后才支持Replication HA.最新发布的5.11修正了5.10中的诸多Bug,因此建议使用最新版本5.11稳定版

版本选择情况:

  • Java - openjdk 1.7.
  • Zookeeper - zookeeper 3.4.
  • ActiveMQ - activemq 5.11.0

检查Hosts配置

Replication HA容错实现

引入zookeeper,可以实现(n-1)/2的容错

zookeeper默认端口:2181

配置zookeeper

下载,解压缩到/usr/local/zookeepe
拷贝编辑conf/zoo.cfg,追加以下内容

server.1=192.168.98.128:2888:388
server.2=192.168.98.129:2888:388
server.3=192.168.98.130:2888:388
#server.1中,1为权重值,这里标记为server.

在/tmp/zookeeper下新建myid文件,内容为上面提及的A,即第一台值为1,第二台为2,以此类推
修改zookeeper日志位置,bin/zkEnv.sh

```
if [ "x${ZOO_LOG_DIR}" = "x" 
the
ZOO_LOG_DIR=".
fi
```

配置activemq

修改activemq安装路径conf下activemq.xml文件

#### 启动服务

##### 启动zookeeper服务(允许以普通用户权限执行)

`/usr/local/zookeeper/bin/zkServer.sh start`

正确的输出如下

`/usr/local/zookeeper/bin/zkServer.sh start

JMX enabled by defaul

Using config: /usr/local/zookeeper/bin/../conf/zoo.cf

Starting zookeeper ... STARTE

`

##### 启动activemq服务(允许以普通用户权限执行)

`/usr/local/apache-actvemq-5.10.0/bin/activemq start`

正确的输出(Master Node)

`INFO | Master started: tcp://centos1:3828

INFO | Apache ActiveMQ 5.10.0 (localhost, ID:centos1-39590-1423392241168-0:1) is startin

INFO | Listening for connections at: tcp://centos1:61616?maximumConnections=1000&wireFormat.maxFrameSize=10485760

INFO | Connector openwire starte

INFO | Listening for connections at: amqp://centos1:5672?maximumConnections=1000&wireFormat.maxFrameSize=10485760

INFO | Connector amqp starte

INFO | Listening for connections at: stomp://centos1:61613?maximumConnections=1000&wireFormat.maxFrameSize=10485760

INFO | Connector stomp starte

INFO | Listening for connections at: mqtt://centos1:1883?maximumConnections=1000&wireFormat.maxFrameSize=10485760

INFO | Connector mqtt starte

INFO | Listening for connections at ws://centos1:61614?maximumConnections=1000&wireFormat.maxFrameSize=10485760

INFO | Connector ws starte

INFO | Apache ActiveMQ 5.10.0 (localhost, ID:centos1-39590-1423392241168-0:1) starte

INFO | For help or more information please see: http://activemq.apache.or

WARN | Store limit is 102400 mb (current store usage is 0 mb). The data directory: /usr/local/apache-activemq-5.10.0/data/leveldb only has 12949 mb of usable space - resetting to maximum available disk space: 12949 m

ERROR | Temporary Store limit is 51200 mb, whilst the temporary data directory: /usr/local/apache-activemq-5.10.0/data/localhost/tmp_storage only has 12949 mb of usable space - resetting to maximum available 12949 mb

INFO | ActiveMQ WebConsole available at http://0.0.0.0:8161

INFO | Initializing Spring FrameworkServlet 'dispatcher

INFO | jolokia-agent: No access restrictor found at classpath:/jolokia-access.xml, access to all MBeans is allowe

INFO | Slave has connected: 8b95b053-5c96-4c03-8aa2-cb767e000f6

INFO | Slave has now caught up: 8b95b053-5c96-4c03-8aa2-cb767e000f6

INFO | Slave has connected: 01ff5a37-6cc9-4c11-82e2-612d0757cebb

`

正确的输出(Slave Node)

`INFO | Opening socket connection to server centos1/192.168.98.128:218

INFO | Socket connection established to centos1/192.168.98.128:2181, initiating sessio

INFO | Session establishment complete on server centos1/192.168.98.128:2181, sessionid = 0x14b68b9b2670003, negotiated timeout = 400

WARN | listeners are taking too long to process the event

WARN | listeners are taking too long to process the event

WARN | listeners are taking too long to process the event

WARN | listeners are taking too long to process the event

INFO | Using the pure java LevelDB implementation

INFO | Attaching to master: tcp://centos1:3828

INFO | Slave starte

INFO | Attaching... Downloaded 0.00/0.00 kb and 1/1 file

INFO | Attached

`

如果遇到如下错误,请按照文末Bug AMQ-5225的文章处理

`INFO | Stopped LevelDB[/usr/local/apache-activemq-5.10.0/data/leveldb

`

#### Failover切换测试

##### master节点中断

在确认状态后,slave节点进行选举,最先达到收敛状态的节点成为新的Master。

##### slave节点中断

在经过互相确认之后,master节点不变,继续提供服务。

#### rollback回滚测试

离线节点重新启动后,经过互相状态确认,加入集群,同时从Master节点下载最新的增量数据,完成收敛。

### 加入开机自启动

为了实现系统快速恢复,将zookeeper和activemq服务加入rc.local条目。

`su - qipo -c '/usr/local/zookeeper/bin/zkServer.sh start

su - qipo -c '/usr/local/activemq/bin/activemq start'

`

#### 客户端设定

加入无效连接侦测

`failover:(tcp://host01:61616,tcp://host02:61616,tcp://host03:61616)?nested.wireFormat.maxInactivityDuration=1000`

##### Spring连接设定

` <!-- ActiveMQ 连接池配置 --&gt

    <bean id="activemqPoolFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"&gt

        <property name="connectionFactory"&gt

            <bean class="org.apache.activemq.ActiveMQConnectionFactory"&gt

                <property name="brokerURL"&gt

                    <value>failover:(tcp://web1t-priv:61616,tcp://web2p-priv:61616,tcp://web3p-priv:61616)?nested.wi

eFormat.maxInactivityDuration=1000</value&gt

                </property&gt

            </bean&gt

        </property&gt

    </bean&gt

`

## 拓展部分

### JMS性能调优

### 所需开放端口

For **Zookeeper** to work correctly, 3 ports have to be opened on the server’s firewall:
  • 2181 – the port that clients will use to connect to the ZK ensembl

  • 2888 – port used by ZK for quorum electio

  • 3888 – port used by ZK for leader election

    The firewall changes that you’ll need for ActiveMQ are:

  • 61616 – default Openwire por

  • 8161 – Jetty port for web consol

  • 61619 – LevelDB peer-to-peer replication port

    连接测试工具

    有网友写了简易的JMS测试工具-amqutil,本次测试中使用到该小工具和Web Console。

    小工具的使用范例如下

    `amqutil -r -d test -U failover:(tcp://centos1:61616,tcp://centos2:61616,tcp://centos3:61616

    amqutil -b -d test -U failover:(tcp://centos1:61616,tcp://centos2:61616,tcp://centos3:61616)

相关链接

Setup ActiveMQ, Zookeeper, and Replicated LevelDB running in JDK 8 and CentOS

zookeeper与activemq最新存储replicatedLevelDB整合

bug AMQ-5225

Failover Transport Reference

amqutil project