0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · درس

تعريف المسارات والاختبارات الشرطية

تعلّموا تعريف المسارات الديناميكية باستخدام الاختبارات الشرطية، مثل Path وHost وMethod، لتوجيه الطلبات إلى خدمات محددة.

تعريف المسارات والاختبارات الشرطية درس مجاني في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 4 دروس في المجموع.

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

Routing with API Gateways

Welcome! In a microservices architecture, clients don't talk directly to every service. Instead, they interact with an API Gateway.

This gateway acts as a single entry point, directing incoming requests to the correct backend service. This process is called routing.

What Are Predicates?

In Spring Cloud Gateway, predicates are key to routing. Think of them as 'conditions' or 'rules'.

  • A predicate evaluates to true or false.
  • If a predicate (or set of predicates) for a route evaluates to true, the gateway forwards the request using that route.
  • If false, the gateway tries the next route.

Gateway Project Foundation

Routes are typically configured in your Spring Boot application's application.yml or application.properties file. Here's a minimal Spring Boot application that *could* host our gateway configuration. The routing logic itself will go into the YAML.

You can run this basic app to see it working:

package com.coddykit.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class GatewayApplication {

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

  @GetMapping("/test-gateway")
  public String testGateway() {
    return "Hello from Gateway!";
  }
}

Basic Route Structure

A route in Spring Cloud Gateway needs three main things:

  • id: A unique identifier for the route.
  • uri: The URI of the backend service to which requests will be forwarded.
  • predicates: A list of conditions that must be met for this route to activate.

We'll primarily define these in application.yml.

The Path Predicate

The Path predicate is one of the most common. It matches the request URI path against a specified pattern.

  • You can use wildcards like * (matches one path segment) and ** (matches multiple path segments).
  • For example, /users/* matches /users/1 but not /users/admin/1.
  • /users/** matches both /users/1 and /users/admin/1.

Path Predicate in Action

Here's how you'd configure a route using the Path predicate to send all requests starting with /api/users to a backend user-service:

spring:
  cloud:
    gateway:
      routes:
        - id: user_service_route
          uri: http://localhost:8081 # Your backend user service
          predicates:
            - Path=/api/users/**

The Host Predicate

The Host predicate matches requests based on the hostname in the request header.

  • This is useful for routing requests from different domains or subdomains to specific services.
  • You can also use wildcards here, like *.example.com to match any subdomain of example.com.

Host Predicate Example

Let's say you want to route all requests coming from api.mycompany.com to a specific backend service. Here's how:

spring:
  cloud:
    gateway:
      routes:
        - id: api_domain_route
          uri: http://localhost:8082 # Your backend API service
          predicates:
            - Host=api.mycompany.com

The Method Predicate

The Method predicate matches requests based on their HTTP method (also known as HTTP verb).

  • Common methods include GET, POST, PUT, DELETE, and PATCH.
  • This allows you to direct different types of operations (e.g., reading vs. creating data) to distinct backend endpoints or services.

Method Predicate Example

To ensure only POST requests to /products are routed to a specific service, and other methods are handled elsewhere:

spring:
  cloud:
    gateway:
      routes:
        - id: create_product_route
          uri: http://localhost:8083 # Service for creating products
          predicates:
            - Path=/products
            - Method=POST

Predicate Power Check

You've learned about Path, Host, and Method predicates. Now, let's test your understanding!

Recap: Routes & Predicates

Great job! You've learned the fundamentals of defining routes and using predicates in Spring Cloud Gateway.

  • Routes direct traffic to backend services.
  • Predicates are the conditions (like Path, Host, Method) that determine if a route matches.
  • Multiple predicates on a single route act as an AND condition.

This powerful combination allows for flexible and intelligent traffic management in your microservices.

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

هل درس «تعريف المسارات والاختبارات الشرطية» مجاني؟

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

ماذا ستتعلم في «تعريف المسارات والاختبارات الشرطية»؟

تعلّموا تعريف المسارات الديناميكية باستخدام الاختبارات الشرطية، مثل Path وHost وMethod، لتوجيه الطلبات إلى خدمات محددة. تتمرن على API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)؟

لا تُشترط خبرة سابقة. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.

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

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

هل يمكنني كتابة وتشغيل أكواد في درس API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) هذا؟

نعم. كل درس في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

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

  1. البوابة مقابل الخدمات المصغّرة التقليدية
  2. إعداد مشروع بوابة أساسي
  3. تعريف المسارات والاختبارات الشرطية
  4. فهم الأساس التفاعلي
← العودة إلى API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)