Netty monitors traffic through MBean

original
2016/11/19 16:03
Reading 8.1K

Netty traffic statistics
Netty provides a traffic packet for traffic statistics, as shown in the following figure:

It provides a global GlobalTrafficShapingHandler and a ChannelTrafficShapingHandler for channels respectively, and provides a TrafficCounter to record real-time traffic statistics.

Simple use:

 ChannelPipeline p = socketChannel.pipeline(); p.addLast(new GlobalTrafficShapingHandler(Executors.newScheduledThreadPool(1), 1000));

The method trafficCounter () provided by GlobalTrafficShapingHandler can be used to obtain the TrafficCounter object,
TrafficCounter provides some common methods:

 /** * @return the Read Throughput in bytes/s computes in the last check interval. */ public long lastReadThroughput() { return lastReadThroughput; } /** * @return the Write Throughput in bytes/s computes in the last check interval. */ public long lastWriteThroughput() { return lastWriteThroughput; } /** * @return the number of bytes read during the last check Interval. */ public long lastReadBytes() { return lastReadBytes; } /** * @return the number of bytes written during the last check Interval. */ public long lastWrittenBytes() { return lastWrittenBytes; } /** * @return the current number of bytes read since the last checkInterval. */ public long currentReadBytes() { return currentReadBytes.get(); } /** * @return the current number of bytes written since the last check Interval. */ public long currentWrittenBytes() { return currentWrittenBytes.get(); } /** * @return the Time in millisecond of the last check as of System.currentTimeMillis(). */ public long lastTime() { return lastTime.get(); } /** * @return the cumulativeWrittenBytes */ public long cumulativeWrittenBytes() { return cumulativeWrittenBytes.get(); } /** * @return the cumulativeReadBytes */ public long cumulativeReadBytes() { return cumulativeReadBytes.get(); } /** * @return the lastCumulativeTime in millisecond as of System.currentTimeMillis() * when the cumulative counters were reset to 0. */ public long lastCumulativeTime() { return lastCumulativeTime; } /** * @return the realWrittenBytes */ public AtomicLong getRealWrittenBytes() { return realWrittenBytes; } /** * @return the realWriteThroughput */ public long getRealWriteThroughput() { return realWriteThroughput; }

MBean of JMX

JMX is Java Management Extensions, and MBeans are Managed Beans
An MBean is a managed Java object, similar to a JavaBean. A device, an application, or any resource can be represented as an MBean, MBeans will expose an interface, which can read or write properties in some objects. Generally, an MBean needs to define an interface, ending with an MBean, such as the following MBeans used to count the traffic of netty:

 public interface IoAcceptorStatMBean { public long getWrittenBytesThroughput(); public long getReadBytesThroughput(); }

Implementation of MBean:

 public class IoAcceptorStat implements IoAcceptorStatMBean { @Override public long getWrittenBytesThroughput() { return SocksServer.getInstance().getTrafficCounter() .lastWriteThroughput(); } @Override public long getReadBytesThroughput() { return SocksServer.getInstance().getTrafficCounter() .lastReadThroughput(); } }

Next, we need to manage the MBean we defined

 MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); IoAcceptorStat mbean = new IoAcceptorStat(); try { ObjectName acceptorName = new ObjectName(mbean.getClass().getPackage().getName() + ":type=IoAcceptorStat"); mBeanServer.registerMBean(mbean, acceptorName); } catch (Exception e) { e.printStackTrace(); }

1. Apply for an MBeanServer object from ManagementFactory
2. Given an identification ObjectName, it uses the form (package name: type=class name)
3. The mbean is registered through the registerMBean method, and the ObjectName is used as the identifier

Next, you can use jconsole, the tool provided by JDK, to view the MBean

Monitor the traffic of netty

The above code is part of the project shadowlocks netty, which is a client of shadowlocks implemented based on netty,
More information: https://my.oschina.net/OutOfMemory/blog/744475
github: https://github.com/ksfzhaohui/shadowsocks-netty

Start the shadowlocks netty program, use jconsole to connect, open YouTube, open a video at random, and switch to the MBean tab of Jconsole to monitor in real time

The write speed of the monitored agent is actually the speed of the browser to download data

The read speed of the monitored agent is actually the upload speed of the browser

summary

It is necessary to monitor the application traffic in the development phase. You can roughly estimate the application bandwidth, and at the same time, it is convenient for us to check whether the specified standards are met

Personal blog: codingo.xyz

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
twenty-nine Collection
zero fabulous
 Back to top
Top