Gateway网关(API网关)是现代分布式系统和微服务架构中的关键组件,它作为系统的统一入口,处理所有客户端请求并将其路由到适当的后端服务。
浏览器将请求统一传给网关然后通过网关去给每一个服务去发送请求order的请求就发给service-order
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
在每一个要添加网关的服务上面加上和网关一样的公共前缀@RequestMapping("/api/order")
注意:在远程调openfeign用上面也要加上和网关一样的公共前缀
1、在网关模块创建一个存放断言工厂的类目录predicate
package com.atguigu.gateway.predicate;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
// Spring Cloud Gateway提供的抽象基类,用于创建自定义路由断言
import org.springframework.cloud.gateway.handler.predicate.GatewayPredicate;
import org.springframework.cloud.gateway.handler.predicate.QueryRoutePredicateFactory;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.server.ServerWebExchange;
import java.util.function.Predicate;
* 命名规范:类名必须以RoutePredicateFactory结尾
@Component // 标识为Spring组件,会被自动扫描注册
public class vipRoutePredicateFactory extends AbstractRoutePredicateFactory<vipRoutePredicateFactory.Config> {
public List<String> shortcutFieldOrder() {
return Arrays.asList("param", "value");
public vipRoutePredicateFactory() {
super(Config.class); // 重要:必须传递配置类的Class对象
* @param config 包含配置参数的Config对象
* @return 返回GatewayPredicate实例
public Predicate<ServerWebExchange> apply(Config config) {
return new GatewayPredicate() {
* @param exchange 包含请求/响应信息的上下文对象
public boolean test(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
String paramValue = request.getQueryParams().getFirst(config.getParam());
return StringUtils.hasText(paramValue) &&
paramValue.equals(config.getValue());
@NotEmpty(message = "参数名不能为空")
private String param; // 要检查的查询参数名
@NotEmpty(message = "匹配值不能为空")
private String value; // 期望的参数值
// Getter和Setter方法(必须提供,供框架调用)
public void setParam(String param) {
public void setValue(String value) {
#遵守的路径如果路径以/api/order开头的就会转发到service-order服务
#----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#解释:请求必须满足请求头为/api/product/并且传的参数必须包含param的值为user和value的值为leon
在 Spring Cloud Gateway 中,过滤器(Filter) 是一种可以拦截并处理 HTTP 请求和响应的组件。它类似于 Servlet 中的 Filter,但基于 响应式编程(Reactive) 实现,主要用于: