前言

动机

最近在学习Netty框架,发现Netty是支持Http协议的。加上以前看过Spring-MVC的源码,就想着二者能不能结合一下,整一个简易的web框架(PS:其实不是整,是抄)

效果

项目地址:terabithia

0.3版本使用效果如下,其实就是Spring-MVC的Controller的写法

@RestController
@RequestMapping(value = "/hello")
public class HelloController { /**
* request url:/hello/testGet?strParam=test&intParam=1&floatParam=2&doubleParam=3
*/
@RequestMapping(value = "/testGet", method = {RequestMethod.GET})
public Object testGet(ParamWrapperRequest request, String strParam, Integer intParam, Float floatParam, Double doubleParam) throws Exception{
System.out.println(request.getParameterNames());
// 获取参数
System.out.println("strParam:" + strParam);
System.out.println("intParam:" + intParam);
System.out.println("floatParam:" + floatParam);
System.out.println("doubleParam:" + doubleParam); System.out.println("testGet");
return request.getParameterMap();
}
}

前置条件

以下列出各个版本需要掌握的知识:

  • 0.1版本:Netty
  • 0.2版本:Netty
  • 0.3版本:Netty + Spring-MVC

0.1版本

0.1版本就是Netty原生API对Http协议的支持

如何实现

HttpServer初始化Netty服务,HttpServerCodecHttpObjectAggregator提供了对Http的支持,HttpServerHandler处理业务逻辑。

public class HttpServer {

    private static final int PORT = 8080;

    public static void main(String[] args) {
final EventLoopGroup bossGroup = new NioEventLoopGroup(1);
final EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
final ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
// 临时存放已完成三次握手的请求的队列
.option(ChannelOption.SO_BACKLOG, 1024)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline()
.addLast(new HttpServerCodec())
//把多个消息转换为一个单一的FullHttpRequest或是FullHttpResponse
.addLast(new HttpObjectAggregator(65536))
.addLast(new HttpServerHandler());
}
}); final Channel ch = b.bind(PORT).sync().channel();
ch.closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
} } public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> { @Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
// 业务逻辑处理。。。
String result = "hello world"; FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK);
response.headers().set(CONTENT_TYPE, "text/plain; charset=UTF-8");
response.content().writeBytes(result.getBytes(StandardCharsets.UTF_8));
response.headers().setInt(CONTENT_LENGTH, response.content().readableBytes());
response.headers().set(CONNECTION, CLOSE); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
}
}

局限性

无法专注于业务处理,HttpServerHandler需要处理Netty的API和业务逻辑

0.2版本

0.1版本在同一个Handler中处理业务和网络是不合适的,想办法将HttpServerHandler解耦, 划分Handler和Controller。Handler处理Netty API,Controller处理业务逻辑。

如何实现

此版本不讲代码,讲思路(PS:主要是我懒得写)

  • HttpServerHandler处理Netty API,转发请求到Controller以及处理Controller的返回值

    1. 获取请求uri,根据uri找到相对应的Controller以及方法
    2. 调用Controller的方法
    3. 处理Controller的返回值,封装成HttpResponse返回
  • Controller处理业务逻辑,怎么在Spring-MVC用Controller,在这里就怎么用

    Controller的主要问题是如何根据uri找到相对应的Controller以及方法,下面给出两种思路:

    1. 自定义接口方式,并有Map存放uri与Controller的关系

      // 表示是Controller的接口
      public interface Action {
      Object doAction(FullHttpRequest request);
      }
      // 业务Controller
      public class HelloAction implements Action{
      @Override
      public Object doAction(FullHttpRequest request) {
      // 业务逻辑处理
      return null;
      }
      }
      // 使用Map存放uri与Controller的关系
      Map<String, Action> actionMap = new ConcurrentHashMap<>();
      actionMap.put("/hello", new HelloAction()); // 而在HttpServerHandler调用就更简单了,不用反射
      actionMap.get(uri).doAction(request);
    2. 自定义注解方式

      // 表示是Controller的注解
      @Target({ElementType.TYPE, ElementType.METHOD})
      @Retention(RetentionPolicy.RUNTIME)
      public @interface RequestMapping { String value() default ""; } // 业务Controller
      @RequestMapping("/hello")
      public class HelloController {
      @RequestMapping("/index")
      public FullHttpResponse index(FullHttpRequest request) {
      return null;
      }
      } /*
      * 而在HttpServerHandler调用
      */
      Class<?> clazz = obj.getClass();
      // 获取类上的注解
      RequestMapping mapping = clazz.getAnnotation(RequestMapping.class);
      if (mapping != null) {
      String mappingUri = mapping.value();
      // 获取controller的所有方法
      for (Method actionMethod : clazz.getMethods()) {
      // 获取方法上的注解
      RequestMapping subMapping = actionMethod.getAnnotation(RequestMapping.class);
      if (subMapping != null) {
      String subMappingUri = subMapping.value();
      // 如果uri符合注解规则,则反射调用方法
      if (uri.equalsIgnoreCase(mappingUri + subMappingUri)) {
      return (FullHttpResponse) actionMethod.invoke(obj, request);
      }
      }
      }
      }

局限性

没有一般web框架的过滤器,参数处理,返回值处理等。业务代码仍然深度依赖Netty API

0.3版本

结构

依赖:Netty + SpringBoot

处理流程图如下:

思路

下文说的Handler就是Controller

  1. 先说说为什么依赖SpringBoot,因为代码抄的是Spring-MVC的,而且SpringBoot提供的容器API真的很方便。
  2. 调度器:作为一个业务框架,需要业务逻辑对底层的依赖,也就是要满足大部分业务处理不会用到Netty的API,也就是Netty专注于接收和响应HTTP请求
  3. HandlerMapping:存储了请求路径与HandlerMethod的关系,可以通过uri定位到对应的Controller的方法
  4. HandlerMethod:存储了Controller和业务处理方法的信息
  5. ArgumentResolver:负责将请求参数转换成业务处理方法所需的参数(PS:Spring的DataBinder是个好东西)
  6. ReturnValueHandler:负责返回值的转换,比如将Map转成JSON

实现

实现有太多东西讲了,文章讲不完。我抄了一个简化版的Spring-MVC,就在文章开头的项目地址,感兴趣的自行去看吧。

Netty-如何写一个Http服务器的更多相关文章

  1. 用C写一个web服务器(二) I/O多路复用之epoll

    .container { margin-right: auto; margin-left: auto; padding-left: 15px; padding-right: 15px } .conta ...

  2. 使用node.js 文档里的方法写一个web服务器

    刚刚看了node.js文档里的一个小例子,就是用 node.js 写一个web服务器的小例子 上代码 (*^▽^*) //helloworld.js// 使用node.js写一个服务器 const h ...

  3. 使用Node.js原生API写一个web服务器

    Node.js是JavaScript基础上发展起来的语言,所以前端开发者应该天生就会一点.一般我们会用它来做CLI工具或者Web服务器,做Web服务器也有很多成熟的框架,比如Express和Koa.但 ...

  4. 用C写一个web服务器(一) 基础功能

    .container { margin-right: auto; margin-left: auto; padding-left: 15px; padding-right: 15px } .conta ...

  5. 用C写一个web服务器(四) CGI协议

    * { margin: 0; padding: 0 } body { font: 13.34px helvetica, arial, freesans, clean, sans-serif; colo ...

  6. 网络编程—【自己动手】用C语言写一个基于服务器和客户端(TCP)!

    如果想要自己写一个服务器和客户端,我们需要掌握一定的网络编程技术,个人认为,网络编程中最关键的就是这个东西--socket(套接字). socket(套接字):简单来讲,socket就是用于描述IP地 ...

  7. 用java写一个web服务器

    一.超文本传输协议 Web服务器和浏览器通过HTTP协议在Internet上发送和接收消息.HTTP协议是一种请求-应答式的协议——客户端发送一个请求,服务器返回该请求的应答.HTTP协议使用可靠的T ...

  8. Tomcat源码分析 (一)----- 手写一个web服务器

    作为后端开发人员,在实际的工作中我们会非常高频地使用到web服务器.而tomcat作为web服务器领域中举足轻重的一个web框架,又是不能不学习和了解的. tomcat其实是一个web框架,那么其内部 ...

  9. 手写一个Web服务器,极简版Tomcat

    网络传输是通过遵守HTTP协议的数据格式来传输的. HTTP协议是由标准化组织W3C(World Wide Web Consortium,万维网联盟)和IETF(Internet Engineerin ...

  10. 用C写一个web服务器(三) Linux下用GCC进行项目编译

    .container { margin-right: auto; margin-left: auto; padding-left: 15px; padding-right: 15px } .conta ...

随机推荐

  1. C# Thread.Sleep 不精准的问题以及解决方案

    1.问题 最近在写一个熔断的 SDK,其中一种策略是根据慢请求来进行熔断. 我们在测试的时候,在对应 API 里面采用了 Thread.Sleep(ms) 来模拟慢请求. 设置的慢请求阈值是 RT 1 ...

  2. MyBatisPlus实现分页和查询操作就这么简单

    <SpringBoot整合MybatisPlus基本的增删改查,保姆级教程>在这篇文章中,我们详细介绍了分页的具体实现方法.但是,在日常的开发中还需要搜索功能的.下面让我们一起动起手来,实 ...

  3. postman 脚本和变量

    背景 后端接口有登录或鉴权验证,通过 swagger 调用比较费劲,并且 java 的 swagger 库(不够自动化,嵌套类支持需要各种配置才能正常显示 schema)个人感觉也没有 .net co ...

  4. Cocos---监听、触摸事件、坐标系转换

    监听.触摸事件.坐标系转换 Creator的系统事件 分为"节点系统事件"和"全局系统事件". 节点系统事件:触发在节点上,包括鼠标事件和触摸事件. 全局系统事 ...

  5. mui|mui.plusReady里面的函数不执行??

    无论是在本地的浏览器还是在iPhone上真机运行都出现奇怪的错误,比如说子页面样式成为乱码,无法跳转子页面等等,一开始并没有意识到是mui.plusReady的问题,后来调试时发现是plusReady ...

  6. 大白话讲Java的锁

    偏向锁 对一个对象的锁偏向于某个线程,在markword中记录线程id 下次相同的线程来,直接就可以获取锁 轻量级锁 对象的Markword记录锁地址 跟线程栈里面的锁记录Lock Record的锁地 ...

  7. 「ARC 139F」Many Xor Optimization Problems【线性做法,踩标】

    「ARC 139F」Many Xor Optimization Problems 对于一个长为 \(n\) 的序列 \(a\),我们记 \(f(a)\) 表示从 \(a\) 中选取若干数,可以得到的最 ...

  8. 解决Mysql搭建成功后执行sql语句报错以及区分大小写问题

    刚搭建完mysql 8.0以后会: 一.表区分大小写, 二.执行正确的sql语句成功且会报:[Err] 1055 - Expression #1 of ORDER BY clause is not i ...

  9. JavasScript打印年月日时间代码

    就是Date的API,直接上代码啦. //打印中文的日期 function printChineseDateTime() { var now=new Date(); var str = now.get ...

  10. 基于.NET6的开源工业物联网网关

    什么是工业物联网网关 工业物联网网关(IIoTGateway)是一种硬件设备或软件程序,作为本地设备(如PLC.扫码枪.机器人.数控机床.非标上位机等)与云端系统(如物联网平台.SCADA系统.MES ...