netty模拟http
·
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.20.Final</version>
</dependency>
package com.example.demo.controller.http;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
/**
* 服务端
*/
public class TestServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup bossGrop=new NioEventLoopGroup();
EventLoopGroup workerGroup=new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap=new ServerBootstrap();
serverBootstrap.group(bossGrop,workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new TestServerInitializer());
//绑定端口
ChannelFuture channelFuture=serverBootstrap.bind(7777).sync();
//监听关闭通道
channelFuture.channel().closeFuture().sync();
}finally {
//优雅关闭
workerGroup.shutdownGracefully();
bossGrop.shutdownGracefully();
}
}
}
package com.example.demo.controller.http;
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> {
/**
* 初始化通道
* @param socketChannel
* @throws Exception
*/
@Override
protected void initChannel(SocketChannel socketChannel) throws Exception {
//向管道加入处理器
//得到管道
ChannelPipeline pipeline = socketChannel.pipeline();
//加入一个http的 编-解码器
pipeline.addLast("MyHttpServerCodec",new HttpServerCodec());
//增加一个自定义的handler
pipeline.addLast("MyTestHttpServerHandler",new TestHttpServerHandler());
}
}
package com.example.demo.controller.http;
import com.sun.jndi.toolkit.url.Uri;
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.http.*;
import io.netty.util.CharsetUtil;
import java.net.URI;
/**
* 测试http服务处理程序
*/
public class TestHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {
/**
* 通道读取 读取客户端的数据
* @param channelHandlerContext
* @param
* @throws Exception
*/
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject msg) throws Exception {
//判断msg 是不是http请求
if(msg instanceof HttpRequest){
System.out.println("msg类型:"+msg.getClass());
System.out.println("客户端地址:"+channelHandlerContext.channel().remoteAddress());
HttpRequest request=(HttpRequest) msg;
URI uri=new URI(request.uri());
if("/favicon.ico".equals(uri.getPath())){
System.out.println("不做响应");
return;
}
//回复信息给浏览器
ByteBuf byteBuf= Unpooled.copiedBuffer("nihao wo shi fuwuqi", CharsetUtil.UTF_8);
//构造一个http响应 即httpresponse
DefaultFullHttpResponse response=new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,byteBuf);
//设置请求头
response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH,byteBuf.readableBytes());
//写入并刷新缓冲区
channelHandlerContext.writeAndFlush(response);
}
}
}
启动服务器,浏览器输入


更多推荐



所有评论(0)