分布式Hibernate search详解

作者:liuyazhuang 时间:2023-08-14 18:53:17 

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

怎么工作:

-Hibernate Search 支持使用JMS back-end 和 master/slave(s) 索引进行分布式配置
- mater通过网络共享暴露索引 (例如通过NFS。。。)
- slave(s) 复制mater的索引到本地

版本

- Apache Tomcat 6.0.20
- Hibernate Search 3.1.1 GA
- Apache ActiveMQ 5.3.0
- Spring 2.5.6
- XBean-Spring 3.6
${local.index.dir} - directory to store master index
${master.index.dir} - directory to copy master index to, it's shared network location for replication with slave(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>

其次,需要在web application MEAT-INF/context.xml(Tocmat)中配置配置JNDI资源(JMS Connection Factory 和 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文件中配置Hibernate Searc


<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>

With those configurations in place Hibernate Search master is ready to run.
正确配置好之后,Hibernate Search master就可以运行了。

Slave 索引配置

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

${server} - 运行ActiveMQ中介服务器
${lcoal.index.dir} - 存储本地索引的目录(master的拷贝)
${mater.index.share}-挂载mater索引的网络共享目录.

首先,我们需要在网站应用 MEAT-INF/context.xml(Tocmat)中配置配置JNDI资源(JMS Connection Factory 和 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中配置Hibernate Search


<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测试的题外话.唯一的问题是需要使用Spring JNDI templates模拟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>

来源:http://blog.csdn.net/l1028386804/article/details/48830331

标签:hibernate,search
0
投稿

猜你喜欢

  • 使用Docker部署SpringBoot项目的实现方法

    2022-12-05 02:44:35
  • Google:10个值得玩味的段子

    2008-01-24 12:54:00
  • 美工属过渡行业 职业发展的N种路线

    2008-01-17 10:48:00
  • 域名的概念与机制(4)

    2010-02-05 19:24:00
  • 让网站飞:不用插件直接启动WordPress的Gzip网页压缩

    2011-09-27 19:18:48
  • Google服务过程中的秘密

    2007-12-25 12:19:00
  • Win10 + Ubuntu 16.04双系统完美安装教程【详细】

    2023-11-05 09:26:38
  • Vscode远程连接Ubuntu出错问题的解决方法

    2023-11-03 15:53:41
  • Linux操作系统上安装MySQL数据库的方法

    2009-09-20 20:10:00
  • 防止黑客入侵最高招:关闭系统端口

    2009-05-28 08:28:00
  • 详解在Linux下搭建Git服务器

    2023-07-18 19:47:51
  • SEO实战之图片优化篇

    2008-10-21 19:16:00
  • 博客网站优化的60个技巧

    2009-02-04 10:57:00
  • 动易CMS产品SiteFactory 3.6正式发布

    2010-09-29 21:31:00
  • 百度越来越像站内搜索引擎了

    2009-02-12 12:34:00
  • linux查看文件系统块大小与内存页大小的简单方法

    2023-10-09 18:02:51
  • Google 搜索联盟介绍

    2007-11-13 12:35:00
  • rancher下的kubernetes之构建标准化vmware镜像的方法步骤

    2022-02-24 11:33:18
  • 如何做一个赚钱的淘宝客

    2010-05-10 10:44:00
  • CentOS 7安装完成后初始化的方法

    2021-04-11 01:53:51
  • asp之家 网站运营 m.aspxhome.com