使用MySQL进行ActiveMQ的消息持久化,并设置主从模式
1、MySQL数据库的binlog日志模式要设置为 mixed 模式
2、设置使用数据库锁 useDatabaseLock="true" ,如果不设置,在主从模式下会报主键重复的异常。
- <persistenceAdapter>
- <jdbcPersistenceAdapter dataDirectory="${activemq.data}" dataSource="#mysql-ds" useDatabaseLock="true"
- lockKeepAlivePeriod="10000"/>
- </persistenceAdapter>
3、使用数据库锁的时候,master启动后,slave由于数据库锁的原因,启动时处于等待状态,这时slave无法提供服务,当master当机后,数据库锁释放,slave获得数据库锁,可以提供服务。
4、虽然设置的主从模式,但当master当机时,接收者会有数据丢失问题,而且问题比较严重。考虑可能是由于没有 Journal 的原因,但不知道为什么ActiveMQ 5.0以后版本不支持 journaledJDBC ,也可能是没有找到正确的设置方法。(原因是在消息发送端设置了异步发送,导致发送和接收的消息数不一致,修改为同步发送后,消息不丢失,发送和接收的消息数一致)
ActiveMQ使用MySQL进行持久化,并配置Master-Slave模式配置样例:
Master:
- <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>
- <broker brokerName="broker-1-1" persistent="true" xmlns="http://activemq.apache.org/schema/core">
- <transportConnectors>
- <transportConnector name="default" uri="tcp://0.0.0.0:61616"/>
- </transportConnectors>
- <persistenceAdapter>
- <jdbcPersistenceAdapter dataDirectory="${activemq.data}" dataSource="#mysql-ds" useDatabaseLock="true"
- lockKeepAlivePeriod="10000"/>
- </persistenceAdapter>
- <systemUsage>
- <systemUsage sendFailIfNoSpaceAfterTimeout="3000">
- <memoryUsage>
- <memoryUsage limit="20 mb"/>
- </memoryUsage>
- <storeUsage>
- <storeUsage limit="100 mb"/>
- </storeUsage>
- <tempUsage>
- <tempUsage limit="50 mb"/>
- </tempUsage>
- </systemUsage>
- </systemUsage>
- <shutdownHooks>
- <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
- </shutdownHooks>
- </broker>
- <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://127.0.0.1:3306/activemq?relaxAutoCommit=true&useUnicode=true&characterEncoding=UTF-8"/>
- <property name="username" value="root"/>
- <property name="password" value="neusoft"/>
- <property name="maxActive" value="200"/>
- <property name="poolPreparedStatements" value="true"/>
- </bean>
- <import resource="jetty.xml"/>
- </beans>
Slave:
- <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>
- <broker brokerName="broker-1-2" persistent="true"
- masterConnectorURI="tcp://127.0.0.1:61616" shutdownOnMasterFailure="false" xmlns="http://activemq.apache.org/schema/core">
- <transportConnectors>
- <transportConnector name="default" uri="tcp://0.0.0.0:61617"/>
- </transportConnectors>
- <persistenceAdapter>
- <jdbcPersistenceAdapter dataDirectory="${activemq.data}" dataSource="#mysql-ds" useDatabaseLock="true"
- lockKeepAlivePeriod="10000"/>
- </persistenceAdapter>
- <systemUsage>
- <systemUsage sendFailIfNoSpaceAfterTimeout="3000">
- <memoryUsage>
- <memoryUsage limit="20 mb"/>
- </memoryUsage>
- <storeUsage>
- <storeUsage limit="100 mb"/>
- </storeUsage>
- <tempUsage>
- <tempUsage limit="50 mb"/>
- </tempUsage>
- </systemUsage>
- </systemUsage>
- <shutdownHooks>
- <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
- </shutdownHooks>
- </broker>
- <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="url" value="jdbc:mysql://127.0.0.1:3306/activemq?relaxAutoCommit=true&useUnicode=true&characterEncoding=UTF-8"/>
- <property name="username" value="root"/>
- <property name="password" value="neusoft"/>
- <property name="maxActive" value="200"/>
- <property name="poolPreparedStatements" value="true"/>
- </bean>
- <import resource="jetty.xml"/>
- </beans>
转载于:https://blog.51cto.com/nettm/1159523