分布式Hibernate search

分布式HibernateSearch支持多个节点Read/Write。在网上找到一篇完整介绍的文章,我粗陋的翻译了一下。原文连接http://aredko.blogspot.com/2009/12/distributed-hibernate-search-with.html(需要fanqiang)

分布式HibernateSearch与ApacheTomcat6,ActiveMQ和Spring.今天我将跟大家分享我的经验,以master/slave(s)方式配置分布式HibernateSearch并整合ApacheActiveMQ,Spring,应用程序额容器是ApacheTomcat6。

怎么工作:

-HibernateSearch支持使用JMSback-end和master/slave(s)索引进行分布式配置

-mater通过网络共享暴露索引(例如通过NFS。。。)

-slave(s)复制mater的索引到本地

版本

-ApacheTomcat6.0.20

-HibernateSearch3.1.1GA

-ApacheActiveMQ5.3.0

-Spring2.5.6

-XBean-Spring3.6

${local.index.dir}-directorytostoremasterindex

${master.index.dir}-directorytocopymasterindexto,it'ssharednetworklocationforreplicationwithslave(s)

Mater索引的配置

Mater的配置稍显复杂。以下是配置属性说明

$(local.index.dir)-存储mater索引的目录

$(master.index.dir)-拷贝mater索引的目标目录,通过网络定位供slave(s)复制。

首先,为了简单起见,我们将ActiveMQ中介运行在同一台服务器上。因此,我们在WEB-INF/activemq.xml文件中使用简单的嵌入式中介配置。

<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:amq="http://activemq.apache.org/schema/core"
 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-2.0.xsd
 http://activemq.apache.org/schema/core
 http://activemq.apache.org/schema/core/activemq-core.xsd">

 <amq:broker brokerName="HibernateSearchBroker">
     <amq:managementContext>
         <amq:managementContext createConnector="false"/>
     </amq:managementContext>

     <amq:transportConnectors>
         <amq:transportConnector uri="tcp://localhost:61616" /> 
     </amq:transportConnectors>
  </amq:broker>

  <amq:queue name="queue/hibernatesearch" physicalName="hibernateSearchQueue" />
</beans>

其次,需要在webapplicationMEAT-INF/context.xml(Tocmat)中配置配置JNDI资源(JMSConnectionFactory和Quene)

<!-- ActiveMQ ConnectionFactory -->
<Resource
name="jms/ConnectionFactory"
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://0.0.0.0:61616?trace=true"
brokerName="HibernateSearchBroker" />

<!-- ActiveMQ HibernateSearch queue -->
<Resource
name="queue/hibernatesearch"
auth="Container" type="org.apache.activemq.command.ActiveMQQueue"
description="Hibernate search queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="hibernateSearchQueue" />

接下来在Hibernate.cfg.xml文件中配置HibernateSearch

<property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSMasterDirectoryProvider</property>
<property name="hibernate.search.default.indexBase">${local.index.dir}</property>
<property name="hibernate.search.default.sourceBase">${master.index.dir}</property>
<property name="hibernate.search.default.refresh">60</property>

Master和slave之间代码的一个重要区别是:master中必须包含实现AbstractJMSHibernateSearchController的子类作为监听器,例如

import javax.jms.MessageListener;

import org.hibernate.Session;
import org.hibernate.search.backend.impl.jms.AbstractJMSHibernateSearchController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JMSHibernateSearchController
 extends AbstractJMSHibernateSearchController
     implements MessageListener {

 @Override
 protected void cleanSessionIfNeeded(Session session) {
    // clean session here ...     
 }

 @Override
 protected Session getSession() {
    // return new session here ...     
 }
}

最后在Spring的配置文件applicationContext.xml文件中,加入以下配置

<bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
 <property name="config" value="WEB-INF/activemq.xml" />
 <property name="start" value="true" />
</bean>  

<bean name="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
 <property name="jndiName" value="java:comp/env/jms/ConnectionFactory" />
</bean>

<bean name="jmsHibernateSearchQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
 <property name="jndiName" value="java:comp/env/queue/hibernatesearch" />

<bean id="hibernateSearchController" class="<your implementation of AbstractJMSHibernateSearchController>" />

<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer" depends-on="broker">
 <property name="connectionFactory" ref="jmsConnectionFactory"/>
 <property name="destination" ref="jmsHibernateSearchQueue"/>
 <property name="messageListener" ref="hibernateSearchController" />
</bean>

WiththoseconfigurationsinplaceHibernateSearchmasterisreadytorun.

正确配置好之后,HibernateSearchmaster就可以运行了。

Slave索引配置

Slave(s)的配置要简单得多,一下是配置属性说明

${server}-运行ActiveMQ中介服务器

${lcoal.index.dir}-存储本地索引的目录(master的拷贝)

${mater.index.share}-挂载mater索引的网络共享目录.

首先,我们需要在网站应用MEAT-INF/context.xml(Tocmat)中配置配置JNDI资源(JMSConnectionFactory和Quene)

...
<!-- ActiveMQ ConnectionFactory -->
<Resource
name="jms/ConnectionFactory"
auth="Container"
type="org.apache.activemq.ActiveMQConnectionFactory"
description="JMS Connection Factory"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
brokerURL="tcp://${server}:61616?trace=true"
brokerName="HibernateSearchBroker" />

<!-- ActiveMQ HibernateSearch queue -->
<Resource
name="queue/hibernatesearch"
auth="Container" type="org.apache.activemq.command.ActiveMQQueue"
description="Hibernate search queue"
factory="org.apache.activemq.jndi.JNDIReferenceFactory"
physicalName="hibernateSearchQueue" />
...

接下来我们需要在hibernate.cfg.xml中配置HibernateSearch

<property name="hibernate.search.default.directory_provider">org.hibernate.search.store.FSSlaveDirectoryProvider</property>
<property name="hibernate.search.default.indexBase">${local.index.dir}</property>
<property name="hibernate.search.default.sourceBase">${master.index.share}</property>
<property name="hibernate.search.default.refresh">60</property>
<property name="hibernate.search.worker.backend">jms</property>
<property name="hibernate.search.worker.jms.connection_factory">java:comp/env/jms/ConnectionFactory</property>
<property name="hibernate.search.worker.jms.queue">java:comp/env/queue/hibernatesearch</property>
<property name="hibernate.search.worker.jndi.java.naming.factory.initial">org.apache.activemq.jndi.ActiveMQInitialContextFactory</property>

配置到此结束

说些关于JUnit测试的题外话.唯一的问题是需要使用SpringJNDItemplates模拟JNDI,例如

<bean name="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jms/ConnectionFactory" />
    <property name="jndiTemplate">
        <bean class="org.springframework.mock.jndi.ExpectedLookupTemplate">
            <constructor-arg index="0" value="java:comp/env/jms/ConnectionFactory" />
            <constructor-arg index="1">
                <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                    <property name="brokerURL">
                        <value>tcp://0.0.0.0:61616</value>
                    </property>
                </bean>
            </constructor-arg>
        </bean>
    </property>
</bean>

<bean name="jmsHibernateSearchQueue" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/queue/hibernatesearch" />
    <property name="jndiTemplate">
       <bean class="org.springframework.mock.jndi.ExpectedLookupTemplate">
           <constructor-arg index="0" value="java:comp/env/queue/hibernatesearch" />
           <constructor-arg index="1">
               <bean id="jmsHibernateSearchQueue"   class="org.apache.activemq.command.ActiveMQQueue">
                   <constructor-arg value="queue/hibernateSearchQueue"/>
               </bean>               
           </constructor-arg>
       </bean>
    </property>
</bean>

相关推荐