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

L'URI lb:// e il locator del discovery

Comprenda come Spring Cloud Gateway risolve lo schema lb:// tramite il service discovery e come creare automaticamente route dai servizi registrati.

Lezione 4 di 413 passaggi

L'URI lb:// e il locator del discovery è una lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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.

Gratis per iniziare

Impara API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «L'URI lb:// e il locator del discovery» è gratuita?

Sì — il testo completo di «L'URI lb:// e il locator del discovery» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), passa a CoddyKit PRO. Il corso API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include 4 lezioni in totale.

Cosa imparerò in «L'URI lb:// e il locator del discovery»?

Comprenda come Spring Cloud Gateway risolve lo schema lb:// tramite il service discovery e come creare automaticamente route dai servizi registrati. Eserciti API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

Non è richiesta alcuna esperienza precedente. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «L'URI lb:// e il locator del discovery»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

Sì. Ogni lezione API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Integrazione con Eureka/Consul
  2. Routing dinamico con la service discovery
  3. Bilanciamento del carico con Spring Cloud LoadBalancer
  4. L'URI lb:// e il locator del discovery
← Torna a API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)