我正在尝试在本地的 Spring Boot 应用程序中使用 Hazelcast。但似乎每个实例都是单独启动的(没有任何错误)并且没有加入现有的成员列表。
实例1:
Members {size:1, ver:1} [
Member [10.111.130.204]:5701 - 6d4a2864-5c90-4063-a2cc-deb64a950622 this
]
实例2:
Members {size:1, ver:1} [
Member [10.111.130.204]:5702 - 196c9d1f-7fd4-4c48-8dec-0fb913671610 this
]
我按照此处的指南设置了一个简单的环境https://guides.hazelcast.org/hazelcast-embedded-springboot/
有人可以帮我弄清楚我在这里缺少什么吗?
PS:
我也尝试创建以下配置,仍然是相同的行为。
@Bean
public Config hazelCastConfig() {
Config config = new Config();
config.getNetworkConfig().setPortAutoIncrement(true);
config.getGroupConfig().setName("hazelcast-cluster");
config.setManagementCenterConfig(new ManagementCenterConfig().setEnabled(true).setUrl("http://localhost:8080/hazelcast-mancenter"));
NetworkConfig network = config.getNetworkConfig();
JoinConfig join = network.getJoin();
join.getMulticastConfig().setEnabled(true);
return config;
}
@Bean
public HazelcastInstance hazelcastInstance(Config hazelCastConfig) {
return Hazelcast.newHazelcastInstance(hazelCastConfig);
}
示例日志[实例1]
2021-12-15 18:08:06.186 INFO 38768 --- [ main] com.example.hzdemo.HzDemoApplication : Starting HzDemoApplication
2021-12-15 18:08:06.192 INFO 38768 --- [ main] com.example.hzdemo.HzDemoApplication : No active profile set, falling back to default profiles: default
2021-12-15 18:08:07.776 INFO 38768 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8081 (http)
2021-12-15 18:08:07.783 INFO 38768 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-12-15 18:08:07.783 INFO 38768 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.55]
2021-12-15 18:08:07.854 INFO 38768 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-12-15 18:08:07.854 INFO 38768 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1542 ms
2021-12-15 18:08:07.943 WARN 38768 --- [ main] 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
2021-12-15 18:08:07.966 INFO 38768 --- [ main] com.hazelcast.instance.AddressPicker : [LOCAL] [hazelcast-cluster] [3.12.6] Prefer IPv4 stack is true, prefer IPv6 addresses is false
2021-12-15 18:08:07.985 INFO 38768 --- [ main] com.hazelcast.instance.AddressPicker : [LOCAL] [hazelcast-cluster] [3.12.6] Picked [10.111.130.204]:5701, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5701], bind any local is true
2021-12-15 18:08:07.994 INFO 38768 --- [ main] com.hazelcast.system : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] Hazelcast 3.12.6 (20200130 - be02cc5) starting at [10.111.130.204]:5701
2021-12-15 18:08:07.994 INFO 38768 --- [ main] com.hazelcast.system : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
2021-12-15 18:08:08.208 INFO 38768 --- [ main] c.h.s.i.o.impl.BackpressureRegulator : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] Backpressure is disabled
2021-12-15 18:08:08.576 INFO 38768 --- [ main] com.hazelcast.instance.Node : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] Creating MulticastJoiner
2021-12-15 18:08:08.676 INFO 38768 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] Starting 16 partition threads and 9 generic threads (1 dedicated for priority tasks)
2021-12-15 18:08:08.678 INFO 38768 --- [ main] c.h.internal.diagnostics.Diagnostics : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] Diagnostics disabled. To enable add -Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2021-12-15 18:08:08.686 INFO 38768 --- [ main] com.hazelcast.core.LifecycleService : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] [10.111.130.204]:5701 is STARTING
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.hazelcast.internal.networking.nio.SelectorOptimizer (file:/Users/<user>/.gradle/caches/modules-2/files-2.1/com.hazelcast/hazelcast/3.12.6/7640af720dfb86de81d93617271e2959dea6bfdc/hazelcast-3.12.6.jar) to field sun.nio.ch.SelectorImpl.selectedKeys
WARNING: Please consider reporting this to the maintainers of com.hazelcast.internal.networking.nio.SelectorOptimizer
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2021-12-15 18:08:11.096 INFO 38768 --- [ main] c.h.internal.cluster.ClusterService : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6]
Members {size:1, ver:1} [
Member [10.111.130.204]:5701 - 6d4a2864-5c90-4063-a2cc-deb64a950622 this
]
2021-12-15 18:08:11.111 INFO 38768 --- [ main] c.h.i.m.ManagementCenterService : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] Hazelcast will connect to Hazelcast Management Center on address:
http://localhost:8080/hazelcast-mancenter
2021-12-15 18:08:11.117 INFO 38768 --- [ main] com.hazelcast.core.LifecycleService : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] [10.111.130.204]:5701 is STARTED
2021-12-15 18:08:11.389 INFO 38768 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8081 (http) with context path ''
2021-12-15 18:08:11.404 INFO 38768 --- [ main] com.example.hzdemo.HzDemoApplication : Started HzDemoApplication in 5.715 seconds (JVM running for 6.731)
2021-12-15 18:08:12.126 INFO 38768 --- [.MC.Task.Poller] c.h.i.m.ManagementCenterService : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] Failed to pull tasks from Management Center
2021-12-15 18:08:12.126 INFO 38768 --- [MC.State.Sender] c.h.i.m.ManagementCenterService : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] Failed to connect to: http://localhost:8080/hazelcast-mancenter/collector.do
2021-12-15 18:08:12.126 INFO 38768 --- [MC.State.Sender] com.hazelcast.client.impl.ClientEngine : [10.111.130.204]:5701 [hazelcast-cluster] [3.12.6] Applying a new client selector :ClientSelector{any}
示例日志[实例2]
2021-12-15 18:09:06.831 INFO 39059 --- [ main] com.example.hzdemo.HzDemoApplication : Starting HzDemoApplication
2021-12-15 18:09:06.843 INFO 39059 --- [ main] com.example.hzdemo.HzDemoApplication : No active profile set, falling back to default profiles: default
2021-12-15 18:09:07.943 INFO 39059 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8082 (http)
2021-12-15 18:09:07.953 INFO 39059 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-12-15 18:09:07.954 INFO 39059 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2021-12-15 18:09:08.024 INFO 39059 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-12-15 18:09:08.024 INFO 39059 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1047 ms
2021-12-15 18:09:08.162 INFO 39059 --- [ main] com.hazelcast.instance.AddressPicker : [LOCAL] [hazelcast-cluster] [3.12.6] Prefer IPv4 stack is true, prefer IPv6 addresses is false
2021-12-15 18:09:08.188 INFO 39059 --- [ main] com.hazelcast.instance.AddressPicker : [LOCAL] [hazelcast-cluster] [3.12.6] Picked [10.111.130.204]:5702, using socket ServerSocket[addr=/0:0:0:0:0:0:0:0,localport=5702], bind any local is true
2021-12-15 18:09:08.213 INFO 39059 --- [ main] com.hazelcast.system : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] Hazelcast 3.12.6 (20200130 - be02cc5) starting at [10.111.130.204]:5702
2021-12-15 18:09:08.213 INFO 39059 --- [ main] com.hazelcast.system : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
2021-12-15 18:09:08.490 INFO 39059 --- [ main] c.h.s.i.o.impl.BackpressureRegulator : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] Backpressure is disabled
2021-12-15 18:09:08.917 INFO 39059 --- [ main] com.hazelcast.instance.Node : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] Creating MulticastJoiner
2021-12-15 18:09:09.101 INFO 39059 --- [ main] c.h.s.i.o.impl.OperationExecutorImpl : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] Starting 16 partition threads and 9 generic threads (1 dedicated for priority tasks)
2021-12-15 18:09:09.104 INFO 39059 --- [ main] c.h.internal.diagnostics.Diagnostics : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] Diagnostics disabled. To enable add -Dhazelcast.diagnostics.enabled=true to the JVM arguments.
2021-12-15 18:09:09.107 INFO 39059 --- [ main] com.hazelcast.core.LifecycleService : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] [10.111.130.204]:5702 is STARTING
2021-12-15 18:09:11.423 INFO 39059 --- [ main] c.h.internal.cluster.ClusterService : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6]
Members {size:1, ver:1} [
Member [10.111.130.204]:5702 - 196c9d1f-7fd4-4c48-8dec-0fb913671610 this
]
2021-12-15 18:09:11.424 WARN 39059 --- [ main] com.hazelcast.instance.Node : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] Config seed port is 5701 and cluster size is 1. Some of the ports seem occupied!
2021-12-15 18:09:11.451 INFO 39059 --- [ main] c.h.i.m.ManagementCenterService : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] Hazelcast will connect to Hazelcast Management Center on address:
http://localhost:8080/hazelcast-mancenter
2021-12-15 18:09:11.460 INFO 39059 --- [ main] com.hazelcast.core.LifecycleService : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] [10.111.130.204]:5702 is STARTED
2021-12-15 18:09:11.620 INFO 39059 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2021-12-15 18:09:11.761 INFO 39059 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8082 (http) with context path ''
2021-12-15 18:09:11.765 INFO 39059 --- [ main] com.example.hzdemo.HzDemoApplication : Started HzDemoApplication in 5.854 seconds (JVM running for 6.337)
2021-12-15 18:09:12.470 INFO 39059 --- [.MC.Task.Poller] c.h.i.m.ManagementCenterService : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] Failed to pull tasks from Management Center
2021-12-15 18:09:12.470 INFO 39059 --- [MC.State.Sender] c.h.i.m.ManagementCenterService : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] Failed to connect to: http://localhost:8080/hazelcast-mancenter/collector.do
2021-12-15 18:09:12.471 INFO 39059 --- [MC.State.Sender] com.hazelcast.client.impl.ClientEngine : [10.111.130.204]:5702 [hazelcast-cluster] [3.12.6] Applying a new client selector :ClientSelector{any}
ifconfig 输出:
utun2: flags=80d1<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST> mtu 1390
inet 10.111.130.204 --> 10.111.130.204 netmask 0xffffc000
当我使用域名通过 TCP/IP 形成集群时,我在 Cloud Foundry 中的 Hazelcast 中遇到了类似的问题。
我设法根据每个应用程序实例的索引添加增量延迟来对此进行排序,因此每个实例都会在不同的时间出现。
# Hazelcast configuration for embedded mode in cluster
#
# For a full configuration example, see:
# https://github.com/hazelcast/hazelcast/blob/master/hazelcast/src/main/resources/hazelcast-full-example.yaml
hazelcast:
cluster-name: my-hazelcast-cluster
# For other system properties, see:
# https://docs.hazelcast.com/hazelcast/5.2/system-properties
properties:
hazelcast.phone.home.enabled: false
network:
port:
auto-increment: false
port-count: 1
port: 5701 # Matches the network policy configured in Cloud Foundry
join:
auto-detection:
enabled: false
multicast:
enabled: false
tcp-ip:
enabled: true # Looks up for other members via TPC
member-list:
- my-application.apps.internal # Matches the route configured in Cloud Foundry
import javax.validation.constraints.Min;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;
import lombok.Data;
/**
* Configuration properties for the start-up delay.
*/
@Data
@Validated
@Configuration
@ConfigurationProperties("app.start-up-delay")
public class StartupDelayProperties {
/**
* Whether the start-up delay is enabled or not.
*/
private boolean enabled;
/**
* Multiplier for calculating the start-up delay according to the Cloud Foundry instance index. As follows:
* <p>
* {@code startUpDelay = applicationInstanceIndex * instanceIndexMultiplier }
* <p>
* Example: consider there are 4 application instances in Cloud Foundry, indexed from 0 to 3.
* And the {@code instanceIndexMultiplier} has been defined as 15.
* The start-up delay for each instance will be as follows:
*
* <ul>
* <li>Instance #0: 0 seconds
* <li>Instance #1: 15 seconds
* <li>Instance #2: 30 seconds
* <li>Instance #3: 45 seconds
* </ul>
*/
@Min(0)
private int instanceIndexMultiplier = 15;
/**
* Maximum start-up delay in seconds.
*/
@Min(0)
private int maxDelay = 45;
}
import static java.lang.Math.min;
import static org.springframework.core.Ordered.HIGHEST_PRECEDENCE;
import java.util.concurrent.TimeUnit;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
/**
* Component for adding a start-up delay to the application, based on the Cloud Foundry instance index.
* <p>
* This helps to ensure that each application instance becomes available at a different time.
* And it's particularly useful for giving enough time to the Cloud Foundry DNS server to assign domain names to the application instances, helping the Hazelcast cluster to be established.
*/
@Slf4j
@Component
@RequiredArgsConstructor
@Order(HIGHEST_PRECEDENCE) // This should be loaded really early
@ConditionalOnProperty("app.start-up-delay.enabled")
public class StartupDelay {
private final StartupDelayProperties startupDelayProperties;
/**
* The index number of the application instance in Cloud Foundry.
*/
@Setter
@Value("${CF_INSTANCE_INDEX:0}")
private Integer applicationInstanceIndex;
@PostConstruct
void delay() {
int startUpDelay = calculateStartUpDelay();
try {
log.info("Delaying the application start up in '{}' second(s)...", startUpDelay);
TimeUnit.SECONDS.sleep(startUpDelay);
log.info("Resuming the application start up...");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
private int calculateStartUpDelay() {
int delay = applicationInstanceIndex * startupDelayProperties.getInstanceIndexMultiplier();
int maxDelay = startupDelayProperties.getMaxDelay();
return min(delay, maxDelay);
}
}