我在嵌入式/P2P模式下的Spring Boot应用程序中使用Hazelcast作为Hibernate 2LC提供程序。启动应用程序后,我可以看到为我的 Hazelcast 集群创建了两个成员。当我启动整个应用程序的第二个实例时,将创建另外两个成员。
目前,我将以下配置与HazelcastCacheRegionFactory
(非本地(一起使用。
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.jpa.properties.hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory
spring.jpa.properties.hibernate.generate_statistics=true
在Hazelcast管理中心,看起来一个成员是另一个成员的双倍。但是,当启动我的 Spring Boot 应用程序的第二个实例时,这两个成员仍然存在,并且它们总共增加到四个。
我已经尝试在我的hazelcast.xml
中将备份计数设置为 0.
启动应用程序的第一个实例时,我收到以下日志:
2019-10-09 12:04:51.055 WARN 21088 --- [ restartedMain] c.h.instance.HazelcastInstanceFactory : Hazelcast is starting in a Java modular environment (Java 9 and newer) but without proper access to required Java packages. Use additional Java arguments to provide Hazelcast access to Java internal API. The internal API access is used to get the best performance results. Arguments to be used:
--add-modules java.se --add-exports java.base/jdk.internal.ref=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.nio=ALL-UNNAMED --add-opens java.base/sun.nio.ch=ALL-UNNAMED --add-opens java.management/sun.management=ALL-UNNAMED --add-opens jdk.management/com.sun.management.internal=ALL-UNNAMED
2019-10-09 12:04:51.058 INFO 21088 --- [ restartedMain] c.h.config.AbstractConfigLocator : Loading 'hazelcast.xml' from the classpath.
2019-10-09 12:04:51.238 WARN 21088 --- [ restartedMain] c.h.config.AbstractXmlConfigHelper : Name of the hazelcast schema location is incorrect, using default
2019-10-09 12:04:51.717 INFO 21088 --- [ restartedMain] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.12.2] Prefer IPv4 stack is true, prefer IPv6 addresses is false
2019-10-09 12:04:53.004 INFO 21088 --- [ restartedMain] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.12.2] Picked [10.29.47.243]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
2019-10-09 12:04:53.016 INFO 21088 --- [ restartedMain] com.hazelcast.system : [10.29.47.243]:5701 [dev] [3.12.2] Hazelcast 3.12.2 (20190802 - e34b163) starting at [10.29.47.243]:5701
2019-10-09 12:04:53.016 INFO 21088 --- [ restartedMain] com.hazelcast.system : [10.29.47.243]:5701 [dev] [3.12.2] Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
2019-10-09 12:04:53.240 INFO 21088 --- [ restartedMain] c.h.s.i.o.impl.BackpressureRegulator : [10.29.47.243]:5701 [dev] [3.12.2] Backpressure is disabled
2019-10-09 12:04:55.174 INFO 21088 --- [ restartedMain] com.hazelcast.instance.Node : [10.29.47.243]:5701 [dev] [3.12.2] Creating MulticastJoiner
2019-10-09 12:04:55.313 INFO 21088 --- [ restartedMain] c.h.s.i.o.impl.OperationExecutorImpl : [10.29.47.243]:5701 [dev] [3.12.2] Starting 4 partition threads and 3 generic threads (1 dedicated for priority tasks)
2019-10-09 12:04:55.314 INFO 21088 --- [ restartedMain] c.h.internal.diagnostics.Diagnostics : [10.29.47.243]:5701 [dev] [3.12.2] Diagnostics disabled. To enable add -Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2019-10-09 12:04:55.328 INFO 21088 --- [ restartedMain] com.hazelcast.core.LifecycleService : [10.29.47.243]:5701 [dev] [3.12.2] [10.29.47.243]:5701 is STARTING
2019-10-09 12:04:57.466 INFO 21088 --- [ restartedMain] c.h.internal.cluster.ClusterService : [10.29.47.243]:5701 [dev] [3.12.2]
Members {size:1, ver:1} [
Member [10.29.47.243]:5701 - bf933bd9-9214-4022-a104-514bc5f56267 this
]
2019-10-09 12:04:57.486 INFO 21088 --- [ restartedMain] c.h.i.m.ManagementCenterService : [10.29.47.243]:5701 [dev] [3.12.2] Hazelcast will connect to Hazelcast Management Center on address:
http://localhost:8080/hazelcast-mancenter
2019-10-09 12:04:57.495 INFO 21088 --- [ restartedMain] com.hazelcast.core.LifecycleService : [10.29.47.243]:5701 [dev] [3.12.2] [10.29.47.243]:5701 is STARTED
2019-10-09 12:04:57.544 INFO 21088 --- [MC.State.Sender] com.hazelcast.client.impl.ClientEngine : [10.29.47.243]:5701 [dev] [3.12.2] Applying a new client selector :ClientSelector{any}
2019-10-09 12:04:57.651 INFO 21088 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2019-10-09 12:04:58.089 INFO 21088 --- [ restartedMain] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2019-10-09 12:04:58.163 INFO 21088 --- [ restartedMain] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2019-10-09 12:04:58.274 INFO 21088 --- [ restartedMain] org.hibernate.Version : HHH000412: Hibernate Core {5.3.10.Final}
2019-10-09 12:04:58.276 INFO 21088 --- [ restartedMain] org.hibernate.cfg.Environment : HHH000206: hibernate.properties not found
2019-10-09 12:04:58.533 INFO 21088 --- [ restartedMain] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
2019-10-09 12:04:58.777 INFO 21088 --- [ restartedMain] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL8Dialect
2019-10-09 12:04:59.122 INFO 21088 --- [ restartedMain] c.h.h.HazelcastCacheRegionFactory : Starting up HazelcastCacheRegionFactory
2019-10-09 12:04:59.124 INFO 21088 --- [ restartedMain] c.h.config.AbstractConfigLocator : Loading 'hazelcast.xml' from the classpath.
2019-10-09 12:04:59.127 WARN 21088 --- [ restartedMain] c.h.config.AbstractXmlConfigHelper : Name of the hazelcast schema location is incorrect, using default
2019-10-09 12:04:59.165 INFO 21088 --- [ restartedMain] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.12.2] Prefer IPv4 stack is true, prefer IPv6 addresses is false
2019-10-09 12:04:59.949 INFO 21088 --- [ restartedMain] com.hazelcast.instance.AddressPicker : [LOCAL] [dev] [3.12.2] Picked [10.29.47.243]:5703, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5703], bind any local is true
2019-10-09 12:04:59.950 INFO 21088 --- [ restartedMain] com.hazelcast.system : [10.29.47.243]:5703 [dev] [3.12.2] Hazelcast 3.12.2 (20190802 - e34b163) starting at [10.29.47.243]:5703
2019-10-09 12:04:59.950 INFO 21088 --- [ restartedMain] com.hazelcast.system : [10.29.47.243]:5703 [dev] [3.12.2] Copyright (c) 2008-2019, Hazelcast, Inc. All Rights Reserved.
2019-10-09 12:04:59.956 INFO 21088 --- [ restartedMain] c.h.s.i.o.impl.BackpressureRegulator : [10.29.47.243]:5703 [dev] [3.12.2] Backpressure is disabled
2019-10-09 12:05:01.343 INFO 21088 --- [ restartedMain] com.hazelcast.instance.Node : [10.29.47.243]:5703 [dev] [3.12.2] Creating MulticastJoiner
2019-10-09 12:05:01.348 INFO 21088 --- [ restartedMain] c.h.s.i.o.impl.OperationExecutorImpl : [10.29.47.243]:5703 [dev] [3.12.2] Starting 4 partition threads and 3 generic threads (1 dedicated for priority tasks)
2019-10-09 12:05:01.349 INFO 21088 --- [ restartedMain] c.h.internal.diagnostics.Diagnostics : [10.29.47.243]:5703 [dev] [3.12.2] Diagnostics disabled. To enable add -Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2019-10-09 12:05:01.349 INFO 21088 --- [ restartedMain] com.hazelcast.core.LifecycleService : [10.29.47.243]:5703 [dev] [3.12.2] [10.29.47.243]:5703 is STARTING
2019-10-09 12:05:01.473 INFO 21088 --- [ restartedMain] c.h.i.cluster.impl.MulticastJoiner : [10.29.47.243]:5703 [dev] [3.12.2] Trying to join to discovered node: [10.29.47.243]:5701
2019-10-09 12:05:01.478 INFO 21088 --- [cached.thread-2] com.hazelcast.nio.tcp.TcpIpConnector : [10.29.47.243]:5703 [dev] [3.12.2] Connecting to /10.29.47.243:5701, timeout: 10000, bind-any: true
2019-10-09 12:05:01.486 INFO 21088 --- [.IO.thread-in-0] com.hazelcast.nio.tcp.TcpIpConnection : [10.29.47.243]:5701 [dev] [3.12.2] Initialized new cluster connection between /10.29.47.243:5701 and /10.29.47.243:51948
2019-10-09 12:05:01.487 INFO 21088 --- [.IO.thread-in-0] com.hazelcast.nio.tcp.TcpIpConnection : [10.29.47.243]:5703 [dev] [3.12.2] Initialized new cluster connection between /10.29.47.243:51948 and /10.29.47.243:5701
2019-10-09 12:05:07.483 INFO 21088 --- [ration.thread-0] c.h.internal.cluster.ClusterService : [10.29.47.243]:5701 [dev] [3.12.2]
Members {size:2, ver:2} [
Member [10.29.47.243]:5701 - bf933bd9-9214-4022-a104-514bc5f56267 this
Member [10.29.47.243]:5703 - 9862ded6-c26d-4da8-929f-f8616a4bf633
]
2019-10-09 12:05:07.489 INFO 21088 --- [ration.thread-0] c.h.internal.cluster.ClusterService : [10.29.47.243]:5703 [dev] [3.12.2]
Members {size:2, ver:2} [
Member [10.29.47.243]:5701 - bf933bd9-9214-4022-a104-514bc5f56267
Member [10.29.47.243]:5703 - 9862ded6-c26d-4da8-929f-f8616a4bf633 this
]
2019-10-09 12:05:08.481 WARN 21088 --- [ restartedMain] com.hazelcast.instance.Node : [10.29.47.243]:5703 [dev] [3.12.2] Config seed port is 5701 and cluster size is 2. Some of the ports seem occupied!
2019-10-09 12:05:08.482 INFO 21088 --- [ restartedMain] c.h.i.m.ManagementCenterService : [10.29.47.243]:5703 [dev] [3.12.2] Hazelcast will connect to Hazelcast Management Center on address:
http://localhost:8080/hazelcast-mancenter
2019-10-09 12:05:08.487 INFO 21088 --- [ restartedMain] com.hazelcast.core.LifecycleService : [10.29.47.243]:5703 [dev] [3.12.2] [10.29.47.243]:5703 is STARTED
如何解决每个应用程序实例有两个成员的问题?每个实例有两个成员是否有实际用途?
按@Mesut提供解决方案
我在 Spring Boot 的application.properties
文件中添加了以下语句(config 可以放在其他地方(。
spring.jpa.properties.hibernate.cache.hazelcast.instance_name=your-instance-name
您还必须创建一个名为"您的实例名称"的 bean。在 Spring Boot 中,这可以通过注释一个返回带有@Bean
的 Hazelcast 实例的方法来实现。这可以在您的主类或@Configuration
类中完成。
@Bean
public Config hazelcastConfig() {
Config config = new Config();
config.setInstanceName("your-instance-name");
config.setProperty("hazelcast.phone.home.enabled", "false");
return config;
}
@Bean
public HazelcastInstance hazelcastInstance(Config config) {
return Hazelcast.newHazelcastInstance(config);
}
这导致应用程序的每个实例只有一个成员(非常好(。但是,与管理中心的连接不再有效(在我的情况下并不那么重要(。
如果要共享同一实例,则应在休眠配置和hazelcast.xml
中使用相同的实例名称。
hibernate.cache.hazelcast.instance_name
是休眠端的配置参数。
您可以在此处查看文档以获取更多信息。