Netty源码分析之服务启动】的更多相关文章

本节主要分析server的启动过程. Netty是基于Nio实现的,所以也离不开selector.serverSocketChannel.socketChannel和selectKey等,只不过Netty把这些实现都封装在了底层. 从示例可以看出,一切从ServerBootstrap开始. ServerBootstrap实例中需要两个NioEventLoopGroup实例,分别为boss和work,有不同的分工:1. boss负责请求的accept操作.2. work负责请求的read.writ…
一.首先来看一段服务端的示例代码: public class NettyTestServer { public void bind(int port) throws Exception{ EventLoopGroup bossgroup = new NioEventLoopGroup();//创建BOSS线程组 EventLoopGroup workgroup = new NioEventLoopGroup();//创建WORK线程组 try{ ServerBootstrap b = new S…
一.先来看一下客户端示例代码. public class NettyClientTest { public void connect(int port, String host) throws Exception { EventLoopGroup group = new NioEventLoopGroup();//与服务端不同,客户端只需要一个IO线程组 try { Bootstrap b = new Bootstrap(); b.group(group) .option(ChannelOpti…
Netty服务端启动代码: public final class EchoServer { static final int PORT = Integer.parseInt(System.getProperty("port", "8007")); public static void main(String[] args) throws Exception { // Configure the server. EventLoopGroup bossGroup = n…
Netty源码分析第一章:  Server启动流程 概述: 本章主要讲解server启动的关键步骤, 读者只需要了解server启动的大概逻辑, 知道关键的步骤在哪个类执行即可, 并不需要了解每一步的运作机制, 之后会对每个模块进行深度分析 第一节:服务端初始化 首先看下在我们用户代码中netty的使用最简单的一个demo: //创建boss和worker线程(1) EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGro…
Netty源码分析第一章:Netty启动流程   第三节:服务端channel初始化 回顾上一小节的initAndRegister()方法: final ChannelFuture initAndRegister() { Channel channel = null; try { //创建channel channel = channelFactory.newChannel(); //初始化channel init(channel); } catch (Throwable t) { //忽略非关…
本文接着前两篇文章来讲,主要讲服务端类剩下的部分,我们还是来先看看服务端的代码 /** * Created by chenhao on 2019/9/4. */ public final class SimpleServer { public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGro…
Netty源码分析第一章:  Server启动流程 第二节:NioServerSocketChannel的创建 我们如果熟悉Nio, 则对channel的概念则不会陌生, channel在相当于一个通道, 用于数据的传输 Netty将jdk的channel进行了包装, 并为其扩展了更多的功能 在netty中也分为服务端channel和客户端channel, 在Nio模式下, 服务端channel对应的类为NioServerSocketChannel, 包装的jdk的ServerSocketCha…
Netty源码分析第一章:Netty启动流程   第四节:注册多路复用 回顾下以上的小节, 我们知道了channel的的创建和初始化过程, 那么channel是如何注册到selector中的呢?我们继续分析 回到上一小节的代码: final ChannelFuture initAndRegister() { Channel channel = null; try { //创建channel channel = channelFactory.newChannel(); //初始化channel i…
上一篇博客[Netty源码分析]Netty服务端bind端口过程 我们介绍了服务端绑定端口的过程,这一篇博客我们介绍一下客户端连接服务端的过程. ChannelFuture future = boostrap.connect("127.0.0.1", 8080).sync(); 调用BootStrap的connect函数 public ChannelFuture connect(String inetHost, int inetPort) { return connect(InetSo…