Skip to content

Commit 79a6377

Browse files
committed
add netty http server in spring boot sample
1 parent fce4de2 commit 79a6377

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.ipman.netty.springboot.sample.http;
2+
3+
import io.netty.bootstrap.ServerBootstrap;
4+
import io.netty.channel.*;
5+
import io.netty.channel.nio.NioEventLoopGroup;
6+
import io.netty.channel.socket.SocketChannel;
7+
import io.netty.channel.socket.nio.NioServerSocketChannel;
8+
import io.netty.handler.codec.http.HttpServerCodec;
9+
import io.netty.handler.codec.http.HttpServerExpectContinueHandler;
10+
import io.netty.handler.logging.LogLevel;
11+
import io.netty.handler.logging.LoggingHandler;
12+
13+
/**
14+
* Created by ipipman on 2022/1/19.
15+
*
16+
* @version V1.0
17+
* @Package com.ipman.netty.springboot.sample.http
18+
* @Description: (用一句话描述该文件做什么)
19+
* @date 2022/1/19 10:00 下午
20+
*/
21+
public class HttpServer {
22+
23+
public static void main(String[] args) {
24+
// 主从模式
25+
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
26+
EventLoopGroup workerGroup = new NioEventLoopGroup();
27+
28+
try {
29+
ServerBootstrap b = new ServerBootstrap();
30+
b.group(bossGroup, workerGroup)
31+
.channel(NioServerSocketChannel.class)
32+
.handler(new LoggingHandler(LogLevel.INFO))
33+
.childHandler(new ChannelInitializer<SocketChannel>() {
34+
@Override
35+
protected void initChannel(SocketChannel ch) throws Exception {
36+
// HTTP 模式
37+
ChannelPipeline p = ch.pipeline();
38+
p.addLast(new HttpServerCodec());
39+
p.addLast(new HttpServerExpectContinueHandler());
40+
p.addLast(new HttpServerHandler());
41+
}
42+
});
43+
ChannelFuture ch = b.bind(8099).sync();
44+
45+
System.out.println("Open Http Server : http://127.0.0.1:8099");
46+
ch.channel().closeFuture().sync();
47+
48+
} catch (Exception ex) {
49+
ex.printStackTrace();
50+
} finally {
51+
workerGroup.shutdownGracefully();
52+
bossGroup.shutdownGracefully();
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.ipman.netty.springboot.sample.http;
2+
3+
import io.netty.buffer.Unpooled;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelHandler.Sharable;
6+
import io.netty.channel.ChannelHandlerContext;
7+
import io.netty.channel.SimpleChannelInboundHandler;
8+
import io.netty.handler.codec.http.*;
9+
10+
import java.nio.charset.StandardCharsets;
11+
12+
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
13+
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE;
14+
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_LENGTH;
15+
import static io.netty.handler.codec.http.HttpHeaderValues.TEXT_PLAIN;
16+
17+
18+
/**
19+
* Created by ipipman on 2022/1/19.
20+
*
21+
* @version V1.0
22+
* @Package com.ipman.netty.springboot.sample.http
23+
* @Description: (用一句话描述该文件做什么)
24+
* @date 2022/1/19 9:50 下午
25+
*/
26+
@Sharable
27+
public class HttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {
28+
29+
private static final byte[] CONTEXT = "hello coding".getBytes(StandardCharsets.UTF_8);
30+
31+
/**
32+
* 当读取完毕时
33+
*
34+
* @param ctx
35+
* @throws Exception
36+
*/
37+
@Override
38+
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
39+
ctx.flush();
40+
}
41+
42+
@Override
43+
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
44+
if (msg instanceof HttpRequest) {
45+
HttpRequest req = (HttpRequest) msg;
46+
FullHttpResponse response = new DefaultFullHttpResponse(req.protocolVersion(), OK, Unpooled.wrappedBuffer(CONTEXT));
47+
response.headers()
48+
.set(CONTENT_TYPE, TEXT_PLAIN)
49+
.set(CONTENT_LENGTH, response.content().readableBytes());
50+
51+
ChannelFuture f = ctx.write(response);
52+
}
53+
}
54+
55+
@Override
56+
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
57+
cause.printStackTrace();
58+
ctx.close();
59+
}
60+
}

0 commit comments

Comments
 (0)