Netty学习1(入门demo)
1.定义:
Netty是一个NIO客户端服务器网络框架,用于快速开发可维护性、高性能的网络应用。
2. HelloWorld
- 一个简单的Netty程序,包含服务器端server和客户端client,下面直接上代码…
1.pom
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.19.Final</version>
</dependency>
2.NettyServer
package Netty;
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;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import java.net.InetSocketAddress;
public class NettyServer {
public void start(int port){
EventLoopGroup bossGroup = new NioEventLoopGroup(1); //boss线程组,接收传入的连接,并将连接分配给worker线程组
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class) //在这里,我们指定使用NioServerSocketChannel类来实例化一个新的Channel(通道)来接受传入的连接。
.childHandler(new ChannelInitializer<SocketChannel>() { //设置服务器处理程序
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast("decoder", new StringDecoder());
ch.pipeline().addLast("encoder", new StringEncoder());
ch.pipeline().addLast(new NettyServerHandler()); //自定义handler
};
})
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true);
ChannelFuture future = bootstrap.bind(port).sync(); //启动服务器
System.out.println("netty Server port : " + port );
future.channel().closeFuture().sync();
} catch (Exception e) {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new NettyServer().start(8081);
}
}
3.NettyServerHandler:
````
package Netty;import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;public class NettyServerHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {System.out.println("Netty server 激活...");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("Netty server 接收消息:"+ msg);
ctx.write("hello too");
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
* 4.NettyClient
package Netty;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class NettyClient {
public void run(String host,int port) {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast("decoder", new StringDecoder());
p.addLast("encoder", new StringEncoder());
p.addLast(new NettyClientHandler());
}
});
ChannelFuture future = null;
try {
future = b.connect(host, port).sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
}
} finally {
group.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
new NettyClient().run("127.0.0.1",8081);
}
}
\-5. NettyClientHandler
package Netty;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class NettyClientHandler extends ChannelInboundHandlerAdapter{
@Override
public void channelActive(ChannelHandlerContext ctx) {
System.out.println("netty client 启动...");
ctx.channel().writeAndFlush("hello lin");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
System.out.println("netty 服务器响应信息:"+msg);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
}
````
6.Netty 一些主要技术点:
bootstrap : netty 客户端程序的引入类
serverBootstrap:netty 服务端程序的引入类
channel : 连接主体
channelHandler:netty 的处理程序,包括数据的出站、入站、解码、编码、具体业务处理 …功能都在这里完成。
channelPipeline:存放channelHandler的容器
EvenLoop:线程,每个channel都会注册到EvenLoop上面执行
channelFuture:异步执行状态
解码器:将字节流转换成java对象
编码器:将java对象转换成字节流
还没有评论,来说两句吧...