我用这个例子 进行性能测试
有人说netty的表现如此之快.它可以处理1,00,000+并发请求/秒(请查看以下链接)
http://www.jboss.org/netty/performance/20090303-mheath.html
http://www.jboss.org/netty/performance/20090607-asalihefendic.html
但是当我尝试这个例子时,它只给我107个req/sec,同时有1000个请求
ab -n 10000 -c 1000 http://localhost:8080/
Server Software:
Server Hostname: localhost
Server Port: 8080
Document Path: /
Document Length: 230 bytes
Concurrency Level: 1000
Time taken for tests: 92.784 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Total transferred: 2900000 bytes
HTML transferred: 2300000 bytes
Requests per second: 107.78 [#/sec] (mean)
Time per request: 9278.431 [ms] (mean)
Time per request: 9.278 [ms] (mean, across all concurrent requests)
Transfer rate: 30.52 [Kbytes/sec] …Run Code Online (Sandbox Code Playgroud) 我需要在 Android 平台上开发一个应用程序,使客户端能够与服务器通信/请求命令,并使服务器能够响应客户端的请求。
我读过 Netty 并且我想将它实现到我的项目中,但我是套接字编程的新手,它是用 Java 编写的,所以我想将它实现到 Android 并不难。我正在网上寻找示例,但我在为 Android 找到一个很好的示例时迷失了方向。
这里有人可以给我示例/ s开始或告诉我如何在我的应用程序中实现这些功能?
我有以下代码来回显 Netty 收到的输入-
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
public class MyHandler extends SimpleChannelInboundHandler{
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
ByteBuf in = (ByteBuf) msg;
try {
System.out.println(in.toString(io.netty.util.CharsetUtil.US_ASCII));
} finally {
in.release();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我调用它如下-
try {
ServerBootstrap b = new ServerBootstrap(); // (2)
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(new ChannelInitializer<SocketChannel>() { // (4)
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new MyHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 Apache HttpClient(流畅的 API)将数据 POST 到 netty 服务器。
我尝试了一些变体,我将在这里放两个:
客户:
Request.Post(uri).bodyString("content value", ContentType.TEXT_PLAIN).useExpectContinue().execute().returnContent().asString();
Run Code Online (Sandbox Code Playgroud)
服务器:
final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), req);
System.out.println(decoder.getBodyHttpDatas().size());
Run Code Online (Sandbox Code Playgroud)
调用 getBodyHttpDatas() 会抛出:
io.netty.handler.codec.http.multipart.HttpPostRequestDecoder$NotEnoughDataDecoderException
Run Code Online (Sandbox Code Playgroud)
客户:
Request.Post(uri).bodyForm(new BasicNameValuePair("value", "content value")).useExpectContinue().execute().returnContent().asString();
Run Code Online (Sandbox Code Playgroud)
服务器:
final HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(new DefaultHttpDataFactory(false), req);
final InterfaceHttpData data1 = decoder.getBodyHttpData("value");
while (decoder.hasNext()) {
final InterfaceHttpData data = decoder.next();
if (data != null) {
try {
Attribute attribute = (Attribute) data;
System.out.println(attribute.getValue());
} finally {
data.release();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这不会打印任何输出——decoder.hasNext() 是假的。
我们在 Netty 之上实现 SSL。但目前的设计存在缺陷。如果失败,客户端将重试连接服务器。这在网络或服务器负载过重的问题中是需要的。但错误的客户端凭据会导致持续失败。
有一些解决方案:
不幸的是我不知道如何使用 Netty 来实现这一点。几个问题:
请在这里帮忙。
使用 Java 8 和 Netty 4.1.1.Final,我原以为下面的测试用例会成功,但它超时了。我不理解 wrt nettys 事件循环和任务调度是怎么回事?
public class SchedulerTest {
CountDownLatch latch;
TimerHandler handler;
static class TimerHandler extends ChannelInboundHandlerAdapter {
ChannelHandlerContext ctx;
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
super.channelActive(ctx);
this.ctx = ctx;
}
private void timeout(final long ms) {
ctx.executor().schedule(() -> {
ctx.fireUserEventTriggered(ms);
}, ms, TimeUnit.MILLISECONDS);
}
}
static class TimeoutReactor extends ChannelInboundHandlerAdapter {
CountDownLatch latch;
public TimeoutReactor(CountDownLatch latch) {
super();
this.latch = latch;
}
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { …Run Code Online (Sandbox Code Playgroud) 例如,如果我想构建一个websocket服务器,我想知道该initChannel方法中应该放什么。然后我在netty的源码中找到了websocket示例,其中我需要执行以下操作:
public void initChannel(final SocketChannel ch) throws Exception {
ch.pipeline().addLast(
new HttpRequestDecoder(),
new HttpObjectAggregator(65536),
new HttpResponseEncoder(),
new WebSocketServerProtocolHandler("/websocket"),
new CustomTextFrameHandler());
}
Run Code Online (Sandbox Code Playgroud)
但我不知道为什么需要按这样的顺序放置对象。在描述中HttpObjectAggregator我发现了这样的内容:
Be aware that you need to have the {@link HttpResponseEncoder} or {@link HttpRequestEncoder} before the {@link HttpObjectAggregator} in the {@link ChannelPipeline}.
但在上面的代码中,HttpObjectAggregator对象位于HttpResponseEncoder对象之前。我很困惑。我怎么知道我正在以正确的顺序放置这些对象?
我正在尝试学习 netty 通道处理程序,并且我在本教程中遇到了困难。
在文件中NettyServer.java,作者提到了将通道处理程序注册到通道管道。
ch.pipeline().addLast(
new RequestDecoder(),
new ResponseDataEncoder(),
new ProcessingHandler());
Run Code Online (Sandbox Code Playgroud)
这个顺序让我有点困惑。我会按如下方式注册订单
响应被解码是正确的顺序。
ch.pipeline().addLast(
new RequestDecoder(),
new ProcessingHandler(),
new ResponseDataEncoder());
Run Code Online (Sandbox Code Playgroud)Netty 中不同排序背后的原因是什么?
我正在尝试使用 netty 4.1.16.Final 创建简单的 HTTP 服务器。
以下是 HTTP 服务器的代码 -
EventLoopGroup masterGroup = new NioEventLoopGroup();
EventLoopGroup slaveGroup = new NioEventLoopGroup();
final ServerBootstrap bootstrap =
new ServerBootstrap()
.group(masterGroup, slaveGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ch.pipeline().addLast("codec", new HttpServerCodec());
ch.pipeline().addLast("aggregator",
new HttpObjectAggregator(512 * 1024));
ch.pipeline().addLast("request",
new HTTPSimpleChannelInboundHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
channel = bootstrap.bind(8080).sync();
Run Code Online (Sandbox Code Playgroud)
HTTP 处理程序类的代码HTTPSimpleChannelInboundHandler如下 -
public class HTTPSimpleChannelInboundHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) {
HttpResponseStatus responseStatus = OK;
FullHttpResponse …Run Code Online (Sandbox Code Playgroud) 在 Netty 中,ChannelHandlerContext.fireChannelRead()当您想要将控制权移交给下一个处理程序时,您可以调用您的处理程序。这些文档的编写风格有点尴尬,有时很难获得预期的要点,这常常让我想知道一个方法应该做什么(即使在查阅了 Javadoc 之后)。对于这个方法,我特别想知道它应该指示什么样的事件。Javadoc 说
Channel 收到一条消息
从使用它的处理程序的角度来看,这确实没有意义。当然频道已经收到消息了;处理程序已经知道,既然它已经收到了它,那为什么还要告诉上下文呢?这让我认为调用这个方法有不同的含义,但是:
对我来说,使用类似的方法通知上下文会更有意义handlerFinishedRead()。然后fireChannelRead()保留给管道调用。
有人可以解释一下netty方法究竟是ChannelHandlerContext flush()做什么的吗?它会立即通过网络发送所有消息,从而有效地绕过管道中的任何处理程序停止任何定义的处理吗?