最近一直在找一个开源的工具,用来管理和配置集群环境里面的配置文件和配置项。后来发现jolokia可以做类似的东西。所以就拿过来用一用看看效果。
做了两个例子来验证jolokia的效果和功能。
例子1是一个standalone的应用程序,很简单的一个MBean。我把我做的流程记录下来供将来参考:
HelloMBean.java | Hello.java | HelloAgent.java
HelloMBean.java定义了MBean的接口:
public interface HelloMBean { public String getName(); public void setName(String name); public void printHello(); public void printHello(String whoName); }
Hello.java实现了这个接口:
public class Hello implements HelloMBean { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public void printHello() { System.out.println("Hello World, " + name); } public void printHello(String whoName) { System.out.println("Hello , " + whoName); } }
HelloAgent.java注册MBean,并启动这个应用程序:
import javax.management.MBeanServer; import java.lang.management.ManagementFactory; import javax.management.MBeanServerFactory; import javax.management.ObjectName; public class HelloAgent { public static void main(String[] args) throws Exception { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName helloName = new ObjectName("jolokia:name=HelloWorld"); server.registerMBean(new Hello(), helloName); System.out.println("start....."); Thread.sleep(1000000); } }
下面的工作就是如果用jolokia来监视,配置这个应用程序:
下载jolokia,然后解压缩到一个目录下面例如:/home/ilxlf/jolokia/jolokia-0.95
$> cd ~/jolokia/jolokia-0.95/agents/
有一个jar包叫做:jolokia-jvm6.jar 这里我们用到的是jolokia的一种MBean监控方式:jvm方式。实质上就是把这个jar包attach到你的应用程序,以此来达到监控的目的。
$> java -jar jolokia-jvm6.jar
运行上面的命令会显示当前该机器上正在运行的MBean server的所有程序的PID。
我们上面的HelloAgent也在其中。这里假设HelloAgent的PID是27463
$> java -jar jolokia-jvm6.jar --agentContext /HelloAgent start 27463
Started Jolokia for PID 27463
http://localhost:8778/HelloAgent/
import org.jolokia.client.*; import org.jolokia.client.request.*; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Config { public static void main(String[] args) throws Exception { J4pClient j4pClient = new J4pClient("http://localhost:8778/HelloAgent/"); J4pReadRequest req = new J4pReadRequest("jolokia:name=HelloWorld"); J4pReadResponse resp = j4pClient.execute(req); Map<String,String> vals = resp.getValue(); Set<String> sset = vals.keySet(); Iterator<String> iter = sset.iterator(); while(iter.hasNext()){ String key = iter.next(); System.out.println(key); System.out.println(vals.get(key)); } } }
public interface ConfigurationMBean { public String getName(); public void setName(String name); public String getValue(); public void setValue(String value); public void printHello(); } public class Configuration implements ConfigurationMBean { private String name; private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public Configuration(String name){ this.name = name; this.value = "1234"; } public Configuration(){ this.name = "first class"; this.value = "123"; } @Override public String getName() { // TODO Auto-generated method stub return name; } @Override public void setName(String name) { // TODO Auto-generated method stub this.name = name; } @Override public void printHello() { // TODO Auto-generated method stub System.out.println(this.name); } }
import java.io.IOException; import java.lang.management.ManagementFactory; import javax.management.Attribute; import javax.management.AttributeNotFoundException; import javax.management.InstanceAlreadyExistsException; import javax.management.InstanceNotFoundException; import javax.management.IntrospectionException; import javax.management.InvalidAttributeValueException; import javax.management.MBeanAttributeInfo; import javax.management.MBeanException; import javax.management.MBeanInfo; import javax.management.MBeanRegistrationException; import javax.management.MBeanServer; import javax.management.MBeanServerFactory; import javax.management.MalformedObjectNameException; import javax.management.NotCompliantMBeanException; import javax.management.ObjectName; import javax.management.ReflectionException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ilxlf.jmx.common.Configuration; public class JMXServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("enter--doGet"); super.doGet(req, resp); System.out.println("exit--doGet"); } @Override public void init() throws ServletException { // TODO Auto-generated method stub System.out.println("enter--init"); super.init(); MBeanServer server = MBeanServerFactory.createMBeanServer("com.ilxlf.jmx.common"); ObjectName configuration = null; try { configuration = new ObjectName("com.ilxlf.jmx.common=NewValue"); server.registerMBean(new Configuration(), configuration); } catch (MalformedObjectNameException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstanceAlreadyExistsException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MBeanRegistrationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NotCompliantMBeanException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MBeanException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("exit--init"); } }
import org.jolokia.client.*; import org.jolokia.client.request.*; import java.util.Iterator; import java.util.Map; import java.util.Set; public class Config { public static void main(String[] args) throws Exception { J4pClient j4pClient = new J4pClient("http://localhost:8080/jolokia"); J4pReadRequest req = new J4pReadRequest("com.ilxlf.jmx.common:name=NewValue"); J4pReadResponse resp = j4pClient.execute(req); Map<String,String> vals = resp.getValue(); Set<String> sset = vals.keySet(); Iterator<String> iter = sset.iterator(); while(iter.hasNext()){ String key = iter.next(); System.out.println(key); System.out.println(vals.get(key)); } } }