netty实现http服务器

简介: 1. HttpServerHttpServer作为http服务器的server端:public final class HttpServer { private static final String IP = "127.

1. HttpServer

HttpServer作为http服务器的server端:

public final class HttpServer {

    private static final String IP = "127.0.0.1";
    private static final int PORT = 9000;
    private static final int BIZ_THREAD_SIZE = 100;

    private static final Logger logger = LoggerFactory.getLogger(HttpServer.class);

    public static void main(String[] args) throws Exception {
        System.out.println("启动Server...");
        HttpServer.start();
    }

    public static void start() throws Exception {
        initServerConfig();

        // new NioEventLoopGroup()默认线程数为CPU核心数的两倍(Runtime.getRuntime().availableProcessors() * 2),所以这里不用额外指定。
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        // 这里worker不能使用默认,如果并发大的话需要增加线程数量
        EventLoopGroup workerGroup = new NioEventLoopGroup(BIZ_THREAD_SIZE);

        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    // ChannelOption.SO_BACKLOG对应的是tcp/ip协议listen函数中的backlog参数。函数listen(int socketfd, int backlog)用来初始化服务端可连接队列。
                    // 服务端处理客户端连接请求是顺序处理的,所以同一时间只能处理一个客户端连接,多个客户端来的时候,服务端将不能处理的客户端连接请求放在队列中等待处理,backlog参数指定了队列的大小。
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .handler(new LoggingHandler(LogLevel.INFO))
                    .childOption(ChannelOption.SO_KEEPALIVE, true) // 子channel配置,保持长连接
                    .childHandler(new HttpServerInitializer());

            // sync追踪代码可以得到:this.await(),实现阻塞
            ChannelFuture channelFuture = bootstrap.bind(IP, PORT).sync();

            // 启动失败则关闭线程组
            if (!channelFuture.isSuccess()) {
                shutdown(bossGroup, workerGroup);
                throw new RuntimeException(channelFuture.cause());
            }

            channelFuture.channel().closeFuture().sync();
            logger.info("bootstrap channel closed...");
        } finally {
            shutdown(bossGroup, workerGroup);
        }
    }

    // 添加路径映射和过滤器映射
    private static void initServerConfig() {
        ServerContext.setFilter(ServerContext.MAPPING_ALL, BaseFilter.class);
        ServerContext.setFilter("/template", TemplateFilter.class);
        ServerContext.setAction(ServerContext.MAPPING_ALL, DefaultIndexAction.class);
        ServerContext.setAction("/template", TemplateAction.class);
        ServerContext.setAction("/files", FileAction.class);
        ServerContext.setROOT("root");
        ServerContext.setPORT(8090);
    }

    private static void shutdown(EventLoopGroup bossGroup, EventLoopGroup workerGroup) {
        bossGroup.shutdownGracefully();
        workerGroup.shutdownGracefully();
    }
}

HttpServerInitializer设置http编解码器,以及请求aggregator等:

public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    public void initChannel(SocketChannel socketChannel) {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline
                //或者使用HttpRequestDecoder & HttpResponseEncoder
                .addLast(new HttpServerCodec())
                //把HttpObjectAggregator放入管道里。HttpObjectAggregator会把多个消息转换为一个单一的FullHttpRequest或是FullHttpResponse
                .addLast(new HttpObjectAggregator(1024 * 1024))
                //压缩Http消息
//                            .addLast(new HttpChunkContentCompressor())
                //大文件支持,文件上传时使用
//                            .addLast(new ChunkedWriteHandler())
                .addLast(new HttpServerExpectContinueHandler())
                .addLast(new HttpServerDispatcherHandler());
    }
}

入栈处理器,用于转发http请求,类似于DispatcherServlet:

public class HttpServerDispatcherHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        if (!(msg instanceof FullHttpRequest)) {
            return;
        }

        FullHttpRequest fullHttpRequest = (FullHttpRequest) msg;
        final Request customerRequest = Request.build(ctx, fullHttpRequest);
        final Response customerResponse = Response.build(ctx, customerRequest);
        if (customerRequest.getRequestUrl().equals(HttpHelper.FAVICON_ICO)) {
            return;
        }

        // 过滤器放行之后,执行action,实际处理业务逻辑
        boolean passFilter = doFilter(customerRequest, customerResponse);
        if (passFilter) {
            doAction(customerRequest, customerResponse);
        }

        //如果发送请求未被触发,则触发之,否则跳过。
        if(false == customerResponse.isSent()) {
            customerResponse.response();
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) {
        ctx.flush();
    }

    /**
     * 过滤器
     * @param request
     * @param response
     * @return
     */
    private boolean doFilter(Request request, Response response) {
        //全局过滤器
        Filter filter = ServerContext.getFilter(ServerContext.MAPPING_ALL);
        if(null != filter) {
            if(false == filter.doFilter(request, response)) {
                return false;
            }
        }

        //自定义过滤器
        filter = ServerContext.getFilter(request.getRequestUrl());
        if(null != filter) {
            if(false == filter.doFilter(request, response)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 请求处理器
     * 相当于servlet
     * @param request
     * @param response
     */
    private void doAction(Request request, Response response) {
        Action action = ServerContext.getAction(request.getRequestUrl());
        if (null == action) {
            //查找匹配所有路径的Action
            action = ServerContext.getAction(ServerContext.MAPPING_ALL);
            if(null == action) {
                // 非Action方法,调用静态文件读取
                action = Singleton.get(FileAction.class);
            }
        }

        action.doAction(request, response);
    }
}

过滤器

public interface Filter {
    
    /**
     * 执行过滤
     * @param request 请求对象
     * @param response 响应对象
     * @return 如果返回true,则继续执行下一步内容,否则中断
     */
    boolean doFilter(Request request, Response response);
}

过滤器示例:

public class TemplateFilter implements Filter {

    @Override
    public boolean doFilter(Request request, Response response) {
        System.out.println("welcome in template filter...");
        return true;
    }
}

action

action用于实际处理业务,顶层接口:

public interface Action {

    void doAction(Request request, Response response);
}

抽象Adapter:

public abstract class AbstractAction implements Action {

    @Override
    public void doAction(Request request, Response response) {
        String method = request.getMethod();
        if (HttpHelper.METHOD_GET.equals(method)) {
            doGet(request, response);
        } else if (HttpHelper.METHOD_POST.equals(method)) {
            doPost(request, response);
        } else if (HttpHelper.METHOD_PUT.equals(method)) {
            doPut(request, response);
        } else if (HttpHelper.METHOD_DELETE.equals(method)) {
            doDelete(request, response);
        } else if (HttpHelper.METHOD_HEAD.equals(method)) {
            doHead(request, response);
        } else if (HttpHelper.METHOD_OPTIONS.equals(method)) {
            doOptions(request, response);
        } else if (HttpHelper.METHOD_TRACE.equals(method)) {
            doTrace(request, response);
        }
    }

    protected void doGet(Request request, Response response) {}

    protected void doPost(Request request, Response response) {}

    protected void doPut(Request request, Response response) {}

    protected void doDelete(Request request, Response response) {}

    protected void doOptions(Request request, Response response) {}

    protected void doHead(Request request, Response response) {}

    protected void doTrace(Request request, Response response) {}
}

action示例:

public class TemplateAction extends AbstractAction {

    @Override
    protected void doGet(Request request, Response response) {
        response.setContentType(HttpHelper.CONTENT_TYPE_JSON);
        response.setContent("welcome in template action, do get...");
    }

    @Override
    protected void doPost(Request request, Response response) {
        response.setContent("welcome in template action, do post...");
    }
}

。。。

目录
相关文章
|
4月前
|
C# 图形学 开发者
Unity开发中使用UnityWebRequest从HTTP服务器下载资源。
总之,UnityWebRequest就是游戏开发者手中的万能钓鱼竿,既可以获取文本数据,也能钓上图片资源,甚至是那声音的涟漪。使用UnityWebRequest的时候,你需要精心准备,比如确定URL、配置请求类型和头信息;发起请求;巧妙处理钓获的数据;还需要机智面对网络波澜,处理各种可能出现的错误。按照这样的过程,数据的钓取将会是一次既轻松愉快也效率高效的编程钓鱼之旅。
230 18
|
4月前
|
应用服务中间件 网络安全 数据安全/隐私保护
网关服务器配置指南:实现自动DHCP地址分配、HTTP服务和SSH无密码登录。
哇哈哈,道具都准备好了,咱们的魔术秀就要开始了。现在,你的网关服务器已经魔法满满,自动分配IP,提供网页服务,SSH登录如入无人之境。而整个世界,只会知道效果,不会知道是你在幕后操控一切。这就是真正的数字世界魔法师,随手拈来,手到擒来。
240 14
|
3月前
|
JSON 前端开发 Go
Go语言实战:创建一个简单的 HTTP 服务器
本篇是《Go语言101实战》系列之一,讲解如何使用Go构建基础HTTP服务器。涵盖Go语言并发优势、HTTP服务搭建、路由处理、日志记录及测试方法,助你掌握高性能Web服务开发核心技能。
|
3月前
|
Go
如何在Go语言的HTTP请求中设置使用代理服务器
当使用特定的代理时,在某些情况下可能需要认证信息,认证信息可以在代理URL中提供,格式通常是:
304 0
|
5月前
|
存储 安全 数据安全/隐私保护
HFS-快速创建HTTP服务器
鉴于HFS的操作简便和方便快捷,它在满足快速,临时的文件分享和传输需求上,能够发挥出巨大的作用。只要明确了以上的安全警告,并做好了必需的安全设置,HFS将是一款实用的HTTP服务器工具。
431 9
|
10天前
|
弹性计算 ice
阿里云4核8G云服务器配置价格:热门ECS实例及CPU处理器型号说明
阿里云2025年4核8G服务器配置价格汇总,涵盖经济型e实例、计算型c9i等热门ECS实例,CPU含Intel Xeon及AMD EPYC系列,月费159元起,年付低至1578元,按小时计费0.45元起,实际购买享折扣优惠。
160 1
|
4天前
|
弹性计算 ice
阿里云4核8g服务器多少钱一年?1个月和1小时价格,省钱购买方法分享
阿里云4核8G服务器价格因实例类型而异,经济型e实例约159元/月,计算型c9i约371元/月,按小时计费最低0.45元。实际购买享折扣,1年最高可省至1578元,附主流ECS实例及CPU型号参考。
|
4天前
|
存储 弹性计算 安全
阿里云渠道商:新手如何选择阿里云ECS实例?
阿里云ECS凭借弹性扩展、稳定可靠与安全防护,助力企业高效上云。本文系统解析实例规格选择关键因素:业务场景匹配、性能评估、成本优化、地域部署与扩展规划,结合计费模式与实际需求,提供科学选型建议,助您精准匹配资源,提升云上效能。(238字)
|
4天前
|
存储 弹性计算 安全
阿里云渠道商:新手怎么选阿里云ECS实例规格?
本文深入解析阿里云ECS的弹性、稳定与安全优势,详解实例规格选择的关键因素,涵盖应用场景、性能需求、成本预算、地理位置与扩展性,助力用户精准选型,优化业务部署与成本效益。
|
1月前
|
弹性计算 Devops Shell
用阿里云 DevOps Flow 实现 ECS 部署自动化:从准备到落地的完整指南
阿里云 DevOps Flow 是一款助力开发者实现自动化部署的高效工具,支持代码流水线构建、测试与部署至ECS实例,显著提升交付效率与稳定性。本文详解如何通过 Flow 自动部署 Bash 脚本至 ECS,涵盖环境准备、流水线搭建、源码接入、部署流程设计及结果验证,助你快速上手云上自动化运维。
157 0

热门文章

最新文章