我将一个应用程序迁移到 Spring Boot,包括从 XML 迁移到 Java 配置。该应用程序使用 Hazelcast 并且确实有效。但是,我在 JConsole 中看不到 Hazelcast MBean 了。我设法找到的关于启用 JMX 的唯一内容是属性
properties.put("hazelcast.jmx", true);
但这并没有帮助。这是迁移前的配置:
<hz:hazelcast id="hzInstance">
<hz:config>
<hz:group name="gsynth" password="gsynth"/>
<hz:properties>
<hz:property name="hazelcast.logging.type">slf4j</hz:property>
<hz:property name="hazelcast.jmx">true</hz:property>
<hz:property name="hazelcast.version.check.enabled">false</hz:property>
<hz:property name="hazelcast.icmp.enabled">true</hz:property>
<hz:property name="hazelcast.shutdownhook.enabled">true</hz:property>
<hz:property name="hazelcast.max.operation.timeout">60000</hz:property>
<hz:property name="hazelcast.restart.on.max.idle">true</hz:property>
</hz:properties>
<hz:network port="5701" port-auto-increment="false">
<hz:join>
<hz:multicast enabled="false"/>
<hz:tcp-ip enabled="true">
<hz:members>${members.list}</hz:members>
</hz:tcp-ip>
</hz:join>
</hz:network>
</hz:config>
</hz:hazelcast>
<hz:map id="serviceHash" instance-ref="hzInstance" name="service-hash"/>
<hz:map id="persisterHash" instance-ref="hzInstance" name="persister-hash"/>
现在看起来像这样:
public class HazelcastConfig
{
@Bean
public HazelcastInstance hzInstance(Config hzConfig)
{
return Hazelcast.newHazelcastInstance(hzConfig);
}
@Bean
public Config hzConfig(@Value("${members.list}") String membersList)
{
Properties properties = new Properties();
properties.put("hazelcast.logging.type", "slf4j");
properties.put("hazelcast.jmx", true);
properties.put("hazelcast.version.check.enabled", false);
properties.put("hazelcast.icmp.enabled", true);
properties.put("hazelcast.shutdownhook.enabled", true);
properties.put("hazelcast.max.operation.timeout", 60000);
properties.put("hazelcast.restart.on.max.idle", true);
return new Config().setGroupConfig(new GroupConfig().setName("gsynth").setPassword("gsynth")).setProperties(
properties).setNetworkConfig(
new NetworkConfig().setPort(5701).setPortAutoIncrement(false).setJoin(
new JoinConfig().setMulticastConfig(new MulticastConfig().setEnabled(false)).setTcpIpConfig(
new TcpIpConfig().setEnabled(true).addMember(membersList))));
}
@Bean
public IMap serviceHash(HazelcastInstance hzInstance)
{
return hzInstance.getMap("service-hash");
}
@Bean
public IMap persisterHash(HazelcastInstance hzInstance)
{
return hzInstance.getMap("persister-hash");
}
}
感谢您的帮助!
最佳答案
最后,我发现了问题:问题是我使用的是这样的属性:
Properties properties = new Properties();
properties.put("hazelcast.logging.type", "slf4j");
properties.put("hazelcast.jmx", true);
...
所以true
被设置为 boolean 值。在内部,Hazelcast 的源代码在文件 GroupProperties.java
中变为 null。第 752 行:
String configValue = (config != null) ? config.getProperty(name) : null;
如果你深入挖掘,你会发现 config.getProperty(name)
由于 Properties.java
第 969 行的这个奇特的东西,将返回 null文件(如 JDK 1.8.0_102 中所示):
Object oval = super.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
oval
在本例中对应于“hazelcast.jmx”键,因此为 true
但按要求为 BOOLEAN 而不是 String。
修复方法很简单:
Properties properties = new Properties();
properties.put("hazelcast.logging.type", "slf4j");
properties.put("hazelcast.jmx", "true");
...
只需将所有非字符串值设置为字符串即可。
希望它能帮助别人!
关于java - Hazelcast MBean 丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50588002/