HTTP 是基于请求/响应模式的:客户端向服务器发送一个 HTTP 请求,然后服务器将会返回一个 HTTP 响应。Netty 提供了多种编码器和解码处理器以简化对这个协议的使用。
一个HTTP 请求/响应可能由多个数据部分组成,并且它总是以一个LastHttpContent 部分作为结束。FullHttpRequest 和FullHttpResponse 消息是特殊的子类型,分别代表了完整的请求和响应。所有类型的HTTP 消息(FullHttpRequest、LastHttpContent等等)都实现了HttpObject 接口。
HTTP 解码处理器和编码器:
添加对http的支持:
聚合HTTP 消息
由于HTTP 的请求和响应可能由许多部分组成,因此你需要聚合它们以形成完整的消息。为了消除这项繁琐的任务,Netty 提供了一个聚合器,它可以将多个消息部分合并为FullHttpRequest 或者FullHttpResponse 消息。通过这样的方式,你将总是看到完整的消息内容。
HTTP 压缩
当使用HTTP 时,建议开启压缩功能以尽可能多地减小传输数据的大小。虽然压缩会带来一些CPU 时钟周期上的开销,但是通常来说它都是一个好主意,特别是对于文本数据来说。Netty 为压缩和解压缩提供了ChannelHandler 实现,它们同时支持gzip 和deflate 编码。
使用HTTPS
启用HTTPS 只需要将SslHandler 添加到ChannelPipeline 的ChannelHandler 组合中。
实战:开发一个http应用
服务启动类:
/**
* http服务 启动类
*/
public class HttpServer {
public static final int port = 8080; //设置服务端端口
private static EventLoopGroup boss = new NioEventLoopGroup(1);
private static EventLoopGroup work = new NioEventLoopGroup(2);
private static ServerBootstrap b = new ServerBootstrap();
private static final boolean SSL = false;
public static void main(String[] args) throws Exception {
final SslContext sslCtx;
if (SSL) {
//netty为我们提供的ssl加密,缺省
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContextBuilder.forServer(ssc.certificate(),
ssc.privateKey()).build();
} else {
sslCtx = null;
}
try {
b.group(boss,work);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ServerHandlerInit(sslCtx));
// 服务器绑定端口监听
ChannelFuture f = b.bind(port).sync();
System.out.println("服务端启动成功,端口是:"+port);
// 监听服务器关闭监听
f.channel().closeFuture().sync();
} finally {
boss.shutdownGracefully();
work.shutdownGracefully();
}
}
}
添加处理器
/**
* 初始化ChannelPipeline上的处理器
*/
public class ServerHandlerInit extends ChannelInitializer<SocketChannel> {
private final SslContext sslCtx;
public ServerHandlerInit(SslContext sslCtx) {
this.sslCtx = sslCtx;
}
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline ph = ch.pipeline();
if (sslCtx != null) {
ph.addLast(sslCtx.newHandler(ch.alloc()));
}
//http响应编码
ph.addLast("encode",new HttpResponseEncoder());
//http请求编码
ph.addLast("decode",new HttpRequestDecoder());
//聚合http请求
ph.addLast("aggre",new HttpObjectAggregator(10*1024*1024));
//启用http压缩
ph.addLast("compressor",new HttpContentCompressor());
//自己的业务处理
ph.addLast("busi",new BusinessHandler());
}
}
业务处理器
/**
* http 服务处理类
*/
public class BusinessHandler extends ChannelInboundHandlerAdapter {
/*
* 收到消息时,返回信息
*/
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg)
throws Exception {
String responseData = "";
//接收到完成的http请求
FullHttpRequest httpRequest = (FullHttpRequest)msg;
try{
String path = httpRequest.uri();
String body = httpRequest.content().toString(CharsetUtil.UTF_8);
HttpMethod method = httpRequest.method();
if(!"/index".equalsIgnoreCase(path)){
responseData = "非法请求:"+path;
doBackResponse(responseData,ctx,"text/plain;charset=UTF-8", HttpResponseStatus.BAD_REQUEST);
return;
}
//处理http GET请求
if(HttpMethod.GET.equals(method)){
System.out.println("body:"+body);
//返回的html
responseData = "<!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <title>Title</title>\n" +
"</head>\n" +
"<body>\n" +
"\n" +
"hello world\n" +
"\n" +
"</body>\n" +
"</html>";
doBackResponse(responseData,ctx,"text/html;charset=UTF-8", HttpResponseStatus.OK);
}
//处理http POST请求
if(HttpMethod.POST.equals(method)){
//.....
responseData = "hello world";
doBackResponse(responseData,ctx,"text/plain;charset=UTF-8", HttpResponseStatus.OK);
}
}catch(Exception e){
System.out.println("处理请求失败!");
e.printStackTrace();
}finally{
httpRequest.release();
}
}
/**
* 返回响应
* @param content
* @param ctx
* @param status
*/
private void doBackResponse(String content, ChannelHandlerContext ctx,String contentType,
HttpResponseStatus status){
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,status,
Unpooled.copiedBuffer(content, CharsetUtil.UTF_8));
response.headers().set(HttpHeaderNames.CONTENT_TYPE,contentType);
ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
/**
* 建立连接成功后
* @param ctx
* @throws Exception
*/
@Override
public void channelActive(ChannelHandlerContext ctx)
throws Exception {
System.out.println("连接的客户端地址:"
+ ctx.channel().remoteAddress());
}
}
启动HttpServer,访问http://localhost:8080/index
开发客户端
/**
* http 客户端
*/
public class HttpClient {
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.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch)
throws Exception {
ch.pipeline().addLast(new HttpClientCodec());
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("/index");
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",8080);
}
}
接受服务端的响应数据
/**
* 处理响应的数据
*/
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();
}
}
运行客户端可以看到接受到了来自服务端的消息