基于http的netty demo
1.引入netty的pom
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.10.Final</version>
</dependency>
2.编写代码
1 package com.bill.httpdemo;
2
3
4 import io.netty.bootstrap.ServerBootstrap;
5 import io.netty.channel.ChannelFuture;
6 import io.netty.channel.EventLoopGroup;
7 import io.netty.channel.nio.NioEventLoopGroup;
8 import io.netty.channel.socket.nio.NioServerSocketChannel;
9
10 public class HttpServer {
11
12 public static void main(String[] args) throws Exception {
13
14 // 这2个group都是死循环,阻塞式
15 EventLoopGroup bossGroup = new NioEventLoopGroup();
16 EventLoopGroup workerGroup = new NioEventLoopGroup();
17
18 try {
19 ServerBootstrap serverBootstrap = new ServerBootstrap();
20 serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).
21 childHandler(new HttpServerInitializer());
22
23 ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
24 channelFuture.channel().closeFuture().sync();
25 } finally {
26 bossGroup.shutdownGracefully();
27 workerGroup.shutdownGracefully();
28 }
29 }
30
31 }
32
33 package com.bill.httpdemo;
34
35 import io.netty.buffer.ByteBuf;
36 import io.netty.buffer.Unpooled;
37 import io.netty.channel.ChannelHandlerContext;
38 import io.netty.channel.SimpleChannelInboundHandler;
39 import io.netty.handler.codec.http.*;
40 import io.netty.util.CharsetUtil;
41
42 import java.net.URI;
43
44 public class HttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {
45
46 /**
47 * 读取客户端请求,并且返回给客户端数据的方法
48 */
49 @Override
50 protected void channelRead0(ChannelHandlerContext channelHandlerContext, HttpObject httpObject) throws Exception {
51
52 if(!(httpObject instanceof HttpRequest)) {
53 return;
54 }
55
56 System.out.println("excute channelRead0");
57
58 HttpRequest httpRequest = (HttpRequest) httpObject;
59
60 URI uri = new URI(httpRequest.uri());
61 System.out.println(uri.getPath());
62
63 ByteBuf content = Unpooled.copiedBuffer("Hello World", CharsetUtil.UTF_8);
64 FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
65 HttpResponseStatus.OK, content);
66 response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
67 response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
68
69 channelHandlerContext.writeAndFlush(response);
70 }
71 }
72
73 package com.bill.httpdemo;
74
75 import io.netty.channel.ChannelInitializer;
76 import io.netty.channel.ChannelPipeline;
77 import io.netty.channel.socket.SocketChannel;
78 import io.netty.handler.codec.http.HttpServerCodec;
79
80 public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {
81
82 @Override
83 protected void initChannel(SocketChannel socketChannel) throws Exception {
84
85 ChannelPipeline pipeline = socketChannel.pipeline();
86
87 pipeline.addLast("HttpServerCodec", new HttpServerCodec());
88 pipeline.addLast("HttpServerHandler", new HttpServerHandler());
89 }
90 }
3.启动服务器,执行HttpServer的main方法
4.浏览器输入网址:
http://127.0.0.1:8899/hello/world
客户端输出:
服务器输出:
完整代码下载:
https://download.csdn.net/download/mweibiao/10551574
基于http的netty demo的更多相关文章
- 基于websocket的netty demo
前面2文 基于http的netty demo 基于socket的netty demo 讲了netty在http和socket的使用,下面讲讲netty如何使用websocket websocket是h ...
- 基于socket的netty demo
前面一文说了 基于http的netty demo 和http不一样,http可以用浏览器来充当客户端调用,所以基于socket的netty,必须要编写客户端和服务器的代码 实现功能: 客户端给服务器发 ...
- 一个基于vue的仪表盘demo
最近写了一个基于vue的仪表盘,其中 主要是和 transform 相关的 css 用的比较多.给大家分享一下,喜欢的话点个赞呗?嘿嘿 截图如下: 实际效果查看地址:https://jhcan333. ...
- 基于websocket vue 聊天demo 解决方案
基于websocket vue 聊天demo 解决方案 demo 背景 电商后台管理的客服 相关技术 vuex axios vue websocket 聊天几种模型 一对一模型 一对一 消息只一个客户 ...
- 基于Lucene的文件检索Demo
通过Lucene实现了简单的文件检索功能的Demo.这个Demo支持基于文件内容的检索,支持中文分词和高亮显示. 下面简单的介绍下核心的类 1)索引相关的类 1.FileIndexBuilder -- ...
- Java NIO框架Netty demo
Netty是什么 Netty是一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. 也就是说,Netty 是一个基于NI ...
- netty demo
Netty 4.0 demo netty是一个异步,事件驱动的网络编程框架&工具,使用netty,可以快速开发从可维护,高性能的协议服务和客户端应用.是一个继mina之后,一个非常受欢迎的 ...
- 基于NIO的Netty网络框架
Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用户可以方便的主动获取或者 ...
- .Net语言 APP开发平台——Smobiler学习日志:基于Access数据库的Demo
说明:该demo是基于Access数据库进行客户信息的新增.查看.编辑 新增客户信息和客户列表 Demo下载:https://github.com/comsmobiler/demo-videos 中 ...
随机推荐
- 如何使用TradingView(TV)回测数字货币交易策略
更多精彩内容,欢迎关注公众号:数量技术宅.想要获取本期分享的完整策略代码,请加技术宅微信:sljsz01 TradingView平台简介 前段时间,有粉丝找到技术宅,表示他有一个常用的交易平台,叫做T ...
- day6(短信验证接口)
1.注册容联云账号 1.1注册账号 https://www.yuntongxun.com/user/login 1.2登录即可看到开发者账号信息 1.3 添加测试账号 2.使用容联云发送代码测试 ' ...
- 第8.25节 Python风格的__getattribute__属性访问方法语法释义及使用
一. 引言 在<第8.13节 Python类中内置方法__repr__详解>老猿介绍了在命令行方式直接输入"对象"就可以调用repr内置函数或__repr__方法查看对 ...
- Docker下Python Flask+Redis+MySQL+RQ队列简单配置
本篇博文主要讲解Docker下使用RQ队列的通信配置,主要是网上的部分文章写的不太清楚,特写一篇 作者使用docker-compose.yml文件调度各部分文件Dockerfile,起初是这样写的 v ...
- AtCoder Regular Contest 109
Contest Link 为什么还没有 Official Editorial 啊--哦,原来是日文题解,那没事了. A - Hands 有两幢 100 层的楼房 \(A,B\) ,将地面所在的楼层称为 ...
- 三、Zookeeper简介
一.简介 zookeeper 主要使用场景:分布式系统的分布式协同服务.协同工作就是通过某种方式,让着节点的信息能够同步和共享,依赖于进程间的通信.通信方式有俩种. 通过网络进行信息共享 现实工作中, ...
- Python之Windows服务
1.首先要安装pywin32-220.win-amd64-py2.7.exe 2. SvcDoRun:服务启动的时候会执行的方法 SvcStop:服务停止的时候会执行的方法 # coding=utf- ...
- 优化Windows电脑常见方法,提高速度,释放硬盘C盘
开始,我们首先让电脑变得易于使用一,提高开机速度常见的使电脑变卡的原因是:一台电脑同时安装了多个杀毒软件.一台电脑安装多个杀毒软件不仅占用你电脑大量内存.有时甚至会产生冲突,这会导致电脑运行非常缓慢, ...
- Day5 - 05 函数的参数-关键字参数
可变参数可以传入任意个参数,并在函数调用时自动组为一个tuple,而关键字参数允许传入任意个携带参数名的参数,这些关键字参数在函数内部自动组为一个dict. >>> ...
- Oracle数据泵常用命令
导读:expdp和impdp是oracle数据库之间移动数据的工具,本文简单总结了数据泵的常用命令,希望对大家有帮助. 前言 expdp和impdp是oracle数据库之间移动数据的工具.expd ...