04 ,lambda 介绍,helloWorld :
程序员文章站
2024-01-28 09:43:46
...
1 ,lambda 工作台 :
https://cn-northwest-1.console.amazonaws.cn/lambda/home?region=cn-northwest-1#/functions
2 ,lambda 开发指南 :
https://docs.amazonaws.cn/lambda/latest/dg/welcome.html
3 ,删除 lambda 函数 :
4 ,新建 lambda 函数 :
- 进入控制台 - 创建函数 - 从头创建 - 函数名(aa) - 环境 java8
- 选择角色 :
- 创建
- 添加触发器 : sqs
- 内存 : 2G
- 网络与安全组 :
- 重试次数 : 2
5 ,java 依赖 : pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>com.lefrcycle.lambda</groupId>
<artifactId>lambda01_test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- s3 -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-s3</artifactId>
<version>1.11.636</version>
</dependency>
<!-- sqs -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-sqs</artifactId>
<version>1.11.636</version>
</dependency>
<!-- lambda -->
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.2.0</version>
</dependency>
<!-- jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.7</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
6 ,java 代码 :
- 接收消息用的 pojo 类 :
package com.lifecycle;
public class Person {
private String id;
private String name;
public Person(){}
public Person(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Person{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
}
7 ,编写主代码 :
package com.lifecycle;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class Hello implements RequestHandler<Person, String>{
public String handleRequest(Person p, Context context) {
System.out.println(123);
return "456";
}
}
8 ,压缩 :
maven : package
9 ,上传 :
lambda01_test-1.0-SNAPSHOT
10 ,配置测试事件 :
11 ,测试 :
12 ,成功的标志 :
看到我们输出的字符串