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

lb:// URI 与发现定位器

了解 Spring Cloud Gateway 如何通过服务发现解析 lb:// 方案,以及如何根据已注册服务自动创建路由。

第 4 / 4 课13 个步骤

lb:// URI 与发现定位器 是 CoddyKit 上的免费 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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.

免费开始

用 AI 导师学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「lb:// URI 与发现定位器」课时是免费的吗?

是的 — 「lb:// URI 与发现定位器」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程的其余内容,请升级到 CoddyKit PRO。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。

「lb:// URI 与发现定位器」这节课中我会学到什么?

了解 Spring Cloud Gateway 如何通过服务发现解析 lb:// 方案,以及如何根据已注册服务自动创建路由。 你通过在浏览器中直接运行的动手代码来练习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「lb:// URI 与发现定位器」课时需要多长时间?

大多数 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. lb:// URI 与发现定位器
← 返回 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway)