2017-06-14 13 views
13

Ai đó có thể giải thích sự khác biệt chính giữa Akka HTTPNetty? Netty cũng cung cấp các giao thức khác như FTP. Akka HTTP có thể được sử dụng trong Scala và Java và là build on the actor model. Nhưng ngoài việc này cả hai đều không đồng bộ. Khi nào tôi sử dụng Akka HTTP và khi Netty? Các trường hợp sử dụng điển hình cho cả hai là gì?Sự khác biệt giữa Akka HTTP và Netty

Trả lời

2

Dưới đây là những gì tôi nhìn thấy như các lĩnh vực contrastable chính:

Mã hóa Phong cách

Chúng ta hãy Netty của discard server example mà có lẽ là ví dụ đơn giản nhất cho rằng đó là người đầu tiên trong tài liệu.

Đối akka-http này là tương đối đơn giản:

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) 
} 

Đối Netty này là nhiều hơn nữa verbose:

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(); 
    } 
} 

Chỉ

Một trong AKKA-http mạnh lớn nhất, trong tôi ý kiến, là Directives, cung cấp một DSL cho logic xử lý yêu cầu phức tạp. Giả sử, ví dụ, chúng tôi muốn trả lời bằng một thông điệp cho các yêu cầu GETPUT và một thông báo khác cho tất cả các phương thức yêu cầu khác. Đây là rất dễ sử dụng chỉ thị:

val route = 
    (get | put) { 
    complete("You sent a GET or PUT") 
    } ~ 
    complete("Shame shame") 

Của nếu bạn muốn nhận được một mục theo thứ tự và số lượng từ một con đường yêu cầu:

val route = 
    path("order"/Segment/IntNumber) { (item, qty) => 
    complete(s"Your order: item: $item quantity: $qty") 
    } 

Chức năng này không tồn tại trong vòng Netty.

streaming

Một mục cuối cùng tôi sẽ lưu ý là khoảng streaming. akka-http dựa trên akka-stream. Do đó, akka-http xử lý tính chất phát trực tiếp của các thực thể yêu cầu. Hãy Looking Into the Received Data dụ Netty của, cho AKKA này trông giống như

//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 phải xử lý các vấn đề tương tự với bộ đệm byte và vòng lặp 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) 
    } 
} 
Các vấn đề liên quan