netty4.1.6源码2-------创建服务端的channel
1. netty在哪里调用jdk底层的socket去创建netty服务端的socket。
2. 在哪里accept连接。
服务端的启动:
1. 调用jdk底层的api去创建jdk的服务端的channel。Netty封装成自己的channel同时创建一些组件绑定在这个channel上面。
2. 初始化服务端的channel。
3. 注册selector,netty将jdk底层的channel注册到事件轮询器selector上面,并把服务端的channel作为attachment附加到jdk底层的channel上面,有事件轮询出来的时候就可以直接拿这个attachment,这个attachment就是netty封装的服务端的channel。
4. 端口绑定,调用jdk底层的api作为端口的监听。
1. 创建服务端的channel:

ChannelFuture f = b.bind(8888).sync();//服务端创建channel的入口
final ChannelFuture regFuture = initAndRegister(); //创建服务端的channel
final ChannelFuture initAndRegister() {
Channel channel = null;
try {
channel = channelFactory.newChannel(); //.channel方法设置channelFactory,channelFactory是ReflectiveChannelFactory
init(channel);
}
public class ReflectiveChannelFactory<T extends Channel> implements ChannelFactory<T> {
public ReflectiveChannelFactory(Class<? extends T> clazz) {
if (clazz == null) {
throw new NullPointerException("clazz");
}
this.clazz = clazz;
}
@Override
public T newChannel() { //newChannel方法
try {
return clazz.newInstance(); //通过反射创建channel,clazz是NioServerSocketChannel.class,newInstance()调用的是构造函数
} catch (Throwable t) {
throw new ChannelException("Unable to create Channel from class " + clazz, t);
}
}
}
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) //分析.channel方法,传入NioServerSocketChannel通过反射方式调用NioServerSocketChannel类的构造函数
AbstractBootstrap类:
public B channel(Class<? extends C> channelClass) { //channelClass是传进来的NioServerSocketChannel.class
if (channelClass == null) {
throw new NullPointerException("channelClass");
}
return channelFactory(new ReflectiveChannelFactory<C>(channelClass)); //通过ReflectiveChannelFactory构造函数把channelClass封装成ReflectiveChannelFactory,设置channelFactory
}
public B channelFactory(ChannelFactory<? extends C> channelFactory) {
if (channelFactory == null) {
throw new NullPointerException("channelFactory");
}
if (this.channelFactory != null) {
throw new IllegalStateException("channelFactory set already");
}
this.channelFactory = channelFactory; //设置channelFactory
return (B) this;
}
NioServerSocketChannel构造函数:

private static final SelectorProvider DEFAULT_SELECTOR_PROVIDER = SelectorProvider.provider();
private static ServerSocketChannel newSocket(SelectorProvider provider) {
try {
return provider.openServerSocketChannel();
} catch (IOException e) {
throw new ChannelException("Failed to open a server socket.", e);
}
}
public NioServerSocketChannel() { //构造函数
this(newSocket(DEFAULT_SELECTOR_PROVIDER)); //newSocket创建jdk底层的Channel,服务端的channel就创建完毕了
}
public NioServerSocketChannel(ServerSocketChannel channel) {
super(null, channel, SelectionKey.OP_ACCEPT);
config = new NioServerSocketChannelConfig(this, javaChannel().socket()); // this是NioServerSocketChannel也就是netty服务端的channel,
}
super(null, channel, SelectionKey.OP_ACCEPT); //跟进去
AbstractNioChannel类:
protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
super(parent);
this.ch = ch; //通过newSocket创建出来的jdk底层的channel,
this.readInterestOp = readInterestOp;
try {
ch.configureBlocking(false);//设置服务端的channel非阻塞模式,
} catch (IOException e) {
try {
ch.close();
} catch (IOException e2) {
if (logger.isWarnEnabled()) {
logger.warn("Failed to close a partially initialized socket.", e2);
}
}
throw new ChannelException("Failed to enter non-blocking mode.", e);
}
}
//channel有服务端和客户端的channel,都继承自AbstractChannel,
protected AbstractChannel(Channel parent) {
this.parent = parent;
id = newId();//channel唯一的表示
unsafe = newUnsafe();//channel底层tcp相关的操作
pipeline = newChannelPipeline();
}
netty4.1.6源码2-------创建服务端的channel的更多相关文章
- zookeeper源码分析之四服务端(单机)处理请求流程
上文: zookeeper源码分析之一服务端启动过程 中,我们介绍了zookeeper服务器的启动过程,其中单机是ZookeeperServer启动,集群使用QuorumPeer启动,那么这次我们分析 ...
- Netty 4源码解析:服务端启动
Netty 4源码解析:服务端启动 1.基础知识 1.1 Netty 4示例 因为Netty 5还处于测试版,所以选择了目前比较稳定的Netty 4作为学习对象.而且5.0的变化也不像4.0这么大,好 ...
- zookeeper源码分析之五服务端(集群leader)处理请求流程
leader的实现类为LeaderZooKeeperServer,它间接继承自标准ZookeeperServer.它规定了请求到达leader时需要经历的路径: PrepRequestProcesso ...
- Netty源码分析之服务端启动过程
一.首先来看一段服务端的示例代码: public class NettyTestServer { public void bind(int port) throws Exception{ EventL ...
- Nacos(二)源码分析Nacos服务端注册示例流程
上回我们讲解了客户端配置好nacos后,是如何进行注册到服务器的,那我们今天来讲解一下服务器端接收到注册实例请求后会做怎么样的处理. 首先还是把博主画的源码分析图例发一下,让大家对整个流程有一个大概的 ...
- 4. 源码分析---SOFARPC服务端暴露
服务端的示例 我们首先贴上我们的服务端的示例: public static void main(String[] args) { ServerConfig serverConfig = new Ser ...
- Spring Cloud系列(三):Eureka源码解析之服务端
一.自动装配 1.根据自动装配原理(详见:Spring Boot系列(二):Spring Boot自动装配原理解析),找到spring-cloud-starter-netflix-eureka-ser ...
- zookeeper源码分析之一服务端启动过程
zookeeper简介 zookeeper是为分布式应用提供分布式协作服务的开源软件.它提供了一组简单的原子操作,分布式应用可以基于这些原子操作来实现更高层次的同步服务,配置维护,组管理和命名.zoo ...
- Netty源码分析之服务端启动
Netty服务端启动代码: public final class EchoServer { static final int PORT = Integer.parseInt(System.getPro ...
随机推荐
- 最简单的VS-Qt-CMake项目框架
使用qtcreator新建一个空工程,可以得到main.cpp,mainwindow.cpp,mainwindow.h和mainwindow.ui四个文件 下面主要介绍CMakeLists.txt的内 ...
- weblogic11g重置控制密码
Reset the AdminServer Password in WebLogic 11g and 12c If you forget the AdminServer password for yo ...
- js array.filter实例(数组去重)
语法: 循环对数组中的元素调用callback函数, 如果返回true 保留,如果返回false 过滤掉, 返回新数组,老数组不变 var new_array = source_array.filt ...
- System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse()
System.Net.WebException: The operation has timed out at System.Net.HttpWebRequest.GetResponse() 在请求 ...
- Docker源码分析(六):Docker Daemon网络
1. 前言 Docker作为一个开源的轻量级虚拟化容器引擎技术,已然给云计算领域带来了新的发展模式.Docker借助容器技术彻底释放了轻量级虚拟化技术的威力,让容器的伸缩.应用的运行都变得前所未有的方 ...
- LeetCode——Summary Ranges
Description: Given a sorted integer array without duplicates, return the summary of its ranges. For ...
- 《转载》struts旅程《1》
struts简介 Struts是Apache软件基金会(ASF)赞助的一个开源项目.它最初是jakarta项目中的一个子项目,并在2004年3月成为ASF的顶级项目.它通过采用JavaServlet/ ...
- chrome单步调试代码
单步调试代码 所有步骤选项均通过边栏中的可点击图标 表示,但也可以通过快捷键触发(鼠标悬停在操作图标上就可以看到快捷键).下面是简要介绍: 图标/按钮 操作 描述 Resume 继续执行直到下一个断点 ...
- SQL SERVER大话存储结构(5)_SQL SERVER 事务日志解析
本系列上一篇博文链接:SQL SERVER大话存储结构(4)_复合索引与包含索引 1 基本介绍 每个数据库都具有事务日志,用于记录所有事物以及每个事物对数据库所作的操作. 日志的记录 ...
- T-SQL创建作业
/*1.--创建作业 */ /*--调用示例 --每月执行的作业 exec p_createjob @jobname='mm',@sql='select * from syscolumns',@fre ...