Search code examples
spring-bootcachinghazelcast

2 Hazelcast Members per Spring Boot Instance


I am using Hazelcast as a Hibernate 2LC provider in my Spring Boot application in the embedded/P2P mode. Upon starting the application I could see that two members for my Hazelcast cluster are created. When I start a second instance of my whole application, another two members are created.

Currently, I use the following configuration with HazelcastCacheRegionFactory (not local).

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

In the Hazelcast management centre, it looks like one member is the double of the other one. However, when starting a second instance of my Spring Boot application, both member still exist and in total they increase to four.

I already tried to set the backup count to 0 in my hazelcast.xml.

When starting the first instance of my application, I get the following logs:

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

How could I solve the issue of having two members per application instance? Is there an actual use of having two members per instance?

Solution by @Mesut

I added the following statement in the application.properties file of Spring Boot (config could be placed elsewhere).

spring.jpa.properties.hibernate.cache.hazelcast.instance_name=your-instance-name

You also have to create a bean with the name 'your-instance-name'. In Spring Boot this could be achieved by annotating a method which returns a Hazelcast instance with @Bean. This could be done in your main class or a @Configuration class.

@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);
}

This results in having only one member per instance of your application (very good). However, the connection to the management centre does not work anymore (not that important in my situation).


Solution

  • If you wanna share the same instance, you should be using the same instance name in both hibernate config and hazelcast.xml.

    hibernate.cache.hazelcast.instance_name is the configuration parameter in hibernate side.

    You can check documentation here for more info.