注:源代码来自享学课堂,学习之后所做笔记,方便回顾,也给大家一个参考
目录
1.4至此,最简单的netty编写的服务器就成了,启动主函数,通过浏览器访问
2.1 修改ServerHandlerInit,添加上压缩的handler
2.3 transfer-encoding: chunked:接受压缩请求
4.2 修改handler,加上构造函数,并给ptieline加上ssl支持
目录
至此,最简单的netty编写的服务器就成了,启动主函数,通过浏览器访问
浏览器访问,返回的结果和正常调用接口是一样的,/test是被拦截的请求
修改ServerHandlerInit,添加上压缩的handler
transfer-encoding: chunked:接受压缩请求
修改handler,加上构造函数,并给ptieline加上ssl支持
1 netty编写http服务器
1.1 主函数
public class HttpServer {
public static final int port = 6789; //设置服务端端口
public static void main(String[] args) throws Exception {
EventLoopGroup group = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
try {
b.group(group);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ServerHandlerInit ());
// 服务器绑定端口监听
ChannelFuture f = b.bind(port).sync();
System.out.println("服务端启动成功,端口是:"+port);
// 监听服务器关闭监听
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
}
1.2 ServerHandlerInit
public class ServerHandlerInit extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//http响应编码,服务端,对于request是解码,对于response是编码
pipeline.addLast("encode",new HttpResponseEncoder());
//这里我开始写成了HttpResponseDecoder,程序一直报类型转换异常,要注意
pipeline.addLast("decode",new HttpRequestDecoder());
///将http请求聚合在一起,可以通过对象.content()来调用,HttpObject, HttpMessage, HttpContent, FullHttpMessage
pipeline.addLast("aggre",new HttpObjectAggregator(10*1024*1024));
//业务操作,自定义业务操作
pipeline.addLast("busi",new BusiHandler());
}
}
1.3 服务端自定义BusiHandler
public class BusiHandler extends ChannelInboundHandlerAdapter {
private String result = "";
private void send(String content, ChannelHandlerContext ctx,
HttpResponseStatus status) {
//大部分浏览器都是1.1
FullHttpResponse response =
new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, status,
Unpooled.copiedBuffer(content, CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE,
"text/plain;charset=UTF-8");
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
String result = "";
//接收到完成的http请求
FullHttpRequest httpRequest = (FullHttpRequest) msg;
try {
String path = httpRequest.uri();
String body = httpRequest.content().toString(CharsetUtil.UTF_8);
//get,post,delete,update
HttpMethod method = httpRequest.method();
if ("/test".equalsIgnoreCase(path)) {
result = "错误请求:" + path;
send(result, ctx, HttpResponseStatus.BAD_REQUEST);
return;
}
if (HttpMethod.GET.equals(method)) {
System.out.println("body:" + body);
result = "get Response=服务器返回数据";
send(result, ctx, HttpResponseStatus.OK);
}
} catch (Exception e) {
System.out.println("处理请求失败!");
e.printStackTrace();
} finally {
httpRequest.release();
}
}
/*
* 建立连接时,返回消息
*/
@Override
public void channelActive(ChannelHandlerContext ctx)
throws Exception {
System.out.println("连接的客户端地址:"
+ ctx.channel().remoteAddress());
}
}
1.4至此,最简单的netty编写的服务器就成了,启动主函数,通过浏览器访问
浏览器访问,返回的结果和正常调用接口是一样的,/test是被拦截的请求
默认是正常的请求
2 服务器增加压缩支持
2.1 修改ServerHandlerInit,添加上压缩的handler
public class ServerHandlerInit extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//http响应编码,服务端,对于request是解码,对于response是编码
pipeline.addLast("encode",new HttpResponseEncoder());
//这里我开始写成了HttpResponseDecoder,程序一直报类型转换异常,要注意
pipeline.addLast("decode",new HttpRequestDecoder());
///将http请求聚合在一起,可以通过对象.content()来调用,HttpObject, HttpMessage, HttpContent, FullHttpMessage
pipeline.addLast("aggre",new HttpObjectAggregator(10*1024*1024));
//添加压缩支持
pipeline.addLast("compressor",new HttpContentCompressor());
//业务操作,自定义业务操作
pipeline.addLast("busi",new BusiHandler());
}
}
2.2 访问接口,header出现了
2.3 transfer-encoding: chunked:接受压缩请求
3 Client支持
3.1 client主函数
public class HttpClient {
private static final boolean SSL = false;
public void connect(String host, int port) throws Exception {
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(workerGroup);
b.channel(NioSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
//这一句相当于下面的两句
ch.pipeline().addLast(new HttpClientCodec());
// ch.pipeline().addLast(new HttpResponseDecoder());
// ch.pipeline().addLast(new HttpRequestEncoder());
ch.pipeline().addLast("aggre",
new HttpObjectAggregator(10 * 1024 * 1024));
ch.pipeline().addLast("decompressor", new HttpContentDecompressor());
ch.pipeline().addLast("busi", new HttpClientInboundHandler());
}
});
// Start the client.
ChannelFuture f = b.connect(host, port).sync();
URI uri = new URI("/test");
String msg = "Hello";
DefaultFullHttpRequest request =
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
HttpMethod.GET,
uri.toASCIIString(),
Unpooled.wrappedBuffer(msg.getBytes("UTF-8")));
// 构建http请求
request.headers().set(HttpHeaderNames.HOST, host);
request.headers()
.set(HttpHeaderNames.CONNECTION,
HttpHeaderValues.KEEP_ALIVE);
request.headers()
.set(HttpHeaderNames.CONTENT_LENGTH,
request.content().readableBytes());
// 发送http请求
f.channel().write(request);
f.channel().flush();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
}
}
public static void main(String[] args) throws Exception {
HttpClient client = new HttpClient();
client.connect("127.0.0.1", HttpServer.port);
}
}
3.2 HttpClientInboundHandler
public class HttpClientInboundHandler
extends ChannelInboundHandlerAdapter {
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
//开始对服务器的响应做处理
FullHttpResponse httpResponse = (FullHttpResponse)msg;
System.out.println(httpResponse.headers());
ByteBuf content = httpResponse.content();
System.out.println(content.toString(CharsetUtil.UTF_8));
content.release();
}
}
建议直接使用开源的apache的HttpClient
4 Https支持
4.1 修改server主函数
public static void main(String[] args) throws Exception {
//netty为我们提供的ssl加密,缺省
SelfSignedCertificate ssc = new SelfSignedCertificate();
SslContext build = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
EventLoopGroup group = new NioEventLoopGroup();
ServerBootstrap b = new ServerBootstrap();
try {
b.group(group);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ServerHandlerInit(build));
// 服务器绑定端口监听
ChannelFuture f = b.bind(port).sync();
System.out.println("服务端启动成功,端口是:"+port);
// 监听服务器关闭监听
f.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}
4.2 修改handler,加上构造函数,并给ptieline加上ssl支持
public class ServerHandlerInit extends ChannelInitializer<SocketChannel> {
private SslContext build;
public ServerHandlerInit(SslContext build) {
this.build = build;
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
//启用ssl
pipeline.addLast(build.newHandler(ch.alloc()));
//http响应编码,服务端,对于request是解码,对于response是编码
// pipeline.addLast(new HttpServerCodec());这一句可以替代下面的几句
pipeline.addLast("encode",new HttpResponseEncoder());
//这里我开始写成了HttpResponseDecoder,程序一直报类型转换异常,要注意
pipeline.addLast("decode",new HttpRequestDecoder());
///将http请求聚合在一起,可以通过对象.content()来调用,HttpObject, HttpMessage, HttpContent, FullHttpMessage
pipeline.addLast("aggre",new HttpObjectAggregator(10*1024*1024));
//添加压缩支持
pipeline.addLast("compressor",new HttpContentCompressor());
//业务操作,自定义业务操作
pipeline.addLast("busi",new BusiHandler());
}
}
4.3 此时可通过https访问