1. 程式人生 > >《Netty官方文件》本地傳輸介面

《Netty官方文件》本地傳輸介面

原文連結 譯者:gm777
自4.016版本以來, Netty提供了本地的socket傳輸使Linux系統可以使用JNI(JAVA本地介面)。這個傳輸介面不僅有著高效能並且產生更少的垃圾,所以你也許會想嘗試使用一下。

使用本地傳輸介面

由於這個本地傳輸介面是與NIO傳輸介面相容的,你只需要查詢並替代以下的內容:

  • NioEventLoopGroup → EpollEventLoopGroup
  • NioEventLoop → EpollEventLoop
  • NioServerSocketChannel → EpollServerSocketChannel
  • NioSocketChannel → EpollSocketChannel

由於這個本地傳輸介面不在Netty core 核心程式碼中,你需要修改你的pom.xml檔案拉出netty-transport-native-epoll這部分程式碼作為依賴:

kr.motd.maven
 os-maven-plugin
 1.5.0.Final
...
io.netty
 netty-transport-native-epoll
 ${project.version}
 ${os.detected.name}-${os.detected.arch}
 ...

注意以上, 你需要指定正確的classifier配置作為依賴。在pom.xml檔案extensions 擴充套件部分os-maven-plugin的os..detected.name和os.detected.arch 配置屬性會自動配置。需要更多資訊,涉及the homepage of the os-maven-plugin.
想在一個 sbt專案中使用本地傳輸介面,請在加上線在libraryDependencies:
配置
“io.netty” % “netty-transport-native-epoll” % “${project.version}” classifier “linux-x86_64”

構建本地傳輸介面包

如果你已經有了本地傳輸介面的jar包,那你將不用自己去構建本地介面包了,因為jar包中已經包含了已經所需要的包並且會自動載入上。
去構建本地傳輸介面包,你需要使用64-bit核心的2.6及其以上版本的linux作業系統。並請安裝所需要的工具和庫:

 # RHEL/CentOS/Fedora:
 sudo yum install autoconf automake libtool make tar \ glibc-devel libaio-devel \ libgcc.i686 glibc-devel.i686
# Debian/Ubuntu:
sudo apt-get install autoconf automake libtool make tar \ gcc-multilib libaio-dev