activemq mysql集群配置_ActiveMQ专题--集群,高可用方案

ActiveMQ集群部署方式

Master-Slave部署方式

Broker-Cluster部署方式

Master-Slave与Broker-Cluster相结合的部署方式

Shared database Master-Slave方式

ca90ed1b17df

image.png

实战

环境

centos 7

在服务器器部署两个节点,一个是A节点只需要添加配置信息,B节点需要修改相应的端口。

使用数据库进行消息持久化

引入数据库驱动包和数据库连接池mysql驱动包

把数据库驱动放到activemq目录下 lib/extra

如:mysql-connector-java-5.1.41.jar

ca90ed1b17df

image.png

修改activemq.xml文件

开启持久化

A节点配置

ca90ed1b17df

image.png

persistent="true"

activemq默认是用的kahadb的持久化方式,下面修改为mysql的持久化方案

ca90ed1b17df

image.png

配置数据源bean

ca90ed1b17df

image.png

B节点也进行相应的修改

ca90ed1b17df

image.png

ca90ed1b17df

image.png

启动服务

通过jcmd查看java进程,发现并没有启动成功

查看日志错误信息如下:

ca90ed1b17df

image.png

重新修改配置文件

使用java代码进行测试

consumer

package com.study.mq.b2_clustering;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

// http://activemq.apache.org/failover-transport-reference.html

public class ConsumerFailover {

public static void main(String[] args) throws InterruptedException {

// 非failover的公共参数配置通过nested.*,例如 failover:(...)?nested.wireFormat.maxInactivityDuration=1000

// ?randomize=false 随机选择,默认是顺序

// 指定优先切换 failover:(tcp://host1:61616,tcp://host2:61616,tcp://host3:61616)?priorityBackup=true&priorityURIs=tcp://local1:61616,tcp://local2:61616

// maxReconnectDelay重连的最大间隔时间(毫秒)

String brokerUrl = "failover:(tcp://activemq.tkxb.com:61616,tcp://activemq.tkxb.com:61617)?initialReconnectDelay=100";

ConsumerThread queue1 = new ConsumerThread(brokerUrl, "queue-cluster02");

queue1.start();

queue1.join();

}

}

class ConsumerThread extends Thread {

String brokerUrl;

String destinationUrl;

public ConsumerThread(String brokerUrl, String destinationUrl) {

this.brokerUrl = brokerUrl;

this.destinationUrl = destinationUrl;

}

@Override

public void run() {

ActiveMQConnectionFactory connectionFactory;

Connection conn;

Session session;

MessageConsumer consumer;

try {

// brokerURL http://activemq.apache.org/connection-configuration-uri.html

// 1、创建连接工厂

connectionFactory = new ActiveMQConnectionFactory(this.brokerUrl);

// 2、创建连接对象

conn = connectionFactory.createConnection();

conn.start(); // 一定要启动

// 3、创建会话(可以创建一个或者多个session)

session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

// 4、创建点对点接收的目标,queue - 点对点

Destination destination = session.createQueue(destinationUrl);

// 5、创建消费者消息 http://activemq.apache.org/destination-options.html

consumer = session.createConsumer(destination);

// 6、接收消息

consumer.setMessageListener(message -> {

try {

if (message instanceof TextMessage) {

System.out.println("收到文本消息:" + ((TextMessage) message).getText());

} else {

System.out.println(message);

}

} catch (JMSException e) {

e.printStackTrace();

}

});

} catch (JMSException e) {

e.printStackTrace();

}

}

}

provider

package com.study.mq.b2_clustering;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**

* 简单生产者

*/

public class Producer {

public static void main(String[] args) {

String brokerUrl = "failover:(tcp://activemq.tkxb.com:61616,tcp://activemq.tkxb.com:61617)?initialReconnectDelay=100";

new ProducerThread(brokerUrl, "queue-cluster02").start();

}

static class ProducerThread extends Thread {

String brokerUrl;

String destinationUrl;

public ProducerThread(String brokerUrl, String destinationUrl) {

this.brokerUrl = brokerUrl;

this.destinationUrl = destinationUrl;

}

@Override

public void run() {

ActiveMQConnectionFactory connectionFactory;

Connection conn;

Session session;

try {

// 1、创建连接工厂

connectionFactory = new ActiveMQConnectionFactory(brokerUrl);

// 2、创建连接对象md

conn = connectionFactory.createConnection();

conn.start();

// 3、创建会话

session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

// 4、创建点对点发送的目标

Destination destination = session.createQueue(destinationUrl);

// 5、创建生产者消息

MessageProducer producer = session.createProducer(destination);

// 设置生产者的模式,有两种可选 持久化 / 不持久化

producer.setDeliveryMode(DeliveryMode.PERSISTENT);

// 6、创建一条文本消息

String text = "Hello world!";

TextMessage message = session.createTextMessage(text);

for (int i = 0; i < 1; i++) {

// 7、发送消息

producer.send(message);

}

// 8、 关闭连接

session.close();

conn.close();

} catch (JMSException e) {

e.printStackTrace();

}

}

}

}

测试

可以启动consumer,然后收到关闭消费者连接的activemq ,这里我需要关闭A节点。

ca90ed1b17df

image.png

Replicaterd LevelDB Store方式(弃用)

ca90ed1b17df

image.png

Broker-Cluster 部署方式

ca90ed1b17df

image.png

搭建环境

准备在一台服务器上搭建两个节点A,B

A:不修改其他端口,只添加配置信息。

B:需要修改所有的端口信息。

修改配置文件

conf/activemq.xml文件下的标签中添加以下代码

ca90ed1b17df

image.png

节点B的其他端口也需要修改

ca90ed1b17df

image.png

jetty.xml 的web页面访问端口也进行了修改

ca90ed1b17df

image.png

ca90ed1b17df

image.png

节点B也需要添加代码到

ca90ed1b17df

image.png

启动A,B两节点

tail -f /var/activemq/data/activemq.log 查看日志信息

查看A节点的启动日志

ca90ed1b17df

image.png

可以发现B节点并没有启动成功

B节点的日志信息:

ca90ed1b17df

image.png

重新修改B节点的端口:

ca90ed1b17df

image.png

使用jcmd命令

ca90ed1b17df

image.png

看到进程以及启动成功了

查看A节点日志:

ca90ed1b17df

image.png

通过日志可以看出启动成功了,并成功连接B节点了。

总结

修改配置前一定需要停止activemq服务,安装过程遇到问题,可以查看日志。

Activemq集群是通过一种特殊的队列进行集群操作的。

ca90ed1b17df

image.png

使用JAVA代码测试Broker-Cluster集群模式

provider

使用A节点生产数据

package com.study.mq.b2_clustering.network_connector;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**

* 简单生产者

*/

public class Producer {

public static void main(String[] args) {

// 生产者用A节点 activemq.tkxb.com:61616

String brokerUrl = "failover:(tcp://activemq.tkxb.com:61616)?initialReconnectDelay=100";

new ProducerThread(brokerUrl, "queue-cluster01").start();

}

static class ProducerThread extends Thread {

String brokerUrl;

String destinationUrl;

public ProducerThread(String brokerUrl, String destinationUrl) {

this.brokerUrl = brokerUrl;

this.destinationUrl = destinationUrl;

}

@Override

public void run() {

ActiveMQConnectionFactory connectionFactory;

Connection conn;

Session session;

try {

// 1、创建连接工厂

connectionFactory = new ActiveMQConnectionFactory(brokerUrl);

// 2、创建连接对象md

conn = connectionFactory.createConnection();

conn.start();

// 3、创建会话

session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

// 4、创建点对点发送的目标

Destination destination = session.createQueue(destinationUrl);

// 5、创建生产者消息

MessageProducer producer = session.createProducer(destination);

// 设置生产者的模式,有两种可选 持久化 / 不持久化

producer.setDeliveryMode(DeliveryMode.PERSISTENT);

// 6、创建一条文本消息

String text = "Hello world!";

TextMessage message = session.createTextMessage(text);

for (int i = 0; i < 1; i++) {

// 7、发送消息

producer.send(message);

}

// 8、 关闭连接

session.close();

conn.close();

} catch (JMSException e) {

e.printStackTrace();

}

}

}

}

consumer

使用B节点消费数据

package com.study.mq.b2_clustering.network_connector;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

// http://activemq.apache.org/networks-of-brokers.html

public class ConsumerNetowork {

public static void main(String[] args) throws InterruptedException {

// 消费者用A节点 activemq.tkxb.com:61616

String brokerUrl = "failover:(tcp://activemq.tkxb.com:61617)?initialReconnectDelay=100";

ConsumerThread queue1 = new ConsumerThread(brokerUrl, "queue-cluster01");

queue1.start();

queue1.join();

}

}

class ConsumerThread extends Thread {

String brokerUrl;

String destinationUrl;

public ConsumerThread(String brokerUrl, String destinationUrl) {

this.brokerUrl = brokerUrl;

this.destinationUrl = destinationUrl;

}

@Override

public void run() {

ActiveMQConnectionFactory connectionFactory;

Connection conn;

Session session;

MessageConsumer consumer;

try {

// brokerURL http://activemq.apache.org/connection-configuration-uri.html

// 1、创建连接工厂

connectionFactory = new ActiveMQConnectionFactory(this.brokerUrl);

// 2、创建连接对象

conn = connectionFactory.createConnection();

conn.start(); // 一定要启动

// 3、创建会话(可以创建一个或者多个session)

session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

// 4、创建点对点接收的目标,queue - 点对点

Destination destination = session.createQueue(destinationUrl);

// 5、创建消费者消息 http://activemq.apache.org/destination-options.html

consumer = session.createConsumer(destination);

// 6、接收消息

consumer.setMessageListener(message -> {

try {

if (message instanceof TextMessage) {

System.out.println("收到文本消息:" + ((TextMessage) message).getText());

} else {

System.out.println(message);

}

} catch (JMSException e) {

e.printStackTrace();

}

});

} catch (JMSException e) {

e.printStackTrace();

}

}

}

Master-Slave与Broker-Cluster结合

ca90ed1b17df

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值