kafka(五)
一、Group元数据
主要负责consumer group的管理,每个GroupCoordinator只负责管理一个consumer group的一个子集。
主要功能:处理JoinGroupRequest请求,处理SyncGroupRequest请求,完成分区分配功能,通过GroupMetadataManager和内部topic维护offset信息,记录group相关信息。
消费者元数据:MemberMetadata
memberId:对应消费者id
groupId:记录消费者所在的Consumer Group的id
assigment:分区信息
supportProtocols:对应消费者支持的PartitionAssignor,
awaitingJoinCallback:与joinGroupRequest的相关的回调函数
awaitingSyncCallback:与SyncGroupRequest的相关的回调函数
sessionTimeoutMs:心跳超时时间
latestHeatbeat:最后一次心跳时间戳
isLeaving:标识对应消费者是否已经离开了Consumer Group
Consumer Group的元数据信息:GroupMetadata
groupId:对应Consumer Group的id
members:消费者信息
state:当前group的状态
generationId:年代信息
leaderId:消费者leader
protocol:PartitionAssignor
GroupMetadataManager:
管理group的分区offset,分区管理,分区数量管理,处理分区拉取请求,分区提交请求
二、GroupCoordinator
功能:分组状态管理,负载均衡,consumer心跳交互
主要字段:
groupConfig:配置信息
offsetConfig:记录offsetMetadata相关配置信息
groupManager:GroupMetadata对象
三、监控
1.jmx
agent类用于监听一个类的信息
package com.dxz.study;
import java.io.IOException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import javax.management.InstanceAlreadyExistsException;
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.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;
import com.sun.jdmk.comm.HtmlAdaptorServer;
public class HelloWorldAgent {
public static void main(String[] args) throws MalformedObjectNameException, NullPointerException,
InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, IOException {
int rmiPort = 1066;
String jmxServerName = "TestJMXServer";
Registry registry = LocateRegistry.createRegistry(rmiPort);
MBeanServer mbs = MBeanServerFactory.createMBeanServer(jmxServerName);
HtmlAdaptorServer adapter = new HtmlAdaptorServer();
ObjectName adapterName;
adapterName = new ObjectName(jmxServerName + ":name=" + "htmladapter");
adapter.setPort(8082);
adapter.start();
mbs.registerMBean(adapter, adapterName);
ObjectName objName = new ObjectName(jmxServerName + ":name=" + "HelloWorld");
mbs.registerMBean(new HelloWorld(), objName);
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + rmiPort + "/" + jmxServerName);
System.out.println("JMXServiceURL: " + url.toString());
JMXConnectorServer jmxConnServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
jmxConnServer.start();
}
}
如果是动态的mbean,可以通过实现DynamicMbean来完成
metrics
<dependencies> <dependency> <groupId>com.codahale.metrics</groupId> <artifactId>metrics-core</artifactId> <version>${metrics.version}</version> </dependency> </dependencies>
public class QueueManager {
private final Queue queue;
public QueueManager(MetricRegistry metrics, String name) {
this.queue = new Queue();
metrics.register(MetricRegistry.name(QueueManager.class, name, "size"),
new Gauge<Integer>() {
@Override
public Integer getValue() {
return queue.size();
}
});
}
}
metrics下包含counter(用于计数),gauge用于记录集合长度,meter用于汇总等度量信息
以及mbeanReport,csvReport等监控记录组件