使用Hazelcast作为二级缓存的Spring + Hibernate

时间:2016-01-13 15:23:16

标签: java spring hibernate caching hazelcast

我有一个配置了Hibernate(hibernate核心4.2.8)的spring项目(spring core 3.1.2),我想将Hazelcast设置为二级缓存。我希望缓存以P2P嵌入式集群模式分发(每个应用程序实例在同一台机器上运行hazelcast实例)。

这是我当前的sessionFactory配置。

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

这个配置似乎可以在我的本地机器上运行,因为我运行了一个小测试来检查二级缓存命中。

问题是: 我必须进行哪些其他配置才能在实例之间分配缓存。不同的机器如何“相互了解”? 另外,有没有办法创建一个测试场景,检查缓存是否确实分布在几台机器中?(例如:launch 2 jvms)一个例子将不胜感激。

欢迎提供有关此配置的任何其他提示或警告。

免责声明:这是我第一次使用Hazelcast。

我的Hazelcast版本是3.5.4

谢谢!

1 个答案:

答案 0 :(得分:3)

您无需再进行配置即可形成群集。 只需启动应用程序的另一个实例,两个hazelcast实例应该相互看到并形成一个集群。 默认情况下,hazelcast成员使用多播来互相查找,当然您可以通过向项目添加自定义hazelcast.xml来更改此行为。

以下是Spring-Hibernate-Hazelcast集成的详细示例。

https://github.com/hazelcast/hazelcast-code-samples/tree/master/hazelcast-integration/spring-hibernate-2ndlevel-cache

如果您想使用Hazelcast配置,

applicationContext-hazelcast.xml是要修改的文件。 例如,在此示例项目中,port-auto-increment设置为false 这意味着如果指定的端口已被占用,Hazelcast将无法启动。 (默认为5701)

只需将此属性设置为true并启动另一个Application实例,您应该看到缓存已分发。

请不要忘记在开始第一个实例之前注释Application类的最后一行以保持进程活着

启动第一个实例,如下所示;

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

}
相关问题