Spring 中的 Hazelcast 忽略了我的 NetworkConfig
Hazelcast in Spring Boot ignoring my NetworkConfig
我使用 Hazelcast 作为我的主要数据存储,由 JPA 支持到数据库。我试图让它不使用多播在我们的开发环境中查找其他实例,因为我们正在处理不同的 classes 等并指向我们自己的数据库,但 Hazelcast 仍在连接。我知道它正在调用我的 HazelcastConfiguration class,但它也在 jar 文件中使用 hazelcast-defaults.xml 并创建了一个集群。
@Bean(name = "hazelcastInstance")
public HazelcastInstance getHazelcastInstance(Config config) {
return new HazelcastInstanceFactory(config).getHazelcastInstance();
}
@Bean(name = "hazelCastConfig")
public Config config ()
{
MapConfig userMapConfig = buildUserMapConfig();
...
Config config = new Config();
config.setNetworkConfig(buildNetworkConfig());
return config;
}
private NetworkConfig buildNetworkConfig () {
NetworkConfig networkConfig = new NetworkConfig();
JoinConfig join = new JoinConfig();
MulticastConfig multicastConfig = new MulticastConfig();
multicastConfig.setEnabled(false);
join.setMulticastConfig(multicastConfig);
TcpIpConfig tcpIpConfig = new TcpIpConfig();
tcpIpConfig.setEnabled(false);
join.setTcpIpConfig(tcpIpConfig);
networkConfig.setJoin(join);
return networkConfig;
}
现在我可以看到正在调用这些,并且它必须使用我的配置,因为实体返回到我的数据库,但我在启动时也得到了这个:
2017-06-20 14:41:24.311 INFO 3741 --- [ main] c.h.i.cluster.impl.MulticastJoiner : [10.10.0.125]:5702 [dev] [3.8.1] Trying to join to discovered node: [10.10.0.127]:5702
2017-06-20 14:41:34.870 INFO 3741 --- [ached.thread-14] c.hazelcast.nio.tcp.InitConnectionTask : [10.10.0.125]:5702 [dev] [3.8.1] Connecting to /10.10.0.127:5702, timeout: 0, bind-any: true
2017-06-20 14:41:34.972 INFO 3741 --- [ached.thread-14] c.h.nio.tcp.TcpIpConnectionManager : [10.10.0.125]:5702 [dev] [3.8.1] Established socket connection between /10.10.0.125:54917 and /10.10.0.127:5702
2017-06-20 14:41:41.181 INFO 3741 --- [thread-Acceptor] c.h.nio.tcp.SocketAcceptorThread : [10.10.0.125]:5702 [dev] [3.8.1] Accepting socket connection from /10.10.0.146:60449
2017-06-20 14:41:41.183 INFO 3741 --- [ached.thread-21] c.h.nio.tcp.TcpIpConnectionManager : [10.10.0.125]:5702 [dev] [3.8.1] Established socket connection between /10.10.0.125:5702 and /10.10.0.146:60449
2017-06-20 14:41:41.185 INFO 3741 --- [ration.thread-0] com.hazelcast.system : [10.10.0.125]:5702 [dev] [3.8.1] Cluster version set to 3.8
2017-06-20 14:41:41.187 INFO 3741 --- [ration.thread-0] c.h.internal.cluster.ClusterService : [10.10.0.125]:5702 [dev] [3.8.1]
Members [3] {
Member [10.10.0.127]:5702 - e02dd47f-7bac-42d6-abf9-eeb62bdb1884
Member [10.10.0.146]:5702 - 9239d33e-3b60-4bf5-ad81-da14524197ca
Member [10.10.0.125]:5702 - 847d0008-6540-438d-bea6-7d8b19b8141a this
}
有人有想法吗?
配置 TCP IP 集群的一种简单方法是使用 hazelcast.xml 配置文件。
<hazelcast>
...
<network>
<port auto-increment="true">5701</port> // check if this is valid for the usecase
<join>
<multicast enabled="false">
</multicast>
<tcp-ip enabled="true">
<hostname>machine1</hostname>
<hostname>machine2</hostname>
<hostname>machine3:5799</hostname>
<interface>192.168.1.0-7</interface> // set values as per your env
<interface>192.168.1.21</interface>
</tcp-ip>
</join>
...
</network>
...
</hazelcast>
如下配置所示,当多播的启用属性设置为false时,tcp-ip必须设置为true。对于 none-multicast 选项,必须列出集群成员主机名 and/or IP 地址的全部或子集。请注意,不必在此处列出所有集群成员,但当新成员加入时,其中至少有一个必须在集群中处于活动状态。 tcp-ip 标签接受名为“conn-timeout-seconds”的属性。默认值为 5。如果列出了很多 IP,并且成员无法正确构建集群,建议增加此值。
正在加载配置文件
将 hazelcast.xml 文件添加到 src/main/resources 文件夹,spring 启动会自动为您配置 hazelcast。您可以选择使用 spring.hazelcast.config 配置 属性.
在属性或 YAML 文件中配置 hazelcast.xml 文件的位置
# application.yml
spring:
hazelcast:
config: classpath:[path To]/hazelcast.xml
# application.properties
spring.hazelcast.config=classpath:[path To]/hazelcast.xml
我遇到的问题是 Apache Camel 及其 HazelcastComponent。它不会自动获取您的 Hazelcast 实例。当我像这样配置 HazelcastComponent 时,它开始使用正确的 HazelcastInstance:
@Bean(name = "hazelcast")
HazelcastComponent hazelcastComponent() {
HazelcastComponent hazelcastComponent = new HazelcastComponent();
hazelcastComponent.setHazelcastInstance(hazelcastInstance);
return hazelcastComponent;
}
我使用 Hazelcast 作为我的主要数据存储,由 JPA 支持到数据库。我试图让它不使用多播在我们的开发环境中查找其他实例,因为我们正在处理不同的 classes 等并指向我们自己的数据库,但 Hazelcast 仍在连接。我知道它正在调用我的 HazelcastConfiguration class,但它也在 jar 文件中使用 hazelcast-defaults.xml 并创建了一个集群。
@Bean(name = "hazelcastInstance")
public HazelcastInstance getHazelcastInstance(Config config) {
return new HazelcastInstanceFactory(config).getHazelcastInstance();
}
@Bean(name = "hazelCastConfig")
public Config config ()
{
MapConfig userMapConfig = buildUserMapConfig();
...
Config config = new Config();
config.setNetworkConfig(buildNetworkConfig());
return config;
}
private NetworkConfig buildNetworkConfig () {
NetworkConfig networkConfig = new NetworkConfig();
JoinConfig join = new JoinConfig();
MulticastConfig multicastConfig = new MulticastConfig();
multicastConfig.setEnabled(false);
join.setMulticastConfig(multicastConfig);
TcpIpConfig tcpIpConfig = new TcpIpConfig();
tcpIpConfig.setEnabled(false);
join.setTcpIpConfig(tcpIpConfig);
networkConfig.setJoin(join);
return networkConfig;
}
现在我可以看到正在调用这些,并且它必须使用我的配置,因为实体返回到我的数据库,但我在启动时也得到了这个:
2017-06-20 14:41:24.311 INFO 3741 --- [ main] c.h.i.cluster.impl.MulticastJoiner : [10.10.0.125]:5702 [dev] [3.8.1] Trying to join to discovered node: [10.10.0.127]:5702
2017-06-20 14:41:34.870 INFO 3741 --- [ached.thread-14] c.hazelcast.nio.tcp.InitConnectionTask : [10.10.0.125]:5702 [dev] [3.8.1] Connecting to /10.10.0.127:5702, timeout: 0, bind-any: true
2017-06-20 14:41:34.972 INFO 3741 --- [ached.thread-14] c.h.nio.tcp.TcpIpConnectionManager : [10.10.0.125]:5702 [dev] [3.8.1] Established socket connection between /10.10.0.125:54917 and /10.10.0.127:5702
2017-06-20 14:41:41.181 INFO 3741 --- [thread-Acceptor] c.h.nio.tcp.SocketAcceptorThread : [10.10.0.125]:5702 [dev] [3.8.1] Accepting socket connection from /10.10.0.146:60449
2017-06-20 14:41:41.183 INFO 3741 --- [ached.thread-21] c.h.nio.tcp.TcpIpConnectionManager : [10.10.0.125]:5702 [dev] [3.8.1] Established socket connection between /10.10.0.125:5702 and /10.10.0.146:60449
2017-06-20 14:41:41.185 INFO 3741 --- [ration.thread-0] com.hazelcast.system : [10.10.0.125]:5702 [dev] [3.8.1] Cluster version set to 3.8
2017-06-20 14:41:41.187 INFO 3741 --- [ration.thread-0] c.h.internal.cluster.ClusterService : [10.10.0.125]:5702 [dev] [3.8.1]
Members [3] {
Member [10.10.0.127]:5702 - e02dd47f-7bac-42d6-abf9-eeb62bdb1884
Member [10.10.0.146]:5702 - 9239d33e-3b60-4bf5-ad81-da14524197ca
Member [10.10.0.125]:5702 - 847d0008-6540-438d-bea6-7d8b19b8141a this
}
有人有想法吗?
配置 TCP IP 集群的一种简单方法是使用 hazelcast.xml 配置文件。
<hazelcast>
...
<network>
<port auto-increment="true">5701</port> // check if this is valid for the usecase
<join>
<multicast enabled="false">
</multicast>
<tcp-ip enabled="true">
<hostname>machine1</hostname>
<hostname>machine2</hostname>
<hostname>machine3:5799</hostname>
<interface>192.168.1.0-7</interface> // set values as per your env
<interface>192.168.1.21</interface>
</tcp-ip>
</join>
...
</network>
...
</hazelcast>
如下配置所示,当多播的启用属性设置为false时,tcp-ip必须设置为true。对于 none-multicast 选项,必须列出集群成员主机名 and/or IP 地址的全部或子集。请注意,不必在此处列出所有集群成员,但当新成员加入时,其中至少有一个必须在集群中处于活动状态。 tcp-ip 标签接受名为“conn-timeout-seconds”的属性。默认值为 5。如果列出了很多 IP,并且成员无法正确构建集群,建议增加此值。
正在加载配置文件
将 hazelcast.xml 文件添加到 src/main/resources 文件夹,spring 启动会自动为您配置 hazelcast。您可以选择使用 spring.hazelcast.config 配置 属性.
在属性或 YAML 文件中配置 hazelcast.xml 文件的位置# application.yml
spring:
hazelcast:
config: classpath:[path To]/hazelcast.xml
# application.properties
spring.hazelcast.config=classpath:[path To]/hazelcast.xml
我遇到的问题是 Apache Camel 及其 HazelcastComponent。它不会自动获取您的 Hazelcast 实例。当我像这样配置 HazelcastComponent 时,它开始使用正确的 HazelcastInstance:
@Bean(name = "hazelcast")
HazelcastComponent hazelcastComponent() {
HazelcastComponent hazelcastComponent = new HazelcastComponent();
hazelcastComponent.setHazelcastInstance(hazelcastInstance);
return hazelcastComponent;
}