1. Kafka消息队列运维监控概述

Kafka作为分布式流处理平台,在生产环境中需要专业的运维监控和管理。本文将详细介绍Kafka集群监控、Topic管理、消费者优化、性能调优的完整解决方案,帮助运维人员有效管理Kafka集群。

1.1 核心挑战

  1. 集群监控: 实时监控Kafka集群和Broker状态
  2. Topic管理: 管理Topic分区和副本
  3. 消费者优化: 优化消费者组和消费性能
  4. 性能调优: 优化Kafka性能和吞吐量
  5. 故障诊断: 快速定位Kafka相关问题

1.2 技术架构

1
2
3
4
5
Kafka监控 → 数据采集 → 性能分析 → 告警通知 → 自动优化
↓ ↓ ↓ ↓ ↓
集群指标 → 监控代理 → 数据存储 → 告警引擎 → 调优脚本
↓ ↓ ↓ ↓ ↓
Topic管理 → 分区监控 → 消费者管理 → 自动修复 → 运维记录

2. Kafka监控系统

2.1 Maven依赖配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!-- pom.xml -->
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Boot Data Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

<!-- Kafka Client -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.5.0</version>
</dependency>

<!-- Kafka Admin -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>3.5.0</version>
</dependency>

<!-- Micrometer监控 -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
</dependencies>

2.2 应用配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# application.yml
server:
port: 8080

spring:
redis:
host: localhost
port: 6379
database: 0

# Kafka监控配置
kafka-monitor:
bootstrap-servers: "localhost:9092" # Kafka集群地址
cluster-name: "production-cluster" # 集群名称
collection-interval: 10000 # 采集间隔(毫秒)
topic-alert-threshold: 80 # Topic使用率告警阈值(%)
consumer-lag-threshold: 10000 # 消费者延迟告警阈值
broker-alert-threshold: 85 # Broker资源告警阈值(%)

3. Kafka监控服务

3.1 Kafka监控实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Kafka集群监控数据实体类
*/
@Data
@TableName("kafka_cluster_monitor")
public class KafkaClusterMonitor {

@TableId(type = IdType.AUTO)
private Long id; // 主键ID

private String clusterName; // 集群名称

private String hostname; // 主机名

private String ip; // IP地址

private Integer brokerCount; // Broker数量

private Integer topicCount; // Topic数量

private Integer partitionCount; // 分区数量

private Integer replicaCount; // 副本数量

private Long totalMessages; // 总消息数

private Long totalBytes; // 总字节数

private Double throughput; // 吞吐量(消息/秒)

private Double avgLatency; // 平均延迟(毫秒)

private Integer underReplicatedPartitions; // 未充分复制分区数

private Integer offlinePartitions; // 离线分区数

private String clusterStatus; // 集群状态

private Date collectTime; // 采集时间

private Date createTime; // 创建时间
}

/**
* Kafka Topic监控数据实体类
*/
@Data
@TableName("kafka_topic_monitor")
public class KafkaTopicMonitor {

@TableId(type = IdType.AUTO)
private Long id; // 主键ID

private String clusterName; // 集群名称

private String topicName; // Topic名称

private Integer partitionCount; // 分区数量

private Integer replicaCount; // 副本数量

private Long messageCount; // 消息数量

private Long logSize; // 日志大小

private Double messageRate; // 消息速率(消息/秒)

private Double byteRate; // 字节速率(字节/秒)

private Long consumerLag; // 消费者延迟

private Integer underReplicatedPartitions; // 未充分复制分区数

private Integer offlinePartitions; // 离线分区数

private String topicStatus; // Topic状态

private Date collectTime; // 采集时间

private Date createTime; // 创建时间
}

3.2 Kafka监控服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/**
* Kafka监控服务
* 负责Kafka集群和Topic数据的采集、存储和分析
*/
@Service
public class KafkaMonitorService {

@Autowired
private KafkaClusterMonitorMapper kafkaClusterMonitorMapper;

@Autowired
private KafkaTopicMonitorMapper kafkaTopicMonitorMapper;

@Autowired
private RedisTemplate<String, Object> redisTemplate;

@Autowired
private AlertService alertService;

private AdminClient adminClient;
private KafkaConsumer<String, String> consumer;

/**
* 初始化Kafka客户端
*/
@PostConstruct
public void initKafkaClients() {
try {
// 创建AdminClient
Properties adminProps = new Properties();
adminProps.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
adminProps.put(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, 30000);
adminClient = AdminClient.create(adminProps);

// 创建Consumer
Properties consumerProps = new Properties();
consumerProps.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
consumerProps.put(ConsumerConfig.GROUP_ID_CONFIG, "kafka-monitor-group");
consumerProps.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumerProps.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest");
consumer = new KafkaConsumer<>(consumerProps);

log.info("Kafka客户端初始化成功");

} catch (Exception e) {
log.error("初始化Kafka客户端失败: {}", e.getMessage(), e);
}
}

/**
* 采集Kafka集群数据
* 定期采集Kafka集群和Topic信息
*/
@Scheduled(fixedRate = 10000) // 每10秒执行一次
public void collectKafkaData() {
try {
// 1. 采集集群信息
collectClusterInfo();

// 2. 采集Topic信息
collectTopicInfo();

} catch (Exception e) {
log.error("采集Kafka数据失败: {}", e.getMessage(), e);
}
}

/**
* 采集集群信息
*/
private void collectClusterInfo() {
try {
// 1. 获取集群信息
KafkaClusterInfo clusterInfo = getClusterInfo();

// 2. 创建集群监控数据
KafkaClusterMonitor monitorData = createClusterMonitorData(clusterInfo);

// 3. 保存到数据库
kafkaClusterMonitorMapper.insert(monitorData);

// 4. 更新缓存
updateClusterCache(monitorData);

// 5. 检查集群告警
checkClusterAlert(monitorData);

log.debug("采集集群信息: brokerCount={}, topicCount={}",
monitorData.getBrokerCount(), monitorData.getTopicCount());

} catch (Exception e) {
log.error("采集集群信息失败: {}", e.getMessage(), e);
}
}

/**
* 获取集群信息
*/
private KafkaClusterInfo getClusterInfo() {
KafkaClusterInfo clusterInfo = new KafkaClusterInfo();

try {
// 获取Broker信息
DescribeClusterResult clusterResult = adminClient.describeCluster();
Collection<Node> nodes = clusterResult.nodes().get();
clusterInfo.setBrokerCount(nodes.size());

// 获取Topic信息
ListTopicsResult topicsResult = adminClient.listTopics();
Set<String> topicNames = topicsResult.names().get();
clusterInfo.setTopicCount(topicNames.size());

// 获取分区和副本信息
int totalPartitions = 0;
int totalReplicas = 0;
int underReplicatedPartitions = 0;
int offlinePartitions = 0;

for (String topicName : topicNames) {
DescribeTopicsResult topicsDescResult = adminClient.describeTopics(Arrays.asList(topicName));
Map<String, TopicDescription> topicDescriptions = topicsDescResult.all().get();

TopicDescription topicDescription = topicDescriptions.get(topicName);
if (topicDescription != null) {
totalPartitions += topicDescription.partitions().size();

for (TopicPartitionInfo partitionInfo : topicDescription.partitions()) {
totalReplicas += partitionInfo.replicas().size();

// 检查未充分复制的分区
if (partitionInfo.replicas().size() < partitionInfo.replicas().size()) {
underReplicatedPartitions++;
}

// 检查离线分区
if (partitionInfo.leader() == null) {
offlinePartitions++;
}
}
}
}

clusterInfo.setPartitionCount(totalPartitions);
clusterInfo.setReplicaCount(totalReplicas);
clusterInfo.setUnderReplicatedPartitions(underReplicatedPartitions);
clusterInfo.setOfflinePartitions(offlinePartitions);

// 计算吞吐量和延迟
clusterInfo.setThroughput(calculateThroughput());
clusterInfo.setAvgLatency(calculateAvgLatency());

// 设置集群状态
clusterInfo.setClusterStatus(determineClusterStatus(clusterInfo));

} catch (Exception e) {
log.error("获取集群信息失败: {}", e.getMessage(), e);
}

return clusterInfo;
}

/**
* 计算吞吐量
*/
private double calculateThroughput() {
try {
// 简化实现,实际应该从JMX或Kafka监控工具获取
return 1000.0; // 1000 消息/秒
} catch (Exception e) {
return 0.0;
}
}

/**
* 计算平均延迟
*/
private double calculateAvgLatency() {
try {
// 简化实现,实际应该从JMX或Kafka监控工具获取
return 10.0; // 10ms
} catch (Exception e) {
return 0.0;
}
}

/**
* 确定集群状态
*/
private String determineClusterStatus(KafkaClusterInfo clusterInfo) {
String status = "Healthy";

if (clusterInfo.getUnderReplicatedPartitions() > 0) {
status = "UnderReplicated";
} else if (clusterInfo.getOfflinePartitions() > 0) {
status = "OfflinePartitions";
} else if (clusterInfo.getThroughput() < 100) {
status = "LowThroughput";
}

return status;
}

/**
* 创建集群监控数据
*/
private KafkaClusterMonitor createClusterMonitorData(KafkaClusterInfo clusterInfo) {
KafkaClusterMonitor monitorData = new KafkaClusterMonitor();

// 设置基本信息
monitorData.setClusterName(clusterInfo.getClusterName());
monitorData.setHostname(getHostname());
monitorData.setIp(getLocalIpAddress());
monitorData.setCollectTime(new Date());
monitorData.setCreateTime(new Date());

// 设置集群信息
monitorData.setBrokerCount(clusterInfo.getBrokerCount());
monitorData.setTopicCount(clusterInfo.getTopicCount());
monitorData.setPartitionCount(clusterInfo.getPartitionCount());
monitorData.setReplicaCount(clusterInfo.getReplicaCount());

// 设置性能信息
monitorData.setThroughput(clusterInfo.getThroughput());
monitorData.setAvgLatency(clusterInfo.getAvgLatency());

// 设置状态信息
monitorData.setUnderReplicatedPartitions(clusterInfo.getUnderReplicatedPartitions());
monitorData.setOfflinePartitions(clusterInfo.getOfflinePartitions());
monitorData.setClusterStatus(clusterInfo.getClusterStatus());

return monitorData;
}

/**
* 采集Topic信息
*/
private void collectTopicInfo() {
try {
// 获取所有Topic
ListTopicsResult topicsResult = adminClient.listTopics();
Set<String> topicNames = topicsResult.names().get();

for (String topicName : topicNames) {
try {
// 获取Topic详细信息
DescribeTopicsResult topicsDescResult = adminClient.describeTopics(Arrays.asList(topicName));
Map<String, TopicDescription> topicDescriptions = topicsDescResult.all().get();

TopicDescription topicDescription = topicDescriptions.get(topicName);
if (topicDescription != null) {
// 创建Topic监控数据
KafkaTopicMonitor topicMonitor = createTopicMonitorData(topicDescription);

// 保存到数据库
kafkaTopicMonitorMapper.insert(topicMonitor);

// 更新缓存
updateTopicCache(topicMonitor);

// 检查Topic告警
checkTopicAlert(topicMonitor);
}

} catch (Exception e) {
log.error("处理Topic失败: topicName={}, error={}",
topicName, e.getMessage());
}
}

} catch (Exception e) {
log.error("采集Topic信息失败: {}", e.getMessage(), e);
}
}

/**
* 创建Topic监控数据
*/
private KafkaTopicMonitor createTopicMonitorData(TopicDescription topicDescription) {
KafkaTopicMonitor topicMonitor = new KafkaTopicMonitor();

// 设置基本信息
topicMonitor.setClusterName("production-cluster");
topicMonitor.setTopicName(topicDescription.name());
topicMonitor.setCollectTime(new Date());
topicMonitor.setCreateTime(new Date());

// 设置分区和副本信息
topicMonitor.setPartitionCount(topicDescription.partitions().size());

int replicaCount = 0;
int underReplicatedPartitions = 0;
int offlinePartitions = 0;

for (TopicPartitionInfo partitionInfo : topicDescription.partitions()) {
replicaCount += partitionInfo.replicas().size();

// 检查未充分复制的分区
if (partitionInfo.replicas().size() < partitionInfo.replicas().size()) {
underReplicatedPartitions++;
}

// 检查离线分区
if (partitionInfo.leader() == null) {
offlinePartitions++;
}
}

topicMonitor.setReplicaCount(replicaCount);
topicMonitor.setUnderReplicatedPartitions(underReplicatedPartitions);
topicMonitor.setOfflinePartitions(offlinePartitions);

// 设置性能信息
topicMonitor.setMessageRate(calculateTopicMessageRate(topicDescription.name()));
topicMonitor.setByteRate(calculateTopicByteRate(topicDescription.name()));
topicMonitor.setConsumerLag(calculateConsumerLag(topicDescription.name()));

// 设置状态信息
topicMonitor.setTopicStatus(determineTopicStatus(topicMonitor));

return topicMonitor;
}

/**
* 计算Topic消息速率
*/
private double calculateTopicMessageRate(String topicName) {
try {
// 简化实现,实际应该从JMX或Kafka监控工具获取
return 100.0; // 100 消息/秒
} catch (Exception e) {
return 0.0;
}
}

/**
* 计算Topic字节速率
*/
private double calculateTopicByteRate(String topicName) {
try {
// 简化实现,实际应该从JMX或Kafka监控工具获取
return 1024.0; // 1024 字节/秒
} catch (Exception e) {
return 0.0;
}
}

/**
* 计算消费者延迟
*/
private long calculateConsumerLag(String topicName) {
try {
// 简化实现,实际应该从消费者组信息获取
return 0;
} catch (Exception e) {
return 0;
}
}

/**
* 确定Topic状态
*/
private String determineTopicStatus(KafkaTopicMonitor topicMonitor) {
String status = "Healthy";

if (topicMonitor.getUnderReplicatedPartitions() > 0) {
status = "UnderReplicated";
} else if (topicMonitor.getOfflinePartitions() > 0) {
status = "OfflinePartitions";
} else if (topicMonitor.getConsumerLag() > 10000) {
status = "HighConsumerLag";
}

return status;
}

/**
* 更新集群缓存
*/
private void updateClusterCache(KafkaClusterMonitor monitorData) {
try {
String cacheKey = "kafka:cluster:" + monitorData.getClusterName();
redisTemplate.opsForValue().set(cacheKey, monitorData, Duration.ofMinutes(5));

} catch (Exception e) {
log.warn("更新集群缓存失败: {}", e.getMessage());
}
}

/**
* 更新Topic缓存
*/
private void updateTopicCache(KafkaTopicMonitor topicMonitor) {
try {
String cacheKey = "kafka:topic:" + topicMonitor.getTopicName();
redisTemplate.opsForValue().set(cacheKey, topicMonitor, Duration.ofMinutes(5));

} catch (Exception e) {
log.warn("更新Topic缓存失败: {}", e.getMessage());
}
}

/**
* 检查集群告警
*/
private void checkClusterAlert(KafkaClusterMonitor monitorData) {
try {
String alertType = null;
String alertLevel = null;
String alertMessage = null;

// 检查未充分复制分区告警
if (monitorData.getUnderReplicatedPartitions() > 0) {
alertType = "KAFKA_UNDER_REPLICATED_PARTITIONS";
alertLevel = "WARNING";
alertMessage = String.format("Kafka未充分复制分区: %d", monitorData.getUnderReplicatedPartitions());
}

// 检查离线分区告警
if (monitorData.getOfflinePartitions() > 0) {
alertType = "KAFKA_OFFLINE_PARTITIONS";
alertLevel = "CRITICAL";
alertMessage = String.format("Kafka离线分区: %d", monitorData.getOfflinePartitions());
}

// 检查吞吐量告警
if (monitorData.getThroughput() < 100) {
alertType = "KAFKA_LOW_THROUGHPUT";
alertLevel = "WARNING";
alertMessage = String.format("Kafka吞吐量过低: %.2f", monitorData.getThroughput());
}

// 发送告警
if (alertType != null) {
sendClusterAlert(monitorData, alertType, alertLevel, alertMessage);
}

} catch (Exception e) {
log.error("检查集群告警失败: {}", e.getMessage(), e);
}
}

/**
* 检查Topic告警
*/
private void checkTopicAlert(KafkaTopicMonitor topicMonitor) {
try {
String alertType = null;
String alertLevel = null;
String alertMessage = null;

// 检查未充分复制分区告警
if (topicMonitor.getUnderReplicatedPartitions() > 0) {
alertType = "KAFKA_TOPIC_UNDER_REPLICATED";
alertLevel = "WARNING";
alertMessage = String.format("Topic未充分复制分区: %s, 数量: %d",
topicMonitor.getTopicName(), topicMonitor.getUnderReplicatedPartitions());
}

// 检查离线分区告警
if (topicMonitor.getOfflinePartitions() > 0) {
alertType = "KAFKA_TOPIC_OFFLINE";
alertLevel = "CRITICAL";
alertMessage = String.format("Topic离线分区: %s, 数量: %d",
topicMonitor.getTopicName(), topicMonitor.getOfflinePartitions());
}

// 检查消费者延迟告警
if (topicMonitor.getConsumerLag() > 10000) {
alertType = "KAFKA_TOPIC_CONSUMER_LAG";
alertLevel = "WARNING";
alertMessage = String.format("Topic消费者延迟过高: %s, 延迟: %d",
topicMonitor.getTopicName(), topicMonitor.getConsumerLag());
}

// 发送告警
if (alertType != null) {
sendTopicAlert(topicMonitor, alertType, alertLevel, alertMessage);
}

} catch (Exception e) {
log.error("检查Topic告警失败: {}", e.getMessage(), e);
}
}

/**
* 发送集群告警
*/
private void sendClusterAlert(KafkaClusterMonitor monitorData, String alertType, String alertLevel, String alertMessage) {
try {
String alertKey = "kafka:cluster:alert:" + monitorData.getClusterName() + ":" + alertType;
Boolean hasAlert = redisTemplate.hasKey(alertKey);

if (hasAlert == null || !hasAlert) {
AlertMessage alert = new AlertMessage();
alert.setType(alertType);
alert.setLevel(alertLevel);
alert.setMessage(alertMessage);
alert.setTimestamp(new Date());
alert.setHostname(monitorData.getHostname());

alertService.sendAlert(alert);

redisTemplate.opsForValue().set(alertKey, "1", Duration.ofMinutes(5));

log.warn("发送集群告警: clusterName={}, type={}, level={}",
monitorData.getClusterName(), alertType, alertLevel);
}

} catch (Exception e) {
log.error("发送集群告警失败: {}", e.getMessage(), e);
}
}

/**
* 发送Topic告警
*/
private void sendTopicAlert(KafkaTopicMonitor topicMonitor, String alertType, String alertLevel, String alertMessage) {
try {
String alertKey = "kafka:topic:alert:" + topicMonitor.getTopicName() + ":" + alertType;
Boolean hasAlert = redisTemplate.hasKey(alertKey);

if (hasAlert == null || !hasAlert) {
AlertMessage alert = new AlertMessage();
alert.setType(alertType);
alert.setLevel(alertLevel);
alert.setMessage(alertMessage);
alert.setTimestamp(new Date());
alert.setHostname(topicMonitor.getClusterName());

alertService.sendAlert(alert);

redisTemplate.opsForValue().set(alertKey, "1", Duration.ofMinutes(5));

log.warn("发送Topic告警: topicName={}, type={}, level={}",
topicMonitor.getTopicName(), alertType, alertLevel);
}

} catch (Exception e) {
log.error("发送Topic告警失败: {}", e.getMessage(), e);
}
}

/**
* 获取实时集群数据
*/
public KafkaClusterMonitor getRealTimeClusterData(String clusterName) {
String cacheKey = "kafka:cluster:" + clusterName;
return (KafkaClusterMonitor) redisTemplate.opsForValue().get(cacheKey);
}

/**
* 获取实时Topic数据
*/
public KafkaTopicMonitor getRealTimeTopicData(String topicName) {
String cacheKey = "kafka:topic:" + topicName;
return (KafkaTopicMonitor) redisTemplate.opsForValue().get(cacheKey);
}

/**
* 获取主机名
*/
private String getHostname() {
try {
return InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
return "unknown";
}
}

/**
* 获取本地IP地址
*/
private String getLocalIpAddress() {
try {
return InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
return "127.0.0.1";
}
}

/**
* 关闭Kafka客户端
*/
@PreDestroy
public void shutdownKafkaClients() {
try {
if (adminClient != null) {
adminClient.close();
}

if (consumer != null) {
consumer.close();
}

log.info("Kafka客户端关闭成功");

} catch (Exception e) {
log.error("关闭Kafka客户端失败: {}", e.getMessage(), e);
}
}
}

/**
* Kafka集群信息实体类
*/
@Data
public class KafkaClusterInfo {
private String clusterName; // 集群名称
private Integer brokerCount; // Broker数量
private Integer topicCount; // Topic数量
private Integer partitionCount; // 分区数量
private Integer replicaCount; // 副本数量
private Integer underReplicatedPartitions; // 未充分复制分区数
private Integer offlinePartitions; // 离线分区数
private Double throughput; // 吞吐量
private Double avgLatency; // 平均延迟
private String clusterStatus; // 集群状态
}

4. Kafka管理服务

4.1 Kafka管理服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* Kafka管理服务
* 提供Kafka集群管理功能
*/
@Service
public class KafkaManagementService {

@Autowired
private AdminClient adminClient;

@Autowired
private AlertService alertService;

/**
* 创建Topic
*/
public void createTopic(String topicName, int partitions, int replicationFactor) {
try {
// 创建Topic配置
NewTopic newTopic = new NewTopic(topicName, partitions, (short) replicationFactor);

// 创建Topic
CreateTopicsResult result = adminClient.createTopics(Arrays.asList(newTopic));
result.all().get();

log.info("创建Topic成功: topicName={}, partitions={}, replicationFactor={}",
topicName, partitions, replicationFactor);

} catch (Exception e) {
log.error("创建Topic失败: topicName={}, error={}", topicName, e.getMessage(), e);
}
}

/**
* 删除Topic
*/
public void deleteTopic(String topicName) {
try {
// 删除Topic
DeleteTopicsResult result = adminClient.deleteTopics(Arrays.asList(topicName));
result.all().get();

log.info("删除Topic成功: topicName={}", topicName);

} catch (Exception e) {
log.error("删除Topic失败: topicName={}, error={}", topicName, e.getMessage(), e);
}
}

/**
* 增加Topic分区
*/
public void increaseTopicPartitions(String topicName, int newPartitionCount) {
try {
// 创建分区配置
Map<String, NewPartitions> newPartitionsMap = new HashMap<>();
newPartitionsMap.put(topicName, NewPartitions.increaseTo(newPartitionCount));

// 增加分区
CreatePartitionsResult result = adminClient.createPartitions(newPartitionsMap);
result.all().get();

log.info("增加Topic分区成功: topicName={}, newPartitionCount={}",
topicName, newPartitionCount);

} catch (Exception e) {
log.error("增加Topic分区失败: topicName={}, error={}", topicName, e.getMessage(), e);
}
}

/**
* 获取Topic详细信息
*/
public TopicDescription getTopicDescription(String topicName) {
try {
DescribeTopicsResult result = adminClient.describeTopics(Arrays.asList(topicName));
Map<String, TopicDescription> topicDescriptions = result.all().get();

return topicDescriptions.get(topicName);

} catch (Exception e) {
log.error("获取Topic详细信息失败: topicName={}, error={}", topicName, e.getMessage(), e);
return null;
}
}

/**
* 获取所有Topic
*/
public Set<String> getAllTopics() {
try {
ListTopicsResult result = adminClient.listTopics();
return result.names().get();

} catch (Exception e) {
log.error("获取所有Topic失败: {}", e.getMessage(), e);
return new HashSet<>();
}
}

/**
* 获取消费者组信息
*/
public List<ConsumerGroupDescription> getConsumerGroups() {
try {
ListConsumerGroupsResult result = adminClient.listConsumerGroups();
Collection<ConsumerGroupListing> consumerGroupListings = result.all().get();

List<String> groupIds = consumerGroupListings.stream()
.map(ConsumerGroupListing::groupId)
.collect(Collectors.toList());

DescribeConsumerGroupsResult describeResult = adminClient.describeConsumerGroups(groupIds);
Map<String, ConsumerGroupDescription> consumerGroupDescriptions = describeResult.all().get();

return new ArrayList<>(consumerGroupDescriptions.values());

} catch (Exception e) {
log.error("获取消费者组信息失败: {}", e.getMessage(), e);
return new ArrayList<>();
}
}

/**
* 重置消费者组偏移量
*/
public void resetConsumerGroupOffset(String groupId, String topicName, long offset) {
try {
// 创建偏移量配置
Map<TopicPartition, OffsetAndMetadata> offsetMap = new HashMap<>();
TopicPartition topicPartition = new TopicPartition(topicName, 0);
offsetMap.put(topicPartition, new OffsetAndMetadata(offset));

// 重置偏移量
AlterConsumerGroupOffsetsResult result = adminClient.alterConsumerGroupOffsets(groupId, offsetMap);
result.all().get();

log.info("重置消费者组偏移量成功: groupId={}, topicName={}, offset={}",
groupId, topicName, offset);

} catch (Exception e) {
log.error("重置消费者组偏移量失败: groupId={}, topicName={}, error={}",
groupId, topicName, e.getMessage(), e);
}
}

/**
* 获取Broker信息
*/
public Collection<Node> getBrokers() {
try {
DescribeClusterResult result = adminClient.describeCluster();
return result.nodes().get();

} catch (Exception e) {
log.error("获取Broker信息失败: {}", e.getMessage(), e);
return new ArrayList<>();
}
}

/**
* 获取集群配置
*/
public Config getClusterConfig() {
try {
DescribeConfigsResult result = adminClient.describeConfigs(
Arrays.asList(new ConfigResource(ConfigResource.Type.BROKER, "0")));
Map<ConfigResource, Config> configs = result.all().get();

return configs.values().iterator().next();

} catch (Exception e) {
log.error("获取集群配置失败: {}", e.getMessage(), e);
return null;
}
}
}

5. 总结

本文详细介绍了Kafka消息队列运维监控与管理的完整解决方案,包括:

5.1 核心技术点

  1. 集群监控: 实时监控Kafka集群和Broker状态
  2. Topic管理: 管理Topic分区和副本
  3. 消费者优化: 优化消费者组和消费性能
  4. 性能调优: 优化Kafka性能和吞吐量
  5. 告警通知: 多级告警、智能通知

5.2 架构优势

  1. 实时监控: 10秒间隔的实时Kafka数据采集
  2. 智能告警: 基于阈值的智能告警机制
  3. 自动化管理: 自动化的Topic和消费者管理
  4. 多维度分析: 集群、Topic、消费者等多维度分析

5.3 最佳实践

  1. 监控策略: 设置合理的Kafka监控阈值
  2. 管理策略: 根据业务需求执行Topic和消费者管理
  3. 性能优化: 合理配置Kafka参数
  4. 预防措施: 提前预防Kafka集群问题

通过以上架构设计,可以构建完善的Kafka运维监控系统,实现Kafka集群的有效管理和优化。