Skip to main content

获取客户端IP的流量数据

ip的监控数据定义在IpStat中

    private Date                start               = new Date();
/**
* 当前统计了多久,单位:毫秒
*/
private long duration;
/**
* 时长类型,单位:秒,譬如60,3600等
*/
private Long durationType;
/**
* 客户端ip
*/
private String ip;

/**
* 解码异常的次数
*/
private AtomicInteger decodeErrorCount = new AtomicInteger();
/**
* 收到该IP连接请求的次数
*/
private AtomicInteger requestCount = new AtomicInteger();
/**
* 本IP已发送的字节数
*/
private AtomicLong sentBytes = new AtomicLong();
/**
* 本IP已发送的packet数
*/
private AtomicLong sentPackets = new AtomicLong();
/**
* 本IP已处理的字节数
*/
private AtomicLong handledBytes = new AtomicLong();
/**
* 本IP已处理的packet数
*/
private AtomicLong handledPackets = new AtomicLong();
/**
* 处理消息包耗时,单位:毫秒
*/
private AtomicLong handledPacketCosts = new AtomicLong();
/**
* 本IP已接收的字节数
*/
private AtomicLong receivedBytes = new AtomicLong();
/**
* 本IP已接收了多少次TCP数据包
*/
private AtomicLong receivedTcps = new AtomicLong();
/**
* 本IP已接收的packet数
*/
private AtomicLong receivedPackets = new AtomicLong();

使用步骤

  • 实现IpStatListener

package org.tio.showcase.websocket.server;

public class ShowcaseIpStatListener implements IpStatListener {
@SuppressWarnings("unused")
private static Logger log = LoggerFactory.getLogger(ShowcaseIpStatListener.class);

public static final ShowcaseIpStatListener me = new ShowcaseIpStatListener();

private ShowcaseIpStatListener() {
}

@Override
public void onExpired(TioConfig tioConfig, IpStat ipStat) {
//在这里把统计数据入库中或日志
// if (log.isInfoEnabled()) {
// log.info("可以把统计数据入库\r\n{}", Json.toFormatedJson(ipStat));
// }
}

@Override
public void onAfterConnected(ChannelContext channelContext, boolean isConnected, boolean isReconnect, IpStat ipStat) throws Exception {
// if (log.isInfoEnabled()) {
// log.info("onAfterConnected\r\n{}", Json.toFormatedJson(ipStat));
// }
}

@Override
public void onDecodeError(ChannelContext channelContext, IpStat ipStat) {
// if (log.isInfoEnabled()) {
// log.info("onDecodeError\r\n{}", Json.toFormatedJson(ipStat));
// }
}

@Override
public void onAfterSent(ChannelContext channelContext, Packet packet, boolean isSentSuccess, IpStat ipStat) throws Exception {
// if (log.isInfoEnabled()) {
// log.info("onAfterSent\r\n{}\r\n{}", packet.logstr(), Json.toFormatedJson(ipStat));
// }
}

@Override
public void onAfterDecoded(ChannelContext channelContext, Packet packet, int packetSize, IpStat ipStat) throws Exception {
// if (log.isInfoEnabled()) {
// log.info("onAfterDecoded\r\n{}\r\n{}", packet.logstr(), Json.toFormatedJson(ipStat));
// }
}

@Override
public void onAfterReceivedBytes(ChannelContext channelContext, int receivedBytes, IpStat ipStat) throws Exception {
// if (log.isInfoEnabled()) {
// log.info("onAfterReceivedBytes\r\n{}", Json.toFormatedJson(ipStat));
// }
}

@Override
public void onAfterHandled(ChannelContext channelContext, Packet packet, IpStat ipStat, long cost) throws Exception {
// if (log.isInfoEnabled()) {
// log.info("onAfterHandled\r\n{}\r\n{}", packet.logstr(), Json.toFormatedJson(ipStat));
// }
}

}

  • 初始化时添加监听器和监控时段
//注意的是:要保证下面两行代码的顺序,不能先addDuration()后setIpStatListener
serverTioConfig.setIpStatListener(ShowcaseIpStatListener.me);
serverTioConfig.ipStats.addDuration(Time.MINUTE_1 * 5);
  • OK了,什么时候拉黑IP以及把监控数据入库都在ShowcaseIpStatListener中实现哦