2017-06-14 18 views
13

के बीच मतभेद क्या कोई Akka HTTP और Netty के बीच प्रमुख अंतरों को समझा सकता है? नेटटी एफ़टीपी जैसे अन्य प्रोटोकॉल भी प्रदान करता है। अकाका HTTP का उपयोग स्कैला और जावा में किया जा सकता है और build on the actor model है। लेकिन इसके अलावा दोनों अतुल्यकालिक हैं। मैं अकका HTTP और नेटटी कब उपयोग करूंगा? दोनों के लिए सामान्य उपयोग के मामले क्या हैं?अक्का HTTP और नेटटी

कोडिंग शैली

के ले netty के discard server example जो संभवतः सबसे आसान उदाहरण है यह देखते हुए कि यह दस्तावेज में पहला है दो:

उत्तर

2

यहाँ है कि मैं क्या प्राथमिक contrastable क्षेत्रों के रूप में देखते हैं।

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

netty के लिए यह बहुत अधिक वर्बोज़ है:

akka-http के लिए यह अपेक्षाकृत आसान होता है

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 है, जो जटिल अनुरोध हैंडलिंग तर्क के लिए एक डीएसएल प्रदान करता है। मान लीजिए, उदाहरण के लिए, हम 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") 
    } 

यह कार्यक्षमता netty के भीतर मौजूद नहीं है।

एक आखिरी आइटम मैं ध्यान दें जाएगा स्ट्रीमिंग स्ट्रीमिंग के आसपास है। अक्का-http akka-stream पर आधारित है। इसलिए, अक्का-http अनुरोध संस्थाओं की स्ट्रीमिंग प्रकृति को अच्छी तरह से संभालती है। netty के 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) 
    } 
} 
संबंधित मुद्दे