1. Netty网络框架运维监控概述

Netty作为高性能网络框架,在生产环境中需要专业的运维监控和优化。本文将详细介绍Netty连接监控、性能调优、内存管理、线程池优化的完整解决方案,帮助运维人员有效管理Netty应用。

1.1 核心挑战

  1. 连接监控: 实时监控Netty连接数和状态
  2. 性能调优: 优化Netty性能和吞吐量
  3. 内存管理: 管理Netty内存使用和泄漏
  4. 线程池优化: 优化EventLoop线程池
  5. 故障诊断: 快速定位Netty相关问题

1.2 技术架构

1
2
3
4
5
Netty监控 → 数据采集 → 性能分析 → 告警通知 → 自动优化
↓ ↓ ↓ ↓ ↓
连接指标 → 监控代理 → 数据存储 → 告警引擎 → 调优脚本
↓ ↓ ↓ ↓ ↓
内存管理 → 线程监控 → 性能调优 → 自动修复 → 运维记录

2. Netty监控系统

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
<!-- 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>

<!-- Netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.100.Final</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
19
20
# application.yml
server:
port: 8080

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

# Netty监控配置
netty-monitor:
server-port: 9999 # Netty服务端口
boss-threads: 1 # Boss线程数
worker-threads: 8 # Worker线程数
connection-timeout: 30000 # 连接超时时间(毫秒)
max-connections: 10000 # 最大连接数
collection-interval: 5000 # 采集间隔(毫秒)
memory-threshold: 80 # 内存使用率告警阈值(%)
connection-threshold: 90 # 连接数告警阈值(%)

3. Netty监控服务

3.1 Netty监控实体类

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
/**
* Netty连接监控数据实体类
*/
@Data
@TableName("netty_connection_monitor")
public class NettyConnectionMonitor {

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

private String hostname; // 主机名

private String ip; // IP地址

private String serverName; // 服务器名称

private Integer totalConnections; // 总连接数

private Integer activeConnections; // 活跃连接数

private Integer inactiveConnections; // 非活跃连接数

private Long totalBytesReceived; // 总接收字节数

private Long totalBytesSent; // 总发送字节数

private Long totalMessagesReceived; // 总接收消息数

private Long totalMessagesSent; // 总发送消息数

private Double avgResponseTime; // 平均响应时间

private Double maxResponseTime; // 最大响应时间

private Long memoryUsed; // 内存使用量

private Long memoryMax; // 最大内存

private Double memoryUsage; // 内存使用率

private Integer eventLoopThreads; // EventLoop线程数

private Integer activeEventLoops; // 活跃EventLoop数

private String nettyStatus; // Netty状态

private Date collectTime; // 采集时间

private Date createTime; // 创建时间
}

/**
* Netty性能监控数据实体类
*/
@Data
@TableName("netty_performance_monitor")
public class NettyPerformanceMonitor {

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

private String hostname; // 主机名

private String serverName; // 服务器名称

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

private Double latency; // 延迟(毫秒)

private Long channelCount; // Channel数量

private Long pendingTasks; // 待处理任务数

private Long completedTasks; // 已完成任务数

private Double taskCompletionRate; // 任务完成率

private Long directMemoryUsed; // 直接内存使用量

private Long heapMemoryUsed; // 堆内存使用量

private Double cpuUsage; // CPU使用率

private String performanceStatus; // 性能状态

private Date collectTime; // 采集时间

private Date createTime; // 创建时间
}

3.2 Netty监控服务

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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/**
* Netty监控服务
* 负责Netty连接和性能数据的采集、存储和分析
*/
@Service
public class NettyMonitorService {

@Autowired
private NettyConnectionMonitorMapper nettyConnectionMonitorMapper;

@Autowired
private NettyPerformanceMonitorMapper nettyPerformanceMonitorMapper;

@Autowired
private RedisTemplate<String, Object> redisTemplate;

@Autowired
private AlertService alertService;

private EventLoopGroup bossGroup;
private EventLoopGroup workerGroup;
private ServerBootstrap serverBootstrap;
private Channel serverChannel;

// 监控统计信息
private AtomicLong totalBytesReceived = new AtomicLong(0);
private AtomicLong totalBytesSent = new AtomicLong(0);
private AtomicLong totalMessagesReceived = new AtomicLong(0);
private AtomicLong totalMessagesSent = new AtomicLong(0);
private AtomicInteger activeConnections = new AtomicInteger(0);
private AtomicInteger totalConnections = new AtomicInteger(0);

/**
* 启动Netty服务器
*/
@PostConstruct
public void startNettyServer() {
try {
// 创建EventLoopGroup
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup(8);

// 创建ServerBootstrap
serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();

// 添加监控Handler
pipeline.addLast("monitor", new NettyMonitorHandler());

// 添加业务Handler
pipeline.addLast("handler", new NettyBusinessHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);

// 绑定端口
serverChannel = serverBootstrap.bind(9999).sync().channel();

log.info("Netty服务器启动成功,端口: 9999");

} catch (Exception e) {
log.error("启动Netty服务器失败: {}", e.getMessage(), e);
}
}

/**
* 采集Netty连接数据
* 定期采集Netty连接和性能信息
*/
@Scheduled(fixedRate = 5000) // 每5秒执行一次
public void collectNettyData() {
try {
// 1. 采集连接信息
collectConnectionInfo();

// 2. 采集性能信息
collectPerformanceInfo();

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

/**
* 采集连接信息
*/
private void collectConnectionInfo() {
try {
// 1. 获取连接信息
NettyConnectionInfo connectionInfo = getConnectionInfo();

// 2. 创建连接监控数据
NettyConnectionMonitor monitorData = createConnectionMonitorData(connectionInfo);

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

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

// 5. 检查连接告警
checkConnectionAlert(monitorData);

log.debug("采集连接信息: activeConnections={}, totalConnections={}",
monitorData.getActiveConnections(), monitorData.getTotalConnections());

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

/**
* 获取连接信息
*/
private NettyConnectionInfo getConnectionInfo() {
NettyConnectionInfo connectionInfo = new NettyConnectionInfo();

try {
// 设置连接统计信息
connectionInfo.setTotalConnections(totalConnections.get());
connectionInfo.setActiveConnections(activeConnections.get());
connectionInfo.setInactiveConnections(totalConnections.get() - activeConnections.get());

// 设置数据传输统计信息
connectionInfo.setTotalBytesReceived(totalBytesReceived.get());
connectionInfo.setTotalBytesSent(totalBytesSent.get());
connectionInfo.setTotalMessagesReceived(totalMessagesReceived.get());
connectionInfo.setTotalMessagesSent(totalMessagesSent.get());

// 计算平均响应时间
connectionInfo.setAvgResponseTime(calculateAvgResponseTime());
connectionInfo.setMaxResponseTime(calculateMaxResponseTime());

// 获取内存信息
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage();

connectionInfo.setMemoryUsed(heapUsage.getUsed());
connectionInfo.setMemoryMax(heapUsage.getMax());
connectionInfo.setMemoryUsage((double) heapUsage.getUsed() / heapUsage.getMax() * 100);

// 获取EventLoop信息
connectionInfo.setEventLoopThreads(workerGroup.executorCount());
connectionInfo.setActiveEventLoops(workerGroup.executorCount());

// 设置Netty状态
connectionInfo.setNettyStatus(serverChannel != null && serverChannel.isActive() ? "Running" : "Stopped");

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

return connectionInfo;
}

/**
* 计算平均响应时间
*/
private double calculateAvgResponseTime() {
// 简化实现,实际应该从统计数据中计算
return 10.0; // 10ms
}

/**
* 计算最大响应时间
*/
private double calculateMaxResponseTime() {
// 简化实现,实际应该从统计数据中计算
return 100.0; // 100ms
}

/**
* 创建连接监控数据
*/
private NettyConnectionMonitor createConnectionMonitorData(NettyConnectionInfo connectionInfo) {
NettyConnectionMonitor monitorData = new NettyConnectionMonitor();

// 设置基本信息
monitorData.setHostname(getHostname());
monitorData.setIp(getLocalIpAddress());
monitorData.setServerName("netty-server");
monitorData.setCollectTime(new Date());
monitorData.setCreateTime(new Date());

// 设置连接信息
monitorData.setTotalConnections(connectionInfo.getTotalConnections());
monitorData.setActiveConnections(connectionInfo.getActiveConnections());
monitorData.setInactiveConnections(connectionInfo.getInactiveConnections());

// 设置数据传输信息
monitorData.setTotalBytesReceived(connectionInfo.getTotalBytesReceived());
monitorData.setTotalBytesSent(connectionInfo.getTotalBytesSent());
monitorData.setTotalMessagesReceived(connectionInfo.getTotalMessagesReceived());
monitorData.setTotalMessagesSent(connectionInfo.getTotalMessagesSent());

// 设置性能信息
monitorData.setAvgResponseTime(connectionInfo.getAvgResponseTime());
monitorData.setMaxResponseTime(connectionInfo.getMaxResponseTime());

// 设置内存信息
monitorData.setMemoryUsed(connectionInfo.getMemoryUsed());
monitorData.setMemoryMax(connectionInfo.getMemoryMax());
monitorData.setMemoryUsage(connectionInfo.getMemoryUsage());

// 设置EventLoop信息
monitorData.setEventLoopThreads(connectionInfo.getEventLoopThreads());
monitorData.setActiveEventLoops(connectionInfo.getActiveEventLoops());

// 设置状态信息
monitorData.setNettyStatus(connectionInfo.getNettyStatus());

return monitorData;
}

/**
* 采集性能信息
*/
private void collectPerformanceInfo() {
try {
// 1. 获取性能信息
NettyPerformanceInfo performanceInfo = getPerformanceInfo();

// 2. 创建性能监控数据
NettyPerformanceMonitor monitorData = createPerformanceMonitorData(performanceInfo);

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

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

// 5. 检查性能告警
checkPerformanceAlert(monitorData);

log.debug("采集性能信息: throughput={}, latency={}ms",
monitorData.getThroughput(), monitorData.getLatency());

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

/**
* 获取性能信息
*/
private NettyPerformanceInfo getPerformanceInfo() {
NettyPerformanceInfo performanceInfo = new NettyPerformanceInfo();

try {
// 计算吞吐量
performanceInfo.setThroughput(calculateThroughput());

// 计算延迟
performanceInfo.setLatency(calculateLatency());

// 获取Channel数量
performanceInfo.setChannelCount(activeConnections.get());

// 获取任务统计信息
performanceInfo.setPendingTasks(calculatePendingTasks());
performanceInfo.setCompletedTasks(calculateCompletedTasks());
performanceInfo.setTaskCompletionRate(calculateTaskCompletionRate());

// 获取内存信息
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage();

performanceInfo.setDirectMemoryUsed(calculateDirectMemoryUsed());
performanceInfo.setHeapMemoryUsed(heapUsage.getUsed());

// 获取CPU使用率
performanceInfo.setCpuUsage(calculateCpuUsage());

// 设置性能状态
performanceInfo.setPerformanceStatus(determinePerformanceStatus(performanceInfo));

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

return performanceInfo;
}

/**
* 计算吞吐量
*/
private double calculateThroughput() {
// 简化实现,实际应该从统计数据中计算
return totalMessagesReceived.get() / 5.0; // 每秒消息数
}

/**
* 计算延迟
*/
private double calculateLatency() {
// 简化实现,实际应该从统计数据中计算
return 10.0; // 10ms
}

/**
* 计算待处理任务数
*/
private long calculatePendingTasks() {
// 简化实现,实际应该从EventLoop中获取
return 0;
}

/**
* 计算已完成任务数
*/
private long calculateCompletedTasks() {
// 简化实现,实际应该从EventLoop中获取
return totalMessagesReceived.get();
}

/**
* 计算任务完成率
*/
private double calculateTaskCompletionRate() {
long completed = calculateCompletedTasks();
long pending = calculatePendingTasks();
long total = completed + pending;

return total > 0 ? (double) completed / total * 100 : 100.0;
}

/**
* 计算直接内存使用量
*/
private long calculateDirectMemoryUsed() {
try {
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
MemoryUsage nonHeapUsage = memoryBean.getNonHeapMemoryUsage();
return nonHeapUsage.getUsed();
} catch (Exception e) {
return 0;
}
}

/**
* 计算CPU使用率
*/
private double calculateCpuUsage() {
try {
OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
return osBean.getProcessCpuLoad() * 100;
} catch (Exception e) {
return 0.0;
}
}

/**
* 确定性能状态
*/
private String determinePerformanceStatus(NettyPerformanceInfo performanceInfo) {
String status = "Normal";

if (performanceInfo.getThroughput() < 100) {
status = "Low Throughput";
} else if (performanceInfo.getLatency() > 100) {
status = "High Latency";
} else if (performanceInfo.getCpuUsage() > 80) {
status = "High CPU Usage";
}

return status;
}

/**
* 创建性能监控数据
*/
private NettyPerformanceMonitor createPerformanceMonitorData(NettyPerformanceInfo performanceInfo) {
NettyPerformanceMonitor monitorData = new NettyPerformanceMonitor();

// 设置基本信息
monitorData.setHostname(getHostname());
monitorData.setServerName("netty-server");
monitorData.setCollectTime(new Date());
monitorData.setCreateTime(new Date());

// 设置性能信息
monitorData.setThroughput(performanceInfo.getThroughput());
monitorData.setLatency(performanceInfo.getLatency());
monitorData.setChannelCount(performanceInfo.getChannelCount());
monitorData.setPendingTasks(performanceInfo.getPendingTasks());
monitorData.setCompletedTasks(performanceInfo.getCompletedTasks());
monitorData.setTaskCompletionRate(performanceInfo.getTaskCompletionRate());

// 设置内存信息
monitorData.setDirectMemoryUsed(performanceInfo.getDirectMemoryUsed());
monitorData.setHeapMemoryUsed(performanceInfo.getHeapMemoryUsed());

// 设置CPU信息
monitorData.setCpuUsage(performanceInfo.getCpuUsage());

// 设置状态信息
monitorData.setPerformanceStatus(performanceInfo.getPerformanceStatus());

return monitorData;
}

/**
* 更新连接缓存
*/
private void updateConnectionCache(NettyConnectionMonitor monitorData) {
try {
String cacheKey = "netty:connection:realtime";
redisTemplate.opsForValue().set(cacheKey, monitorData, Duration.ofMinutes(5));

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

/**
* 更新性能缓存
*/
private void updatePerformanceCache(NettyPerformanceMonitor monitorData) {
try {
String cacheKey = "netty:performance:realtime";
redisTemplate.opsForValue().set(cacheKey, monitorData, Duration.ofMinutes(5));

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

/**
* 检查连接告警
*/
private void checkConnectionAlert(NettyConnectionMonitor monitorData) {
try {
String alertType = null;
String alertLevel = null;
String alertMessage = null;

// 检查连接数告警
if (monitorData.getActiveConnections() > 9000) {
alertType = "NETTY_CONNECTIONS_HIGH";
alertLevel = "CRITICAL";
alertMessage = String.format("Netty连接数过高: %d", monitorData.getActiveConnections());
} else if (monitorData.getActiveConnections() > 8000) {
alertType = "NETTY_CONNECTIONS_WARNING";
alertLevel = "WARNING";
alertMessage = String.format("Netty连接数较高: %d", monitorData.getActiveConnections());
}

// 检查内存使用率告警
if (monitorData.getMemoryUsage() > 90) {
alertType = "NETTY_MEMORY_HIGH";
alertLevel = "CRITICAL";
alertMessage = String.format("Netty内存使用率过高: %.2f%%", monitorData.getMemoryUsage());
} else if (monitorData.getMemoryUsage() > 80) {
alertType = "NETTY_MEMORY_WARNING";
alertLevel = "WARNING";
alertMessage = String.format("Netty内存使用率较高: %.2f%%", monitorData.getMemoryUsage());
}

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

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

/**
* 检查性能告警
*/
private void checkPerformanceAlert(NettyPerformanceMonitor monitorData) {
try {
String alertType = null;
String alertLevel = null;
String alertMessage = null;

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

// 检查延迟告警
if (monitorData.getLatency() > 200) {
alertType = "NETTY_LATENCY_HIGH";
alertLevel = "WARNING";
alertMessage = String.format("Netty延迟过高: %.2fms", monitorData.getLatency());
}

// 检查CPU使用率告警
if (monitorData.getCpuUsage() > 90) {
alertType = "NETTY_CPU_HIGH";
alertLevel = "CRITICAL";
alertMessage = String.format("Netty CPU使用率过高: %.2f%%", monitorData.getCpuUsage());
}

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

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

/**
* 发送连接告警
*/
private void sendConnectionAlert(NettyConnectionMonitor monitorData, String alertType, String alertLevel, String alertMessage) {
try {
String alertKey = "netty:connection:alert:" + 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("发送连接告警: type={}, level={}", alertType, alertLevel);
}

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

/**
* 发送性能告警
*/
private void sendPerformanceAlert(NettyPerformanceMonitor monitorData, String alertType, String alertLevel, String alertMessage) {
try {
String alertKey = "netty:performance:alert:" + 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("发送性能告警: type={}, level={}", alertType, alertLevel);
}

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

/**
* 获取实时连接数据
*/
public NettyConnectionMonitor getRealTimeConnectionData() {
String cacheKey = "netty:connection:realtime";
return (NettyConnectionMonitor) redisTemplate.opsForValue().get(cacheKey);
}

/**
* 获取实时性能数据
*/
public NettyPerformanceMonitor getRealTimePerformanceData() {
String cacheKey = "netty:performance:realtime";
return (NettyPerformanceMonitor) 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";
}
}

/**
* 关闭Netty服务器
*/
@PreDestroy
public void shutdownNettyServer() {
try {
if (serverChannel != null) {
serverChannel.close().sync();
}

if (workerGroup != null) {
workerGroup.shutdownGracefully();
}

if (bossGroup != null) {
bossGroup.shutdownGracefully();
}

log.info("Netty服务器关闭成功");

} catch (Exception e) {
log.error("关闭Netty服务器失败: {}", e.getMessage(), e);
}
}
}

/**
* Netty连接信息实体类
*/
@Data
public class NettyConnectionInfo {
private Integer totalConnections; // 总连接数
private Integer activeConnections; // 活跃连接数
private Integer inactiveConnections; // 非活跃连接数
private Long totalBytesReceived; // 总接收字节数
private Long totalBytesSent; // 总发送字节数
private Long totalMessagesReceived; // 总接收消息数
private Long totalMessagesSent; // 总发送消息数
private Double avgResponseTime; // 平均响应时间
private Double maxResponseTime; // 最大响应时间
private Long memoryUsed; // 内存使用量
private Long memoryMax; // 最大内存
private Double memoryUsage; // 内存使用率
private Integer eventLoopThreads; // EventLoop线程数
private Integer activeEventLoops; // 活跃EventLoop数
private String nettyStatus; // Netty状态
}

/**
* Netty性能信息实体类
*/
@Data
public class NettyPerformanceInfo {
private Double throughput; // 吞吐量
private Double latency; // 延迟
private Long channelCount; // Channel数量
private Long pendingTasks; // 待处理任务数
private Long completedTasks; // 已完成任务数
private Double taskCompletionRate; // 任务完成率
private Long directMemoryUsed; // 直接内存使用量
private Long heapMemoryUsed; // 堆内存使用量
private Double cpuUsage; // CPU使用率
private String performanceStatus; // 性能状态
}

4. Netty监控Handler

4.1 Netty监控Handler

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
/**
* Netty监控Handler
* 负责监控Netty连接和消息处理
*/
public class NettyMonitorHandler extends ChannelInboundHandlerAdapter {

private static final Logger log = LoggerFactory.getLogger(NettyMonitorHandler.class);

private AtomicLong bytesReceived = new AtomicLong(0);
private AtomicLong bytesSent = new AtomicLong(0);
private AtomicLong messagesReceived = new AtomicLong(0);
private AtomicLong messagesSent = new AtomicLong(0);

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
// 连接建立时增加活跃连接数
NettyMonitorService.incrementActiveConnections();
NettyMonitorService.incrementTotalConnections();

log.info("客户端连接建立: {}", ctx.channel().remoteAddress());

super.channelActive(ctx);
}

@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// 连接断开时减少活跃连接数
NettyMonitorService.decrementActiveConnections();

log.info("客户端连接断开: {}", ctx.channel().remoteAddress());

super.channelInactive(ctx);
}

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
// 统计接收的数据
if (msg instanceof ByteBuf) {
ByteBuf byteBuf = (ByteBuf) msg;
bytesReceived.addAndGet(byteBuf.readableBytes());
messagesReceived.incrementAndGet();

// 更新全局统计
NettyMonitorService.addBytesReceived(byteBuf.readableBytes());
NettyMonitorService.incrementMessagesReceived();
}

log.debug("接收到消息: {}", msg);

} catch (Exception e) {
log.error("处理接收消息失败: {}", e.getMessage(), e);
}

super.channelRead(ctx, msg);
}

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
try {
// 统计发送的数据
if (msg instanceof ByteBuf) {
ByteBuf byteBuf = (ByteBuf) msg;
bytesSent.addAndGet(byteBuf.readableBytes());
messagesSent.incrementAndGet();

// 更新全局统计
NettyMonitorService.addBytesSent(byteBuf.readableBytes());
NettyMonitorService.incrementMessagesSent();
}

log.debug("发送消息: {}", msg);

} catch (Exception e) {
log.error("处理发送消息失败: {}", e.getMessage(), e);
}

super.write(ctx, msg, promise);
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("Netty连接异常: {}", cause.getMessage(), cause);

// 关闭连接
ctx.close();
}
}

/**
* Netty业务Handler
* 负责处理业务逻辑
*/
public class NettyBusinessHandler extends ChannelInboundHandlerAdapter {

private static final Logger log = LoggerFactory.getLogger(NettyBusinessHandler.class);

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
try {
// 处理业务逻辑
String response = processBusinessLogic(msg);

// 发送响应
ByteBuf responseBuf = Unpooled.copiedBuffer(response, CharsetUtil.UTF_8);
ctx.writeAndFlush(responseBuf);

} catch (Exception e) {
log.error("处理业务逻辑失败: {}", e.getMessage(), e);

// 发送错误响应
String errorResponse = "ERROR: " + e.getMessage();
ByteBuf errorBuf = Unpooled.copiedBuffer(errorResponse, CharsetUtil.UTF_8);
ctx.writeAndFlush(errorBuf);
}
}

/**
* 处理业务逻辑
*/
private String processBusinessLogic(Object msg) {
// 模拟业务处理
try {
Thread.sleep(10); // 模拟处理时间
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}

return "OK: " + msg.toString();
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
log.error("业务处理异常: {}", cause.getMessage(), cause);

// 关闭连接
ctx.close();
}
}

5. 总结

本文详细介绍了Netty网络框架运维监控与优化的完整解决方案,包括:

5.1 核心技术点

  1. 连接监控: 实时监控Netty连接数和状态
  2. 性能监控: 监控吞吐量、延迟、CPU使用率
  3. 内存管理: 监控堆内存和直接内存使用
  4. 线程池优化: 优化EventLoop线程池配置
  5. 告警通知: 多级告警、智能通知

5.2 架构优势

  1. 实时监控: 5秒间隔的实时Netty数据采集
  2. 智能告警: 基于阈值的智能告警机制
  3. 性能优化: 自动化的Netty性能优化
  4. 多维度分析: 连接数、性能、内存等多维度分析

5.3 最佳实践

  1. 监控策略: 设置合理的Netty监控阈值
  2. 优化策略: 根据性能情况执行针对性优化
  3. 内存管理: 合理配置Netty内存参数
  4. 预防措施: 提前预防Netty性能问题

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