有人能解释一下Akka HTTP和Netty之间的主要区别吗? Netty也提供其他协议,如FTP。 Akka HTTP可用于Scala和Java,并且是build on the actor model。但除此之外,这两者都是异步的。何时使用Akka HTTP和Netty?两者的典型用例是什么?Akka HTTP和Netty的区别
13
A
回答
2
这里是我所看到的主要contrastable方面:
编码风格
让我们网状的discard server example这大概是最简单的例子给出它的文档中的第一个。
对于akka-http
这是比较简单的:
object WebServer {
def main(args: Array[String]) {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
val route =
extractRequestEntity { entity =>
onComplete(entity.discardBytes(materializer)) { _ =>
case _ => complete(StatusCodes.Ok)
}
}
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
}
对于网状这是更为详细:
public class DiscardServerHandler extends ChannelInboundHandlerAdapter { // (1)
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) { // (2)
// Discard the received data silently.
((ByteBuf) msg).release(); // (3)
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
}
public class DiscardServer {
private int port;
public DiscardServer(int port) {
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
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 DiscardServerHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(port).sync(); // (7)
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new DiscardServer(8080).run();
}
}
指令
一个阿卡-HTTP的最大的优势,在我意见,是Directives,它为复杂的请求处理逻辑提供DSL。例如,假设我们想用GET
和PUT
请求的一条消息进行响应,并针对所有其他请求方法提供另一条消息。这用指令是很容易的:
val route =
(get | put) {
complete("You sent a GET or PUT")
} ~
complete("Shame shame")
,如果你想从一个请求路径的订单项目及数量:
val route =
path("order"/Segment/IntNumber) { (item, qty) =>
complete(s"Your order: item: $item quantity: $qty")
}
此功能不网状中存在。
流
最后一个项目我想说明的是全能流。 akka-http基于akka-stream
。因此,akka-http很好地处理了请求实体的流式性质。以网状的Looking Into the Received Data例如,阿卡这看起来像
//a stream with a Source, intermediate processing steps, and a Sink
val entityToConsole : (RequestEntity) => Future[Done] =
(_ : RequestEntity)
.getDataBytes()
.map(_.utf8String)
.to(Sink.foreach[String](println))
.run()
val route =
extractRequestEntity { entity =>
onComplete(entityToConsole(entity)) { _ =>
case Success(_) => complete(200, "all data written to console")
case Failure(_) => complete(404, "problem writing to console)
}
}
的Netty必须用字节的缓冲区和while循环处理同样的问题:
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
try {
while (in.isReadable()) { // (1)
System.out.print((char) in.readByte());
System.out.flush();
}
} finally {
ReferenceCountUtil.release(msg); // (2)
}
}
相关问题
- 1. HTTP组件,Restlet,Apache Mina和Netty的区别
- 2. akka http配置中的idle-timeout和request timeout有什么区别?
- 3. 区别://和http:\\
- 4. Netty 4.0 ChannelInboundByteHandler和ChannelInboundMessageHandler之间的区别
- 5. netty中`sync`和`await`的区别
- 6. Akka HTTP客户端akka-http akka-http-jackson
- 7. 的Netty ChannelUpstreamHandler和HTTP管道
- 8. Netty 4:处理HTTP和HTTPS
- 9. Apache Spark错误的akka-remote netty版本
- 10. “/ page”和“http://www.domain.com/page”之间的区别
- 11. file:// android_assets /和http://someurl.com/之间的区别?
- 12. Netty中的ChannelBuffer.copy()和ChannelBuffer.duplicate()有什么区别
- 13. REST和HTTP协议有什么区别?
- 14. python urllib2 for http和https工作区别
- 15. NHTTP和HTTP有什么区别?
- 16. NSURLConnection和ASI-HTTP-Request有什么区别?
- 17. Golang的net/http和net/http/fcgi的区别
- 18. http://java.sun.com/jsp/jstl/core和http://java.sun.com/jstl/core之间的区别
- 19. HTTP servlet和SOAP over HTTP之间的区别是什么?
- 20. 网站的http://website.com和http://www.website.com网址有什么区别?
- 21. HTTP查询参数和HTTP表单参数之间的区别?
- 22. Akka Http错误:InvalidContentLengthException
- 23. Akka http连接池
- 24. 将Akka HTTP连接到Akka流
- 25. 如何绑定akka http与akka流?
- 26. 高并发HTTP与Netty和NIO
- 27. 混合Akka 2,Play2-mini框架和HTTP
- 28. akka-http:从http路由发送元素到akka接收器
- 29. HTTP和HTTP REST服务有什么区别?
- 30. HTTP参数和HTTP头文件有什么区别?