Loading...
Loading...
Compare original and translation side by side
| Approach | Cold Start | Best For | Complexity |
|---|---|---|---|
| Micronaut | < 1s | Complex apps, DI needed, enterprise | Medium |
| Raw Java | < 500ms | Simple handlers, minimal overhead | Low |
| 方案 | 冷启动时间 | 最佳适用场景 | 复杂度 |
|---|---|---|---|
| Micronaut | <1秒 | 复杂应用、需要依赖注入的企业级场景 | 中等 |
| 原生Java | <500毫秒 | 简单处理器、追求最低开销 | 低 |
my-lambda-function/
├── build.gradle (or pom.xml)
├── src/
│ └── main/
│ ├── java/
│ │ └── com/example/
│ │ └── Handler.java
│ └── resources/
│ └── application.yml (Micronaut only)
└── serverless.yml (or template.yaml)my-lambda-function/
├── build.gradle (或pom.xml)
├── src/
│ └── main/
│ ├── java/
│ │ └── com/example/
│ │ └── Handler.java
│ └── resources/
│ └── application.yml (仅Micronaut项目需要)
└── serverless.yml (或template.yaml)@FunctionBean("my-function")
public class MyFunction implements Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private final MyService service;
public MyFunction(MyService service) {
this.service = service;
}
@Override
public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent request) {
// Process request
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody("{\"message\": \"Success\"}");
}
}@FunctionBean("my-function")
public class MyFunction implements Function<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
private final MyService service;
public MyFunction(MyService service) {
this.service = service;
}
@Override
public APIGatewayProxyResponseEvent apply(APIGatewayProxyRequestEvent request) {
// 处理请求
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody("{\"message\": \"Success\"}");
}
}public class MyHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
// Singleton pattern for warm invocations
private static final MyService service = new MyService();
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody("{\"message\": \"Success\"}");
}
}public class MyHandler implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> {
// 单例模式,用于热调用复用
private static final MyService service = new MyService();
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
return new APIGatewayProxyResponseEvent()
.withStatusCode(200)
.withBody("{\"message\": \"Success\"}");
}
}// GOOD: Initialize once, reuse across invocations
private static final DynamoDbClient dynamoDb = DynamoDbClient.builder()
.region(Region.US_EAST_1)
.build();
// AVOID: Creating clients in handler method
public APIGatewayProxyResponseEvent handleRequest(...) {
DynamoDbClient client = DynamoDbClient.create(); // Slow on every invocation
}// 推荐:仅初始化一次,在多个调用间复用
private static final DynamoDbClient dynamoDb = DynamoDbClient.builder()
.region(Region.US_EAST_1)
.build();
// 避免:在处理器方法中创建客户端
public APIGatewayProxyResponseEvent handleRequest(...) {
DynamoDbClient client = DynamoDbClient.create(); // 每次调用都会变慢
}@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
try {
// Business logic
return successResponse(result);
} catch (ValidationException e) {
return errorResponse(400, e.getMessage());
} catch (Exception e) {
context.getLogger().log("Error: " + e.getMessage());
return errorResponse(500, "Internal error");
}
}@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent request, Context context) {
try {
// 业务逻辑
return successResponse(result);
} catch (ValidationException e) {
return errorResponse(400, e.getMessage());
} catch (Exception e) {
context.getLogger().log("Error: " + e.getMessage());
return errorResponse(500, "内部错误");
}
}service: my-java-lambda
provider:
name: aws
runtime: java21
memorySize: 512
timeout: 10
package:
artifact: build/libs/function.jar
functions:
api:
handler: com.example.Handler
events:
- http:
path: /{proxy+}
method: ANYservice: my-java-lambda
provider:
name: aws
runtime: java21
memorySize: 512
timeout: 10
package:
artifact: build/libs/function.jar
functions:
api:
handler: com.example.Handler
events:
- http:
path: /{proxy+}
method: ANYAWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: build/libs/function.jar
Handler: com.example.Handler
Runtime: java21
MemorySize: 512
Timeout: 10
Events:
ApiEvent:
Type: Api
Properties:
Path: /{proxy+}
Method: ANYAWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: build/libs/function.jar
Handler: com.example.Handler
Runtime: java21
MemorySize: 512
Timeout: 10
Events:
ApiEvent:
Type: Api
Properties:
Path: /{proxy+}
Method: ANYCreate a Java Lambda function using Micronaut to handle user REST APICreate a Java Lambda function using Micronaut to handle user REST APIMy Java Lambda has 3 second cold start, how do I optimize it?My Java Lambda has 3 second cold start, how do I optimize it?Configure CI/CD for Java Lambda with SAMConfigure CI/CD for Java Lambda with SAM