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

3

我有一个使用Spring Core 3.1.2配置的项目,集成了Hibernate Core 4.2.8,现在想要将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>

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

问题是: 我必须做哪些其他配置才能使缓存在实例之间分布。不同的机器将如何“知道彼此”? 还有,有没有一种方法可以创建一个测试场景,以检查缓存确实分布在几台机器之间?(例如:启动2个JVM)如果有示例,那就太好了。

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

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

我的Hazelcast版本是3.5.4

谢谢!

1个回答

3
你不需要再进行任何配置来形成集群。 只需启动应用程序的另一个实例,两个hazelcast实例就会相互发现并形成集群。 默认情况下,hazelcast成员使用组播来相互发现,当然你可以通过将自定义的hazelcast.xml添加到项目中来更改这种行为。
这是Spring-Hibernate-Hazelcast集成的详细示例。

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

applicationContext-hazelcast.xml是需要修改的文件,如果您想玩转Hazelcast配置。 例如,在这个示例项目中,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

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接