编写net.reg文件(双击导入注册表,并重新启动机器 ),内容如下:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters]
"ForwardBroadcasts"=dword:00000000
"IPEnableRouter"=dword:00000000
"UseDomainNameDevolution"=dword:00000001
"EnableICMPRedirect"=dword:00000000
"DeadGWDetectDefault"=dword:00000000
"DontAddDefaultGatewayDefault"=dword:00000000
"TcpTimedWaitDelay"=dword:0000001e
"SynAttackProtect"=dword:00000002
"TcpMaxPortsExhausted"=dword:00000005
"TcpMaxHalfOpen"=dword:000001f4
"TcpMaxHalfOpenRetried"=dword:00000190
"MaxUserPort"=dword:0000fffe
"TcpMaxConnectResponseRetransmissions"=dword:00000002
"TcpMaxDataRetransmissions"=dword:00000002
"KeepAliveTime"=dword:000493e0
以上配置调整了最大可用端口,并启动了半连接数安全设置,使服务器具有一定的防攻击能力,因此在客户端测试连接时,如果同时并发数过多,可能会导致部分连接失败。
使用官方示例 :
https://github.com/mrniko/netty-socketio-demo
并做以下修改:
EventChatLauncher
public class EventChatLauncher {
private static final Logger log = LoggerFactory.getLogger(EventChatLauncher.class);
public static void main(String[] args) throws InterruptedException {
Configuration config = new Configuration();
//config.setHostname("localhost");
config.setPort(9092);
final SocketIOServer server = new SocketIOServer(config);
server.addEventListener("chatevent", ChatObject.class, new DataListener() {
@Override
public void onData(SocketIOClient client, ChatObject data, AckRequest ackRequest) {
//server.getBroadcastOperations().sendEvent("chatevent", data);
client.sendEvent("chatevent", data);
log.warn(server.getAllClients().size()+":"+data.getUserName()+":"+data.getMessage());
}
});
server.start();
Thread.sleep(Integer.MAX_VALUE);
server.stop();
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.corundumstudio.socketiogroupId>
<artifactId>netty-socketioartifactId>
<version>1.0version>
<packaging>jarpackaging>
<inceptionYear>2012inceptionYear>
<properties>
<main.class>com.corundumstudio.socketio.demo.EventChatLaunchermain.class>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<licenses>
<license>
<name>Apache v2name>
<url>http://www.apache.org/licenses/LICENSE-2.0.htmlurl>
<distribution>manualdistribution>
license>
licenses>
<developers>
<developer>
<id>mrnikoid>
<name>Nikita Koksharovname>
<roles>
<role>Architectrole>
<role>Developerrole>
roles>
<timezone>+4timezone>
developer>
developers>
<dependencies>
<dependency>
<groupId>com.corundumstudio.socketiogroupId>
<artifactId>netty-socketioartifactId>
<version>1.7.7version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.7version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojogroupId>
<artifactId>exec-maven-pluginartifactId>
<version>1.3.2version>
<executions>
<execution>
<goals>
<goal>javagoal>
goals>
execution>
executions>
<configuration>
<mainClass>${main.class}mainClass>
configuration>
plugin>
<plugin>
<artifactId>maven-compiler-pluginartifactId>
<version>3.2version>
<configuration>
<source>1.7source>
<target>1.7target>
<encoding>UTF-8encoding>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-shade-pluginartifactId>
<version>2.4.3version>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>shadegoal>
goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${main.class}mainClass>
transformer>
transformers>
configuration>
execution>
executions>
plugin>
plugins>
build>
project>
增加src/main/resources/log4j.properties
#开启DEBUG LOG会严重影响程序性能
log4j.rootLogger=WARN,FILE
log4j.appender.STDOUT=org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout=org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.conversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %-5p\: %m\t(@%t)\n
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.File=./app.log
log4j.appender.FILE.Append= true
log4j.appender.FILE.Encoding=utf-8
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} %-5p\: %m\t(@%t)\n
#开启DEBUG LOG会严重影响程序性能
log4j.category.com.corundumstudio.socketio.demo=WARN
#Disable other log
#log4j.logger.org.springframework=OFF
#log4j.logger.org.apache.struts2=OFF
#log4j.logger.com.opensymphony.xwork2=OFF
#log4j.logger.com.mybatis=OFF
#log4j.logger.org.hibernate=OFF
使用node做测试,本次测试使用的node版为5.12.0 64位
node安装请参数其他文档
测试脚本test.js
var count = 0;
var max = 2000;
var sockets = new Array();
for(var i=0;ifunction socketClient(id) {
var userName = 'user' + id;
var message = 'message' + id;
var io = require('socket.io-client');
var socket = io.connect('http://192.168.1.199:9092',{'force new connection': true});
socket.on('connect', function() {
sockets[id]=socket;
count++;
console.log('id:'+id+':connect count:'+count);
//socket.emit('chatevent', {userName: userName, message: message});
});
}
在测试脚本目录下执行以下命令:
npm install socket.io-client
通过mvn clean package打包,在target下生成:netty-socketio-1.0.jar
编写启动批处理:
start-server.bat
java -server -Xms1024m -Xmx2048m -XX:PermSize=128m -XX:MaxPermSize=256m -jar netty-socketio-1.0.jar
运行批处理程序:start-server.bat
编写启动批处理:
start-client.bat
node test.js
运行批处理程序:start-client.bat
每次建立2000个连接,在连接建立后发送一条消息,服务器回复同样消息。
可重复运行以上批处理文件。
在连接数达到2万左右时,服务器CPU占用已经非常高,经常在90%以上
在连接数达到6万左右时,服务器CPU长期维持在90%以上,但仍能继续连接客户端,在6.3左右连接时,已经出现连接缓慢及失败现象。需要使用多网卡及更好的CPU