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的更多相关文章

  1. 基于websocket的netty demo

    前面2文 基于http的netty demo 基于socket的netty demo 讲了netty在http和socket的使用,下面讲讲netty如何使用websocket websocket是h ...

  2. 基于socket的netty demo

    前面一文说了 基于http的netty demo 和http不一样,http可以用浏览器来充当客户端调用,所以基于socket的netty,必须要编写客户端和服务器的代码 实现功能: 客户端给服务器发 ...

  3. 一个基于vue的仪表盘demo

    最近写了一个基于vue的仪表盘,其中 主要是和 transform 相关的 css 用的比较多.给大家分享一下,喜欢的话点个赞呗?嘿嘿 截图如下: 实际效果查看地址:https://jhcan333. ...

  4. 基于websocket vue 聊天demo 解决方案

    基于websocket vue 聊天demo 解决方案 demo 背景 电商后台管理的客服 相关技术 vuex axios vue websocket 聊天几种模型 一对一模型 一对一 消息只一个客户 ...

  5. 基于Lucene的文件检索Demo

    通过Lucene实现了简单的文件检索功能的Demo.这个Demo支持基于文件内容的检索,支持中文分词和高亮显示. 下面简单的介绍下核心的类 1)索引相关的类 1.FileIndexBuilder -- ...

  6. Java NIO框架Netty demo

    Netty是什么 Netty是一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络服务器和客户端程序. 也就是说,Netty 是一个基于NI ...

  7. netty demo

    Netty 4.0 demo   netty是一个异步,事件驱动的网络编程框架&工具,使用netty,可以快速开发从可维护,高性能的协议服务和客户端应用.是一个继mina之后,一个非常受欢迎的 ...

  8. 基于NIO的Netty网络框架

    Netty是一个高性能.异步事件驱动的NIO框架,它提供了对TCP.UDP和文件传输的支持,Netty的所有IO操作都是异步非阻塞的,通过Future-Listener机制,用户可以方便的主动获取或者 ...

  9. .Net语言 APP开发平台——Smobiler学习日志:基于Access数据库的Demo

    说明:该demo是基于Access数据库进行客户信息的新增.查看.编辑 新增客户信息和客户列表 Demo下载:https://github.com/comsmobiler/demo-videos  中 ...

随机推荐

  1. mySQL初学者需要掌握的【数据库与表的基本操作】

    本内容会持续更新的哦! 注:"字段"="列","记录''="行" 文章目录 一:数据库的基本操作 二.数据表的基本操作 1.创建与 ...

  2. charles 常用功能 (六)抓包结果列表指展示关注的接口(focus on 功能)

    添加关注的接口 2.启用关注接口,添加过滤的地址 3.重新抓包结果 不在上一步配置中的接口,都会隐藏在other host中

  3. django项目初始化

    1.为了方便管理app,我们添加专门的apps文件夹来存放所有的app.结构如下 1.1设置完apps文件夹以后我们需要对配置文件做相应的更改 1.1.1.在seetings.py里添加django文 ...

  4. 第7.14节 Python类中的实例方法详析

    第7.14节 Python类中的实例方法详析 一.    实例方法的定义 在本章前面章节已经介绍了类的实例方法,实例方法的定义有三种方式: 1.    类体中定义实例方法 第一种方式很简单,就是在类体 ...

  5. IIS-logfiles详解以及日志日期问题

    IIS日志的含义IIS是Internet Information Server的缩写,意思是英特网信息服务,日志就是运行的记录 IIS日志的默认目录就是%systemroot%\system32\lo ...

  6. java多线程之消费生产模型-使用synchronized解决虚假唤醒

    package com.wenshao.juc; /** * 生产者和消费者案例 * * @author Administrator * */ public class TestProductorAn ...

  7. SpringBoot集成Swagger2并配置多个包路径扫描

    1. 简介   随着现在主流的前后端分离模式开发越来越成熟,接口文档的编写和规范是一件非常重要的事.简单的项目来说,对应的controller在一个包路径下,因此在Swagger配置参数时只需要配置一 ...

  8. AOP 有几种实现方式?

    1. 回顾 AOP 是什么? 维基百科解释如下: 面向切面的程序设计(Aspect-oriented programming,AOP,又译作面向方面的程序设计.剖面导向程序设计)是计算机科学中的一种程 ...

  9. UML—20—001

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...

  10. 一次数独生成及解题算法的剖析(Java实现)

    数独生成及解题算法剖析(Java实现) 关键词 数独9x9 数独生成算法 数独解题算法 序言 最近业务在巩固Java基础,编写了一个基于JavaFX的数独小游戏(随后放链接).写到核心部分发现平时玩的 ...