API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · Lektion

Die lb://-URI & der Discovery-Locator

Verstehen Sie, wie Spring Cloud Gateway das Schema lb:// über Service Discovery auflöst und wie sich Routen automatisch aus registrierten Services erzeugen lassen.

Lektion 4 von 413 Schritte

Die lb://-URI & der Discovery-Locator ist eine kostenlose API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Kostenlos starten

Lerne API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
12
Lektionen
48

Häufig gestellte Fragen

Ist die Lektion „Die lb://-URI & der Discovery-Locator“ kostenlos?

Ja — der vollständige Text von „Die lb://-URI & der Discovery-Locator“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Die lb://-URI & der Discovery-Locator“?

Verstehen Sie, wie Spring Cloud Gateway das Schema lb:// über Service Discovery auflöst und wie sich Routen automatisch aus registrierten Services erzeugen lassen. Du übst API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) zu starten?

Keine Vorkenntnisse erforderlich. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Die lb://-URI & der Discovery-Locator“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Lektion Code schreiben und ausführen?

Ja. Jede API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Integration mit Eureka und Consul
  2. Dynamisches Routing mit Service Discovery
  3. Load Balancing mit Spring Cloud LoadBalancer
  4. Die lb://-URI & der Discovery-Locator
← Zurück zu API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)