API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · บทเรียน

URI lb:// และตัวระบุตำแหน่งบริการ

ทำความเข้าใจวิธีที่ Spring Cloud Gateway แก้ไขโครงร่าง lb:// ผ่านการค้นหาบริการ และวิธีสร้างเส้นทางโดยอัตโนมัติจากบริการที่ลงทะเบียนไว้

บทเรียน 4 จาก 413 ขั้นตอน

URI lb:// และตัวระบุตำแหน่งบริการ เป็นบทเรียน API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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: true

Auto-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/42

Lowercasing 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: true

Mixing 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=1

When 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: DEBUG

Quick 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-id resolves 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.

เริ่มต้นได้ฟรี

เรียนรู้ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “URI lb:// และตัวระบุตำแหน่งบริการ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “URI lb:// และตัวระบุตำแหน่งบริการ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การผสานรวมกับ Eureka และ Consul
  2. การกำหนดเส้นทางแบบไดนามิกด้วยการค้นหาบริการ
  3. การกระจายโหลดด้วย Spring Cloud LoadBalancer
  4. URI lb:// และตัวระบุตำแหน่งบริการ
← กลับไปที่ API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)