一瞧捌、前言
??netty是什么管跺?根據(jù)自己的理解义黎,就是一句話:非阻塞、基于事件驅(qū)動的網(wǎng)絡(luò)應(yīng)用服務(wù)開發(fā)框架豁跑,可以用來開發(fā)高性能廉涕、高可靠的服務(wù)端和客戶端網(wǎng)絡(luò)程序。關(guān)于netty架構(gòu)的介紹艇拍,可參考Netty框架入門的介紹狐蜕,本文做為入門篇,通過實例代碼介紹如何使用netty淑倾。
首先配置開發(fā)環(huán)境馏鹤,開發(fā)環(huán)境使用的是IDEA,新建個空的MAVEN項目娇哆,在pom.xml中添加依賴湃累。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>zkh360</groupId>
<artifactId>zkh-netty</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.netty/netty-all -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.36.Final</version>
</dependency>
</dependencies>
</project>
echo server是回顯服務(wù),用戶輸入什么碍讨,服務(wù)器將輸入信息返回給客戶端治力。EchoServer代碼如下:
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;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* @Author:Mark
* @Email: yunxiang.lon@gmail.com
* @Description:
* @Date: Created on 20:52 2019/6/5
* @Modify by:
*/
public class EchoServer {
private int port;
public DiscardServer(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(new ChannelInitializer<SocketChannel>() { // (4)
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new DiscardServerHandler());
}
})
.option(ChannelOption.SO_BACKLOG, 128) // (5)
.childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
// Bind and start to accept incoming connections.
ChannelFuture f = b.bind(port).sync(); // (7)
// Wait until the server socket is closed.
// In this example, this does not happen, but you can do that to gracefully
// shut down your server.
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
int port = 8090;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
}
new DiscardServer(port).run();
}
}
服務(wù)器端消息處理代碼如下
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.ReferenceCountUtil;
/**
* @Author:Mark
* @Email: yunxiang.lon@gmail.com
* @Description:
* @Date: Created on 20:34 2019/6/5
* @Modify by:
*/
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg){
ctx.write(msg); //(1)
ctx.close(); //(2)
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){
// Close the connection when an exception is raised.
cause.printStackTrace();
ctx.close();
}
}
運行服務(wù)端程序,telnet 27.0.0.1 8090勃黍,輸入宵统,顯示如下:
上面只是簡單的入門,回顯是輸入一個字符回顯一個