如何查询Hystrix的伺服MBean?

时间:2016-08-26 21:44:25

标签: java jmx hystrix servo

我可以在JConsole中访问MBean,它们显示为com.netflix.servo> HystrixCommand> countSuccess> (实际命令及其属性)

我找不到任何关于如何查询这些对象和值的示例,例如countSuccess,countFailure等。

我最接近的是ObjectName o = new ObjectName("com.netflix.servo:name=countSuccess,instance=T6JmxStatCommand,type=HystrixCommand"); https://github.com/n0rad/hands-on-hystrix/blob/master/src/test/java/fr/n0rad/hands/on/hystrix/t6/T6JmxStatMain.java,但显然不起作用。

通过以下代码注册了mbeans:HystrixPlugins.getInstance().registerMetricsPublisher(HystrixServoMetricsPublisher.getInstance());

1 个答案:

答案 0 :(得分:0)

发现查询的对象名称没有任何问题。实际上它与JConsole中的相同;截图右侧显示的完整语法如下: enter image description here

另外,我需要在Eclipse运行配置中添加一些系统属性: -Dcom.sun.management.jmxremote.rmi.port=8700 -Dcom.sun.management.jmxremote.port=8600 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

然后我可以使用端口8600进行连接。在代码中:

package com.awgtek.miscpocs.lognfetch.client;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

public class TestJMXConn {

    public static void main(String[] args) throws Exception {
      JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:8600/jmxrmi");
      JMXConnector connect = JMXConnectorFactory.connect(url);
      MBeanServerConnection mbsc = connect.getMBeanServerConnection();
     // ObjectName o = new ObjectName("com.netflix.servo:name=countSuccess,type=HystrixCommand,instance=LogAndFetchRestServicePostCommand");
      ObjectName o = new ObjectName("com.netflix.servo:name=countSuccess,type=HystrixCommand,instance=LogAndFetchRestServiceGetCommand");
      Object value = mbsc.getAttribute(o, "value");
      System.out.println("the value: " + value);
      connect.close();


    }

}