Zookeeper ActiveMQ and implement specific strategies to achieve cluster

A. Official documents

Use Master- Slave implementation ZooKeeper achieve, it is ActiveMQ be an effective high-availability solutions, high availability principle: using ZooKeeper (cluster) register all ActiveMQ Broker. Only one of the Broker can provide services outside (ie Master node), the other is in a standby state Broker is regarded as Slave. If the Master can not provide service due to a fault, the use of internal mechanisms ZooKeeper election will elect a Master Broker acts as a node from the Slave, continue to provide services;
official website the following documents:
Here Insert Picture Description
http://activemq.apache.org/replicated-leveldb -store.html

II. Deployment Scenarios

ActiveMQ Cluster Environment Preparation:
(1) First, we download apache-activemq-5.11.1- bin.tar.gz, our one master node up, then we (the cluster can be achieved on a node 192.168.1.111) in
( 2) Zookeeper program

Host ip Messaging Port Communication port Node directory / usr / local / under
192.168.1.111 2181 2888:3888 zookeeper
192.168.1.112 2181 2888:3888 zookeeper
192.168.1.113 2181 2888:3888 zookeeper

(3) ActiveMQ Program

Host ip Trunking communication Communication port Console port Node directory / usr / local / under
192.168.1.111 62621 51511 8161 activemq-cluster/node1/
192.168.1.111 62622 51512 8162 activemq-cluster/node2/
192.168.1.111 62623 51512 8163 activemq-cluster/node3/

2: Set up a zookeeper environment
3: Continue to build activemq environment (1) in the 192.168.1.111 node, create / usr / local / activemq- cluster folder, extract the apache-activemq-5.11.1- bin.tar.gz file, then extract the good rename the file, as follows:
1 Run: mkdir / usr / local / ActiveMQ Cluster-
2 commands: CD Software /
. 3 commands: tar -zxvf apache-activemq-5.11.1 -bin.tar.gz -C
/ usr / local / ActiveMQ-Cluster /
. 4 command: cd / usr / local / activemq -cluster / 5 commands: mv apache-activemq-5.11.1 / node1
doing so, the decompression is repeated again apache-activemq-5.11.1- bin .tar.gz files to / usr / local / activemq- cluster / down, build node3 node2 and folders, as follows:
Here Insert Picture Description

(2)那我们现在已经解压好了三个mq节点也就是node1、node2、node3,下面 我们要做的事情就是更改每个节点不同的配置和端口(由于是在一台机器上实 现集群)。
1 修改控制台端口(默认为8161),在mq安装路径下的conf/jetty.xml进 行修改即可。(三个节点都要修改,并且端口都不同)
1命令:cd /usr/local/activemq-cluster/node1/conf/
2命令:vim /usr/local/activemq-cluster/node1/conf/jetty.xml
Here Insert Picture Description
三个节点都需要修改为8161、8162、8163!!!
2 集群配置文件修改:我们在mq安装路径下的conf/activemq.xml进行修 改其中的持久化适配器,修改其中的bind、zkAddress、hostname、zkPath。 然后也需要修改mq的brokerName,并且每个节点名称都必须相同。 命令:vim /usr/local/activemq-cluster/node1/conf/activemq.xml
第一处修改:

brokerName=”activemq-cluster”(三个节点都需要修改)

Here Insert Picture Description
第二处修改:

先注释掉适配器中的kahadb

Here Insert Picture Description
第三处修改:添加新的leveldb配置如下(三个节点都需要修改):

Node1:
<persistenceAdapter>
<!--kahaDB directory="${activemq.data}/kahadb"/ --> <replicatedLevelDB
directory="${activemq.data}/leveldb"
replicas="3"
bind="tcp://0.0.0.0:62621" zkAddress="192.168.1.111:2181,192.168.1.112:2181,192.168.1.113:2181" hostname="bhz111"
zkPath="/activemq/leveldb-stores"/>
</persistenceAdapter>
Node2:
<persistenceAdapter>
<!--kahaDB directory="${activemq.data}/kahadb"/ --> <replicatedLevelDB
directory="${activemq.data}/leveldb"
replicas="3"
bind="tcp://0.0.0.0:62622" zkAddress="192.168.1.111:2181,192.168.1.112:2181,192.168.1.113:2181" hostname="bhz111"
zkPath="/activemq/leveldb-stores"
/>
</persistenceAdapter>
Node3:
<persistenceAdapter>
<!--kahaDB directory="${activemq.data}/kahadb"/ --> <replicatedLevelDB
directory="${activemq.data}/leveldb"
replicas="3"
bind="tcp://0.0.0.0:62623" zkAddress="192.168.1.111:2181,192.168.1.112:2181,192.168.1.113:2181" hostname="bhz111"
zkPath="/activemq/leveldb-stores"
/>
</persistenceAdapter>

第四处修改:(修改通信的端口,避免冲突)
命令:

vim /usr/local/activemq-cluster/node1/conf/activemq.xml 

修改这个文件的通信端口号,三个节点都需要修改(51511,51512,51513)
Here Insert Picture Description
Ok,到此为止,我们的activemq集群环境已经搭建完毕!

三 启动测试

第一步:启动zookeeper集群,命令:zkServer.sh start
第二步:启动mq集群:顺序启动mq:命令如下:

/usr/local/activemq-cluster/node1/bin/activemq start(关闭stop)
/usr/local/activemq-cluster/node2/bin/activemq start(关闭stop)
/usr/local/activemq-cluster/node3/bin/activemq start(关闭stop) 

第三步:查看日志信息:

tail -f /usr/local/activemq-cluster/node1/data/activemq.log 
tail -f /usr/local/activemq-cluster/node2/data/activemq.log 
tail -f /usr/local/activemq-cluster/node3/data/activemq.log 
如果不报错,我们的集群启动成功,可以使用控制台查看!

Step four: brokerUrl cluster configuration can be modified:

failover:(tcp://192.168.1.111:51511,tcp://192.168.1.111:51512,tcp://1 92.168.1.111:51513)?Randomize=false
/usr/local/activemq-cluster/node1/bin/activemq stop 
/usr/local/activemq-cluster/node2/bin/activemq stop 
/usr/local/activemq-cluster/node3/bin/activemq stop zkServer.sh stop

Four: Load Balancing configuration is as follows:

Cluster 1 Cluster 2 links:

<networkConnectors>
<networkConnector uri="static:
(tcp://192.168.1.112:51514,tcp://192.168.1.112:51515,
tcp://192.168.1.112:51516)" duplex="false"/>
</networkConnectors>

Cluster 1 Cluster 2 links:

<networkConnectors>
<networkConnector uri="static:
(tcp://192.168.1.111:51511,tcp://192.168.1.111:51512,
tcp://192.168.1.111:51513)" duplex="false"/>
</networkConnectors>

Five Java code implementation

Receiver


import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Receiver {

	public static void main(String[] args)  {
		try {
			//第一步:建立ConnectionFactory工厂对象,需要填入用户名、密码、以及要连接的地址,均使用默认即可,默认端口为"tcp://localhost:61616"
			ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
					ActiveMQConnectionFactory.DEFAULT_USER, 
					ActiveMQConnectionFactory.DEFAULT_PASSWORD, 
					"failover:(tcp://192.168.1.111:51511,tcp://192.168.1.111:51512,tcp://192.168.1.111:51513)?Randomize=false");
			
			//第二步:通过ConnectionFactory工厂对象我们创建一个Connection连接,并且调用Connection的start方法开启连接,Connection默认是关闭的。
			Connection connection = connectionFactory.createConnection();
			connection.start();
			
			//第三步:通过Connection对象创建Session会话(上下文环境对象),用于接收消息,参数配置1为是否启用是事务,参数配置2为签收模式,一般我们设置自动签收。
			Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
			
			//第四步:通过Session创建Destination对象,指的是一个客户端用来指定生产消息目标和消费消息来源的对象,在PTP模式中,Destination被称作Queue即队列;在Pub/Sub模式,Destination被称作Topic即主题。在程序中可以使用多个Queue和Topic。
			Destination destination = session.createQueue("first");
			//第五步:通过Session创建MessageConsumer
			MessageConsumer consumer = session.createConsumer(destination);
			
			while(true){
				TextMessage msg = (TextMessage)consumer.receive();
				if(msg == null) break;
				System.out.println("收到的内容:" + msg.getText());
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}

		
		
		
	}
}

Sender

package bhz.mq.cluster;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;

public class Sender {
	
	public static void main(String[] args) {
		try {
			//第一步:建立ConnectionFactory工厂对象,需要填入用户名、密码、以及要连接的地址,均使用默认即可,默认端口为"tcp://localhost:61616"
			ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
					ActiveMQConnectionFactory.DEFAULT_USER, 
					ActiveMQConnectionFactory.DEFAULT_PASSWORD, 
					"failover:(tcp://192.168.1.111:51511,tcp://192.168.1.111:51512,tcp://192.168.1.111:51513)?Randomize=false");
			
			//第二步:通过ConnectionFactory工厂对象我们创建一个Connection连接,并且调用Connection的start方法开启连接,Connection默认是关闭的。
			Connection connection = connectionFactory.createConnection();
			connection.start();
			
			//第三步:通过Connection对象创建Session会话(上下文环境对象),用于接收消息,参数配置1为是否启用是事务,参数配置2为签收模式,一般我们设置自动签收。
			Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
			
			//第四步:通过Session创建Destination对象,指的是一个客户端用来指定生产消息目标和消费消息来源的对象,在PTP模式中,Destination被称作Queue即队列;在Pub/Sub模式,Destination被称作Topic即主题。在程序中可以使用多个Queue和Topic。
			Destination destination = session.createQueue("first");
			
			//第五步:我们需要通过Session对象创建消息的发送和接收对象(生产者和消费者)MessageProducer/MessageConsumer。
			MessageProducer producer = session.createProducer(null);
			
			//第六步:我们可以使用MessageProducer的setDeliveryMode方法为其设置持久化特性和非持久化特性(DeliveryMode),我们稍后详细介绍。
			//producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
			
			//第七步:最后我们使用JMS规范的TextMessage形式创建数据(通过Session对象),并用MessageProducer的send方法发送数据。同理客户端使用receive方法进行接收数据。最后不要忘记关闭Connection连接。
			
			for(int i = 0 ; i < 500000 ; i ++){
				TextMessage msg = session.createTextMessage("我是消息内容" + i);
				// 第一个参数目标地址
				// 第二个参数 具体的数据信息
				// 第三个参数 传送数据的模式
				// 第四个参数 优先级
				// 第五个参数 消息的过期时间
				producer.send(destination, msg, DeliveryMode.NON_PERSISTENT, 0 , 1000L);
				System.out.println("发送消息:" + msg.getText());
				Thread.sleep(1000);
				
			}

			if(connection != null){
				connection.close();
			}			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}

Published 16 original articles · won praise 5 · Views 366

Guess you like

Origin blog.csdn.net/weixin_36802143/article/details/104627781