Skip to content

Commit 3360e4b

Browse files
committed
选做作业:使用netty实现后端http访问
1 parent 89d0eae commit 3360e4b

2 files changed

Lines changed: 87 additions & 59 deletions

File tree

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,50 @@
1-
//package io.github.kimmking.gateway.outbound;
2-
//
3-
//import io.netty.bootstrap.Bootstrap;
4-
//import io.netty.channel.ChannelFuture;
5-
//import io.netty.channel.ChannelInitializer;
6-
//import io.netty.channel.ChannelOption;
7-
//import io.netty.channel.EventLoopGroup;
8-
//import io.netty.channel.nio.NioEventLoopGroup;
9-
//import io.netty.channel.socket.SocketChannel;
10-
//import io.netty.channel.socket.nio.NioSocketChannel;
11-
//import io.netty.handler.codec.http.HttpRequestEncoder;
12-
//import io.netty.handler.codec.http.HttpResponseDecoder;
13-
//
14-
//public class NettyHttpClient {
15-
// public void connect(String host, int port) throws Exception {
16-
// EventLoopGroup workerGroup = new NioEventLoopGroup();
17-
//
18-
// try {
19-
// Bootstrap b = new Bootstrap();
20-
// b.group(workerGroup);
21-
// b.channel(NioSocketChannel.class);
22-
// b.option(ChannelOption.SO_KEEPALIVE, true);
23-
// b.handler(new ChannelInitializer<SocketChannel>() {
24-
// @Override
25-
// public void initChannel(SocketChannel ch) throws Exception {
26-
// // 客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码
27-
// ch.pipeline().addLast(new HttpResponseDecoder());
28-
// 客户端发送的是httprequest,所以要使用HttpRequestEncoder进行编码
29-
// ch.pipeline().addLast(new HttpRequestEncoder());
30-
// ch.pipeline().addLast(new HttpClientOutboundHandler());
31-
// }
32-
// });
33-
//
34-
// // Start the client.
35-
// ChannelFuture f = b.connect(host, port).sync();
36-
//
37-
//
38-
// f.channel().write(request);
39-
// f.channel().flush();
40-
// f.channel().closeFuture().sync();
41-
// } finally {
42-
// workerGroup.shutdownGracefully();
43-
// }
44-
//
45-
// }
46-
//
47-
// public static void main(String[] args) throws Exception {
48-
// NettyHttpClient client = new NettyHttpClient();
49-
// client.connect("127.0.0.1", 8844);
50-
// }
51-
//}
1+
package io.github.kimmking.gateway.outbound.netty4;
2+
3+
import io.netty.bootstrap.Bootstrap;
4+
import io.netty.channel.ChannelFuture;
5+
import io.netty.channel.ChannelInitializer;
6+
import io.netty.channel.ChannelOption;
7+
import io.netty.channel.EventLoopGroup;
8+
import io.netty.channel.nio.NioEventLoopGroup;
9+
import io.netty.channel.socket.SocketChannel;
10+
import io.netty.channel.socket.nio.NioSocketChannel;
11+
import io.netty.handler.codec.http.HttpRequestEncoder;
12+
import io.netty.handler.codec.http.HttpResponseDecoder;
13+
14+
public class NettyHttpClient {
15+
public void connect(String host, int port) throws Exception {
16+
EventLoopGroup workerGroup = new NioEventLoopGroup();
17+
18+
try {
19+
Bootstrap b = new Bootstrap();
20+
b.group(workerGroup);
21+
b.channel(NioSocketChannel.class);
22+
b.option(ChannelOption.SO_KEEPALIVE, true);
23+
b.handler(new ChannelInitializer<SocketChannel>() {
24+
@Override
25+
public void initChannel(SocketChannel ch) throws Exception {
26+
// 客户端接收到的是httpResponse响应,所以要使用HttpResponseDecoder进行解码
27+
ch.pipeline().addLast(new HttpResponseDecoder());
28+
// 客户端发送的是http request,所以要使用HttpRequestEncoder进行编码
29+
ch.pipeline().addLast(new HttpRequestEncoder());
30+
ch.pipeline().addLast(new NettyHttpClientOutboundHandler());
31+
}
32+
});
33+
34+
// Start the client.
35+
ChannelFuture f = b.connect(host, port).sync();
36+
37+
f.channel().write("");
38+
f.channel().flush();
39+
f.channel().closeFuture().sync();
40+
} finally {
41+
workerGroup.shutdownGracefully();
42+
}
43+
44+
}
45+
46+
public static void main(String[] args) throws Exception {
47+
NettyHttpClient client = new NettyHttpClient();
48+
client.connect("127.0.0.1", 8801);
49+
}
50+
}
Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,51 @@
11
package io.github.kimmking.gateway.outbound.netty4;
22

3+
import static io.netty.handler.codec.http.HttpHeaderNames.*;
4+
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
5+
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
6+
7+
import io.netty.buffer.ByteBuf;
8+
import io.netty.buffer.Unpooled;
39
import io.netty.channel.ChannelHandlerContext;
410
import io.netty.channel.ChannelInboundHandlerAdapter;
11+
import io.netty.handler.codec.http.*;
12+
13+
public class NettyHttpClientOutboundHandler extends ChannelInboundHandlerAdapter {
14+
15+
private HttpRequest request;
516

6-
public class NettyHttpClientOutboundHandler extends ChannelInboundHandlerAdapter {
7-
817
@Override
918
public void channelActive(ChannelHandlerContext ctx)
1019
throws Exception {
11-
12-
20+
21+
1322
}
14-
23+
1524
@Override
1625
public void channelRead(ChannelHandlerContext ctx, Object msg)
1726
throws Exception {
18-
19-
20-
27+
28+
if (msg instanceof HttpRequest) {
29+
request = (HttpRequest) msg;
30+
31+
String uri = request.uri();
32+
System.out.println("Uri:" + uri);
33+
}
34+
if (msg instanceof HttpContent) {
35+
HttpContent content = (HttpContent) msg;
36+
ByteBuf buf = content.content();
37+
System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8));
38+
buf.release();
39+
40+
FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(msg.toString().getBytes("UTF-8")));
41+
response.headers().set(CONTENT_TYPE, "text/plain");
42+
response.headers().set(CONTENT_LENGTH,
43+
response.content().readableBytes());
44+
response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE);
45+
46+
ctx.write(response);
47+
ctx.flush();
48+
}
49+
2150
}
2251
}

0 commit comments

Comments
 (0)