Spring Boot + Hazelcast + Hibernate 5 L2缓存 [英] Spring Boot + Hazelcast + Hibernate 5 L2 Cache

查看:95
本文介绍了Spring Boot + Hazelcast + Hibernate 5 L2缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个休眠(5.0.11.Final)Spring Boot(1.4.1-RELEASE)应用程序,该应用程序使用Hazelcast(3.7.1)作为L2缓存实现.

I have a hibernate (5.0.11.Final) Spring Boot (1.4.1-RELEASE) application that uses Hazelcast (3.7.1) as the L2 cache implementation.

我想说明一下,使用休眠的l2配置,不需要包含我自己的hazelcast.xml文件.

I wanted to clarify that with the hibernate l2 configuration, I do NOT need to include my own hazelcast.xml file.

我之所以问是因为,当我启动该应用程序的实例时,当我同时拥有两个(休眠的l2配置和src/main/resource/hibernate.xml文件)时,我看到创建了2个成员.当我启动另一个实例时,我看到4.我的印象是,每个应用程序实例我只能看到1个成员.

I am asking because when I do have both (hibernate l2 configuration AND a src/main/resource/hibernate.xml file) when I start an instance of this application, I see 2 members created. When I start another instance, I see 4. I was under the impression that I should only see 1 member per application instance.

我正在关注相关线程用户正在经历类似的情况.我首先尝试通过该线程在hibernate配置中以编程方式命名我的hazelcast实例,但这没什么区别.

I was following a related thread where the user was experiencing something similar. I first tried naming my hazelcast instance in the hibernate configuration (programmatically) per this thread, but that made no difference.

当我从类路径&中删除hazelcast.xml时启动一个实例,我只看到1个成员;我想要的是.启动第二名,启动第二名成员;我想要的.

When I remove my hazelcast.xml from the classpath & start an instance, I only see 1 member; what I want. Starting a 2nd, starts a 2nd member; what I want.

这是使用hazelcast(仅通过hibernate配置)实现hibernate l2缓存的正确方法吗?

Is this the correct way of implementing the hibernate l2 cache using hazelcast (ONLY via the hibernate configuration)?

如果是这样,当我想使用通过hazelcast.xml文件提供的细粒度hazelcast配置时会发生什么?

If so, what happens when I want to use fine-grained hazelcast configuration provided via a hazelcast.xml file?

推荐答案

如果Spring Boot 1.4.1找到一个hazelcast.xml文件,但没有找到Config bean,它将从中创建一个Hazelcast实例.

If Spring Boot 1.4.1 finds a hazelcast.xml file and no Config bean, it will create a Hazelcast instance from it.

Hazelcast Hibernate模块使用hibernate.cache.hazelcast.instance_name属性来确定是要新的Hazelcast实例还是现有的实例.

The Hazelcast Hibernate module uses the hibernate.cache.hazelcast.instance_name property to determine whether you want a new Hazelcast instance or an existing one.

如果省略hibernate.cache.hazelcast.instance_name,则会得到一个新的Hazelcast实例(也基于hazelcast.xml),这就是为什么在一个JVM中有两个实例的原因.

If you omit hibernate.cache.hazelcast.instance_name you get a new Hazelcast instance (also based on hazelcast.xml) and this is why you have two instances in the one JVM.

有多种解决方法.您可以阻止Spring Boot自动创建实例,也可以阻止Hazelcast Hibernate自动创建实例.

There are a number of ways to address. You can stop Spring Boot from autocreating an instance, and you can stop Hazelcast Hibernate from autocreating an instance.

如果您使用的是Spring Boot,我的建议是使用也是Spring Bean的Hazelcast实例.这样,bean将由Spring生命周期管理,您将获得优雅的关闭控制.

If you are using Spring Boot, my recommendation would be to use a Hazelcast instance that is also a Spring bean. That way the bean will be managed by the Spring lifecycle, and you get elegant shutdown control.

Spring Boot为您创建的实例是最简单,最明显的方法.要使Hazelcast Hibernate模块也使用它,只需在创建JPA属性时传入实例名称即可,例如: properties.setProperty("hibernate.cache.hazelcast.instance_name", hazelcastInstance.getName());

The instance created by Spring Boot for you is the easiest and most obvious way to do so. To get the Hazelcast Hibernate module to use this also, just pass in the instance name when creating your JPA properties with something like: properties.setProperty("hibernate.cache.hazelcast.instance_name", hazelcastInstance.getName());

这篇关于Spring Boot + Hazelcast + Hibernate 5 L2缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆