Netty(一):第一个Netty程序——HelloWorld

1.用idea创建一个maven工程

2.导入netty的依赖

<dependency>
    <groupId>io.netty</groupId>
    <artifactId>netty-all</artifactId>
    <version>4.1.10.Final</version>
</dependency>

3.写代码

(1) 主类:

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;

/**
 * Created by HK on 2018/5/1.
 */
public class MyFirstProject {


    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup partentGroup = new NioEventLoopGroup();
        EventLoopGroup childGroup = new NioEventLoopGroup();
        //创建一个服务器
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(partentGroup, childGroup).channel(NioServerSocketChannel.class).childHandler(new MyChannelInit());
        ChannelFuture future = serverBootstrap.bind(8888).sync();
    }
}
 

(2) 通道初始化类:

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


/**
 * Created by HK on 2018/5/1.
 */
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {

    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast("httpSocketHandler", new HttpServerCodec());
        pipeline.addLast("myHandler", new MyHandle());
    }
}
 

(3) 通道处理器类:

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;


/**
 * Created by HK on 2018/5/1.
 */
public class MyHandle extends SimpleChannelInboundHandler{


    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        ByteBuf context = Unpooled.copiedBuffer("hello world", CharsetUtil.UTF_8);
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK
                , context);
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
        response.headers().set(HttpHeaderNames.CONTENT_LENGTH,context.readableBytes());
        channelHandlerContext.writeAndFlush(response);
        }
}

这样就OK了,用浏览器访问以下。。

过程是梳理:

ServerBootstrap是一个服务器启动的类netty封装好的底层的细节.

NioEventLoopGroup是一个接收客户端请求的一个类,这个类是一个死循环一直监听接收,由于netty要求服务启动要用两个EventLoopGroup,一个是用来接受请求,一个用来处理请求,可以把他们命名为partent和child.

channel是通道,是netty设计的一个理念,接受请求过后会进入一个通道进行处理请求,通道里会需要一个初始化类ChannelInitializer ,我们要自定义一个类来继承它,来实现我们的自己的初始化,在这个类中我们来添加netty默认的和我们自己的handle,来处理请求。

MyHandle是我们自己的处理器,一般要继承SimpleChannelInboundHandler这个类,来实现自己的请求处理。

netty和其他框架不一样,入门有点困难,一个简单的程序就要有这么多过程,不像其他框架那么好理解,弄清上面这些过程,还的往后继续学习。。加油。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值