Spring Cloud Function [Part 1]

Chiwa Kantawong (Pea)
3 min readAug 5, 2021

--

มันคืออะไร ไม่มีใครอธิบายได้ดีเท่า Web ของ AWS เองครับ

Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda runs your code on a high-availability compute infrastructure and performs all of the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, code monitoring and logging. With Lambda, you can run code for virtually any type of application or backend service. All you need to do is supply your code in one of the languages that Lambda supports.

ซึ่งหนึ่งในภาษาที่ AWS Lambda supports นั่นก็คือ Java 8 และเหมือนจะ 11 ด้วยนะครับ และทั้งนี้ทั้งนั้น พระเองของเรา Spring Boot สามารถสร้าง Application ไปรันบน AWS Lambda ได้เช่นกัน

อ้อบทความนี้ผมถือว่าทุกคนมี Account บน AWS กันแล้วนะครับ

สร้างโปรเจคส์กันเลยครับ

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zengcode</groupId>
<artifactId>spring-cloud-functions</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-functions</name>
<description>spring-cloud-functions</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-function-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>1.0.21.RELEASE</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>aws</shadedClassifierName>
</configuration>
</plugin>
</plugins>
</build>

</project>

เพิ่ม dependencies และ plugins ตามข้างบนนะครับ

ต่อไปเราจะสร้าง Spring Cloud Function จาก Function Interface 3 ตัวนี้กันนะครับ

  • ** Function Interface ก็คือ Interface ที่มี 1 abstract method เท่านั้น แต่สามารถมี default หรือ static method ได้ไม่จำกัด อันนี้มีมาตั้งแต่ Java 8 นะครับ
package com.zengcode.springcloudfunctions;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import java.util.function.Function;
import java.util.function.Supplier;

@SpringBootApplication
public class SpringCloudFunctionsApplication {

public static void main(String[] args) {
SpringApplication.run(SpringCloudFunctionsApplication.class, args);
}

@Bean
public Supplier<String> helloWorld() {
return () -> "Hello World";
}

@Bean
public Function<String, String> getUpperCaseName() {
return (name) -> new StringBuilder(name).toString().toUpperCase();
}
@Bean
public Consumer<String> consumedMessage() {
return (str) -> System.out.println("Consumed : " + str);;
}

}

ซึ่ง Spring Boot จะ mapping request handler ให้เป็น /helloWorld กับ /getUpperCaseName นะครับ

Supplier<String> คือรับ response เป็น String นะครับFunction<String, String> รับ body เป็น String และ response เป็น StringConsumer<String> ก็เป็นการ รับ body เข้ามาทำงานอะไรบางอย่าง จะไม่ response กลับ

ซึ่ง body และ response เป็น Generic Type ซึ่งนั่นก็แสดงว่าเราจะส่งเป็น Object อะไรก็ได้

เสร็จแล้วก็มา run application กันเลย

เราจะเห็นว่า Application เราจะรันที่ port 8080

เรามาลองเรียก helloWorld กันครับ

ต่อไปเรามาเรียก getUpperCaseName

ต่อไปเรามาเรียก consumedMessage

เป็นอันว่า Application ของเราทำงานได้แล้ว เด๋วไว้มาต่อตอนหน้านะครับ

เราจะ ไป Implement RequestHandler และก็ deploy บน AWS Lambda กัน

ตอนที่ 2 https://zengcode.medium.com/spring-cloud-function-part-2-82be5944568f

--

--

Chiwa Kantawong (Pea)
Chiwa Kantawong (Pea)

Written by Chiwa Kantawong (Pea)

Software Development Expert at Central Tech

No responses yet