【问题标题】:Spring + Hibernate with Hazelcast as 2nd level cacheSpring + Hibernate 与 Hazelcast 作为二级缓存
【发布时间】:2016-04-18 15:34:32
【问题描述】:

我有一个配置了 Hibernate(hibernate core 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>

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

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

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

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

我的 Hazelcast 版本是 3.5.4

谢谢!

【问题讨论】:

    标签: java spring hibernate caching hazelcast


    【解决方案1】:

    您无需再进行配置即可组成集群。 只需启动您的应用程序的另一个实例,两个 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
    
    }
    

    【讨论】:

      猜你喜欢
      • 2021-10-07
      • 2016-03-24
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 2015-04-11
      • 1970-01-01
      相关资源
      最近更新 更多