عنوان URI lb:// ومحدِّد الاكتشاف
افهموا كيفية حل Spring Cloud Gateway لمخطط lb:// عبر اكتشاف الخدمات، وكيفية إنشاء المسارات تلقائيًا من الخدمات المسجّلة.
عنوان URI lb:// ومحدِّد الاكتشاف درس مجاني في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
From Hostnames to Service Names
Hardcoding http://10.0.0.5:8080 breaks when instances move. With service discovery the gateway can target a logical service name instead, and resolve real instances at runtime.
The lb Scheme
Prefix a route's uri with lb:// followed by the service id. The gateway hands the request to the load balancer, which picks a live instance.
routes:
- id: orders
uri: lb://order-service
predicates:
- Path=/orders/**How lb Is Resolved
At request time the gateway asks the discovery client (Eureka, Consul, etc.) for instances of order-service, then Spring Cloud LoadBalancer selects one. The lb:// host is replaced by a concrete address.
Required Dependencies
You need a discovery client and the load balancer on the classpath. With Eureka:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>The Discovery Locator
Instead of writing a route per service, enable the DiscoveryClient route locator. It auto-generates a route for every registered service.
spring:
cloud:
gateway:
discovery:
locator:
enabled: trueAuto-Generated Paths
By default each service is reachable at /SERVICE-ID/**. A service named order-service answers at /order-service/**, forwarded as lb://order-service.
# request /order-service/orders/42
# -> lb://order-service /orders/42Lowercasing Service IDs
Eureka often registers service ids in uppercase. Enable lowercasing so paths stay clean and predictable.
spring:
cloud:
gateway:
discovery:
locator:
lower-case-service-id: trueMixing Manual and Auto Routes
You can keep the discovery locator on for convenience while still defining explicit routes for services that need custom predicates or filters.
routes:
- id: orders-custom
uri: lb://order-service
predicates:
- Path=/api/orders/**
filters:
- StripPrefix=1When Auto Routes Are Risky
The locator exposes every service automatically, including internal ones. In production, many teams disable it and define routes explicitly for tighter control.
Health-Aware Selection
Because resolution happens per request, instances that deregister or fail health checks are dropped from the pool. New instances appear automatically as they register.
Verifying Resolution
Enable debug logging for the load balancer to see which instance each request was routed to.
logging:
level:
org.springframework.cloud.loadbalancer: DEBUGQuick Check
What does the lb:// prefix on a route's URI tell the gateway to do?
Recap
You connected the gateway to service discovery:
lb://service-idresolves instances dynamically- A discovery client and load balancer must be present
- The discovery locator auto-creates routes at
/service-id/** - Disable or scope it in production for safety
This is the backbone of dynamic, resilient routing.
الأسئلة الشائعة
هل درس «عنوان URI lb:// ومحدِّد الاكتشاف» مجاني؟
نعم — نص درس «عنوان URI lb:// ومحدِّد الاكتشاف» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)، انتقل إلى CoddyKit PRO. تتضمن دورة API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 4 دروس في المجموع.
ماذا ستتعلم في «عنوان URI lb:// ومحدِّد الاكتشاف»؟
افهموا كيفية حل Spring Cloud Gateway لمخطط lb:// عبر اكتشاف الخدمات، وكيفية إنشاء المسارات تلقائيًا من الخدمات المسجّلة. تتمرن على 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 منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «عنوان URI lb:// ومحدِّد الاكتشاف»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) هذا؟
نعم. كل درس في API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- التكامل مع Eureka وConsul
- التوجيه الديناميكي مع اكتشاف الخدمات
- موازنة التحميل باستخدام Spring Cloud LoadBalancer
- عنوان URI lb:// ومحدِّد الاكتشاف