一、实例要求:
步骤:Netty服务器在8081端口监听,浏览器发出请求“http://localhost:8081/"
服务器可以回复消息给浏览器客户端”hello 我是服务器“,并对特定请求资源进行过滤
目的:Netty可以做Http服务开发,并且理解Handler实例和客户端及其请求的关系
1.1 代码实现
1.1.1 NettyServer代码
package com.netty.NettyHttp;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import java.net.InetSocketAddress;
public class TestServer {
public static void main(String[] args) throws InterruptedException {
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
NioEventLoopGroup workGroup = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap();
try{
serverBootstrap.group(bossGroup,workGroup)
.channel(NioServerSocketChannel.class)
//childHandler()是将handler加入到workGroup中,handler()是将handler加入到bossGroup中
.childHandler(new TestServerInitializer());
ChannelFuture cf = serverBootstrap.bind(8081).sync();
cf.channel().closeFuture().sync();
}catch (Exception e){
e.printStackTrace();
}
finally {
bossGroup.shutdownGracefully();
workGroup.shutdownGracefully();
}
}
}
1.1.2 TestServerInitializer代码实现
package com.netty.NettyHttp;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
public class TestServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//向管道添加处理器
//得到管道
ChannelPipeline pipeline = ch.pipeline();
//加入一个netty 提供的httpServerCodec codec=>[codec-decoder]
//HttpServerCodec 说明
pipeline.addLast("HttpServerCodec", new HttpServerCodec());
//增加一个自定义的handler处理器
pipeline.addLast("MyTestHttpServerHandler",new TestHttpServerHandler());
}
}
1.1.3 TestHttpServerHandler代码
package com.netty.NettyHttp;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
/*
* 说明;
* 1、SimpleChannelInboundHandler是ChannelInboundHandlerAdapter的子类
* 2、HttpObject 客户端和服务器相互通讯的数据被封装成HttpObject
* */
public class TestHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {
//读取客户端的数据
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
//判断msg是不是HttpObject请求
if(msg instanceof HttpRequest){
//拦截ico请求
String uri = ((HttpRequest) msg).uri();
if("/favicon.ico".equals(uri)){
System.out.println("请求了favicon.ico,不做响应");
return;
}
System.out.println("msg类型=" + msg.getClass());
System.out.println("客户端地址;" + ctx.channel().remoteAddress());
//回复信息给浏览器【Http协议】
ByteBuf content = Unpooled.copiedBuffer("hello,我是服务器", CharsetUtil.UTF_8);
//构造一个Http的响应,即Httpresponse
DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,content);
response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
//将构建好的reaponse返回
ctx.writeAndFlush(response);
}
}
}
1.2 结果 💥
浏览器访问;http://localhost:8081
💥推荐阅读💥