0Pricing
API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) · Lesson

The lb:// URI & Discovery Locator

Understand how Spring Cloud Gateway resolves the lb:// scheme through service discovery and how to auto-create routes from registered services.

The lb:// URI & Discovery Locator is a free API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “The lb:// URI & Discovery Locator” lesson free?

Yes — the full text of “The lb:// URI & Discovery Locator” is free to read here on the web, and the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) course, upgrade to CoddyKit PRO.

What will I learn in “The lb:// URI & Discovery Locator”?

Understand how Spring Cloud Gateway resolves the lb:// scheme through service discovery and how to auto-create routes from registered services. You practise API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)?

No prior experience is required. API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The lb:// URI & Discovery Locator” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson?

Yes. Every API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Integrating with Eureka/Consul
  2. Dynamic Routing with Service Discovery
  3. Load Balancing with Spring Cloud LoadBalancer
  4. The lb:// URI & Discovery Locator
← Back to API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)