ActiveMQ 集群部署

ActiveMQ 集群部署

本章演示 ActiveMQ 集群部署,默认您已经安装了 zookeeper 集群,并在各服务器上成功安装了 ActiveMQ 单节点实例如果您的环境还不满足条件请参考前面的文章

ActiveMQ 问题记录

在集群部署过程中会出现各种问题,但是 ActiveMQ集群的部署是比较简单的,如果不能实现集群功能请检查以下配置:

  • 最坑的一个问题是ActiveMQ自己的问题,它少了一个 jar包 failureaccess-1.0.1,没有这个包会出现一堆报错,如果你认为自己的配置没有问题不妨把这个包加到 lib 里试试,maven 地址:https://mvnrepository.com/artifact/com.google.guava/failureaccess

  • 防火墙是否开启(包括虚拟机的,宿主主机的,如果是阿里云的还要进入阿里云配置访问规则,这玩意关了防火墙可干不掉它)

  • 集群名称和IP地址是否配置正确

  • 端口号是否冲突,我这里的三台虚拟机所以不用改端口号,如果是单台虚拟机部署三个ActiveMQ节点,需要注意端口冲突

集群部署

activemq.xml

  • 修改 broker 标签中的 brokerName ,三个实例必须一致

  • 在 persistenceAdapter 标签中添加 replicatedLevelDB,集群和zookeeper相关信息

  <persistenceAdapter>
        	<!--
        	bind:允许访问的地址
        	replicas: 集群实例数量  
        	zkAddress:zookeeper 实例的地址
        	hostname:本机IP地址
       		-->
            <replicatedLevelDB
            	directory="${activemq.data}/leveldb"
            	replicas="3"
            	bind="tcp://0.0.0.0:63631"
            	zkAddress="192.168.31.151:2181,192.168.31.154:2181,192.168.31.157:2181"
            	hostname="192.168.31.151"
            	zkPath="/activemq/leveldb-stores"
            	></replicatedLevelDB>
        </persistenceAdapter>

  • 如果是单台服务器中部署多个ActiveMQ,需要注意以下两点

    • 三个实例中 replicatedLevelDB 下的 bind 配置的端口号不能相同

    • 三个实例中 transportConnectors 标签中的端口号不能相同

jetty.xml

我这里是三个虚拟机,每个虚拟机部署一个 ActiveMQ 实例,所以 jetty.xml 不需要修改

如果是单台服务器,则需要将三个ActiveMQ 中 jetty.xml 的 jettyPort 调整为不同的端口号

<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
    <property name="host" value="0.0.0.0"/>
    <property name="port" value="8161"/>
</bean>

修改完成后重启activemq 集群即可

集群文件备份

配置文件环境是三台虚拟机,每个虚拟机中仅部署一个 ActiveMQ实例

activemq.xml

<beans
  xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>file:${activemq.conf}/credentials.properties</value>
        </property>
    </bean> 
                                <!-- 同一个集群中,所有实例的 brokerName 必须相同 -->
    <broker xmlns="http://activemq.apache.org/schema/core" brokerName="archser_MQ" dataDirectory="${activemq.data}">
        <destinationPolicy>
            <policyMap>
              <policyEntries>
                <policyEntry topic=">" > 
                  <pendingMessageLimitStrategy>
                    <constantPendingMessageLimitStrategy limit="1000"/>
                  </pendingMessageLimitStrategy>
                </policyEntry>
              </policyEntries>
            </policyMap>
        </destinationPolicy>
        <managementContext>
            <managementContext createConnector="false"/>
        </managementContext>
        <persistenceAdapter>
            <!--
            bind:允许访问的地址
            replicas: 集群实例数量  
            zkAddress:zookeeper 实例的地址
            hostname:本机IP地址
               -->
            <replicatedLevelDB
                directory="${activemq.data}/leveldb"
                replicas="3"
                bind="tcp://0.0.0.0:63631"
                zkAddress="192.168.31.151:2181,192.168.31.154:2181,192.168.31.157:2181"
                hostname="192.168.31.151"
                zkPath="/activemq/leveldb-stores"
                ></replicatedLevelDB>
        </persistenceAdapter>
          <systemUsage>
            <systemUsage>
                <memoryUsage>
                    <memoryUsage percentOfJvmHeap="70" />
                </memoryUsage>
                <storeUsage>
                    <storeUsage limit="100 gb"/>
                </storeUsage>
                <tempUsage>
                    <tempUsage limit="50 gb"/>
                </tempUsage>
            </systemUsage>
        </systemUsage>
        <transportConnectors>
            <transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
            <transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&amp;wireFormat.maxFrameSize=104857600"/>
        </transportConnectors>
        <shutdownHooks>
            <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
        </shutdownHooks>
    </broker>
    <import resource="jetty.xml"/>
</beans>
<!-- END SNIPPET: example -->

jetty.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="securityLoginService" class="org.eclipse.jetty.security.HashLoginService">
        <property name="name" value="ActiveMQRealm" />
        <property name="config" value="${activemq.conf}/jetty-realm.properties" />
    </bean>
    <bean id="securityConstraint" class="org.eclipse.jetty.util.security.Constraint">
        <property name="name" value="BASIC" />
        <property name="roles" value="user,admin" />
        <property name="authenticate" value="true" />
    </bean>
    <bean id="adminSecurityConstraint" class="org.eclipse.jetty.util.security.Constraint">
        <property name="name" value="BASIC" />
        <property name="roles" value="admin" />
        <property name="authenticate" value="true" />
    </bean>
    <bean id="securityConstraintMapping" class="org.eclipse.jetty.security.ConstraintMapping">
        <property name="constraint" ref="securityConstraint" />
        <property name="pathSpec" value="/*,/api/*,/admin/*,*.jsp" />
    </bean>
    <bean id="adminSecurityConstraintMapping" class="org.eclipse.jetty.security.ConstraintMapping">
        <property name="constraint" ref="adminSecurityConstraint" />
        <property name="pathSpec" value="*.action" />
    </bean>
    <bean id="rewriteHandler" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
        <property name="rules">
            <list>
                <bean id="header" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                  <property name="pattern" value="*"/>
                  <property name="name" value="X-FRAME-OPTIONS"/>
                  <property name="value" value="SAMEORIGIN"/>
                </bean>
                <bean id="header" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                  <property name="pattern" value="*"/>
                  <property name="name" value="X-XSS-Protection"/>
                  <property name="value" value="1; mode=block"/>
                </bean>
                <bean id="header" class="org.eclipse.jetty.rewrite.handler.HeaderPatternRule">
                  <property name="pattern" value="*"/>
                  <property name="name" value="X-Content-Type-Options"/>
                  <property name="value" value="nosniff"/>
                </bean>
            </list>
        </property>
    </bean>
    <bean id="secHandlerCollection" class="org.eclipse.jetty.server.handler.HandlerCollection">
        <property name="handlers">
            <list>
                   <ref bean="rewriteHandler"/>
                <bean class="org.eclipse.jetty.webapp.WebAppContext">
                    <property name="contextPath" value="/admin" />
                    <property name="resourceBase" value="${activemq.home}/webapps/admin" />
                    <property name="logUrlOnStart" value="true" />
                </bean>
                <bean class="org.eclipse.jetty.webapp.WebAppContext">
                    <property name="contextPath" value="/api" />
                    <property name="resourceBase" value="${activemq.home}/webapps/api" />
                    <property name="logUrlOnStart" value="true" />
                </bean>
                <bean class="org.eclipse.jetty.server.handler.ResourceHandler">
                    <property name="directoriesListed" value="false" />
                    <property name="welcomeFiles">
                        <list>
                            <value>index.html</value>
                        </list>
                    </property>
                    <property name="resourceBase" value="${activemq.home}/webapps/" />
                </bean>
                <bean id="defaultHandler" class="org.eclipse.jetty.server.handler.DefaultHandler">
                    <property name="serveIcon" value="false" />
                </bean>
            </list>
        </property>
    </bean>    
    <bean id="securityHandler" class="org.eclipse.jetty.security.ConstraintSecurityHandler">
        <property name="loginService" ref="securityLoginService" />
        <property name="authenticator">
            <bean class="org.eclipse.jetty.security.authentication.BasicAuthenticator" />
        </property>
        <property name="constraintMappings">
            <list>
                <ref bean="adminSecurityConstraintMapping" />
                <ref bean="securityConstraintMapping" />
            </list>
        </property>
        <property name="handler" ref="secHandlerCollection" />
    </bean>
    <bean id="contexts" class="org.eclipse.jetty.server.handler.ContextHandlerCollection">
    </bean>
    <bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start">
        <property name="host" value="0.0.0.0"/>
        <property name="port" value="8161"/>
    </bean>
    <bean id="Server" depends-on="jettyPort" class="org.eclipse.jetty.server.Server"
        destroy-method="stop">
        <property name="handler">
            <bean id="handlers" class="org.eclipse.jetty.server.handler.HandlerCollection">
                <property name="handlers">
                    <list>
                        <ref bean="contexts" />
                        <ref bean="securityHandler" />
                    </list>
                </property>
            </bean>
        </property>
    </bean>
    <bean id="invokeConnectors" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetObject" ref="Server" />
        <property name="targetMethod" value="setConnectors" />
        <property name="arguments">
        <list>
               <bean id="Connector" class="org.eclipse.jetty.server.ServerConnector">
                   <constructor-arg ref="Server" />
                    <!-- see the jettyPort bean -->
                   <property name="host" value="#{systemProperties['jetty.host']}" />
                   <property name="port" value="#{systemProperties['jetty.port']}" />
               </bean> 
            </list>
        </property>
    </bean>
    <bean id="configureJetty" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="org.apache.activemq.web.config.JspConfigurer.configureJetty" />
        <property name="arguments">
            <list>
                <ref bean="Server" />
                <ref bean="secHandlerCollection" />
            </list>
        </property>
    </bean>
    <bean id="invokeStart" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" 
        depends-on="configureJetty, invokeConnectors">
        <property name="targetObject" ref="Server" />
        <property name="targetMethod" value="start" />      
    </bean>
</beans>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ActiveMQ 是一个流行的开源消息队列软件,它支持多种协议和编程语言,能够满足企业级的消息传递需求。要部署 ActiveMQ 集群,可以按照以下步骤操作: 1. 安装 ActiveMQ:在每个节点上安装 ActiveMQ,并确保其版本相同。 2. 配置 ActiveMQ:在每个节点上修改 ActiveMQ 的配置文件,以便将节点加入到集群中。其中需要修改的配置项包括: - `activemq.xml`:在 `<broker>` 元素中添加 `<networkConnectors>` 子元素,指定节点之间的网络连接。例如: ```xml <networkConnectors> <networkConnector uri="static:(tcp://node1:61616,tcp://node2:61616)"/> </networkConnectors> ``` - `activemq.xml`:在 `<broker>` 元素中添加 `<destinationPolicy>` 子元素,指定消息目的地的复制策略。例如: ```xml <destinationPolicy> <policyMap> <policyEntries> <policyEntry topic=">" replicated="true"/> <policyEntry queue=">" replicated="true"/> </policyEntries> </policyMap> </destinationPolicy> ``` - `activemq.xml`:在 `<broker>` 元素中添加 `<clusterConnectors>` 子元素,指定节点之间的集群连接。例如: ```xml <clusterConnectors> <clusterConnector name="myCluster" uri="static:(tcp://node1:61616,tcp://node2:61616)"/> </clusterConnectors> ``` 3. 启动 ActiveMQ:在每个节点上启动 ActiveMQ。 4. 检查集群状态:使用 ActiveMQ 自带的 web 界面或者 JMX 工具来检查集群状态。在 web 界面中,可以查看 `Broker` 页签下的 `Network Connections` 和 `Destinations`,以确保节点之间的连接正常并且消息目的地被正确地复制。 5. 测试集群:使用消息生产者和消费者来测试集群。在测试时,可以分别向不同的节点发送消息,并在不同的节点上接收消息,以确保消息能够正确地被传递和复制。 以上就是 ActiveMQ 集群部署的基本步骤,需要注意的是,在实际部署过程中可能会遇到各种问题,需要根据具体情况进行调整和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值