zl程序教程

您现在的位置是:首页 >  其它

当前栏目

JavaDemo——Netty的HttpServer

Netty JavaDemo
2023-09-11 14:16:28 时间

maven导入:

		<dependency>
		    <groupId>io.netty</groupId>
		    <artifactId>netty-all</artifactId>
		    <version>5.0.0.Alpha2</version>
		</dependency>

demo:

/**
 * 2021年7月20日下午4:17:58
 */
package testNettyHttpServer;

import java.net.URI;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;

/**
 * @author XWF
 *
 */
public class TestNettyHttpServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ServerBootstrap bootstrap = new ServerBootstrap();
		NioEventLoopGroup bootGroup = new NioEventLoopGroup();
		NioEventLoopGroup workerGroup = new NioEventLoopGroup();
		bootstrap.group(bootGroup, workerGroup);
		bootstrap.channel(NioServerSocketChannel.class);
		bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

			@Override
			protected void initChannel(SocketChannel ch) throws Exception {
				ChannelPipeline p = ch.pipeline();
				p.addLast(new HttpRequestDecoder());
				p.addLast(new HttpObjectAggregator(10240));
				p.addLast(new HttpResponseEncoder());
				p.addLast(new MyHandler());
			}
		});
		try {
			ChannelFuture future = bootstrap.bind("127.0.0.1", 1234).sync();
			if(future.isSuccess()) {
				System.out.println("server start success.");
			}
			future.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			bootGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}

}

class MyHandler extends SimpleChannelInboundHandler<FullHttpRequest>{

	@Override
	protected void messageReceived(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
		System.out.println("-Method=" + msg.method());
		System.out.println("-uri=" + msg.uri());
		System.out.println("-query=" + URI.create(msg.uri()).getQuery());
		System.out.println("-content=" + new String(msg.content().copy().array()));
		FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK);
		response.content().writeBytes(("HELLO_" + msg.method()).getBytes());
		ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
	}
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("Active:" + ctx);
	}
	
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		System.out.println("Inactive:" + ctx);
	}
}

运行结果:

get测试:

post测试: