【原】jmx
作者:david_zhang@sh 【转载时请以超链接形式标明文章】
https://www.cnblogs.com/david-zhang-index/p/17011271.html
首先,什么是jmx?它的全称是java management extensions,它是java平台管理和监控接口。为什么要搞它,因为对所有程序来说,能监控运行中的应用程序是非常重要的。
jmx架构图
1,jmx连接本地
创建MBean
public interface HelloMBean { public String getName(); public void setName(String name); public String getAge(); public void setAge(String age); public String helloWorld(); public String helloWorld(String str); public void getTel(); }
创建实现类
public class Hello implements HelloMBean{ public String name; public String age; @Override public String getName() { System.out.println("get name:"+name); return name; } @Override public void setName(String name) { System.out.println("set name:"+name); this.name = name; } @Override public String getAge() { System.out.println("get age:"+age); return age; } @Override public void setAge(String age) { System.out.println("set age:"+age); this.age = age; } @Override public String helloWorld() { System.out.println("hello world"); return "hello world"; } @Override public String helloWorld(String str) { System.out.println("hello world:"+str); return "hello world:"+str; } @Override public void getTel() { System.out.println("get tel"); } }
创建agent管理
import javax.management.*; import java.lang.management.ManagementFactory; public class HelloAgent { public static void main(String[] args) throws MalformedObjectNameException, NotCompliantMBeanException, InstanceAlreadyExistsException, MBeanRegistrationException, InterruptedException { //设置MBean名称,格式一级目录jmx,二级目录type=hello,MBean对象名称 String jmxName = "jmx:type=hello, name=helloWorld001"; //获得MBeanServer,ManagementFactory.getPlatformMBeanServer() returns a reference to the existing MBean server within the JVM. JConsole looks at the beans on that server. //If you use createMBeanServer(), that will create an entirely new server. JConsole has no knowledge of it, and so will not see the beans registered with it. MBeanServer server = ManagementFactory.getPlatformMBeanServer(); //创建objectName ObjectName helloName = new ObjectName(jmxName); //创建并注册MBean server.registerMBean(new Hello(), helloName); //休眠主线程 Thread.sleep(60*60*1000); } }
打开jconsole
2,jmx连接远程-1
修改agent管理代码
import javax.management.*; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXConnectorServer; import javax.management.remote.JMXConnectorServerFactory; import javax.management.remote.JMXServiceURL; import java.io.IOException; import java.lang.management.ManagementFactory; import java.net.MalformedURLException; import java.rmi.RemoteException; import java.rmi.registry.LocateRegistry; public class HelloAgentRMI1 { public static void main(String[] args) throws MalformedObjectNameException, NotCompliantMBeanException, InstanceAlreadyExistsException, MBeanRegistrationException, InterruptedException, IOException { //设置MBean名称,格式一级目录jmx,二级目录type=hello,MBean对象名称 String jmxName = "jmx:type=hello, name=helloWorld001"; //获得MBeanServer,ManagementFactory.getPlatformMBeanServer() returns a reference to the existing MBean server within the JVM. JConsole looks at the beans on that server. //If you use createMBeanServer(), that will create an entirely new server. JConsole has no knowledge of it, and so will not see the beans registered with it. MBeanServer server = ManagementFactory.getPlatformMBeanServer(); //创建objectName ObjectName helloName = new ObjectName(jmxName); //创建并注册MBean server.registerMBean(new Hello(), helloName); //注册一个端口 LocateRegistry.createRegistry(9527); //url路径结尾可以随便指定,但需要jconsole来连接,则必须使用jmxrmi JMXServiceURL jmxServiceURL = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9527/jmxrmi"); //休眠主线程 JMXConnectorServer jcs = JMXConnectorServerFactory.newJMXConnectorServer(jmxServiceURL, null, server); jcs.start(); } }
打开jconsole
3,jmx连接远程-2,启动参数
在本地连接的基础上进行修改。在程序启动时加入以下启动参数,便可实现远程连接。 -Djava.rmi.server.hostname=<your-ip> // 你的ip -Dcom.sun.management.jmxremote.port=<your-port> // 开放端口号 -Dcom.sun.management.jmxremote.local.only=false // 是否只能本地连接 -Dcom.sun.management.jmxremote.ssl=false // 是否使用ssl加密 -Dcom.sun.management.jmxremote.authenticate=false // 是否需要账号密码认证
4,jmx连接远程-3,启动参数+配置文件
在本地连接的基础上进行修改。在程序启动时加入以下启动参数,并结合配置文件,便可实现远程连接。 -Dcom.sun.management.config.file=<your-url> // 配置文件路径 -Djava.rmi.server.hostname=<your-ip> 添加配置文件management.properties,填入以下参数: #开放端口号 com.sun.management.jmxremote.port=8888 #是否只能本地连接 com.sun.management.jmxremote.local.only=false #是否使用ssl加密 com.sun.management.jmxremote.ssl=false #是否账号密码认证 com.sun.management.jmxremote.authenticate=false
设置账号密码认证
实际使用中,会采用安全加密的方式来监控程序,这时可以设置ssl加密或账号密码认证。这里介绍账号密码认证的流程。 启动参数配置: -Dcom.sun.management.config.file=<your-url> // 配置文件路径 -Djava.rmi.server.hostname=<your-ip>
配置文件配置:
#开放端口号
com.sun.management.jmxremote.port=9999
#是否只能本地连接
com.sun.management.jmxremote.local.only=false
#是否使用ssl加密
com.sun.management.jmxremote.ssl=false
#密码文件路径
com.sun.managent.jmxremote.password.file=d:/jmxaccess.password
#权限文件路径
com.sun.managent.jmxremote.access.file=d:/jmxaccess.access
文件内容
jmxaccess.password文件内容 hzy 123456 jmxaccess.access文件内容 hzy readwrite
参考连接:
https://blog.csdn.net/DH719491759/article/details/116585911
http://www.tianshouzhi.com/api/tutorials/jmx/28
https://blog.csdn.net/weixin_28717411/article/details/114153984