Idea中编译Netty源码

今天我们来说说在Idea中如何编译Netty源码,因为之前我们介绍了Netty的使用方式,我们学一样的东西,不单单要会怎么用,我们还有知道它的原理。知道一个东西的原理,最好的方式就是阅读源码。所以我们今天来编译Netty的源码。

笔者的开发环境如下:

idea版本:2020.02
netty版本:netty-4.1.51.Final

首先我们要先去GitHub上下载对应的源码,具体地址:Netty源码,我们这儿的下载方式有很多种,笔者采用的方式就是直接下载,当然也是可以用命令git clone将源码给克隆下来。

在这里插入图片描述

然后将下载好的Netty源码进行解压,然后进行重命名,当然你想命名成什么都行,笔者将它重命名成nettySource,然后打开我们的idea

在这里插入图片描述

找到我们刚才下载好的Netty的源码,找到其中的pom.xml文件,直接打开。

在这里插入图片描述

打开的方式open as project

在这里插入图片描述

会报如下的错,是因为我们这个包找不到。

在这里插入图片描述

这个时候我们需要去pom.xml文件中添加如下的内容:

<dependency>
	<groupId>io.netty</groupId>
	<artifactId>netty-all</artifactId>
	<version>4.1.9.Final</version>
	<exclusions>
		<exclusion>
			<groupId>io.netty</groupId>
			<artifactId>netty-tcnative</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>io.netty</groupId>
	<artifactId>netty-example</artifactId>
	<version>4.1.9.Final</version>
	<exclusions>
		<exclusion>
			<groupId>io.netty</groupId>
			<artifactId>netty-tcnative</artifactId>
		</exclusion>
	</exclusions>
</dependency>

在这里插入图片描述

然后我们继续build最后发现我们成功了

在这里插入图片描述

这个我们先建一个Modules测试一下。

在这里插入图片描述

引入对应的依赖,具体如下:

<dependencies>
	<dependency>
		<groupId>io.netty</groupId>
		<artifactId>netty-codec</artifactId>
		<version>4.1.51.Final</version>
	</dependency>
	<dependency>
		<groupId>io.netty</groupId>
    <artifactId>netty-codec-http</artifactId>
    <version>4.1.51.Final</version>
  </dependency>
  <dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-buffer</artifactId>
    <version>4.1.51.Final</version>
  </dependency>
</dependencies>

最后我们开始编写我们的测试类,看看我们的服务端是否能正常的启动,具体的代码如下:

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
public class TestServer {
    public static void main(String[] args) throws InterruptedException {
        //就是一个死循环,不停地检测IO事件,处理IO事件,执行任务
        //创建一个线程组:接受客户端连接   主线程
        EventLoopGroup bossGroup=new NioEventLoopGroup(1);//cpu核心数*2
        //创建一个线程组:接受网络操作   工作线程
        EventLoopGroup workerGroup=new NioEventLoopGroup();  //cpu核心数*2
        //是服务端的一个启动辅助类,通过给他设置一系列参数来绑定端口启动服务
        ServerBootstrap serverBootstrap=new ServerBootstrap();
        // 我们需要两种类型的人干活,一个是老板,一个是工人,老板负责从外面接活,
        // 接到的活分配给工人干,放到这里,bossGroup的作用就是不断地accept到新的连接,将新的连接丢给workerGroup来处理
        serverBootstrap.group(bossGroup,workerGroup)
                //设置使用NioServerSocketChannel作为服务器通道的实现
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG,128) //设置线程队列中等待连接的个数
                .childOption(ChannelOption.SO_KEEPALIVE,true)//保持活动连接状态
                //表示一条新的连接进来之后,该怎么处理,也就是上面所说的,老板如何给工人配活
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
                        nioSocketChannel.pipeline().addLast(new StringDecoder(),new NettyServerHandler());
                    }
                });
        System.out.println(".........server  init..........");
        // 这里就是真正的启动过程了,绑定9090端口,等待服务器启动完毕,才会进入下行代码
        ChannelFuture future = serverBootstrap.bind(9090).sync();
        System.out.println(".........server start..........");
        //等待服务端关闭socket
        future.channel().closeFuture().sync();
        // 关闭两组死循环
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    private int count=0;
    //读取数据事件
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        String byteBuf= (String) msg;
        System.out.println("客户端发来消息:"+byteBuf);
        ctx.writeAndFlush(Unpooled.copiedBuffer("同好"+System.getProperty("line.separator"), CharsetUtil.UTF_8));
    }
    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("Netty Server channelRegistered");
        ctx.fireChannelRegistered();
    }
    //读取数据完毕事件
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.fireChannelReadComplete();
    }
    //异常发生回调
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }
}

然后运行上面的代码,执行的结果如下:

在这里插入图片描述

可以看到我们的服务端就启动起来,接下来就可以我们的源码阅读之旅了。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值