jolokia使用心得

    最近一直在找一個開源的工具,用來管理和配置集羣環境裏面的配置文件和配置項。後來發現jolokia能夠作相似的東西。因此就拿過來用一用看看效果。java

    作了兩個例子來驗證jolokia的效果和功能。web

    例子1是一個standalone的應用程序,很簡單的一個MBean。我把我作的流程記錄下來供未來參考:小程序

    HelloMBean.java  |   Hello.java    |    HelloAgent.javatomcat

    HelloMBean.java定義了MBean的接口:app

public interface HelloMBean {
    public String getName();
    public void setName(String name);
    public void printHello();
    public void printHello(String whoName);
}

    Hello.java實現了這個接口:eclipse

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,並啓動這個應用程序:webapp

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來監視,配置這個應用程序:jvm

     下載jolokia,而後解壓縮到一個目錄下面例如:/home/ilxlf/jolokia/jolokia-0.95ide

      $> 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/

      這樣咱們客戶端就能夠經過上面的這個link來訪問、控制、修改咱們的HelloMBean了。
      客戶端代碼以下:
      
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));
        }
    }
}
運行這個程序就能夠看到HelloMBean的name屬性了。

例子2是一個war應用。如何用jolokia的第二個功能? 用jolokia war來監視咱們本身的war程序。
這裏我選擇了tomcat來部署war包。
我用Eclipse jee建立了一個J2ee的小程序,很是小。包含一個MBean和一個servlet。
先列出MBean的接口和實現類:

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

}

servlet的代碼以下:

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

}

而後把這個j2ee工程打成war包(eclipse j22有export war功能)。而後拷貝到tomcat webapps目錄下面。

下面在把jolokia war也拷貝到tomcat webapps目錄下面。而後啓動tomcat。

下面咱們就能夠用客戶端來訪問你本身的war裏面的MBean了。

客戶端代碼以下:

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

運行這個client,會顯示出ConfigurationMBean的兩個屬性: name and value

後面的工做就是分兩個階段:修改屬性和進一步的深刻了解jolokia的功能,包括在集羣環境中如何使用jolokia來集中管理配置項。
相關文章
相關標籤/搜索