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

请求效果

相关推荐
PXM的算法星球4 天前
spring gateway配合nacos实现负载均衡
spring·gateway·负载均衡
1990_super5 天前
使用ceph-deploy安装和配置RADOS Gateway (RGW)并使用S3访问集群
ceph·gateway
北极糊的狐8 天前
接口返回504 Gateway Time-out 错误,这意味着请求在网关或代理服务器等待上游服务器响应时超时。以下是可能的原因和排查建议:
数据库·gateway
sg_knight8 天前
Spring Cloud Gateway全栈实践:动态路由能力与WebFlux深度整合
java·spring boot·网关·spring·spring cloud·微服务·gateway
放纵日放纵11 天前
微服务—Gateway
微服务·架构·gateway
你我约定有三11 天前
分布式微服务--GateWay(1)
java·开发语言·分布式·微服务·架构·gateway
William一直在路上14 天前
KONG API Gateway中的核心概念
网络·gateway·kong
Java牛马17 天前
SpringCloud之Gateway
网关·spring cloud·gateway·路由·过滤器·断言
yh云想18 天前
《微服务SpringCloud架构实践指南:从Nacos到Gateway的全面解析》
spring cloud·nacos·gateway·openfeign·filter