building scalable network applications with netty

33
Building scalable network applications with Netty Jaap ter Woerds @jaaptw XMS pin: EBDDFF9A

Upload: nljug

Post on 10-Nov-2014

612 views

Category:

Technology


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Building scalable network applications with Netty

Building scalable network applications with Netty

Jaap ter Woerds@jaaptw

XMS pin: EBDDFF9A

Page 2: Building scalable network applications with Netty

The trouble with synchronous IO

Page 3: Building scalable network applications with Netty

The trouble with synchronous IO

Page 4: Building scalable network applications with Netty

The trouble with synchronous IO

Page 5: Building scalable network applications with Netty

Creating your custom server

Page 6: Building scalable network applications with Netty

Creating your custom server

Page 7: Building scalable network applications with Netty

Netty?

Unified, asynchronous programming model. All IO operations return immediately.

Page 8: Building scalable network applications with Netty

Netty?

Channel channel =..channel.write(myMsg).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if(future.isSuccess()){ // Business as usual } else{ // Handle failure } }});

Page 9: Building scalable network applications with Netty

Netty?

Networking application framework with a modular architecture and quite some ready reusable

components.

Page 10: Building scalable network applications with Netty

When should I care about this?

Custom servers / networking applications / proxies

High concurrency

Page 11: Building scalable network applications with Netty

Netty concepts

Page 12: Building scalable network applications with Netty

Netty concepts

ByteBuf random accessable sequence of bytes

Abstraction over plain byte arrays and NIO buffers

Page 13: Building scalable network applications with Netty

Netty concepts

ByteBuf Use: encoding and decoding data

ByteBuf b = .b.writeInt(3).writeBoolean(true);

Page 14: Building scalable network applications with Netty

Netty concepts

Channel Link ('connection') to a resource that provides IO operations like read /

write

Page 15: Building scalable network applications with Netty

Netty concepts

ChannelChannel c = ..;

c.write(myMsg).addListener(CLOSE);

Page 16: Building scalable network applications with Netty

Netty concepts

ChannelHandler IO event listener,handles outbound and / or inbound events.

Page 17: Building scalable network applications with Netty

Netty concepts

ChannelInboundHandler Defines listener methods for incoming IO events

Page 18: Building scalable network applications with Netty

Netty concepts

ChannelInboundHandler

channelActive(ChannelHandlerContext ctx)channelRead(ChannelHandlerContext ctx, Object msg)

channelReadComplete(ChannelHandlerContext ctx)channelInactive(ChannelHandlerContext ctx)

....

Page 19: Building scalable network applications with Netty

Netty concepts

ChannelOutboundHandler Defines listener methods for outgoing IO events

Page 20: Building scalable network applications with Netty

Netty concepts

ChannelOutboundHandler

write(ChannelHandlerContext ctx, Object msg, ChannelPromise..)flush(ChannelHandlerContext ctx)

Page 21: Building scalable network applications with Netty

Netty concepts

ChannelPipelineA List of ChannelHandlers that process incoming and outgoing events

in a specific order

Page 22: Building scalable network applications with Netty

Netty concepts

ChannelInitializerConfigures the pipeline for a Channel. In fact, a special case of

ChannelInboundHandler which creates the handlers for a channel whenerver a new channel is registered.

Page 23: Building scalable network applications with Netty

Netty concepts

ChannelInitializerConfigures the pipeline for a Channel. In fact, a special case of

ChannelInboundHandler which creates the handlers for a channel whenerver a new channel is registered.

Page 24: Building scalable network applications with Netty

Netty concepts

ChannelInitializer @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Decoders pipeline.addLast("frameDecoder", new LineBasedFrameDecoder(MAX_LENGTH)); pipeline.addLast("stringDecoder", STRING_DECODER); pipeline.addLast("moderatorHandler", moderatorHandler); pipeline.addLast("chatServerHandler", chatServerHandler); // Encoder pipeline.addLast("stringEncoder", STRING_ENCODER); }

Page 25: Building scalable network applications with Netty

Netty concepts

Bootstrap

EventLoopGroup bossGroup = new NioEventLoopGroup();EventLoopGroup workerGroup = new NioEventLoopGroup();ServerBootstrap b = new ServerBootstrap();b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, BACKLOG) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(channelInitializer);ChannelFuture f = b.bind(port).sync();f.channel().closeFuture().sync();

Page 26: Building scalable network applications with Netty

Netty architecture

Chain of responsibilityIncoming data flow through the ChannelPipeline

- Framing ByteBuf- Decode frames

- Decode frames in DTOs/POJOs

Page 27: Building scalable network applications with Netty

Netty architecture

Page 28: Building scalable network applications with Netty

Netty components

Out of the box support for:Streaming compression Zlib

SSL stream encryptionHTTP

WebSocketsProtocolBuffer

Page 29: Building scalable network applications with Netty

XMS server protocol layer

Page 30: Building scalable network applications with Netty

Example SSE / CORS

Page 31: Building scalable network applications with Netty

Example SSE / CORS

In the last handler we deal with normal POJO and handle an incoming request without actually caring about the connection layer!

@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) { IncomingMessagePojo m = (IncomingMessagePojo) msg; handle(m);// Login / Send a text message/ Handle create group}

Page 32: Building scalable network applications with Netty

Example SSE / CORS

Page 33: Building scalable network applications with Netty

Thank you!

Example code:https://github.com/jaapterwoerds/jfall-netty4

Further reading:

https://nettio.io

http://xms.mehttp://tech.ebuddy.com