# serverless
**Repository Path**: ck/serverless
## Basic Information
- **Project Name**: serverless
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2026-01-09
- **Last Updated**: 2026-01-09
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# Serverless 函数执行引擎
这是一个基于Spring Boot开发的轻量级Serverless函数执行引擎,用于部署、管理和执行无服务器函数。
## 核心功能
1. **函数生命周期管理**
- 支持通过JAR包注册/更新/删除函数
- 函数隔离执行,使用独立类加载器
- 可配置超时时间和环境变量
2. **多种触发机制**
- **HTTP触发**: 通过REST API调用函数
- **定时触发**: 基于Cron表达式自动执行函数
3. **执行环境隔离**
- 每个函数运行在独立的类加载器中
- 使用线程池管理函数执行
- 支持函数超时控制
4. **监控与指标**
- 收集函数执行次数、执行时间等指标
- 提供API查询函数和系统指标
## 技术架构
- **框架**: Spring Boot 4.0.1
- **语言**: Java 17
- **构建工具**: Maven
- **模块结构**:
- **serverless-api**: 独立的API模块,包含函数开发所需的接口和模型类
- **serverless**: 主引擎模块,包含核心执行逻辑和管理功能
- **核心组件**:
- `FunctionManager`: 函数注册与管理
- `FunctionExecutor`: 函数执行器
- `HttpTrigger`: HTTP触发处理
- `TimerTrigger`: 定时任务调度
- `ServerlessController`: API控制器
- `ServerlessFunction`: 函数开发接口(位于serverless-api模块)
- `ExecutionContext`: 函数执行上下文(位于serverless-api模块)
- `ExecutionResult`: 函数执行结果(位于serverless-api模块)
- `FunctionMetrics`: 函数执行指标(位于serverless-api模块)
## API接口
提供RESTful API,主要端点包括:
- `/serverless/functions/{functionName}/invoke`: 调用函数
- `/serverless/functions`: 管理函数
- `/serverless/timer-tasks`: 管理定时任务
- `/serverless/metrics`: 查询指标
## 工作流程
1. 通过API或代码注册函数(JAR包路径+主类名)
2. 函数被加载到隔离的类加载器环境中
3. 通过HTTP请求或定时任务触发函数执行
4. 执行结果返回给调用者,并记录执行指标
## 项目结构
```
src/
├── main/
│ ├── java/com/example/serverless/
│ │ ├── api/ # REST API控制器
│ │ ├── core/ # 核心组件
│ │ ├── executor/ # 函数执行器
│ │ ├── model/ # 数据模型
│ │ ├── trigger/ # 触发机制
│ │ └── ServerlessEngine.java # 主启动类
│ └── resources/ # 配置文件
└── test/ # 测试代码
```
## 快速开始
1. 编译项目:
```
mvn clean package
```
2. 启动服务:
```
java -jar target/serverless-0.0.1-SNAPSHOT.jar
```
3. 访问API:
- API根地址:http://localhost:8080/serverless
- 系统状态:http://localhost:8080/serverless/status
## 如何实现函数
### 1. 创建函数项目
创建一个新的Maven项目,用于开发你的Serverless函数。
#### 1.1 项目结构
```
my-function/
├── src/
│ └── main/
│ └── java/
│ └── com/
│ └── example/
│ └── functions/
│ └── HelloWorldFunction.java
└── pom.xml
```
#### 1.2 编写pom.xml
```xml
4.0.0
com.example
hello-world-function
1.0-SNAPSHOT
17
com.example
serverless-api
0.0.1-SNAPSHOT
provided
org.apache.maven.plugins
maven-compiler-plugin
3.11.0
17
17
org.apache.maven.plugins
maven-assembly-plugin
3.6.0
jar-with-dependencies
com.example.functions.HelloWorldFunction
make-assembly
package
single
```
### 2. 实现函数类
创建一个实现`ServerlessFunction`接口的类:
```java
package com.example.functions;
import com.example.serverless.model.ExecutionContext;
import com.example.serverless.model.ServerlessFunction;
import java.util.Map;
public class HelloWorldFunction implements ServerlessFunction {
@Override
public Object handle(Map input, ExecutionContext context) throws Exception {
// 获取输入参数
String name = (String) input.getOrDefault("name", "World");
// 获取上下文信息
String requestId = context.getRequestId();
String functionName = context.getFunctionName();
// 构建响应结果
Map result = Map.of(
"message", "Hello, " + name + "!",
"requestId", requestId,
"functionName", functionName,
"timestamp", System.currentTimeMillis()
);
return result;
}
}
```
### 3. 打包函数
将函数项目打包为JAR文件:
```bash
cd my-function
mvn clean package
```
打包完成后,你将在`target`目录下得到一个包含所有依赖的JAR文件,例如:`hello-world-function-1.0-SNAPSHOT-jar-with-dependencies.jar`。
### 4. 注册函数
将打包好的JAR文件复制到Serverless引擎的`functions`目录下,然后使用API注册函数:
#### 4.1 创建functions目录
```bash
mkdir -p functions
```
#### 4.2 复制JAR文件
```bash
cp my-function/target/hello-world-function-1.0-SNAPSHOT-jar-with-dependencies.jar functions/hello-world.jar
```
#### 4.3 注册函数
```bash
curl -X POST http://localhost:8080/serverless/functions/hello-world \
-H "Content-Type: application/json" \
-d '{
"jarPath": "functions/hello-world.jar",
"className": "com.example.functions.HelloWorldFunction",
"timeoutMs": 30000,
"maxMemory": 128000000
}'
```
### 5. 调用函数
函数注册成功后,你可以使用HTTP请求调用它:
#### 5.1 POST方式调用
```bash
curl -X POST http://localhost:8080/serverless/functions/hello-world/invoke \
-H "Content-Type: application/json" \
-d '{"name": "Serverless"}'
```
#### 5.2 GET方式调用
```bash
curl http://localhost:8080/serverless/functions/hello-world/invoke
```
### 6. 查看函数执行结果
调用成功后,你将收到类似以下的响应:
```json
{
"requestId": "12345678-1234-5678-1234-567812345678",
"functionName": "hello-world",
"success": true,
"executionTime": 123,
"result": {
"message": "Hello, Serverless!",
"requestId": "12345678-1234-5678-1234-567812345678",
"functionName": "hello-world",
"timestamp": 1620000000000
}
}
```
### 7. 查看函数指标
你可以通过API查看函数的执行指标:
```bash
curl http://localhost:8080/serverless/functions/hello-world/metrics
```
## 使用示例
### 调用函数
```bash
# POST方式调用函数
curl -X POST http://localhost:8080/serverless/functions/hello-world/invoke \
-H "Content-Type: application/json" \
-d '{"name": "World"}'
# GET方式调用函数
curl http://localhost:8080/serverless/functions/hello-world/invoke
```
### 管理函数
```bash
# 获取所有函数
curl http://localhost:8080/serverless/functions
# 获取函数详情
curl http://localhost:8080/serverless/functions/hello-world
# 删除函数
curl -X DELETE http://localhost:8080/serverless/functions/hello-world
```
### 查看指标
```bash
# 获取所有函数指标
curl http://localhost:8080/serverless/metrics
# 获取特定函数指标
curl http://localhost:8080/serverless/functions/hello-world/metrics
```
## 适用场景
- 微服务架构中的轻量级函数部署
- 事件驱动应用
- 定时任务执行
- 按需计算资源
- API网关后端服务
## 许可证
MIT License