ActiveMQ桥接以及负载分担

9 篇文章 0 订阅
7 篇文章 0 订阅

ActiveMQ可以实现多个mq之间进行路由,假设有两个mq,分别为brokerA和brokerB,当有一条消息发送到brokerA的队列test中,有一个客户端连接到brokerB上,并且要求获取test队列的消息时,brokerA中队列test的消息就会路由到brokerB上,反之brokerB的消息也会路由到brokerA。

如果只通过brokerB来消费消息,可以实现桥接。

如果通过brokerA或brokerB来消费消息,则可实现负载分担。

配置的关键点在于:

<networkConnectors>
            <networkConnector uri="static:(tcp://localhost:61616)" duplex="true"/>
 </networkConnectors>

ActiveMQ已经做的很好了,在Conf的目录下有现成的配置文件

activemq-static-network-broker1.xml

主要信息摘录:

<networkConnectors>
 </networkConnectors>

<transportConnectors>
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61616"/>
        </transportConnectors>


activemq-static-network-broker2.xml

主要信息摘录:

<networkConnectors>
            <networkConnector uri="static:(tcp://localhost:61616)" duplex="true"/>
        </networkConnectors>

 <transportConnectors>
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61618"/>
        </transportConnectors>


我们需要做的就是将ActiveMQ复制1份,单独放置到另外的文件夹中,这样我们就有了2份activemq啦。

分别将第一份中的activemq-static-network-broker1.xml 改名为activemq.xml     第二份中的activemq-static-network-broker2.xml改名为activemq.xml

分别启动2个activemq,日志输出分别如下

 INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi
 INFO | ActiveMQ 5.5.0 JMS Message Broker (static-broker1) is starting
 INFO | For help or more information please see: http://activemq.apache.org/
 INFO | Listening for connections at: tcp://SONGJIE:61616
 INFO | Connector openwire Started
 INFO | ActiveMQ JMS Message Broker (static-broker1, ID:SONGJIE-60899-1369726682642-0:1) started
 INFO | Connector vm://static-broker1 Started
 INFO | Started responder end of duplex bridge NC@ID:SONGJIE-60962-1369726697807-0:1
 INFO | Network connection between vm://static-broker1#0 and tcp:///127.0.0.1:60965(static-broker1) has been established.

 INFO | JMX consoles can connect to service:jmx:rmi:///jndi/rmi://localhost:1100/jmxrmi
 INFO | KahaDB is version 3
 INFO | Recovering from the journal ...
 INFO | Recovery replayed 2 operations from the journal in 0.019 seconds.
 INFO | ActiveMQ 5.5.0 JMS Message Broker (static-broker1) is starting
 INFO | For help or more information please see: http://activemq.apache.org/
 INFO | Listening for connections at: tcp://SONGJIE:61618
 INFO | Connector openwire Started
 INFO | Establishing network connection from vm://static-broker1?async=false&network=true to tcp://localhost:61616
 INFO | Connector vm://static-broker1 Started
 INFO | Network Connector DiscoveryNetworkConnector:NC:BrokerService[static-broker1] Started
 INFO | ActiveMQ JMS Message Broker (static-broker1, ID:SONGJIE-60962-1369726697807-0:1) started
 INFO | Network connection between vm://static-broker1#0 and tcp://localhost/127.0.0.1:61616(static-broker1) has been established.

可见启动成功了。

需要验证一下

一个生产者,生产到broker1,端口为61616,每3秒生产一个消息,总计生产20个。

2个消费着,一个消费者到broker1的61616消费消息,另外一个消费者到broker2的61618端口消费消息。

按照顺序启动生产者、第一消费者、第二消费者,在启动第二消费者的时候需要等第一消费者开始消费的时候再进行启动,以便验证消息被第一消费者消费之后,是否会被第二消费者重复消费。

第一消费者日志,前10个日志略掉。

11
2013-5-28 15:39:40
ActiveMQObjectMessage {commandId = 21, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:13, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:13, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726783382, arrival = 0, brokerInTime = 1369726786422, brokerOutTime = 1369726786422, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@f4d6b3, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
###ActiveMQObjectMessage {commandId = 21, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:13, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:13, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726783382, arrival = 0, brokerInTime = 1369726786422, brokerOutTime = 1369726786422, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@f4d6b3, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
org.apache.activemq.command.ActiveMQObjectMessage
12
2013-5-28 15:39:43
ActiveMQObjectMessage {commandId = 22, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:14, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:14, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726786421, arrival = 0, brokerInTime = 1369726789439, brokerOutTime = 1369726789440, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@85f3d6, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
###ActiveMQObjectMessage {commandId = 22, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:14, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:14, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726786421, arrival = 0, brokerInTime = 1369726789439, brokerOutTime = 1369726789440, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@85f3d6, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
org.apache.activemq.command.ActiveMQObjectMessage
13
2013-5-28 15:39:46
ActiveMQObjectMessage {commandId = 23, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:15, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:15, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726789438, arrival = 0, brokerInTime = 1369726792472, brokerOutTime = 1369726792473, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@17af435, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
###ActiveMQObjectMessage {commandId = 23, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:15, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:15, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726789438, arrival = 0, brokerInTime = 1369726792472, brokerOutTime = 1369726792473, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@17af435, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
org.apache.activemq.command.ActiveMQObjectMessage
14
2013-5-28 15:39:49
ActiveMQObjectMessage {commandId = 24, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:16, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:16, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726792470, arrival = 0, brokerInTime = 1369726795504, brokerOutTime = 1369726795504, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@136e10e, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
###ActiveMQObjectMessage {commandId = 24, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:16, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:16, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726792470, arrival = 0, brokerInTime = 1369726795504, brokerOutTime = 1369726795504, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@136e10e, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
org.apache.activemq.command.ActiveMQObjectMessage
15
2013-5-28 15:39:52
ActiveMQObjectMessage {commandId = 26, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:17, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:17, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726795507, arrival = 0, brokerInTime = 1369726798539, brokerOutTime = 1369726798539, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@17490ad, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
###ActiveMQObjectMessage {commandId = 26, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:17, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:17, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726795507, arrival = 0, brokerInTime = 1369726798539, brokerOutTime = 1369726798539, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@17490ad, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
org.apache.activemq.command.ActiveMQObjectMessage
16
2013-5-28 15:39:55
ActiveMQObjectMessage {commandId = 27, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:19, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:19, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726801569, arrival = 0, brokerInTime = 1369726804601, brokerOutTime = 1369726804601, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@1c68b20, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
###ActiveMQObjectMessage {commandId = 27, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:19, originalDestination = null, originalTransactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:19, producerId = ID:SONGJIE-60899-1369726682642-2:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = null, expiration = 0, timestamp = 1369726801569, arrival = 0, brokerInTime = 1369726804601, brokerOutTime = 1369726804601, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@1c68b20, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
org.apache.activemq.command.ActiveMQObjectMessage
18
2013-5-28 15:40:01


第二消费者日志
ActiveMQMessageConsumer { value=ID:SONGJIE-61352-1369726797634-0:1:1:1, started=true }
ActiveMQObjectMessage {commandId = 57, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:18, originalDestination = null, originalTransactionId = null, producerId = ID:SONGJIE-61158-1369726746561-0:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:18, expiration = 0, timestamp = 1369726798538, arrival = 0, brokerInTime = 1369726798538, brokerOutTime = 1369726801568, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@b2771, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
###ActiveMQObjectMessage {commandId = 57, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:18, originalDestination = null, originalTransactionId = null, producerId = ID:SONGJIE-61158-1369726746561-0:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:18, expiration = 0, timestamp = 1369726798538, arrival = 0, brokerInTime = 1369726798538, brokerOutTime = 1369726801568, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@b2771, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
org.apache.activemq.command.ActiveMQObjectMessage
17
2013-5-28 15:39:58
ActiveMQObjectMessage {commandId = 63, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:20, originalDestination = null, originalTransactionId = null, producerId = ID:SONGJIE-61158-1369726746561-0:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:20, expiration = 0, timestamp = 1369726804599, arrival = 0, brokerInTime = 1369726804599, brokerOutTime = 1369726807634, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@131ebb3, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
###ActiveMQObjectMessage {commandId = 63, responseRequired = false, messageId = ID:SONGJIE-61158-1369726746561-0:1:1:1:20, originalDestination = null, originalTransactionId = null, producerId = ID:SONGJIE-61158-1369726746561-0:1:1:1, destination = queue://TOOL.DEFAULT, transactionId = TX:ID:SONGJIE-61158-1369726746561-0:1:20, expiration = 0, timestamp = 1369726804599, arrival = 0, brokerInTime = 1369726804599, brokerOutTime = 1369726807634, correlationId = null, replyTo = null, persistent = true, type = null, priority = 4, groupID = null, groupSequence = 0, targetConsumerId = null, compressed = false, userID = null, content = org.apache.activemq.util.ByteSequence@131ebb3, marshalledProperties = null, dataStructure = null, redeliveryCounter = 0, size = 0, properties = null, readOnlyProperties = true, readOnlyBody = true, droppable = false}
org.apache.activemq.command.ActiveMQObjectMessage
19
2013-5-28 15:40:04
可以看到第二消费者只消费了序号为17和19的消息,其余消息都被第一消费者消费啦。

但愿有机会测试一下3个Broker。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现ActiveMQ与IBM MQ之间的双向通信,需要使用ActiveMQ的消息桥(Message Bridge)功能。下面是具体的步骤: 1. 在ActiveMQ上配置一个消息桥,将消息从ActiveMQ传递到IBM MQ。 ```xml <networkConnector name="bridge" uri="static:(tcp://<IBM MQ地址>:<IBM MQ端口>)"> <duplex>true</duplex> </networkConnector> ``` 2. 在ActiveMQ上配置一个消费者,用于接收IBM MQ的响应消息。 ```xml <destinationPolicy> <policyMap> <policyEntries> <policyEntry queue="responseQueueName"> <networkBridgeFilterFactory> <conditionalNetworkBridgeFilterFactory replayWhenNoConsumers="true"/> </networkBridgeFilterFactory> </policyEntry> </policyEntries> </policyMap> </destinationPolicy> ``` 3. 在IBM MQ上创建一个队列,用于接收来自ActiveMQ的消息。 4. 在IBM MQ上创建一个监听器,用于接收ActiveMQ的消息并将响应消息发送回ActiveMQ。 5. 在ActiveMQ上创建一个生产者,用于将IBM MQ的响应消息发送回原始的消息发送者。 ```java ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(); connectionFactory.setBrokerURL("tcp://localhost:61616"); Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageProducer producer = session.createProducer(null); Destination replyToDestination = message.getJMSReplyTo(); producer.send(replyToDestination, responseMessage); ``` 通过以上步骤,我们就可以实现ActiveMQ与IBM MQ之间的双向通信。需要注意的是,在配置消息桥和消息监听器时,需要设置正确的目标地址和端口号等参数,以确保消息能够正确地被传递和接收。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值