使用Java RMI访问xmemcached暴露的JMX服务

  我们可以通过JMX监控xmemcached状态,xmemcached使用RMI暴露服务接口:


service:jmx:rmi:///jndi/rmi://ip:7077/xmemcachedServer



  xmemcached提供的MBean包括:

MBean 描述
net.rubyeye.xmemcached.monitor.StatisticsHandlerMBean 用于查看Client统计信息
net.rubyeye.xmemcached.impl.OptimizerMBean 用于调整性能参数
net.rubyeye.xmemcached.XMemcachedClientMBean 动态添加或者删除节点,查看有效服务器等信息


 下面我们一起来看一下具体如何来查看和使用java来调用上述服务。

        1) 首先下载memcachead程序,windows系统下面我们使用memcached.exe,附件提供了memcached-1.2.6-win32-bin。把它解压到硬盘的一个目录中,例如:C:\memcached-1.2.6-win32-bin。


        2) 在 http://code.google.com/p/xmemcached/downloads/list 下载xmemcached-1.3.5-bin-with-dependencies.tar.gz ,在将xmemcached导入到eclipse中。


        3) 在导入的工程里面,编写测试代码:


package net.rubyeye.xmemcached.example;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.exception.MemcachedException;
import net.rubyeye.xmemcached.utils.AddrUtil;

/**
 * 启动参数
 *  -Dxmemcached.jmx.enable=true -Dxmemcached.rmi.port=7077 -Dxmemcached.rmi.name=xmemcachedServer 
 *  -Dxmemcached.statistics.enable=true
 * @author Administrator
 */
public class BaseExample {

	public static void main(String[] args) throws IOException {
		// 构造builder
		MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses("localhost:11211"));
		MemcachedClient memcachedClient = null;
		try {
			// 获取客户端连接对象
			memcachedClient = builder.build();

			// set 键为hello的数据
			memcachedClient.set("hello", 0, "Hello,xmemcached");
			String value = memcachedClient.get("hello");
			System.out.println("hello=" + value);
			memcachedClient.delete("hello");
			value = memcachedClient.get("hello");
			System.out.println("hello=" + value);
		} catch (MemcachedException e) {
			System.err.println("MemcachedClient operation fail");
			e.printStackTrace();
		} catch (TimeoutException e) {
			System.err.println("MemcachedClient operation timeout");
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}



 

 注意需要设置这个类的启动时的JVM参数,内容为:-Dxmemcached.jmx.enable=true -Dxmemcached.rmi.port=7077 -Dxmemcached.rmi.name=xmemcachedServer -Dxmemcached.statistics.enable=true,如下图:

http://my.iteye.com/admin/picture/107450

(可能是RP问题,这里显示不了图片,需要看图的同学情打开上面的URL,看图)


运行该类后,可以通过jconsole去连xmemcached暴露的jmx服务,如下图:


http://my.iteye.com/admin/picture/107452
http://my.iteye.com/admin/picture/107450

(可能是RP问题,这里显示不了图片,需要看图的同学情打开上面的URL,看图)


     4) 使用java程序,通过rmi访问这些MBean,程序如下:


package net.rubyeye.xmemcached.example;

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

import net.rubyeye.xmemcached.XMemcachedClientMBean;

public class RMITest {

	public static void main(String[] args) throws Exception {
		 JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://127.0.0.1:7077/xmemcachedServer");     		 
		 JMXConnector jmxc = JMXConnectorFactory.connect(url, null);     
		 MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
		 
		 // 依赖接口的调用方式
		 ObjectName namingName = ObjectName.getInstance("net.rubyeye.xmemcached:type=XMemcachedClient-MemcachedClient-0");
		 XMemcachedClientMBean proxy = (XMemcachedClientMBean) MBeanServerInvocationHandler.newProxyInstance(mbsc, namingName, XMemcachedClientMBean.class, false);
		 System.out.println(proxy.getServersDescription());
		 System.out.println(proxy.getName());
	}
}



 执行结果:

[127.0.0.1:11211(weight=1)]
MemcachedClient-0


需要指出的是:

ObjectName.getInstance("net.rubyeye.xmemcached:type=XMemcachedClient-MemcachedClient-0");

这行代码的getInstance函数的参数,它是ObjectName的字面量,大家可以在上面的图中找到,对源码感兴趣的同学可以在xmemcached的源码中找到:

		LedacachedMbeanServer.getInstance().registMBean(
				this,
				this.getClass().getPackage().getName() + ":type="
						+ this.getClass().getSimpleName() + "-"
						+ MemcachedClientNameHolder.getName());


 就知道是怎么会事情了。


其他几个接口大家自己可以试验下,有什么心得体会写出来一起交流ING。



你可能感兴趣的:(xmemcached)