使用MySQL进行ActiveMQ的消息持久化,并设置主从模式

1、MySQL数据库的binlog日志模式要设置为 mixed 模式

2、设置使用数据库锁 useDatabaseLock="true" ,如果不设置,在主从模式下会报主键重复的异常。

 
  
  1. <persistenceAdapter> 
  2.    <jdbcPersistenceAdapter dataDirectory="${activemq.data}" dataSource="#mysql-ds" useDatabaseLock="true"  
  3. lockKeepAlivePeriod="10000"/> 
  4. </persistenceAdapter> 

3、使用数据库锁的时候,master启动后,slave由于数据库锁的原因,启动时处于等待状态,这时slave无法提供服务,当master当机后,数据库锁释放,slave获得数据库锁,可以提供服务。

4、虽然设置的主从模式,但当master当机时,接收者会有数据丢失问题,而且问题比较严重。考虑可能是由于没有 Journal 的原因,但不知道为什么ActiveMQ 5.0以后版本不支持 journaledJDBC ,也可能是没有找到正确的设置方法。(原因是在消息发送端设置了异步发送,导致发送和接收的消息数不一致,修改为同步发送后,消息不丢失,发送和接收的消息数一致

ActiveMQ使用MySQL进行持久化,并配置Master-Slave模式配置样例:

Master:

 
  
  1. <beans 
  2.   xmlns="http://www.springframework.org/schema/beans" 
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
  5.   http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> 
  6.  
  7.   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
  8.       <property name="locations"> 
  9.           <value>file:${activemq.conf}/credentials.properties</value> 
  10.       </property> 
  11.   </bean> 
  12.  
  13.   <broker brokerName="broker-1-1" persistent="true" xmlns="http://activemq.apache.org/schema/core"> 
  14.  
  15.     <transportConnectors> 
  16.        <transportConnector name="default" uri="tcp://0.0.0.0:61616"/> 
  17.     </transportConnectors> 
  18.  
  19.     <persistenceAdapter> 
  20.        <jdbcPersistenceAdapter dataDirectory="${activemq.data}" dataSource="#mysql-ds" useDatabaseLock="true"  
  21.        lockKeepAlivePeriod="10000"/> 
  22.     </persistenceAdapter> 
  23.      
  24.     <systemUsage> 
  25.         <systemUsage sendFailIfNoSpaceAfterTimeout="3000"> 
  26.             <memoryUsage> 
  27.                 <memoryUsage limit="20 mb"/> 
  28.             </memoryUsage> 
  29.             <storeUsage> 
  30.                 <storeUsage limit="100 mb"/> 
  31.             </storeUsage> 
  32.             <tempUsage> 
  33.                 <tempUsage limit="50 mb"/> 
  34.             </tempUsage> 
  35.         </systemUsage> 
  36.     </systemUsage> 
  37.  
  38.     <shutdownHooks> 
  39.         <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" /> 
  40.     </shutdownHooks> 
  41.      
  42.   </broker> 
  43.  
  44.   <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
  45.     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
  46.     <property name="url" value="jdbc:mysql://127.0.0.1:3306/activemq?relaxAutoCommit=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/> 
  47.     <property name="username" value="root"/> 
  48.     <property name="password" value="neusoft"/> 
  49.     <property name="maxActive" value="200"/> 
  50.     <property name="poolPreparedStatements" value="true"/> 
  51.   </bean> 
  52.  
  53.   <import resource="jetty.xml"/> 
  54. </beans> 

 

Slave:

 
  
  1. <beans 
  2.   xmlns="http://www.springframework.org/schema/beans" 
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  4.   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
  5.   http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> 
  6.  
  7.   <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
  8.       <property name="locations"> 
  9.           <value>file:${activemq.conf}/credentials.properties</value> 
  10.       </property> 
  11.   </bean> 
  12.  
  13.   <broker brokerName="broker-1-2" persistent="true" 
  14.   masterConnectorURI="tcp://127.0.0.1:61616" shutdownOnMasterFailure="false" xmlns="http://activemq.apache.org/schema/core"> 
  15.     <transportConnectors> 
  16.        <transportConnector name="default" uri="tcp://0.0.0.0:61617"/> 
  17.     </transportConnectors> 
  18.  
  19.     <persistenceAdapter> 
  20.        <jdbcPersistenceAdapter dataDirectory="${activemq.data}" dataSource="#mysql-ds" useDatabaseLock="true"  
  21.        lockKeepAlivePeriod="10000"/> 
  22.     </persistenceAdapter> 
  23.      
  24.     <systemUsage> 
  25.         <systemUsage sendFailIfNoSpaceAfterTimeout="3000"> 
  26.             <memoryUsage> 
  27.                 <memoryUsage limit="20 mb"/> 
  28.             </memoryUsage> 
  29.             <storeUsage> 
  30.                 <storeUsage limit="100 mb"/> 
  31.             </storeUsage> 
  32.             <tempUsage> 
  33.                 <tempUsage limit="50 mb"/> 
  34.             </tempUsage> 
  35.         </systemUsage> 
  36.     </systemUsage> 
  37.  
  38.     <shutdownHooks> 
  39.         <bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" /> 
  40.     </shutdownHooks> 
  41.      
  42.   </broker> 
  43.  
  44.   <bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
  45.     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
  46.     <property name="url" value="jdbc:mysql://127.0.0.1:3306/activemq?relaxAutoCommit=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/> 
  47.     <property name="username" value="root"/> 
  48.     <property name="password" value="neusoft"/> 
  49.     <property name="maxActive" value="200"/> 
  50.     <property name="poolPreparedStatements" value="true"/> 
  51.   </bean> 
  52.  
  53.   <import resource="jetty.xml"/> 
  54.  
  55. </beans>