定义路由与谓词
学习使用谓词(例如 Path、Host 和 Method)定义动态路由,将请求转发到指定服务。
定义路由与谓词 是 CoddyKit 上的免费 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Routing with API Gateways
Welcome! In a microservices architecture, clients don't talk directly to every service. Instead, they interact with an API Gateway.
This gateway acts as a single entry point, directing incoming requests to the correct backend service. This process is called routing.
What Are Predicates?
In Spring Cloud Gateway, predicates are key to routing. Think of them as 'conditions' or 'rules'.
- A predicate evaluates to
trueorfalse. - If a predicate (or set of predicates) for a route evaluates to
true, the gateway forwards the request using that route. - If
false, the gateway tries the next route.
Gateway Project Foundation
Routes are typically configured in your Spring Boot application's application.yml or application.properties file. Here's a minimal Spring Boot application that *could* host our gateway configuration. The routing logic itself will go into the YAML.
You can run this basic app to see it working:
package com.coddykit.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
@GetMapping("/test-gateway")
public String testGateway() {
return "Hello from Gateway!";
}
}Basic Route Structure
A route in Spring Cloud Gateway needs three main things:
id: A unique identifier for the route.uri: The URI of the backend service to which requests will be forwarded.predicates: A list of conditions that must be met for this route to activate.
We'll primarily define these in application.yml.
The Path Predicate
The Path predicate is one of the most common. It matches the request URI path against a specified pattern.
- You can use wildcards like
*(matches one path segment) and**(matches multiple path segments). - For example,
/users/*matches/users/1but not/users/admin/1. /users/**matches both/users/1and/users/admin/1.
Path Predicate in Action
Here's how you'd configure a route using the Path predicate to send all requests starting with /api/users to a backend user-service:
spring:
cloud:
gateway:
routes:
- id: user_service_route
uri: http://localhost:8081 # Your backend user service
predicates:
- Path=/api/users/**The Host Predicate
The Host predicate matches requests based on the hostname in the request header.
- This is useful for routing requests from different domains or subdomains to specific services.
- You can also use wildcards here, like
*.example.comto match any subdomain ofexample.com.
Host Predicate Example
Let's say you want to route all requests coming from api.mycompany.com to a specific backend service. Here's how:
spring:
cloud:
gateway:
routes:
- id: api_domain_route
uri: http://localhost:8082 # Your backend API service
predicates:
- Host=api.mycompany.comThe Method Predicate
The Method predicate matches requests based on their HTTP method (also known as HTTP verb).
- Common methods include
GET,POST,PUT,DELETE, andPATCH. - This allows you to direct different types of operations (e.g., reading vs. creating data) to distinct backend endpoints or services.
Method Predicate Example
To ensure only POST requests to /products are routed to a specific service, and other methods are handled elsewhere:
spring:
cloud:
gateway:
routes:
- id: create_product_route
uri: http://localhost:8083 # Service for creating products
predicates:
- Path=/products
- Method=POSTPredicate Power Check
You've learned about Path, Host, and Method predicates. Now, let's test your understanding!
Recap: Routes & Predicates
Great job! You've learned the fundamentals of defining routes and using predicates in Spring Cloud Gateway.
- Routes direct traffic to backend services.
- Predicates are the conditions (like
Path,Host,Method) that determine if a route matches. - Multiple predicates on a single route act as an AND condition.
This powerful combination allows for flexible and intelligent traffic management in your microservices.
用 AI 导师学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「定义路由与谓词」课时是免费的吗?
是的 — 「定义路由与谓词」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程的其余内容,请升级到 CoddyKit PRO。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。
「定义路由与谓词」这节课中我会学到什么?
学习使用谓词(例如 Path、Host 和 Method)定义动态路由,将请求转发到指定服务。 你通过在浏览器中直接运行的动手代码来练习 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) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「定义路由与谓词」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课中编写并运行代码吗?
能。每节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。