栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

网络I/o编程模型14 netty的http协议服务器

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

网络I/o编程模型14 netty的http协议服务器

一 案例 1.1 案例描述

1.netty服务器使用6666端口监听,浏览器发送的请求:http://localhost:6666/

2.服务器收到消息后,向浏览器发送消息“hello,我是服务器,你在干什么!!!”,并对特定资源进行过滤。

1.2 代码

1.server端

package com.ljf.netty.netty.http;

import com.ljf.netty.netty.tcp.NettyTcpServerHandler3;
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;


public class NettyHttpServer {
    public static void main(String[] args) {
        //创建连个线程组bossGroup和workerGroup
        EventLoopGroup bossGroup=new NioEventLoopGroup(1);//默认为cpu核数*2;
        EventLoopGroup workGroup=new NioEventLoopGroup();
        try {
        //创建服务器端的启动对象,配置参数
        ServerBootstrap bootstrap=new ServerBootstrap();
        //使用链式编程来进行设置
        bootstrap.group(bossGroup,workGroup) //设连个线程组
                .channel(NioServerSocketChannel.class) //使用NioSocketChannel作为服务器的通道实现
               // .option(ChannelOption.SO_BACKLOG,128)//设置线程队列得到的连接个数
                //.childOption(ChannelOption.SO_KEEPALIVE,true)  //设置保持活动连接状态
                .childHandler(new NettyHttpInitializer());//给我们的workerGroup的EventLopp对应的管道设置处理器,自定义的。
        System.out.println(".......netty服务器 is ready.....");
        //启动服务器(并绑定端口)
        ChannelFuture channelFuture=bootstrap.bind(6666).sync();
        //对关闭的通道进行监听
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        finally {
            bossGroup.shutdownGracefully();
            workGroup.shutdownGracefully();
        }
    }
}

2.适配器

public class NettyHttpInitializer extends ChannelInitializer {
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
           //向管道加入处理器
        //得到管道
        ChannelPipeline pipeline=ch.pipeline();
        //加入一个解码器,HttpServerCodec 是netty提供的处理http的编-解码器
        pipeline.addLast("MyHttpServerCodec",new HttpServerCodec());
        // 增加一个自定义的 handler
        pipeline.addLast("MyDefineHandler",new DefineServerHandler());
    }
}

3.自定义handler

package com.ljf.netty.netty.http;

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 DefineServerHandler extends SimpleChannelInboundHandler {
    // 读取客户端数据
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
            //判断msg 是不是httprequest请求
        if(msg instanceof HttpRequest){
            System.out.println("pipeline hashcode:"+ctx.pipeline().hashCode()+" handler 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());
            ByteBuf content=null;
            if("/mb".equals(uri.getPath())){
                System.out.println("请求含有非路径上下文,不能理会....");
                //回复给浏览器的信息
              content= Unpooled.copiedBuffer("请求含有非路径上下文,不能理会....", CharsetUtil.UTF_8);
               // return;
            }
            else{
                //回复给浏览器的信息
                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/plain");
            response.headers().set(HttpHeaderNames.ACCEPT_CHARSET,"utf-8");
            response.headers().set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());

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

4.测试结果

 过滤资源

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939870.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号