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测试:
相关文章
- Netty+Websocket 聊天、推送(实战)
- netty系列之:java中的base64编码器
- netty系列之:在netty中使用protobuf协议
- netty系列之:内置的Frame detection
- Gateway集成Netty服务
- netty系列之:netty中的自动解码器ReplayingDecoder
- netty系列之:netty中的核心编码器bytes数组
- netty系列之:EventExecutor,EventExecutorGroup和netty中的实现
- netty系列之:小白福利!手把手教你做一个简单的代理服务器
- netty系列之:一个价值上亿的网站速度优化方案
- netty系列之:性能为王!创建多路复用http2服务器
- netty系列之:netty实现http2中的流控制
- netty系列之:自动重连
- netty系列之:Event、Handler和Pipeline
- Netty_Netty系列之Netty百万级推送服务设计要点
- Java与Netty实现高性能高并发
- Netty系列之Netty可靠性分析--转载
- 7. 成功解决:io.netty.util.IllegalReferenceCountException: refCnt: 0, decrement: 1