代码之家  ›  专栏  ›  技术社区  ›  Dmitry Senkovich

Hazelcast MBean丢失

  •  0
  • Dmitry Senkovich  · 技术社区  · 6 年前

    我将应用程序迁移到Spring引导,包括从XML迁移到Java配置。这个应用程序使用了Hazelcast,它确实可以工作。然而,我再也看不到在JConsole的哈泽尔卡特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 回复  |  直到 6 年前
        1
  •  1
  •   Dmitry Senkovich    6 年前

    最后,我发现了一个问题:问题是我使用的属性如下:

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

    所以 true 设置为布尔值。其源代码中的内部hazelcast在文件中的此处为空 GroupProperties.java 在752th线上:

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

    如果你再深入一点,你就会发现 config.getProperty(name) 将返回空值,因为在 Properties.java 文件(如JDK 1.8.0 U 102所示):

    Object oval = super.get(key);
    String sval = (oval instanceof String) ? (String)oval : null;
    

    oval 在这种情况下,对应于“hazelcast.jmx”键,因此 但作为 布尔值 根据需要。

    解决方法很简单:

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

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

    希望它能帮助别人!

        2
  •  0
  •   Riaz    6 年前

    您能否通过如下设置系统属性来确保已启用JMX代理?

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