目前vertx介绍请参考官网
http://vertxchina.github.io/vertx-translation-chinese/
我这边介绍的是如何让FC和vertx结合 启动时间达到2-3s
请先参考 aws lambda结合vertx 的源码:
https://github.com/noseka1/vertx-aws-lambda
目录
3. 结合aws-lambda 重写vertx httpserver源码
4. 开始修改aws-lambda ,无非是获取fc的HttpServletRequest将里面的参数获取,转换为 vertx的 HttpServerRequest
以上看完之后 我们将结合fc的程序,
1. maven项目
2. 加入依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>vertx-aliyun</groupId>
<artifactId>vertx-aliyun</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-unit</artifactId>
<version>3.4.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.aliyun.fc.runtime</groupId>
<artifactId>fc-java-core</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.aliyun.fc.runtime</groupId>
<artifactId>fc-java-common</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
3. 结合aws-lambda 重写vertx httpserver源码
原理: 将FC的 HttpServletRequest 和 HttpServletResponse 和vertx的 HttpServerRequest,HttpServerResponse 相互转换
准备: 需要了解servlet 的原理,需要了解vertx的服务器原理
4. 开始修改aws-lambda ,无非是获取fc的HttpServletRequest将里面的参数获取,转换为 vertx的 HttpServerRequest
注:不提供源码,希望你能按步骤学到更多的东西:
以https://github.com/noseka1/vertx-aws-lambda源码为基础
- 修改LambdaServer类
转换为:
并修改为对应的参数设置
- 修改HttpServerResponseImpl类
修改为
再将当前的返回类 对应的参数返回进行修改
关键代码位置如下:请比较aws-lambda的源码类
- 修改HttpServerRequestImpl类
关键修改代码如下:
parms转换
header转换
以上代码基本完成改变。
接着我们使用FC的函数进行部署 只需要修改lambda程序的
SampleApp.java 继承阿里云的HttpRequestHandler,FunctionInitializer
为什么不继承阿里云的StreamHandler?
因为inputStream中没有请求数据。所以我们只能继承以上的函数进行初始化。
只要将对应的new LambdaServer(vertx, context, input, output);
修改为 new LambdaServer(vertx, context, request, response);即可。
总结:
本人以探讨的态度,希望大家能够更深切的了解vertx的原理
只要能吃透servlet和vertx的http 就能实现,希望大家打牢基础,尽快完成以上的改编。