Java基于Netty實(shí)現(xiàn)Http server的實(shí)戰(zhàn)
HTTP協(xié)議基礎(chǔ)知識(shí)

HTTP(超文本傳輸協(xié)議,英文:HyperText Transfer Protocol,縮寫:HTTP)是基于TCP/IP協(xié)議的應(yīng)用層的協(xié)議,常用于分布式、協(xié)作式和超媒體信息系統(tǒng)的應(yīng)用層協(xié)議。

http協(xié)議的主要特點(diǎn):
(1)支持CS(客戶端/服務(wù)器)模式。
(2)使用簡(jiǎn)單,指定URL并攜帶必要的參數(shù)即可。
(3)靈活。傳輸非常多類型的數(shù)據(jù)對(duì)象,通過Content-Type指定即可。
(4)無狀態(tài)。
我們?nèi)粘g覽器輸入一個(gè)url,請(qǐng)求服務(wù)器就是用的http協(xié)議,url的格式如下:

http請(qǐng)求體的組成:
- 方法
- uri
- http版本
- 參數(shù)

請(qǐng)求方法:
- GET 請(qǐng)求獲取Request-URI所標(biāo)識(shí)的資源
- POST 在Request-URI所標(biāo)識(shí)的資源后附加新的數(shù)據(jù)
- HEAD 請(qǐng)求獲取由Request-URI所標(biāo)識(shí)的資源的響應(yīng)消息報(bào)頭
- PUT 請(qǐng)求服務(wù)器存儲(chǔ)一個(gè)資源,并用Request-URI作為其標(biāo)識(shí)
- DELETE 請(qǐng)求服務(wù)器刪除Request-URI所標(biāo)識(shí)的資源
- TRACE 請(qǐng)求服務(wù)器回送收到的請(qǐng)求信息,主要用于測(cè)試或診斷
- CONNECT 保留將來使用
- OPTIONS 請(qǐng)求查詢服務(wù)器的性能,或者查詢與資源相關(guān)的選項(xiàng)和需求
響應(yīng)狀態(tài)碼:
- 1xx Informational
- 2xx Success
- 3xx Redirection
- 4xx Client Error
- 5xx Server Error
HTTP 1 VS HTTP 2:
http 1不支持長(zhǎng)連接,每次請(qǐng)求建立連接,響應(yīng)后就關(guān)閉連接。HTTP2支持長(zhǎng)連接,連接復(fù)用。

Netty的http協(xié)議棧
netty提供了對(duì)http/https協(xié)議的支持,我們可以基于netty很容易寫出http應(yīng)用程序。
(1)編解碼
- HttpRequestEncoder 對(duì) HTTP 請(qǐng)求進(jìn)行編碼,用于客戶端出參
- HttpResponseEncoder 對(duì) HTTP 響應(yīng)進(jìn)行編碼,用于服務(wù)端出參
- HttpRequestDecoder 對(duì) HTTP 請(qǐng)求進(jìn)行解碼,用于服務(wù)端入?yún)⑻幚?/li>
- HttpResponseDecoder 對(duì) HTTP 響應(yīng)進(jìn)行解碼,用于客戶端對(duì)響應(yīng)結(jié)果解析解析
(2)請(qǐng)求體FullHttpRequest和響應(yīng)體FullHttpResponse
FullHttpRequest
- HttpRequest:請(qǐng)求頭信息對(duì)象;
- HttpContent:請(qǐng)求正文對(duì)象,一個(gè) FullHttpRequest 中可以包含多個(gè) HttpContent;
- LastHttpContent:標(biāo)記請(qǐng)求正文的結(jié)束,可能會(huì)包含請(qǐng)求頭的尾部信息;

FullHttpResponse
- HttpResponse:響應(yīng)頭信息對(duì)象;
- HttpContent:響應(yīng)正文對(duì)象,一個(gè) FullHttpResponse 中可以包含多個(gè) HttpContent;
- LastHttpContent:標(biāo)記響應(yīng)正文的結(jié)束,可能會(huì)包含響應(yīng)頭的尾部信息;

(3)HttpObjectAggregator-http消息聚合器
http的post請(qǐng)求包含3部分:
- request line(method、uri、protocol version)
- header
- body
HttpObjectAggregator能夠把多個(gè)部分整合在一個(gè)java對(duì)象中(另外消息體比較大的時(shí)候,還可能會(huì)分成多個(gè)消息,都會(huì)被聚合成一個(gè)整體對(duì)象),方便使用。

基于Netty實(shí)現(xiàn)http server
整體架構(gòu)設(shè)計(jì):

核心類SimpleHttpServer:
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
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.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import lombok.extern.slf4j.Slf4j;
/**
* http server based netty
*
* @author summer
* @version $Id: SimpleHttpServer.java, v 0.1 2022年01月26日 9:34 AM summer Exp $
*/
@Slf4j
public class SimpleHttpServer {
/**
* host
*/
private final static String host = "127.0.0.1";
/**
* 端口號(hào)
*/
private final static Integer port = 8085;
/**
* netty服務(wù)端啟動(dòng)方法
*/
public void start() {
log.info("SimpleHttpServer start begin ");
EventLoopGroup bossEventLoopGroup = new NioEventLoopGroup(1);
EventLoopGroup workerEventLoopGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap()
.group(bossEventLoopGroup, workerEventLoopGroup)
.channel(NioServerSocketChannel.class)
//開啟tcp nagle算法
.childOption(ChannelOption.TCP_NODELAY, true)
//開啟長(zhǎng)連接
.childOption(ChannelOption.SO_KEEPALIVE, true)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel c) {
c.pipeline().addLast(new HttpRequestDecoder())
.addLast(new HttpResponseEncoder())
.addLast(new HttpObjectAggregator(512 * 1024))
.addLast(new SimpleHttpServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(host, port).sync();
log.info("SimpleHttpServer start at port " + port);
channelFuture.channel().closeFuture().sync();
} catch (Exception e) {
log.error("SimpleHttpServer start exception,", e);
} finally {
log.info("SimpleHttpServer shutdown bossEventLoopGroup&workerEventLoopGroup gracefully");
bossEventLoopGroup.shutdownGracefully();
workerEventLoopGroup.shutdownGracefully();
}
}
}實(shí)際處理請(qǐng)求的netty handler是SimpleHttpServerHandler:
import com.alibaba.fastjson.JSON;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import lombok.extern.slf4j.Slf4j;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
/**
* http服務(wù)端處理handler實(shí)現(xiàn)
*
* @author summer
* @version $Id: SimpleHttpServerHandler.java, v 0.1 2022年01月26日 9:44 AM summer Exp $
*/
@Slf4j
public class SimpleHttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest fullHttpRequest) {
try {
log.info("SimpleHttpServerHandler receive fullHttpRequest=" + fullHttpRequest);
String result = doHandle(fullHttpRequest);
log.info("SimpleHttpServerHandler,result=" + result);
byte[] responseBytes = result.getBytes(StandardCharsets.UTF_8);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_0, HttpResponseStatus.OK,
Unpooled.wrappedBuffer(responseBytes));
response.headers().set("Content-Type", "text/html; charset=utf-8");
response.headers().setInt("Content-Length", response.content().readableBytes());
boolean isKeepAlive = HttpUtil.isKeepAlive(response);
if (!isKeepAlive) {
ctx.write(response).addListener(ChannelFutureListener.CLOSE);
} else {
response.headers().set("Connection", "keep-alive");
ctx.write(response);
}
} catch (Exception e) {
log.error("channelRead0 exception,", e);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
cause.printStackTrace();
ctx.close();
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
/**
* 實(shí)際處理
*
* @param fullHttpRequest HTTP請(qǐng)求參數(shù)
* @return
*/
private String doHandle(FullHttpRequest fullHttpRequest) {
if (HttpMethod.GET == fullHttpRequest.method()) {
QueryStringDecoder queryStringDecoder = new QueryStringDecoder(fullHttpRequest.uri());
Map<String, List<String>> params = queryStringDecoder.parameters();
return JSON.toJSONString(params);
} else if (HttpMethod.POST == fullHttpRequest.method()) {
return fullHttpRequest.content().toString();
}
return "";
}
}在主線程中啟動(dòng)服務(wù)端:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SimplehttpserverApplication {
public static void main(String[] args) {
SimpleHttpServer simpleHttpServer = new SimpleHttpServer();
simpleHttpServer.start();
SpringApplication.run(SimplehttpserverApplication.class, args);
}
}啟動(dòng)成功:

利用postman工具發(fā)起http請(qǐng)求進(jìn)行測(cè)試,這里簡(jiǎn)單測(cè)試get請(qǐng)求:
http://127.0.0.1:8085/?name=name1&value=v2

服務(wù)端日志也打印出了處理情況,處理正常:
19:24:59.629 [nioEventLoopGroup-3-3] INFO com.summer.simplehttpserver.SimpleHttpServerHandler - SimpleHttpServerHandler receive fullHttpRequest=HttpObjectAggregator$AggregatedFullHttpRequest(decodeResult: success, version: HTTP/1.1, content: CompositeByteBuf(ridx: 0, widx: 0, cap: 0, components=0))
GET /?name=name1&value=v2 HTTP/1.1
User-Agent: PostmanRuntime/7.26.8
Accept: */*
Cache-Control: no-cache
Postman-Token: 7dda7e2d-6f76-4008-8b74-c2b7d78f4b2e
Host: 127.0.0.1:8085
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
content-length: 0
19:24:59.629 [nioEventLoopGroup-3-3] INFO com.summer.simplehttpserver.SimpleHttpServerHandler - SimpleHttpServerHandler,result={"name":["name1"],"value":["v2"]}
到此這篇關(guān)于Java基于Netty實(shí)現(xiàn)Http server的實(shí)戰(zhàn)的文章就介紹到這了,更多相關(guān)Java Netty實(shí)現(xiàn)Http server內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java計(jì)算工作時(shí)間除去節(jié)假日以及雙休日
這篇文章主要為大家詳細(xì)介紹了java計(jì)算工作時(shí)間除去節(jié)假日以及雙休日的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
Spring Shell應(yīng)用程序開發(fā)流程解析
這篇文章主要介紹了Spring Shell應(yīng)用程序開發(fā)流程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10
MyBatis圖文并茂講解注解開發(fā)多對(duì)多查詢
這篇文章主要介紹了SpringBoot中Mybatis注解多對(duì)多查詢的實(shí)現(xiàn)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
Java中實(shí)現(xiàn)List分隔成子List詳解
大家好,本篇文章主要講的是Java中實(shí)現(xiàn)List分隔成子List詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01
Spring Boot 防止接口惡意刷新和暴力請(qǐng)求的實(shí)現(xiàn)
本文主要介紹了Spring Boot 防止接口惡意刷新和暴力請(qǐng)求的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
Eclipse 開發(fā)java 出現(xiàn)Failed to create the Java Virtual Machine錯(cuò)誤
這篇文章主要介紹了Eclipse 開發(fā)java 出現(xiàn)Failed to create the Java Virtual Machine錯(cuò)誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04
dom4j創(chuàng)建和解析xml文檔的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猟om4j創(chuàng)建和解析xml文檔的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06

