6、spring-cloud-gateway

依赖

xml 复制代码
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
    </dependencies>

配置文件

yml 复制代码
server:
  port: 8080

spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

    gateway:
      routes:
        - id: ms1
          uri: lb://ms1
          predicates:
            - Path=/ms1/**
          filters:
            - RewritePath=/ms1/?(?<segment>.*),/$\{segment}

        - id: ms2
          uri: lb://ms2
          predicates:
            - Path=/ms2/**
          filters:
            - RewritePath=/ms2/?(?<segment>.*),/$\{segment}

        - id: ms3
          uri: lb://ms3
          predicates:
            - Path=/ms3/**
          filters:
            - RewritePath=/ms3/?(?<segment>.*),/$\{segment}

配置类

可自定义断言工厂、拦截器工厂、全局拦截器

自定义全局拦截器

java 复制代码
@Slf4j
@Component
public class MyGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String uri = request.getURI().toString();
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        log.info("请求【{}】开始,时间:{}", uri, LocalDateTime.now());
        // 放行
        Mono<Void> mono = chain
                .filter(exchange)
                .doFinally((result) -> {
                    stopWatch.stop();
                    log.info("请求【{}】结束,时间:{},耗时:{}s", uri, LocalDateTime.now(), stopWatch.getTotalTimeSeconds());
                });
        return mono;
    }

    @Override
    public int getOrder() {
        // 数字越小,优先级越高
        return 0;
    }
}
相关推荐
王八八。5 小时前
linux后台java、postSQL部署命令
java·linux·运维
月落归舟5 小时前
MyBatis缓存机制
java·缓存·mybatis
huipeng9265 小时前
企业级微服务开发实战(一):项目启动与工程化设计
java·开发语言·spring boot·spring cloud·微服务·云原生·架构
爱吃土豆的马铃薯ㅤㅤㅤㅤㅤㅤㅤㅤㅤ6 小时前
java实现excel导入、下载模板方法
java·开发语言·excel
段ヤシ.7 小时前
回顾Java知识点,面试题汇总Day12(持续更新)
java·mybatis
java1234_小锋7 小时前
Spring AI 2.0 开发Java Agent智能体 - MCP(模型上下文协议)
java·人工智能·spring·spring ai
seven97_top7 小时前
两小时入门Sentinel
java·sentinel
叶小鸡7 小时前
Java 篇-项目实战-AI 天机学堂(从 0 到 1)-day1
java·开发语言
bigbearxyz7 小时前
Caused by: java.net.SocketException: Connection reset问题排查
java·keepalived·proxysql
500849 小时前
昇腾 CANN 的五层架构,到底分了哪五层
java·人工智能·分布式·架构·ocr·wpf