最近zookeeper老是故障不断,所以需要一个zookeeper监控,来保证线上环境运行正常。

目前zookeeper获取监控指标有两种方式:

1、通过JMX来监控;

2、通过zookeeper自带的四字命令来获取各种监控指标。

现在我们来介绍第一种,通过JMX来监控。

JMX监控:

以本机环境为例,首先,在zookeeper安装目录的bin文件夹下,修改zkServer.cmd文件,修改完后如下:

@echo off
setlocal
call "%~dp0zkEnv.cmd"
set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
#新添加的内容
set JAVA_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=8999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false

echo on
#将%JAVA_OPTS%加入执行路径
java "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%"  %JAVA_OPTS% %ZOOMAIN% "%ZOOCFG%" %*
endlocal

1、jconsole监控

双击zkServer.cmd运行zookeeper,通过jconsole就可以监控zookeeper运行情况了,截图如下(红色框中为ObjectName)。


2、java代码监控

java代码监控如下:

JMXServiceURL url = new JMXServiceURL(
				"service:jmx:rmi:///jndi/rmi://localhost:8999/jmxrmi");
		JMXConnector connector = JMXConnectorFactory.connect(url);
		MBeanServerConnection mxBean = connector.getMBeanServerConnection();
		ObjectName objectName = new ObjectName(
				"org.apache.ZooKeeperService:name0=StandaloneServer_port-1");
		String[] attrs = { "StartTime", "Version", "TickTime",
				"MinSessionTimeout", "MaxSessionTimeout",
				"NumAliveConnections", "MaxClientCnxnsPerHost",
				"OutstandingRequests", "ClientPort", "PacketsSent",
				"PacketsReceived", "MinRequestLatency", "MaxRequestLatency",
				"AvgRequestLatency" };
		AttributeList attributes = mxBean.getAttributes(objectName, attrs);
		for (Object attribute : attributes) {
			System.out.println(attribute);
		}
		System.out.println("-------分隔符-----------");
		objectName = new ObjectName(
				"org.apache.ZooKeeperService:name0=StandaloneServer_port-1,name1=InMemoryDataTree");
		attributes = mxBean.getAttributes(objectName, new String[] {
				"LastZxid", "WatchCount", "NodeCount" });
		for (Object attribute : attributes) {
			System.out.println(attribute);
		}

2.1、获取objectName

大家可能想知道,如何查看objectName名字,翻看zookeeper官方文档,链接如下:http://zookeeper.apache.org/doc/trunk/zookeeperJMX.html。

以下内容直接复制官网,就不翻译了。

This table details JMX for a standalone server. Typically standalone is only used in development situations.

MBeans, their names and description
MBeanMBean Object NameDescription
ZooKeeperServerStandaloneServer_port<#>Statistics on the running server, also operations to reset these attributes. Note that the object name includes the client port of the server (name suffix).
DataTreeInMemoryDataTreeStatistics on the in memory znode database, also operations to access finer (and more computationally intensive) statistics on the data (such as ephemeral count).

可以看到Mbean ObjectName为StandaloneServer_port<#>InMemoryDataTree,其中StandaloneServer_port<#><#>为zookeeper的myid值,如果只有单台运行,则默认为1。

2.2、获取attribute数组

JMXServiceURL url = new JMXServiceURL(
				"service:jmx:rmi:///jndi/rmi://localhost:8999/jmxrmi");
		JMXConnector connector = JMXConnectorFactory.connect(url);
		MBeanServerConnection mxBean = connector.getMBeanServerConnection();
		ObjectName objectName = new ObjectName(
				"org.apache.ZooKeeperService:name0=StandaloneServer_port-1");
		MBeanInfo beanInfo = mxBean.getMBeanInfo(objectName);
		MBeanAttributeInfo[] infos = beanInfo.getAttributes();
		for (int i = 0; i < infos.length; i++) {
			System.out.println("属性名称:" + infos[i].getName());
		}

下一篇将介绍通过zookeeper的四字命令来进行监控。



Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐