netty源码阅读(1) ---- 环境搭建

1. 工具准备

  • JDK 1.8
  • IntelliJ IDEA
  • Git

2. 源码拉取

官方仓库: https://github.com/netty/netty
fork 到自建仓库后, 经过 IDEA 拉取到本地建立项目.
本文使用的netty版本为 4.1.49.Final-SNAPSHOTgit

3. 环境搭建

首先在 Profile 上根据需求勾选选项,拉取jar包.github

image.png

同时修改pom.xml此处位置为1.8apache

image.png

耐心等待全部jar包拉取完毕, 若是maven拉取速度很慢, 能够更改maven源改成国内镜像:maven

打开IntelliJ IDEA->Settings ->Build, Execution, Deployment -> Build Tools > Maven

image.png

通常状况下, 该路径下并不存在 setting.xml, 这时候就须要手动建立文件, 并复制如下内容便可:工具

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                          https://maven.apache.org/xsd/settings-1.0.0.xsd">

      <mirrors>
        <mirror>  
            <id>alimaven</id>  
            <name>aliyun maven</name>  
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>  
            <mirrorOf>central</mirrorOf>          
        </mirror>  
      </mirrors>
</settings>

完成上述步骤后,到 example 包下运行 EchoServeroop

image.png

会报以下错误:post

image.png

解决方案:ui

cd common
mvn clean compile -Dstylecheck.skip

若是不加 -Dstylecheck.skip 可能没法经过插件的格式检查url

完成上述步骤后, 便可编译经过spa

5. example 模块

example 包中存放的是官方提供的 netty 示例程序, 下面示例了 echo 用例的使用

5.1 echoServer

直接执行 main 方法,输出以下日志:

17:53:40.485 [nioEventLoopGroup-2-1] INFO  i.n.handler.logging.LoggingHandler - [id: 0xc78a216b] REGISTERED
17:53:40.495 [nioEventLoopGroup-2-1] INFO  i.n.handler.logging.LoggingHandler - [id: 0xc78a216b] BIND: 0.0.0.0/0.0.0.0:8007
17:53:40.499 [nioEventLoopGroup-2-1] INFO  i.n.handler.logging.LoggingHandler - [id: 0xc78a216b, L:/0:0:0:0:0:0:0:0:8007] ACTIVE

5.2 echoClient

直接执行 main 方法, 不会输出任何日志, 但在 echoServer 下会输出以下日志:

18:10:15.391 [nioEventLoopGroup-2-1] INFO  i.n.handler.logging.LoggingHandler - [id: 0xc78a216b, L:/0:0:0:0:0:0:0:0:8007] READ: [id: 0xa64a1fcd, L:/127.0.0.1:8007 - R:/127.0.0.1:4762]
18:10:15.394 [nioEventLoopGroup-2-1] INFO  i.n.handler.logging.LoggingHandler - [id: 0xc78a216b, L:/0:0:0:0:0:0:0:0:8007] READ COMPLETE

6. 总结

本篇介绍了如何部署 netty 源码调试环境, 笔者中间也踩了好几个坑, 但愿本篇能对其它小伙伴有所帮助

参考

  1. https://juejin.im/post/5dc217...
  2. https://stackoverflow.com/que...
相关文章
相关标签/搜索