深入浅出Netty之二 server启动

以netty提供的echo server作为分析入口,echoServer代码:

Java代码 复制代码 收藏代码
  1. public void run() {
  2. // 构造NioServerSocketChannelFactory,初始化bootstrap
  3. ServerBootstrap bootstrap = new ServerBootstrap(
  4. new NioServerSocketChannelFactory(
  5. Executors.newCachedThreadPool(),
  6. Executors.newCachedThreadPool()));
  7. // 创建一个自定义的PipelineFactory
  8. bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
  9. public ChannelPipeline getPipeline() throws Exception {
  10. return Channels.pipeline(new EchoServerHandler());
  11. }
  12. });
  13. // 开始bind端口,启动server
  14. bootstrap.bind(new InetSocketAddress(port));
  15. }
public void run() {
		// 构造NioServerSocketChannelFactory,初始化bootstrap
		ServerBootstrap bootstrap = new ServerBootstrap(
				new NioServerSocketChannelFactory(
						Executors.newCachedThreadPool(),
						Executors.newCachedThreadPool()));

		// 创建一个自定义的PipelineFactory
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
			public ChannelPipeline getPipeline() throws Exception {
				return Channels.pipeline(new EchoServerHandler());
			}
		});

		// 开始bind端口,启动server
		bootstrap.bind(new InetSocketAddress(port));
	}

Server启动过程:

一.ServerBootstrap配置,NioServerSocketChannelFactory初始化

1. 构造NioWorkerPool,启动worker线程,默认个数CPU*2

Java代码 复制代码 收藏代码
  1. public NioServerSocketChannelFactory(
  2. Executor bossExecutor, Executor workerExecutor,
  3. int workerCount) {
  4. this(bossExecutor, new NioWorkerPool(workerExecutor, workerCount));
  5. }
           public NioServerSocketChannelFactory(
            Executor bossExecutor, Executor workerExecutor,
            int workerCount) {
        this(bossExecutor, new NioWorkerPool(workerExecutor, workerCount));
    }

2.创建worker数组

Java代码 复制代码 收藏代码
  1. workers = new AbstractNioWorker[workerCount];
  2. for (int i = 0; i < workers.length; i++) {
  3. workers[i] = createWorker(workerExecutor);
  4. }
  5. this.workerExecutor = workerExecutor;
          workers = new AbstractNioWorker[workerCount];

        for (int i = 0; i < workers.length; i++) {
            workers[i] = createWorker(workerExecutor);
        }
        this.workerExecutor = workerExecutor;


Java代码 复制代码 收藏代码
  1. protected NioWorker createWorker(Executor executor) {
  2. return new NioWorker(executor);
  3. }
        protected NioWorker createWorker(Executor executor) {
        	return new NioWorker(executor);
    	}
3.打开选择器
Java代码 复制代码 收藏代码
  1. AbstractNioWorker(Executor executor) {
  2. this.executor = executor;
  3. openSelector();
        AbstractNioWorker(Executor executor) {
        this.executor = executor;
        openSelector();
    }

4. 启动worker线程

Java代码 复制代码 收藏代码
  1. selector = Selector.open();
  2. ....
  3. DeadLockProofWorker.start(executor, new ThreadRenamingRunnable(this, "New I/O worker #" + id));
  4. ...
		selector = Selector.open();
		....
            DeadLockProofWorker.start(executor, new ThreadRenamingRunnable(this, "New I/O  worker #" + id));
		...

5.初始化NioServerSocketPipelineSink,将workerPool注册到PipelineSink中

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值