`
sillycat
  • 浏览: 2543181 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

AMAZON and Lambda(2)Lambda with Java

 
阅读更多
AMAZON and Lambda(2)Lambda with Java

Set up Eclipse
https://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/setup-install.html

I am using the J2EE version, and I am not working on Android Development, so I ignore the ADT part.
Install the AWS plugin https://aws.amazon.com/eclipse

After install the AWS plugin, then we can configure the credential
https://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/setup-credentials.html

Find my key/secret from here
https://console.aws.amazon.com/iam/home?#/home
Select [Users] —> [My Name] —> [Create Access Key], it will generates the Access Key ID and Secret access key
Add the AWS access key to eclipse
[References] —> [AWS Toolkit] —> Put my Value to Access Key ID and Secret Access Key
After the changes, you can see the changes under ~/.aws as well.
>cat ~/.aws/credentials
[default]
aws_access_key_id = xxxxxxxx
aws_secret_access_key = xxxxxxxx
[CarlKey]
aws_access_key_id=xxxxxxxx
aws_secret_access_key=xxxxxxxxx

Check some lambda example from here
>git clone https://github.com/markusklems/aws-lambda-java-example
Check LambdaForm project first

Check another project
>git clone https://github.com/eugenp/tutorials
>cd tutorials/aws
But I only need to check the aws part I think, Check the AWS part only, Import that directory as Maven Project
It seems to be a good example, I start to read the docs
http://www.baeldung.com/java-aws-lambda
maven jar plugin
https://www.jianshu.com/p/7a0e20b30401

This one should be the latest documents
Part 1
https://www.javaworld.com/article/3210726/application-development/serverless-computing-with-aws-lambda.html
Serverless Computing - developers do not have to deal with the servers.
Under this model, the server still fetch the code, deploy the codes and then run the codes, but the price will be times of execution and duration of the execution on AWS lambda.

Platform as a Service PAAS   micro services
Function as a Service FAAS   nano services

Part 2
https://www.javaworld.com/article/3210726/application-development/serverless-computing-with-aws-lambda.html?page=2
Part 3
https://www.javaworld.com/article/3210726/application-development/serverless-computing-with-aws-lambda.html?page=3

The first time we run the lambda, AWS needs to crate a container with your JAR file and deploy it to an EC2 instance. Once it is firstly deployed, the function will run very quickly.

It will be a very tiny java project with pom.xml as follow:
<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.sillycat.lambda</groupId>
    <artifactId>sillycat-lambda-java</artifactId>
    <packaging>jar</packaging>
    <version>1.0</version>
    <name>sillycat-lambda-java</name>

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-lambda-java-core</artifactId>
        <version>1.2.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <createDependencyReducedPom>false</createDependencyReducedPom>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
</project>

The request input class can be defined in java as well WidgetResponse.java
package com.sillycat.lambda.model;

public class WidgetRequest
{
  private String id;
  ...snip getter and setter...
}

The response object or the business POJO in java, Widget.java
package com.sillycat.lambda.model;

public class Widget
{
  private String id;
  private String name;
  public Widget()
  {
    super();
  }
  public Widget( String id, String name )
  {
    super();
    this.id = id;
    this.name = name;
  }
  ...snip..getter and setter...
}

One of the interface about the handler interface class, EchoWidgetHandler.java
package com.sillycat.lambda.handler;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.sillycat.lambda.model.Widget;
import com.sillycat.lambda.model.WidgetRequest;

public class EchoWidgetHandler implements RequestHandler<WidgetRequest, Widget>
{

  @Override
  public Widget handleRequest( WidgetRequest widgetRequest, Context context )
  {
    return new Widget( widgetRequest.getId(), "My Widget 3 " + widgetRequest.getId() );
  }

}

We can directly build the jar and upload that to lambda.
>mvn clean compile package

Then you can find and upload the jar in target/sillycat-lambda-java-1.0.jar
On the console, the Runtime is Java 8, Handler is com.sillycat.lambda.handler.EchoWidgetHandler

The running test data is
{
  "id": "Carl"
}
After run the result will display a JSON response
{
  "id": "Carl",
  "name": "superMy Widget 3 Carl"
}

The first run will be XXX ms, the next will be less than 1ms.

And we can directly deploy from our eclipse plugin as well.
[Upload the function to Lambda] — [Choose an existing Lambda function] Mine is testSqsToS3.zip.

If we unzip the zip file, we can see the
com directory with all the java classes.
lib directory with jar like aws-lambda-java-core-1.2.0.jar

If you add dependency there in pom.xml
It will put more jars there as well.
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>23.6-jre</version>
</dependency>

References:
https://docs.aws.amazon.com/zh_cn/lambda/latest/dg/use-cases.html
http://www.baeldung.com/java-aws-lambda
https://github.com/markusklems/aws-lambda-java-example/wiki/Getting-Started
https://www.javaworld.com/article/3210726/application-development/serverless-computing-with-aws-lambda.html

分享到:
评论

相关推荐

    lambda-java-client-example:使用适用于Java的AWS开发工具包调用lambda函数并获取响应的示例

    在本文中,我们将深入探讨如何使用适用于Java的AWS SDK来调用AWS Lambda函数,并解析返回的响应。Lambda是Amazon Web Services(AWS)提供的一种事件驱动的计算服务,它允许开发者运行代码而无需预置或管理服务器。...

    Serverless Architectures with AWS

    【标题】"Serverless Architectures with AWS" 是一个关于利用亚马逊Web服务(AWS)构建无服务器架构的主题。无服务器架构是一种云计算模型,其中开发者只需关注应用程序的代码,而无需关心运行代码所需的基础设施...

    The Busy Coders Guide to Android Development最终版2019

    Device Catalog: Amazon Fire TV and Fire TV Stick Device Catalog: Samsung DeX Appendix A: CWAC Libraries Appendix B: Android 8.0 Appendix C: Android 9.0 Appendix D: Community Theater and the Appinars

    Java_DynamoDB例子.zip

    Java_DynamoDB例子.zip是一个包含Java编程语言与Amazon DynamoDB交互示例的压缩包。DynamoDB是亚马逊云服务(AWS)提供的一款全托管的NoSQL数据库服务,它提供了高可伸缩性、高性能和低延迟的数据存储解决方案。这个...

    Serverless Applications with Node.js

    AWS Lambda 是亚马逊提供的一种无服务器计算服务,它可以在没有预置或管理服务器的情况下运行代码。Lambda 会自动扩展应用程序以保持一致的响应时间,无论用户负载如何变化。通过 Lambda,开发者只需上传代码,...

    用于CodeBuild和CodeDeploy的Java应用程序:用于CodeBuild和CodeDeploy的Java应用程序

    指定源代码仓库、构建环境(通常为Java运行时环境,如Amazon Linux AMI with OpenJDK)和构建spec文件。 3. **配置构建spec文件**:在你的项目根目录下创建“buildspec.yml”文件,列出构建过程的各个阶段,如安装...

    springboot_with_aws_from_jojoldu

    2. 负载均衡与弹性伸缩:结合Amazon EC2 Auto Scaling和Application Load Balancer,确保应用在高负载时能够自动扩展。 3. 安全与监控:利用AWS IAM进行权限管理,使用CloudWatch进行日志监控和报警。 总结,...

    带有ab的aws

    使用Java开发应用程序时,AWS提供了多种与Java兼容的服务和工具,如EC2(弹性计算云)用于运行Java应用,S3(简单存储服务)用于存储静态文件,RDS(关系数据库服务)支持MySQL、PostgreSQL等数据库,以及Lambda无...

    Coldfusion使用Amazon的AWS里的S3服务例子

    1. **配置AWS SDK**:首先,我们需要下载并引入AWS的Java SDK,压缩包中的`aws-java-sdk`就是这个SDK。在Coldfusion项目中,将SDK的JAR文件添加到类路径中,以便能够调用其提供的API。 2. **创建S3客户端**:使用...

    springboot-withAWS:AWS로혼자스서비스研究

    在本项目"springboot-withAWS"中,我们将深入探讨如何使用Spring Boot框架与Amazon Web Services (AWS)集成,以构建和部署独立的云服务。这个项目着重于利用AWS的多种服务来实现高效、可靠的后端解决方案。以下是...

    sentinel-eater:自动下载Sentinel数据的服务

    实际执行所有这些操作的API :grinning_face_with_smiling_eyes: (可选)将图块下载到提供的S3存储桶中 (可选)响应SNS通知以连续检查所需的图块设定值Sentinel Eater可以在两种模式下运行,当前为HTTP和Amazon。...

Global site tag (gtag.js) - Google Analytics