URI lb:// i lokalizator discovery
Dowiedz się, jak Spring Cloud Gateway rozwiązuje schemat lb:// za pośrednictwem service discovery oraz jak automatycznie tworzyć trasy z zarejestrowanych usług.
URI lb:// i lokalizator discovery to bezpłatna lekcja API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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.
Często zadawane pytania
Czy lekcja „URI lb:// i lokalizator discovery” jest bezpłatna?
Tak — pełny tekst „URI lb:// i lokalizator discovery” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway), przejdź na CoddyKit PRO. Kurs API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) zawiera 4 lekcji w sumie.
Co nauczysz się w „URI lb:// i lokalizator discovery”?
Dowiedz się, jak Spring Cloud Gateway rozwiązuje schemat lb:// za pośrednictwem service discovery oraz jak automatycznie tworzyć trasy z zarejestrowanych usług. Ćwiczysz API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?
Nie wymagamy żadnego doświadczenia. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „URI lb:// i lokalizator discovery”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?
Tak. Każda lekcja API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Integracja z Eureka i Consul
- Dynamiczne trasowanie z wykrywaniem usług
- Równoważenie obciążenia za pomocą Spring Cloud LoadBalancer
- URI lb:// i lokalizator discovery