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

请求效果

相关推荐
柳贯一(逆流河版)5 小时前
Gateway 集成 JWT 身份认证:微服务统一认证的实战指南
微服务·架构·gateway
码界奇点8 小时前
Nginx 502 Bad Gateway从 upstream 日志到 FastCGI 超时深度复盘
运维·nginx·阿里云·性能优化·gateway
半夏知半秋11 小时前
基于skynet框架业务中的gateway实现分析
服务器·开发语言·后端·学习·gateway
耳东哇1 天前
sentinel docker gateway k8s 集群 主从
docker·gateway·sentinel
龙茶清欢2 天前
2、Nginx 与 Spring Cloud Gateway 详细对比:定位、场景与分工
java·运维·spring boot·nginx·spring cloud·gateway
龙茶清欢2 天前
在 Spring Cloud Gateway 中实现跨域(CORS)的两种主要方式
java·spring boot·spring cloud·微服务·gateway
William一直在路上3 天前
Kong Gateway 实操实例:代理上游服务并配置限流插件
gateway·kong
龙茶清欢4 天前
4、除了常见的 services(业务微服务)和 gateway(API 网关)模块外,还必须建立一系列支撑性、平台级、基础设施类模块
微服务·架构·gateway
debug 小菜鸟6 天前
Python + Flask + API Gateway + Lambda + EKS 实战
python·flask·gateway