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被加固的 ...
随机推荐
- Nginx 反向代理Tomcat服务器获取真实IP问题
1.nginx.conf 配置 修改 Server location配置 增加 proxy_set_header X-Real-IP $remote_addr; #保留代理之前的真实客户端ip pro ...
- Swagger Liunx环境搭建(亲测百分百可用)
一.安装nodejs 下载编译好的nodejs安装包,下载地址: https://nodejs.org/dist/v10.10.0/ (作者下载的10.10.0,可根据自己需要下载不同版本) 将下载好 ...
- JavaScript笔记02_对象
目录 1. 函数 1. 函数创建 2. 函数的参数 2. return.break.continue 3. 立即执行函数 4. 对象 5. 枚举对象中的属性 6. 声明提前 1.变量的声明提前 2. ...
- H3C 802.11b/g工作频段划分图
- Windows Cmd 命令管理服务
今天在Windows 干净环境上安装软件过程中,安装完成后,发现部署在IIS 上的网站无法使用,提示 "您提交的参数有误!,请重新提交" 纯净的windows 7 x64位环境, ...
- javascript一个在网页上画线的库
文章;安利一个绘制指引线的JS库leader-line 一个在网页上划线的库感觉很不错.
- Codeforces B. Too Easy Problems
题目描述: time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- C#启动计算器并设计算器为活动窗口
启动计算器,并获取焦点 using System; using System.Runtime.InteropServices; namespace ConsoleApplication3 { clas ...
- 《逆袭团队》第九次团队作业:Beta冲刺与验收准备
项目 内容 软件工程 任课教师博客主页链接 作业链接地址 团队作业9:Beta冲刺与团队项目验收 团队名称 逆袭团队 具体目标 (1)掌握软件黑盒测试技术:(2)学会编制软件项目总结PPT.项目验收报 ...
- python无法导入自己的模块的解决办法