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

请求效果

相关推荐
摘星编程4 天前
Nginx 502 Bad Gateway:从 upstream 日志到 FastCGI 超时复盘
网络·nginx·gateway·php-fpm·fastcgi
网硕互联的小客服4 天前
504 Gateway Timeout:服务器作为网关或代理时未能及时获得响应如何处理?
运维·服务器·gateway
Pierre_5 天前
通过SpringCloud Gateway实现API接口镜像请求(陪跑)网关功能
spring·spring cloud·gateway
kk在加油6 天前
智能门卫:Gateway
gateway
小安同学iter6 天前
Spring Cloud Gateway 网关(五)
java·开发语言·spring cloud·微服务·gateway
JAVA学习通7 天前
Spring Cloud ------ Gateway
java·spring cloud·gateway
weixin_449568709 天前
访问Nginx 前端页面,接口报502 Bad Gateway
前端·nginx·gateway
yangmf204012 天前
LDAP 认证系列(四):Gateway LDAP 认证
大数据·elasticsearch·搜索引擎·gateway·ldap
银迢迢14 天前
SpringCloud微服务技术自用笔记
java·spring cloud·微服务·gateway·sentinel
孤狼程序员18 天前
【Spring Cloud 微服务】2.守护神网关Gateway
spring cloud·微服务·gateway