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

猜你喜欢

转载自sillycat.iteye.com/blog/2410027
今日推荐