【深入解析spring cloud gateway】05 gateway请求转发实验

一、环境准备

三个工程:

eureka-server

eureka-client

gateway

实验目的:通过网关访问对应的微服务:eureka-client。gateway和eureka-client注册到eureka-server上

二、eureka-server和eureka-client准备

eureka-server略

eureka-client

application.yml

yaml 复制代码
eureka:
    client:
        serviceUrl:
            defaultZone: http://localhost:8761/eureka/
server:
    port: 8081
spring:
    application:
        name: hello-service

提供一个接口

java 复制代码
@RestController
@Slf4j
public class HelloController {
  @RequestMapping("/hello")
  public String hello() {
    return "hello"
  }
}

三、gateway配置

pom.xml

xml 复制代码
 <dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

application.yml

yaml 复制代码
server:
  port: 8080
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka
    instance:
      instance-id: gateway
      prefer-ip-address: true
---
spring:
  application:
    name: gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true #开启自动路由功能,根据服务名称自动创建routes
          lower-case-service-id: true #识别小写服务名

定义一个filter用于去掉路径中的/gateway

java 复制代码
@Configuration(proxyBeanMethods = false)
public class FilterConfig {
    @Bean    public RemovePathFilter removePathFilter() {
        return new RemovePathFilter();    }
}

自定义一个GlobalFilter,用于去掉路径/gateway/

java 复制代码
@Slf4j
public class RemovePathFilter implements GlobalFilter, Ordered {

    private static final String PREFIX = "/gateway/";

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("删除api前缀");
        ServerHttpRequest req = exchange.getRequest();

        addOriginalRequestUrl(exchange, req.getURI());
        String path = req.getURI().getRawPath();
        if (path.contains(PREFIX)) {
            String newPath = path.replace(PREFIX, "/");
            req = req.mutate().path(newPath).build();
            exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, req.getURI());
        }
        return chain.filter(exchange.mutate().request(req).build());
    }

    @Override
    public int getOrder() {
        return 0;
    }
}

请求示例

通过访问 http://localhost:8080/hello-service/gateway/hello网关地址,可以正确请求到eureka-client的接口

bash 复制代码
C:\Users\Administrator>curl -X GET http://localhost:8080/hello-service/gateway/hello
hello

四、总结

1、gateway通过服务发现,将请求/hello-service/gateway/hello转发到hello-service上

2、自定义一个GlobalFilter,用于去掉路径/gateway/路径
项目源码地址

相关推荐
Maiko Star12 小时前
Gateway网关拦截自定义header & 用户上下文打通实战
gateway·threadlocal
阿拉斯攀登4 天前
Spring Cloud Alibaba 生态中 RocketMQ 最佳实践
分布式·微服务·rocketmq·springcloud·cloudalibaba
skywalk81634 天前
LLM API Gateway:使用Comate Spec Mode创建大模型调用中转服务器
服务器·人工智能·gateway·comate
我是小妖怪,潇洒又自在4 天前
springcloud alibaba(七)Gateway--服务网关
spring·spring cloud·gateway
boy快快长大7 天前
【Spring Cloud Alibaba】Gateway(一)
数据库·gateway
谷隐凡二7 天前
网关的核心概念及简单演进介绍
gateway
摇滚侠7 天前
2025最新 SpringCloud 教程,Gateway-过滤器-基本使用,笔记58
笔记·spring cloud·gateway
摇滚侠8 天前
2025最新 SpringCloud 教程,Gateway-过滤器-globalFilter,笔记60
笔记·spring cloud·gateway
摇滚侠8 天前
2025最新 SpringCloud 教程,Gateway-过滤器-默认过滤器,笔记59
笔记·spring cloud·gateway