Netty 实现HttpServer

  使用Netty 实现一个简单的Http服务器,可以接受客户端的请求,并且实现拒绝请求一些请求,比如请求favicon.ico 网站图标的时候拒绝请求。

  服务器收到客户端请求之后回传一个简单的消息: "hello, 我是服务器"

1. 代码

1. NettyHttpServerHandler

  处理Http请求,如果不是Http请求则拒绝处理。

复制代码
package netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

import java.net.URI;

public class NettyHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {

    //channelRead0 读取客户端数据
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
        System.out.println("对应的channel=" + ctx.channel() + " pipeline=" + ctx
                .pipeline() + " 通过pipeline获取channel" + ctx.pipeline().channel());
        System.out.println("当前ctx的handler=" + ctx.handler());

        //判断 msg 是不是 httprequest请求
        if (msg instanceof HttpRequest) {
            System.out.println("ctx 类型=" + ctx.getClass());
            System.out.println("pipeline hashcode" + ctx.pipeline().hashCode() + " TestHttpServerHandler hash=" + this.hashCode());
            System.out.println("msg 类型=" + msg.getClass());
            System.out.println("客户端地址" + ctx.channel().remoteAddress());

            //获取到
            HttpRequest httpRequest = (HttpRequest) msg;
            //获取uri, 过滤指定的资源
            URI uri = new URI(httpRequest.uri());
            if ("/favicon.ico".equals(uri.getPath())) {
                System.out.println("请求了 favicon.ico, 不做响应");
                return;
            }
            //回复信息给浏览器 [http协议]

            ByteBuf content = Unpooled.copiedBuffer("hello, 我是服务器", CharsetUtil.UTF_8);
            //构造一个http的相应,即 httpresponse
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=UTF-8");
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());

            //将构建好 response返回
            ctx.writeAndFlush(response);
        }
    }

}
复制代码

2. NettyServerInitializer 初始化器

复制代码
package netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;

public class NettyServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        // 向管道加入处理器
        // 得到管道
        ChannelPipeline pipeline = ch.pipeline();

        // 加入一个netty 提供的httpServerCodec codec =>[coder - decoder]
        // HttpServerCodec 说明
        //1. HttpServerCodec 是netty 提供的处理http的 编-解码器
        pipeline.addLast("MyHttpServerCodec", new HttpServerCodec());
        //2. 增加一个自定义的handler
        pipeline.addLast("MyTestHttpServerHandler", new NettyHttpServerHandler());

        System.out.println("ok~~~~");
    }
}
复制代码

3. NettyHttpServer主类

复制代码
package netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class NettyHttpServer {

    public static void main(String[] args) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new NettyServerInitializer());
            ChannelFuture channelFuture = serverBootstrap.bind(6565).sync();
            System.out.println("server is ok");
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }
    }
}
复制代码

2. 测试

浏览器发送请求:

 服务器端日志如下:

复制代码
ok~~~~
对应的channel=[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.NettyHttpServerHandler)} 通过pipeline获取channel[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045]
当前ctx的handler=netty.NettyHttpServerHandler@464839a5
ctx 类型=class io.netty.channel.DefaultChannelHandlerContext
pipeline hashcode55230216 TestHttpServerHandler hash=1179138469
msg 类型=class io.netty.handler.codec.http.DefaultHttpRequest
客户端地址/127.0.0.1:56045
对应的channel=[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.NettyHttpServerHandler)} 通过pipeline获取channel[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045]
当前ctx的handler=netty.NettyHttpServerHandler@464839a5
对应的channel=[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.NettyHttpServerHandler)} 通过pipeline获取channel[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045]
当前ctx的handler=netty.NettyHttpServerHandler@464839a5
ctx 类型=class io.netty.channel.DefaultChannelHandlerContext
pipeline hashcode55230216 TestHttpServerHandler hash=1179138469
msg 类型=class io.netty.handler.codec.http.DefaultHttpRequest
客户端地址/127.0.0.1:56045
请求了 favicon.ico, 不做响应
对应的channel=[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.NettyHttpServerHandler)} 通过pipeline获取channel[id: 0x0d2eb1d8, L:/127.0.0.1:6565 - R:/127.0.0.1:56045]
当前ctx的handler=netty.NettyHttpServerHandler@464839a5
复制代码

 

posted @   QiaoZhi  阅读(1117)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-03-08 Zookeeper客户端Curator基本API
2019-03-08 zookeeperCli和Java操作zookeeperAPI
2018-03-08 【HugeChm】HugeChm制作chm帮助文档
2018-03-08 Struts2的使用注解配置Action(零配置)
2018-03-08 struts2 package 属性说明
2018-03-08 关于Struts2中param的作用
点击右上角即可分享
微信分享提示