Gateway 搭建

1.创建 moudle 命名为 gateway

2,pom中引入依赖 网关依赖;注册中心依赖等

java 复制代码
<!--        网关依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
<!--     nacos注册中心   -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
<!--        负载均衡-->
              <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

创建启动类

配置yml文件

java 复制代码
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
server:
  port: 80

测试 启动成功

配置请求分发路径

java 复制代码
spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      routes:
        - id: order-route  # 路由名称
          uri: lb://order  # 路由要转发的服务 格式为固定的  lb:// 服务名
          predicates: # 断言
            - Path=/api/order/**  # 路径为 /api/order/** 的请求专项order服务
        - id: product-route
          uri: lb://product
          predicates:
            - Path=/api/product/**

网关路径重写示例:

例如:请求的全路径为 /api/order/getOrder经过网关后 将请求地址省略为 /getOrder

复制代码
filters:
  - RewritePath=/red/?(?<segment>.*),/$\{segment}

自定义网关全局过滤器

java 复制代码
package gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

//自定义全局过滤器
@Component
public class RtGlobalFiler implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String string = request.getURI().toString();
        System.out.println("当前操作时间"+System.currentTimeMillis()+"操做的地址:"+string);

        Mono<Void> doFinally = chain.filter(exchange).doFinally((result) -> {
            System.out.println("当前操作的结束时间" + System.currentTimeMillis());
        });
        return doFinally;
    }

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

gateway yml 跨域配置

请求效果

相关推荐
曼彻斯特的海边15 小时前
RequestRateLimiterGatewayFilterFactory
spring cloud·gateway·限流
·云扬·15 小时前
【PmHub面试篇】Gateway全局过滤器统计接口调用耗时面试要点解析
面试·职场和发展·gateway
devil_mf15 小时前
gateway 网关 路由新增 (已亲测)
gateway
KubeSphere 云原生3 天前
云原生周刊:探索 Gateway API v1.3.0
云原生·gateway
曼彻斯特的海边4 天前
spring-cloud-alibaba-sentinel-gateway
gateway·sentinel·springcloud
xujinwei_gingko5 天前
网关Gateway
微服务·gateway
徐子童6 天前
《Spring Cloud Gateway 快速入门:从路由到自定义 Filter 的完整教程》
java·开发语言·spring cloud·nacos·gateway
亚林瓜子6 天前
AWS API Gateway 配置WAF(中国区)
云计算·gateway·aws·waf
枫super10 天前
Spring Cloud 详解:2025 最新技术与最佳实践
后端·spring·spring cloud·eureka·nacos·gateway·openfeign