Play框架的Netty Http服务器实现
2017-09-25 18:44
513 查看
Play使用Bootstrap初始化服务器Channel,设置Channel类,EventLoop和Handler等。 (Play没有使用专为服务器准备的ServerBoostrap,而是使用更加通用的Bootstrap。这样就可以自己安排对客户端Channel的设置)
private def bind(address: InetSocketAddress): (Channel, Source[Channel, _]) = {
val serverChannelEventLoop = eventLoop.next
// Watches for channel events, and pushes them through a reactive streams publisher.
val channelPublisher = new HandlerPublisher(serverChannelEventLoop, classOf[Channel])
val channelClass = transport match {
case Native => classOf[EpollServerSocketChannel]
case Jdk => classOf[NioServerSocketChannel]
}
//使用Bootstrap创建服务器Channel
val bootstrap = new Bootstrap()
.channel(channelClass)
.group(serverChannelEventLoop)
.option(ChannelOption.AUTO_READ, java.lang.Boolean.FALSE) // publisher does ctx.read()
.handler(channelPublisher)
.localAddress(address)
setOptions(bootstrap.option, nettyConfig.getConfig("option"))
val channel = bootstrap.bind.await().channel()
//将服务器Channel收集到allChannels中
allChannels.add(channel)
//返回服务器Channel, 和Akka Stream Source
(channel, Source.fromPublisher(channelPublisher))
}
当有新的客户端连接服务器时,服务器Channel上的Handler HandlerPublisher会读取到新连接的客户端Channel,然后将客服端Channel发布到到Akka Stream。在Akka Stream Sink中进行客户端Pipeline的设置和Event loop的绑定,并将所有的客户端Channel收集到NettyServer.allChannels属性中。
private def bind(address: InetSocketAddress): (Channel, Source[Channel, _]) = {
val serverChannelEventLoop = eventLoop.next
// Watches for channel events, and pushes them through a reactive streams publisher.
val channelPublisher = new HandlerPublisher(serverChannelEventLoop, classOf[Channel])
val channelClass = transport match {
case Native => classOf[EpollServerSocketChannel]
case Jdk => classOf[NioServerSocketChannel]
}
//使用Bootstrap创建服务器Channel
val bootstrap = new Bootstrap()
.channel(channelClass)
.group(serverChannelEventLoop)
.option(ChannelOption.AUTO_READ, java.lang.Boolean.FALSE) // publisher does ctx.read()
.handler(channelPublisher)
.localAddress(address)
setOptions(bootstrap.option, nettyConfig.getConfig("option"))
val channel = bootstrap.bind.await().channel()
//将服务器Channel收集到allChannels中
allChannels.add(channel)
//返回服务器Channel, 和Akka Stream Source
(channel, Source.fromPublisher(channelPublisher))
}
当有新的客户端连接服务器时,服务器Channel上的Handler HandlerPublisher会读取到新连接的客户端Channel,然后将客服端Channel发布到到Akka Stream。在Akka Stream Sink中进行客户端Pipeline的设置和Event loop的绑定,并将所有的客户端Channel收集到NettyServer.allChannels属性中。
相关文章推荐
- Netty框架的简单使用,实现socket通讯
- Netty+Zookeeper实现一个类似Dubbo的RPC框架
- 用Netty实现的简单HTTP服务器
- scala play框架的idea 实现
- 基于Netty和Zookeeper实现RPC框架
- Netty实现简单HTTP服务器
- 基于Netty的RPC简单框架实现(五):功能测试与性能测试
- netty实现http服务器
- Netty学习——用Netty实现一个简单的Http服务器
- websocket(三) 进阶!netty框架实现websocket达到高并发
- 基于Netty的RPC简单框架实现(三):Kryo实现序列化
- summercool-hsf(基于Netty实现的RPC框架,已经应用国内某移动互联网公司)
- netty简化框架实现
- netty框架的udp方式的简单实现
- 通过 Spring + Netty + Protostuff + ZooKeeper 实现了一个轻量级 RPC 框架
- Netty通信框架Java实现小记
- Netty实现高性能的HTTP服务器
- Netty框架基于UDP实战(一):局域网扫描功能的实现
- Netty+Zookeeper实现一个类似Dubbo的RPC框架
- play框架orm层实现