Netty执行流程分析与重要组件介绍
一、环境搭建
创建工程,引入Netty依赖

二、基于Netty的请求响应Demo
1、TestHttpServerHandle 处理器。读取客户端发送过来的请求,并且向客户端返回hello world响应
package com.example.firstexample; 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; public class TestHttpServerHandle extends SimpleChannelInboundHandler<HttpObject>{ //读取客户端发送过来的请求,并且向客户端返回响应
protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception { if(httpObject instanceof HttpRequest){
ByteBuf content = Unpooled.copiedBuffer("Hello world", CharsetUtil.UTF_8);
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,content);
response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
//写回客户端
channelHandlerContext.writeAndFlush(response);
} }
}
2、TestServerInitializer 类
public class TestServerInitializer extends ChannelInitializer<SocketChannel> {
protected void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("httpServerCode", new HttpServerCodec());
pipeline.addLast("testHttpServerHandler", new TestHttpServerHandle());
}
}
3、TestServer 类
package com.example.firstexample; 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 TestServer { public static void main(String[] args) throws Exception { //bossGroup获取连接
EventLoopGroup bossGroup = new NioEventLoopGroup();
//workerGroup处理连接
EventLoopGroup workerGroup = new NioEventLoopGroup();
try{
ServerBootstrap serverBootstrap = new ServerBootstrap();
//1、启动Bootstrap服务器,服务器关联两个事件循环组bossGroup,workerGroup
//2、并且关联事件处理器,处理器使用TestServerInitializer定义,如TestServerInitializer里使用了处理器TestHttpServerHandle
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
.childHandler(new TestServerInitializer()); ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
channelFuture.channel().closeFuture().sync(); }finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
} }
}
启动main方法
4、测试

5、总结Netty执行流程
1)启动Bootstrap服务器,服务器关联两个事件循环组bossGroup(获取连接),workerGroup(处理连接)
2)并且关联事件处理器,处理器使用TestServerInitializer定义,如TestServerInitializer里使用了处理器TestHttpServerHandle
Netty执行流程分析与重要组件介绍的更多相关文章
- 报时机器人的rasa shell执行流程分析
本文以报时机器人为载体,介绍了报时机器人的对话能力范围.配置文件功能和训练和运行命令,重点介绍了rasa shell命令启动后的程序执行过程. 一.报时机器人项目结构 1.对话能力范围 (1)能够 ...
- Spring 文件上传MultipartFile 执行流程分析
在了解Spring 文件上传执行流程之前,我们必须知道两点: 1.Spring 文件上传是基于common-fileUpload 组件的,所以,文件上传必须引入此包 2.Spring 文件上传需要在X ...
- 深入浅出Mybatis系列(十)---SQL执行流程分析(源码篇)
最近太忙了,一直没时间继续更新博客,今天忙里偷闲继续我的Mybatis学习之旅.在前九篇中,介绍了mybatis的配置以及使用, 那么本篇将走进mybatis的源码,分析mybatis 的执行流程, ...
- 深入浅出Mybatis系列十-SQL执行流程分析(源码篇)
注:本文转载自南轲梦 注:博主 Chloneda:个人博客 | 博客园 | Github | Gitee | 知乎 最近太忙了,一直没时间继续更新博客,今天忙里偷闲继续我的Mybatis学习之旅.在前 ...
- ThinkPHP 框架执行流程分析
总体来说,应用的流程涉及到几个文件:Index.phpThinkPHP.phpThink.class.phpApp.class.phpDispatcher.class.phpThinkPHP/Mode ...
- [转]两表join的multi update语句在MySQL中的执行流程分析
出自:http://hedengcheng.com/?p=209 两表join的multi update语句,执行结果与预计不一致的分析过程 — multi update结论在实际应用中,不要轻易使用 ...
- Hive SQL执行流程分析
转自 http://www.tuicool.com/articles/qyUzQj 最近在研究Impala,还是先回顾下Hive的SQL执行流程吧. Hive有三种用户接口: cli (Command ...
- spark-sql执行流程分析
spark-sql 架构 图1 图1是sparksql的执行架构,主要包括逻辑计划和物理计划几个阶段,下面对流程详细分析. sql执行流程 总体流程 parser:基于antlr框架对 sql解析,生 ...
- Dalvik模式下System.loadLibrary函数的执行流程分析
本文博客地址:http://blog.csdn.net/qq1084283172/article/details/78212010 Android逆向分析的过程中免不了碰到Android so被加固的 ...
随机推荐
- Linux--重要文件
目录 /etc/resolv.conf /etc/host /etc/sysconfig/network /etc/fstab /etc/rc.local /etc/profile /etc/bash ...
- golang的channel实现
golang的channel实现位于src/runtime/chan.go文件.golang中的channel对应的结构是: // Invariants: // At least one of c.s ...
- UART 串口示例代码
/* uart_tx.c */ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #i ...
- Docker存储卷篇
Docker存储卷篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.写时复制(COW)机制 所谓写时复制的效果如上图所示: Docker镜像由多个只读层叠加而成,启动容器 ...
- 个人第五次作业-alpha2测试
课程属于课程 课程链接 作业要求 作业要求链接 团队名称 你的代码我的发 https://www.cnblogs.com/skrchou/p/11885706.html 测试人名称 颜依婷 测试人学号 ...
- storm整合kafka storm-kafka-client
pom.xml-注意jar-log4j---------------------<dependencies> <dependency> <groupId>org.a ...
- 《TensorFlow2深度学习》学习笔记(二)手动搭建并测试简单神经网络(附mnist.npz下载方式)
本实验使用了mnist.npz数据集,可以使用在线方式导入,但是我在下载过程中老是因为网络原因被打断,因此使用离线方式导入,离线包已传至github方便大家下载: https://github.com ...
- Kotlin高阶函数与函数式编程详解
函数可变参数: 在上一次https://www.cnblogs.com/webor2006/p/11518425.html中学到了可变参考,关于可变参数有如下规则说明: “一个方法中,只允许一个参数为 ...
- anyproxy学习4-Linux(Centos)搭建anyproxy环境
前言 anyproxy可以跨平台使用,前面第一篇是搭建在windows机器上,本篇讲如何在linux上搭建anyproxy环境,当然有mac的小伙伴也可以用mac去搭建一个环境. nodejs安装 a ...
- redis node 常用命令
命令窗口 flushall //清空全库 keys * //查看所有 HMSET user1 name liujinyu age 25 //哈希 添加多个值 HSET user1 sex man // ...