Java 弹簧&x2B;以Hazelcast作为二级缓存休眠
我有一个配置了Hibernate(Hibernate core 4.2.8)的spring项目(spring core 3.1.2),我想将Hazelcast设置为二级缓存。我希望缓存以P2P嵌入式集群模式分布(每个应用程序实例在同一台机器上运行一个hazelcast实例) 这是我当前的sessionFactory配置Java 弹簧&x2B;以Hazelcast作为二级缓存休眠,java,spring,hibernate,caching,hazelcast,Java,Spring,Hibernate,Caching,Hazelcast,我有一个配置了Hibernate(Hibernate core 4.2.8)的spring项目(spring core 3.1.2),我想将Hazelcast设置为二级缓存。我希望缓存以P2P嵌入式集群模式分布(每个应用程序实例在同一台机器上运行一个hazelcast实例) 这是我当前的sessionFactory配置 <bean id="sessionFactory" class="org.springframework.orm.hibernate4.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.myProject.beans" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.database">ORACLE</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<!--enable batch operations-->
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.order_inserts">true</prop>
<prop key="hibernate.order_updates">true</prop>
<!-- 2nd level cache configuration-->
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">com.hazelcast.hibernate.HazelcastLocalCacheRegionFactory</prop>
<prop key="hibernate.cache.use_query_cache">false</prop>
</props>
</property>
</bean>
神谕
假的
org.hibernate.dialen.oracle10galent
20
真的
真的
真的
com.hazelcast.hibernate.HazelcastLocalCacheRegionFactory
假的
当我运行一个检查二级缓存命中的小测试时,这个配置似乎在我的本地机器上工作
问题是:
为了在实例之间分布缓存,我必须进行哪些其他配置。不同的机器如何“相互了解”?
另外,是否有一种方法可以创建一个测试场景来检查缓存是否确实分布在两台机器之间?(例如:启动2个JVM)非常感谢举个例子
欢迎提供有关此配置的任何其他提示或警告
免责声明:这是我第一次使用Hazelcast
我的Hazelcast版本是3.5.4
谢谢大家! 您不需要再进行配置来形成集群。 只需启动应用程序的另一个实例,两个hazelcast实例就会看到对方并形成一个集群。 默认情况下,hazelcast成员使用多播相互查找,当然,您可以通过向项目中添加自定义的
hazelcast.xml
来更改此行为
下面是Spring Hibernate Hazelcast集成的详细示例
applicationContext hazelcast.xml
是要修改的文件,如果您想使用hazelcast配置。
例如,在此示例项目中,port auto increment
设置为false
这意味着如果指定的端口已被占用,Hazelcast将不会启动。(默认为5701)
只要将此属性设置为true
并启动另一个应用程序
实例,您就会看到缓存是分布式的
在启动第一个实例之前,请不要忘记注释掉应用程序
类的最后一行,以使进程保持活动状态
如下图所示启动第一个实例
public static void main(String[] args) {
InitializeDB.start();
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DistributedMapDemonstrator distributedMapDemonstrator = context.getBean(DistributedMapDemonstrator.class);
distributedMapDemonstrator.demonstrate();
//Hazelcast.shutdownAll(); Keep instances alive to see form a cluster
}
public static void main(String[] args) {
//InitializeDB.start(); DB will be initialized already by the first instance
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DistributedMapDemonstrator distributedMapDemonstrator = context.getBean(DistributedMapDemonstrator.class);
distributedMapDemonstrator.demonstrate();
//Hazelcast.shutdownAll(); Keep instances alive to see form a cluster
}
第二个,如下所示
public static void main(String[] args) {
InitializeDB.start();
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DistributedMapDemonstrator distributedMapDemonstrator = context.getBean(DistributedMapDemonstrator.class);
distributedMapDemonstrator.demonstrate();
//Hazelcast.shutdownAll(); Keep instances alive to see form a cluster
}
public static void main(String[] args) {
//InitializeDB.start(); DB will be initialized already by the first instance
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DistributedMapDemonstrator distributedMapDemonstrator = context.getBean(DistributedMapDemonstrator.class);
distributedMapDemonstrator.demonstrate();
//Hazelcast.shutdownAll(); Keep instances alive to see form a cluster
}