Netty5入門(1),netty5入門
一、 搭建開發環境
本教程使用的開發環境為MacOSX 10.10+JDK1.8+Eclipse Java EE IDE4.4.1+Maven3.2.5。以下是開發環境的搭建過程。
1. 安裝JDK 1.8
本教程使用的JDK版本為JDK1.8。你可以在http://java.com下載到這個最新的JDK。
JDK的安裝配置就不用多說了,你明白的。
2. 安裝Eclipse Java EE
本教程使用Eclipse為Eclipse Java EE 4.4.1,你可以在http://www.eclipse.org下載這個最新的IDE。
下載後解壓縮到一定的目錄即可。在此我們推薦這個Eclipse版本,是因為它內建了m2e外掛程式,支援Maven項目的建立、打包,不需要你去記憶複雜的mvn命令,畢竟要一下子記住那麼多命令也是需要時間的。
3. 下載安裝Apache Maven.
本教程使用的Maven3.2.5,你可以在http://maven.apache.org下載到這個最新的Maven版本。
Maven的安裝也很簡單,解壓縮到指定目錄即可。
然後配置MAVEN_HOME。編輯~/.bash_profile檔案,增加如下內容:
MAVEN_HOME=/Users/kmyhy/Library/apache-maven-3.2.5
PATH=$MAVEN_HOME/bin:$PATH
export MAVEN_HOME
export PATH
然後當前配置立即生效:
source .bash_profile
在Maven安裝路徑下,找到conf/settings.xml檔案,在其中增加一個localRepository元素:
<localRepository>/Users/kmyhy/.m2/repository</localRepository>
這個是你本地Maven庫的路徑,注意kmyhy請替換成你自己的使用者名稱。
4. 配置 Eclipse
開啟Eclipse,喜好設定 >> Maven >> Installation >> Add,添加Maven的安裝路徑:
在Maven>> User Settings下,設定settings.xml路徑:
注意,如果你的Maven安裝路徑不同,請根據實際情況進行修改,比如kmyhy請替換成你自己的使用者名稱。
二、測試Maven
建立一個Maven Project,選擇archType時,請選擇quickstart:
然後是Group ID和artifact ID:
在App.java上右鍵,Run as >> Java Application,控制台中將輸出“Hello Word!”。
三、測試netty
我們用《netty5使用者指南》中的“echo服務”來測試。
首先需要在pom.xml中增加一個netty5的依賴:
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>5.0.0.Alpha1</version>
</dependency>
然後實現服務端代碼。這將用到兩個類。首先是DiscardServerHandler:
package com.ydtf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelHandlerAdapter;
public class EchoServerHandlerextends ChannelHandlerAdapter { // (1)
public voidchannelRead(ChannelHandlerContext ctx, Object msg) { // (2)
ctx.write(msg); // (1)
ctx.flush(); // (2)
}
@Override
public voidexceptionCaught(ChannelHandlerContext ctx, Throwable cause) { // (4)
// Close theconnection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
}
然後是EchoServer類:
package com.ydtf;
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.SocketChannel;
importio.netty.channel.socket.nio.NioServerSocketChannel;
public class EchoServer {
private int port;
public EchoServer(int port) {
this.port = port;
}
public void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap(); // (2)
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class) // (3)
.childHandler(newChannelInitializer<SocketChannel>() { // (4)
@Override
public voidinitChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoServerHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
// Bind andstart to accept incoming connections.
ChannelFuture f = b.bind(port).sync(); // (7)
// Wait untilthe server socket is closed.
// In this example,this does not happen, but you can do that to gracefully
// shut downyour server.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
} else {
port = 8080;
}
new EchoServer(port).run();
}
}
在Project Explorer視窗,在src/main/java下找到DiscardServer.java,右鍵,選擇Run as >> Java Applicatoin,運行服務端。
開啟終端程式,輸入telnet localhost 8080,連上服務端。然後輸入 hello netty,斷行符號,服務端將立即回顯一個hello netty到終端視窗。