学习第41节
Netty 可视化方案:
• Console 日志定时输出
• JMX 实时展示
• ELKK、TIG、etc
@ChannelHandler.Sharable
public class MetricsHandler extends ChannelDuplexHandler {
private AtomicLong totalConnectionNumber = new AtomicLong();
{
MetricRegistry metricRegistry = new MetricRegistry();
metricRegistry.register("totalConnectionNumber", new Gauge<Long>() {
@Override
public Long getValue() {
return totalConnectionNumber.longValue();
}
});
ConsoleReporter consoleReporter = ConsoleReporter.forRegistry(metricRegistry).build();
consoleReporter.start(10, TimeUnit.SECONDS);
JmxReporter jmxReporter = JmxReporter.forRegistry(metricRegistry).build();
jmxReporter.start();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
totalConnectionNumber.incrementAndGet();
super.channelActive(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
totalConnectionNumber.decrementAndGet();
super.channelInactive(ctx);
}
}
Console输出:
JMX输出:
注:内容参考极客时间相关课程