适合初中级Java程序员修炼手册从0搭建整个Web项目(一)( 二 )
复制代码HttpServerNetty 编写 HTTP 服务器主类
package com.xiaoliuliu.six.finger.web.server;import io.netty.bootstrap.ServerBootstrap;import io.netty.channel.ChannelFuture;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.handler.logging.LogLevel;import io.netty.handler.logging.LoggingHandler;import java.net.InetSocketAddress;/** * @author 小六六 * @version 1.0 * @date 2020/10/13 11:41 * Netty 编写 HTTP 服务器 * 主类 */public class HttpServer {/*** @Des 端口 http请求的端口* @Author 小六六* @Date 2020/10/13 11:42* @Param* @Return*/int port;/*** @Des 构造方法* @Author 小六六* @Date 2020/10/13 11:42* @Param* @Return*/public HttpServer(int port) {this.port = port;}/*** @Des 服务的启动方法* @Author 小六六* @Date 2020/10/13 11:43* @Param* @Return*/public void start() throws Exception {//启动引导类ServerBootstrap bootstrap = new ServerBootstrap();NioEventLoopGroup boss = new NioEventLoopGroup();NioEventLoopGroup work = new NioEventLoopGroup();bootstrap.group(boss, work).handler(new LoggingHandler(LogLevel.DEBUG)).channel(NioServerSocketChannel.class).childHandler(new HttpServerInitializer());ChannelFuture cf = bootstrap.bind(new InetSocketAddress(port)).sync();System.out.println(" server start up on port : " + port);cf.channel().closeFuture().sync();}}复制代码
HttpServerInitializerpackage com.xiaoliuliu.six.finger.web.server;import io.netty.channel.ChannelHandler;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.socket.SocketChannel;import io.netty.handler.codec.http.HttpObjectAggregator;import io.netty.handler.codec.http.HttpServerCodec;/** * @author 小六六 * @version 1.0 * @date 2020/10/13 11:57 * 用于配置 pipeline的处理链 */public class HttpServerInitializer extends ChannelInitializer {@Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {ChannelPipeline pipeline = socketChannel.pipeline();// http 编解码pipeline.addLast(new HttpServerCodec());// http 消息聚合器pipeline.addLast("httpAggregator",new HttpObjectAggregator(512*1024));// 请求处理器pipeline.addLast(new HttpRequestHandler());}}复制代码
HttpRequestHandlerpackage com.xiaoliuliu.six.finger.web.server;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandler;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.handler.codec.http.*;import io.netty.util.AsciiString;import io.netty.util.CharsetUtil;import java.util.HashMap;import java.util.Map;import static io.netty.handler.codec.http.HttpUtil.is100ContinueExpected;/** * @author 小六六 * @version 1.0 * @date 2020/10/13 12:01 * 核心处理http请求的类 , 包括url的匹配核心方法都是在channelRead0方法 */public class HttpRequestHandler extends SimpleChannelInboundHandler
推荐阅读
- 计算机专业大一下学期,该选择学习Java还是Python
- 未来想进入AI领域,该学习Python还是Java大数据开发
- 学习大数据是否需要学习JavaEE
- 从事Java开发时发现基础差,是否应该选择辞职自学一段时间
- 2021年Java和Python的应用趋势会有什么变化?
- 康宁列举蓝宝石玻璃种种缺点 不适合用于消费级市场
- 淘云科技吴玉胜:阿尔法蛋词典笔更适合中小学生使用
- 普通大学计算机专业的本科生,该选择主攻前端还是Java
- 小米11已变“真香”?先别急,这3款旗舰机可能更适合你
- 如何提供老年人适合的数字医疗?平安亮出“一碗汤”服务