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 跨域配置

请求效果

相关推荐
yuluo_YX13 天前
AI Gateway 介绍
人工智能·gateway
网硕互联的小客服16 天前
502 Bad Gateway:服务器作为网关或代理时收到无效响应处理方式
运维·服务器·gateway
sevevty-seven16 天前
什么是Gateway
gateway
秋の花16 天前
【GateWay】和权限验证
java·gateway
欧先生^_^18 天前
org.springframework.cloud.gateway 组件解释
gateway
jarenyVO19 天前
Spring Cloud Gateway 全面学习指南
java·gateway
保持学习ing21 天前
微服务--Gateway网关
java·网关·微服务·gateway
SZ17011023122 天前
IGP(Interior Gateway Protocol,内部网关协议)
运维·服务器·gateway
肥仔哥哥193022 天前
SpringCloud2025+SpringBoot3.5.0+gateway+webflux子服务路由报503
微服务·gateway·最新微服务
亚林瓜子1 个月前
AWS API Gateway配置日志
云计算·gateway·aws·log·cloudwatch