Spring Cloud Function | AWS Lambda | Serverless Sinhala Guide | Java Development Sri Lanka

Spring Cloud Function | AWS Lambda | Serverless Sinhala Guide | Java Development Sri Lanka

ආයුබෝවන් යාළුවනේ!

දැන් තියෙන ඩිජිටල් ලෝකයේ, Software Development කියන දේ දවසින් දවස වෙනස් වෙනවා නේද? අලුත් අලුත් technologies හැමදාම එනවා. මේවා අතරින් Serverless Computing කියන්නේ දැන් ගොඩක්ම ජනප්‍රිය වෙලා තියෙන concept එකක්. "serverless" කිව්වට servers නැතුව නෙවෙයි වැඩ කරන්නේ, ඒත් අපිට servers ගැන හිතන්න, deploy කරන්න, manage කරන්න, scale කරන්න ඕන වෙන්නේ නෑ. ඒක Cloud Provider කෙනෙක් බලාගන්නවා. නියමයි නේද?

ඉතින්, Java Developer කෙනෙක් විදියට ඔයාට Serverless World එකට එන්ටර් වෙන්න පුලුවන් ලේසිම ක්‍රමයක් තමයි Spring Cloud Function. මේ framework එක හරහා අපිට පුළුවන් functions ලියලා ඒවාව AWS Lambda, Azure Functions, Google Cloud Functions වගේ Serverless platforms වලට වගේම, සාමාන්‍ය web application එකක් විදියට හෝ streaming application එකක් විදියටත් deploy කරන්න. ඒ කියන්නේ, එකම codebase එකක් තියන් ඕනෑම තැනක run කරන්න පුලුවන් හැකියාවක්!

මේ guide එකෙන් අපි Spring Cloud Function කියන්නේ මොකක්ද කියලා හොඳට තේරුම් අරගෙන, AWS Lambda එකක function එකක් deploy කරන හැටි පියවරෙන් පියවර බලමු. Java, Spring Boot ගැන මූලික දැනුමක් තියෙනවා නම් මේක ඔයාට ගොඩක් ප්‍රයෝජනවත් වෙයි.

Spring Cloud Function කියන්නේ මොකක්ද?

සරලව කිව්වොත්, Spring Cloud Function කියන්නේ ඔයාට business logic එකක් Java Function එකක් විදියට ලියන්න පුලුවන්, ඒ වගේම ඒ function එක run කරන්න ඕන platform එක ගැන හිතන්නේ නැතුව abstraction එකක් දෙන framework එකක්. මේක Spring Framework එකේම කොටසක් නිසා Spring Boot එකත් එක්ක ගොඩක් හොඳට වැඩ කරනවා.

සාමාන්‍යයෙන් function එකක් කියන්නේ input එකක් අරගෙන output එකක් දෙන එකක්නේ. Spring Cloud Function එකෙන් මේක හුඟක් සරල කරනවා. ඔයාට පුලුවන් java.util.function.Function, Consumer, නැත්නම් Supplier වගේ interface එකක් implement කරලා bean එකක් විදියට ඔයාගේ function එක define කරන්න. මේ framework එක ඔයාගේ function එකේ input, output, serialization, deserialization වගේ හැමදේම handle කරනවා. ඒක නිසා ඔයාට focus කරන්න තියෙන්නේ ඔයාගේ business logic එක විතරයි.

Spring Cloud Function වල වාසි:

  • Abstraction: ඔයාට specific platform API එකක් ගැන හිතන්න ඕනේ නෑ. එකම function code එක AWS Lambda, Azure Functions, Google Cloud Functions වගේ ඕනෑම තැනක deploy කරන්න පුළුවන්.
  • Portability: ඔයාගේ function එක serverless environment එකක විතරක් නෙවෙයි, සාමාන්‍ය REST Endpoint එකක් විදියට web application එකක හෝ streaming data processor එකක් විදියටත් run කරන්න පුළුවන්.
  • Spring Ecosystem Integration: Spring Boot එකේ තියෙන හැම feature එකක්ම වගේ (Dependency Injection, Auto-configuration) මේකටත් පාවිච්චි කරන්න පුළුවන්.
  • Reduced Boilerplate: Serverless platforms වලට අවශ්‍ය කරන boilerplate code අඩු කරනවා.
  • Testability: Spring Cloud Function වලට build කරපු functions Spring Boot applications විදියටම unit සහ integration test කරන්න පුළුවන්.

අපි Lambda එකක් ලියමු! (Simple Spring Cloud Function)

හරි, දැන් අපි Spring Cloud Function එකක් ලියලා AWS Lambda එකක deploy කරන්න ලෑස්ති වෙමු. මුලින්ම අපි Spring Boot Project එකක් හදාගමු. Spring Initializr එකට ගිහින් පහත dependencies ටික එකතු කරගන්න:

  • Spring Boot DevTools (Optional, but useful for development)
  • Spring Web (Optional, if you want to test it as a web app locally)
  • Spring Cloud Function Web
  • Spring Cloud Function Adapter AWS

පහත Project Setup එක බලන්න:


Group: com.example
Artifact: lambda-function
Name: lambda-function
Description: Demo Spring Cloud Function for AWS Lambda
Package name: com.example.lambdafunction
Java: 17 (or your preferred version)
Dependencies:
  - Spring Web
  - Spring Cloud Function Web
  - Spring Cloud Function Adapter AWS

Project එක generate කරලා IDE එකකට import කරගත්තට පස්සේ, අපි අපේ function එක ලියමු. com.example.lambdafunction package එක ඇතුලේ HelloFunction කියලා class එකක් හදමු.

src/main/java/com/example/lambdafunction/HelloFunction.java


package com.example.lambdafunction;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.function.Function;

@Component
public class HelloFunction {

    // Simple String to String function
    @Bean
    public Function<String, String> hello() {
        return input -> {
            System.out.println("Input received for 'hello' function: " + input);
            return "ආයුබෝවන්, " + input + "! Spring Cloud Function එකෙන් ආපු පණිවිඩයක්.";
        };
    }

    // Function with custom input/output DTOs
    @Bean
    public Function<MyInput, MyOutput> processMessage() {
        return input -> {
            System.out.println("Input received for 'processMessage' function: " + input.getMessage());
            String processed = input.getMessage().toUpperCase() + " - PROCESSED";
            return new MyOutput("OK", "සැකසූ පණිවිඩය: " + processed);
        };
    }

    // Input DTO
    public static class MyInput {
        private String message;

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }

    // Output DTO
    public static class MyOutput {
        private String status;
        private String processedMessage;

        public MyOutput(String status, String processedMessage) {
            this.status = status;
            this.processedMessage = processedMessage;
        }

        public String getStatus() {
            return status;
        }

        public String getProcessedMessage() {
            return processedMessage;
        }

        public void setProcessedMessage(String processedMessage) {
            this.processedMessage = processedMessage;
        }
    }
}

මේ code එකේ අපි functions දෙකක් define කරලා තියෙනවා:

  1. hello(): මේක String එකක් input අරගෙන String එකක්ම output කරනවා. මේක ඉතාම සරල function එකක්.
  2. processMessage(): මේක MyInput කියන custom object එකක් input අරගෙන MyOutput කියන custom object එකක් output කරනවා. මේකෙන් පෙන්නන්නේ JSON වගේ structured data එක්ක වැඩ කරන හැටි.

මේ functions දෙකම @Bean විදියට Spring Context එකට register කරලා තියෙනවා. Spring Cloud Function එකෙන් මේ beans අඳුරගෙන incoming requests වලට අදාළ function එක invoke කරනවා.

ඔයාට පුලුවන් මේ project එක local එකේ Spring Boot Application එකක් විදියට run කරලා http://localhost:8080/hello (POST request body: "කසුන්") වගේ endpoint එකකට request එකක් යවලා test කරන්න. එතකොට Spring Cloud Function Web adapter එක වැඩ කරන හැටි බලාගන්න පුළුවන්. /processMessage endpoint එකටත් JSON body එකක් එක්ක POST request එකක් යවලා custom DTO එක test කරන්න පුළුවන්.

AWS Lambda වලට Deploy කරමු

දැන් අපේ function එක ලෑස්තියි. අපි බලමු මේක AWS Lambda වලට deploy කරන්නේ කොහොමද කියලා.

1. Executable JAR එකක් හදමු:

AWS Lambda එකට deploy කරන්න අපිට fat JAR එකක් හදාගන්න ඕනේ. මේ JAR එක ඇතුලේ අපේ code එකට අමතරව dependencies ඔක්කොම තියෙන්න ඕනේ. Maven නම් spring-boot-maven-plugin එක පාවිච්චි කරන්න පුළුවන්. Gradle නම් shadowJar plugin එක ඊට වඩා හොඳයි.

Maven සඳහා (pom.xml) - <build> tag එක ඇතුලේ:


<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <mainClass>com.example.lambdafunction.LambdaFunctionApplication</mainClass> <!-- Replace with your main Spring Boot Application class -->
        <layout>JAR</layout>
    </configuration>
    <!-- Ensure the necessary loader tools are available -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-loader-tools</artifactId>
            <version>3.2.5</version> <!-- Replace with your Spring Boot version -->
        </dependency>
    </dependencies>
</plugin>

දැන් project root directory එකේ command line එකේ මේ command එක run කරන්න:


mvn clean package

මේක target directory එක ඇතුලේ lambda-function-0.0.1-SNAPSHOT.jar වගේ JAR එකක් හදයි. මේක AWS Lambda එකට upload කරන්න පුළුවන්.

Gradle සඳහා (build.gradle) - plugins සහ dependencies blocks වලට පසුව:


plugins {
    id 'java'
    id 'org.springframework.boot' version '3.2.5' // Your Spring Boot version
    id 'io.spring.dependency-management' version '1.1.4'
    id 'com.github.johnrengelman.shadow' version '8.1.1' // Shadow JAR plugin
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'

java {
    sourceCompatibility = '17' // Your Java version
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-function-web'
    implementation 'org.springframework.cloud:spring-cloud-function-adapter-aws'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

// Ensure the application can run locally as a Spring Boot app
bootJar {
    archiveFileName = "${project.name}.jar"
}

// For AWS Lambda, we need a fat JAR (shadow JAR)
shadowJar {
    archiveClassifier.set('aws') // Creates a JAR like lambda-function-0.0.1-SNAPSHOT-aws.jar
}

දැන් project root directory එකේ command line එකේ මේ command එක run කරන්න:


gradle clean build shadowJar

මේක build/libs directory එක ඇතුලේ lambda-function-0.0.1-SNAPSHOT-aws.jar වගේ JAR එකක් හදයි. මේ JAR එක AWS Lambda එකට upload කරන්න.

2. AWS Lambda Function එකක් හදමු:

දැන් අපේ JAR එක ලෑස්තියි. AWS Management Console එකට Login වෙලා Lambda සර්විස් එකට යන්න. (ඒකට කලින් AWS Account එකක් සහ ඒකේ AdministratorAccess වගේ permission තියෙන IAM User කෙනෙක් ඉන්න ඕනේ).

  1. Create function බටන් එක ක්ලික් කරන්න.
  2. Author from scratch තෝරන්න.
  3. Basic information:
    • Function name: MySpringFunction වගේ නමක් දෙන්න. මේක ඔයාගේ Lambda function එකේ නම.
    • Runtime: Java 17 (or your chosen Java version) තෝරන්න.
    • Architecture: x86_64 තෝරන්න.
  4. Change default execution role:
    • Create a new role with basic Lambda permissions තෝරන්න. මේකෙන් CloudWatch Logs වලට ලියන්න පුළුවන් permission එක්ක role එකක් හදනවා.
  5. Create function ක්ලික් කරන්න.

3. JAR එක Upload කර Handler එක Configure කරමු:

Function එක හැදුවට පස්සේ, ඔයාට code tab එකට යන්න පුළුවන්. එතන:

  1. Code source කියන section එකේ Upload from > .zip or .jar file ක්ලික් කරන්න.
  2. ඔයා build කරපු JAR එක (උදා: lambda-function-0.0.1-SNAPSHOT.jar or lambda-function-0.0.1-SNAPSHOT-aws.jar) upload කරන්න.
  3. Runtime settings කියන එකේ Edit ක්ලික් කරන්න.
    • Handler: මෙතනට දෙන්න ඕනේ Spring Cloud Function AWS Adapter එකේ handler class එක. ඒක තමයි org.springframework.cloud.function.adapter.aws.SpringBootStreamHandler.සැලකිය යුතුයි: ඔයාගේ function name එක (අපේ උදාහරණයේ hello හෝ processMessage) Lambda event එකේ function කියන field එකේ (e.g., {"function": "hello", "body": "test"}) හෝ request path එකේ (e.g., /hello) තියෙන්න ඕනේ. API Gateway එකක් හරහා invoke කරනවා නම් මේක path එකෙන් mapping වෙනවා. සරලවම test කරන්න නම් Lambda console එකේ test event එකේ function field එකට hello හෝ processMessage දෙන්න පුළුවන්.
    • Memory (MB): Spring Boot Application එකක් රන් වෙන්න සාමාන්‍යයෙන් වැඩි Memory එකක් ඕන වෙනවා. 256MB හෝ 512MB වගේ දීලා බලන්න. අවශ්‍ය නම් වැඩි කරන්න පුළුවන්.
    • Timeout: Cold Start එකක් වෙලාවට application context initialize වෙන්න වෙලාවක් යන නිසා, 30 seconds වගේ timeout එකක් දෙන්න.
  4. Save කරන්න.

4. Function එක Test කරමු:

දැන් අපි අපේ function එක test කරමු. Lambda console එකේ උඩින් තියෙන Test බටන් එක ක්ලික් කරන්න.

  1. New event එකක් හදන්න:
    • Event name: MyTestEvent වගේ නමක් දෙන්න.
  2. Save කරලා Test ක්ලික් කරන්න.

Event JSON:hello function එක test කරන්න: (මේක String input එකක් බලාපොරොත්තු වෙනවා)


{
  "function": "hello",
  "body": "නුවන්"
}

processMessage function එක test කරන්න: (මේක MyInput DTO එකට match වෙන JSON input එකක් බලාපොරොත්තු වෙනවා)


{
  "function": "processMessage",
  "body": "{\"message\": \"මේක පණිවිඩයක්\"}"
}

සටහන: body එකට යන value එක String එකක් විදියට serialize වෙන නිසා, JSON String එකක් දෙද්දී escape කරන්න ඕනේ. API Gateway එකකින් නම් මේ mapping එක ඔටෝ handle වෙනවා.

ඔයාට Execution results වල function එකෙන් එන output එක බලාගන්න පුළුවන්. Logs වල Spring Boot startup logs ටිකත් පෙනෙයි. සමහර විට Cold Start වෙලාවේ පොඩි delay එකක් තියෙන්න පුළුවන්, මොකද Spring Application Context එක initialize වෙන්න කාලයක් යන නිසා. මේක production වලදී provisioned concurrency වගේ options වලින් අඩු කරගන්න පුළුවන්.

නිගමනය (Conclusion)

ඉතින් යාළුවනේ, මේ guide එකෙන් අපි Spring Cloud Function කියන්නේ මොකක්ද කියලා හොඳට තේරුම් ගත්තා. ඒ වගේම Java වලින් ලියපු Spring Cloud Function එකක් AWS Lambda එකක deploy කරන හැටිත් පියවරෙන් පියවර බැලුවා. මේකෙන් ඔයාට Serverless Computing වලට Java, විශේෂයෙන්ම Spring Boot එක්ක කොච්චර ලේසියෙන් එන්ටර් වෙන්න පුළුවන්ද කියලා තේරෙන්න ඇති. Spring Cloud Function එකේ abstraction එක නිසා ඔයාට code එක වෙනස් කරන්නේ නැතුව ඕනෑම Cloud platform එකකට මාරු වෙන්න පුළුවන් වීම විශාල වාසියක්.

Serverless architectures දැන් ගොඩක් popular වෙලා තියෙන්නේ ඒවායේ තියෙන scalability, cost-effectiveness, සහ maintenance අඩුකම නිසා. මේ trend එකත් එක්ක යන්න Spring Cloud Function ඔයාට ගොඩක් උදව් වෙයි. ඊළඟට, ඔයාට API Gateway එකක් හරහා මේ Lambda function එක expose කරලා full API එකක් හදන හැටි ගැනත් හොයලා බලන්න පුළුවන්.

මේ guide එකට අදාළව ඔයාට තියෙන අත්දැකීම්, ප්‍රශ්න, හෝ අදහස් පහලින් comment කරන්න. ඔයාගේ ඊළඟ project එකට මේ technology එක try කරලා බලන්නත් අමතක කරන්න එපා!

ජය වේවා!