Hazelcast MBean遗失

时间:2018-05-29 15:27:38

标签: java spring spring-boot jmx hazelcast

我将应用程序迁移到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");
    }

}

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

您能否通过将系统属性设置为

来确保您已启用JMX Agent
    -Dcom.sun.management.jmxremote
    -Dcom.sun.management.jmxremote.port=_portNo\_ (to specify JMX port, the default is 1099) (optional)
    -Dcom.sun.management.jmxremote.authenticate=false

然后你尝试更简单的设置,如下所示,以确保它是否有效,如果确实如此,那么检查程序中配置的构造。

Config config = new Config();
config.setProperty("hazelcast.jmx", "true");
HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);

答案 1 :(得分:0)

最后,我发现了一个问题:问题是我正在使用这样的属性:

    Properties properties = new Properties();
    properties.put("hazelcast.logging.type", "slf4j");
    properties.put("hazelcast.jmx", true);
    ...

所以true被设置为布尔值。内部Hazelcast的源代码在第752行的文件GroupProperties.java中变为空:

String configValue = (config != null) ? config.getProperty(name) : null;

如果你深入挖掘,你会看到config.getProperty(name)将返回null,因为Properties.java文件的第969行(如JDK 1.8.0_102中)这个奇特的东西:

Object oval = super.get(key);
String sval = (oval instanceof String) ? (String)oval : null;
在这种情况下,

oval对应于“hazelcast.jmx”键,因此为true,但 BOOLEAN 不是字符串。< / p>

修复很简单:

Properties properties = new Properties();
properties.put("hazelcast.logging.type", "slf4j");
properties.put("hazelcast.jmx", "true");
...

只需将所有非字符串值设为字符串。

希望它有所帮助!