springboot整合activemq,实现broker集群部署(cluster)
1、为实现jms高并发操作,需要对activemq进行集群部署,broker cluster就是activemq自带的解决方案。这里以一台主机来进行演示:原有的activemq作为broker-a,再新建一个activemq服务作为broker-b
2、对broker-b的tcp端口及管理端口进行修改
(1)tcp端口:修改config目录下的activemq.xml ,端口号改为:61617,因为只用到tcp模式,其他方式注掉防止端口冲突
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="openwire" uri="tcp://0.0.0.0:61617?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<!-- <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
-->
</transportConnectors>
(2)管理端口:修改config目录下的jetty.xml,端口号改为:8162
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
<!-- the default port number for the web console -->
<property name="host" value="0.0.0.0"/>
<property name="port" value="8162"/>
</bean>
3、broker-a,broker-b相互添加通信,修改activemq.xml文件,broker-a中配置如下,broker-b同理
<networkConnectors>
<networkConnector uri="static:(tcp://localhost:61617)" duplex="false"/>
</networkConnectors>
4、配置好之后,在程序中application.properties修改配置,将原来的访问地址注掉,改为集群部署
#访问地址
#spring.activemq.broker-url=tcp://127.0.0.1:61616
#集群配置
spring.activemq.broker-url=failover:(tcp://localhost:61616,tcp://localhost:61617)
5、在controller中增加测试方法,启动broker-a、broker-b,访问地址:http://localhost:8080/msg/testdis
@GetMapping("testdis")
public String testdis() { Destination destination = new ActiveMQQueue("test.queue");
for(int i =1;i<10;i++) {
msgService.sendMessage(destination, "测试消息=====第"+i);
} return "测试集群部署";
}
控制台输出如下,说明集群部署成功,activemq还支持master-slave模式,通过文件共享-文件锁的方式来实现集群拓展,这个我们只需要将activemq.xml中的kahaDB路径配置成相同的即可