0Pricing
Spring Boot 4 Microservices & REST APIs · درس

الدوال عديمة الخوادم باستخدام Spring Cloud

استكشف إنشاء الدوال عديمة الخوادم ونشرها باستخدام Spring Cloud Function.

الدوال عديمة الخوادم باستخدام Spring Cloud درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 2 من أصل 3. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Microservices & REST APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Microservices & REST APIs 3 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

What are Serverless Functions?

Serverless functions (also known as Functions as a Service or FaaS) are small, single-purpose pieces of code that run in response to events, without you having to manage any servers.

  • You only pay for the compute time consumed.
  • No server provisioning or scaling to worry about.
  • Focus purely on your code logic.

Introducing Spring Cloud Function

Spring Cloud Function is a framework that helps you write business logic as Java java.util.function types (Supplier, Function, Consumer).

The magic? You write your function once, and Spring Cloud Function makes it deployable across different serverless platforms like AWS Lambda, Azure Functions, or Google Cloud Functions, with minimal changes.

Supplier: No Input, Just Output

A Supplier is a function that takes no arguments but produces a result. Think of it as a data source or a generator.

  • It implements java.util.function.Supplier<T>.
  • It has a single method: T get().
  • Useful for polling, scheduled tasks, or generating initial data.

Your First Supplier Function

This example shows a simple Supplier that returns a greeting message. The @Bean annotation exposes it as a Spring Cloud Function.

Try running it to see the output!

import java.util.function.Supplier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SupplierApp {

    public static void main(String[] args) {
        // Run Spring Boot application context
        ConfigurableApplicationContext context = SpringApplication.run(SupplierApp.class, args);
        // Get the supplier bean and invoke its 'get' method
        Supplier<String> helloSupplier = context.getBean("helloSupplier", Supplier.class);
        System.out.println(helloSupplier.get());
        context.close(); // Clean up context
    }

    @Bean
    public Supplier<String> helloSupplier() {
        return () -> "Hello from CoddyKit Serverless!";
    }
}

Function: Input to Output

A Function is a mapping from one type to another. It takes an argument and produces a result.

  • It implements java.util.function.Function<T, R>.
  • It has a single method: R apply(T t).
  • Perfect for data transformation, processing requests, or applying business logic.

Transform Data with a Function

Here, our upperCaseFunction takes a String and returns its uppercase version. This demonstrates a simple data transformation.

Run the code and observe how the input changes!

import java.util.function.Function;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class FunctionApp {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(FunctionApp.class, args);
        Function<String, String> upperCaseFunction = context.getBean("upperCaseFunction", Function.class);
        String input = "coddykit serverless";
        String output = upperCaseFunction.apply(input);
        System.out.println("Input: " + input);
        System.out.println("Output: " + output);
        context.close();
    }

    @Bean
    public Function<String, String> upperCaseFunction() {
        return value -> value.toUpperCase();
    }
}

Consumer: Input, No Output

A Consumer is a function that takes an argument but produces no result. It's used for performing an action or side effect.

  • It implements java.util.function.Consumer<T>.
  • It has a single method: void accept(T t).
  • Ideal for logging, saving data, sending notifications, or triggering other processes.

Process Data with a Consumer

This printerConsumer takes a String and simply prints it to the console. It consumes the data without returning anything.

See how the message is processed!

import java.util.function.Consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class ConsumerApp {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(ConsumerApp.class, args);
        Consumer<String> printerConsumer = context.getBean("printerConsumer", Consumer.class);
        String message = "Processing complete for this event!";
        System.out.println("Consuming message...");
        printerConsumer.accept(message);
        context.close();
    }

    @Bean
    public Consumer<String> printerConsumer() {
        return value -> System.out.println("Received and processed: " + value);
    }
}

Deployment Agnostic Power

The core strength of Spring Cloud Function is its deployment agnosticism. You write your function once using standard Java interfaces, and the framework provides adapters to run it on various serverless platforms.

This means you're not locked into a specific cloud provider's API, making your code more portable and future-proof.

Function Type Check

Which type of Spring Cloud Function is designed to take an input and produce a transformed output?

Recap: Serverless & Spring Cloud Function

We've explored serverless functions and how Spring Cloud Function enables you to write portable business logic.

  • Serverless means focusing on code, not infrastructure.
  • Spring Cloud Function uses Supplier, Function, and Consumer interfaces.
  • These functions can be deployed to various FaaS platforms, offering great flexibility.

This approach simplifies development and increases the portability of your microservices.

الأسئلة الشائعة

هل درس «الدوال عديمة الخوادم باستخدام Spring Cloud» مجاني؟

نعم — نص درس «الدوال عديمة الخوادم باستخدام Spring Cloud» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 3 دروس في المجموع.

ماذا ستتعلم في «الدوال عديمة الخوادم باستخدام Spring Cloud»؟

استكشف إنشاء الدوال عديمة الخوادم ونشرها باستخدام Spring Cloud Function. تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟

لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 3.

كم من الوقت يستغرق درس «الدوال عديمة الخوادم باستخدام Spring Cloud»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟

نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. النشر على عنقود Kubernetes
  2. الدوال عديمة الخوادم باستخدام Spring Cloud
  3. خط أنابيب CI/CD للخدمات المصغّرة
← العودة إلى Spring Boot 4 Microservices & REST APIs