spring gateway 配置http和websocket路由转发规则

1、转发http配置-普通方式

xml 复制代码
server:
  port: 8088
spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      routes:
        -id: url-proxy-1
          uri: https://blog.csdn.net
          predicates:
            -Path=/csdn

上面这段配置的意思是,配置了一个 id 为 url-proxy-1的URI代理规则,路由的规则为:

当访问地址 http://localhost:8088/csdn/123.jsp 时,会路由到地址 https://blog.csdn.net/123.jsp

字段说明
  • id:我们自定义的路由 ID,保持唯一
  • uri:目标服务地址
  • predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
  • path: 监听需要转发的地址,

2、转发http配置-使用注册中心地址

2.1、直接转发
xml 复制代码
server:
  port: 8088
spring:
  cloud:
    gateway:
      routes:
        - id: http1
          uri: "lb://serviceName" #http转发配置,通过nacos注册中心调用 serviceName
          predicates: 
            - Path=/**

其中 "lb://serviceName" 表示 使用注册中心的地址进行转发,lbloadbalancer的简写,lb://后面一定要填写正确的服务名称,是通过spring.application.name配置的名称,当然也可以直接写 serviceName;

注意: 当 Path 为/**,且uri为 "lb://serviceName" 时表示监听所有服务的地址进行路由转发

2.2、转发指定的path
复制代码
spring:
  cloud:
    gateway:
      routes:
        - id: sdfsd
          uri: "lb:ws://blade-system"
          predicates:
            - Path=/blade-system/websocket/**
      discovery:
        locator:
          enabled: true
    loadbalancer:
      retry:
        enabled: true

假设2个服务的IP地址

  • spring gateway 的服务ip地址为:http://192.168.1.10:8000
  • blade-system服务的ip为:http://192.168.1.10:9000

当我访问 http://192.168.1.10:8000/blade-system/websocket/topic 时,会路由到 blade-system服务的地址: http://192.168.1.10:9000/websocket/topic

3、转发websocket配置

spring gatewat 默认情况下是不转发websocket请求的,所以需要我们自行配置转发规则;

xml 复制代码
spring:
  cloud:
    gateway:
      routes:
        - id: http1
          uri: "ws://127.0.0.1:8081" #websocket配置,直接地址使用方式
          predicates: 
            - Path=/websocket_1
        - id: websocket1
          #uri: "lb:ws://serviceName" #websocket配置,通过nacos注册中心调用serviceName
          predicates: 
            - Path=/websocket_2

4、转发多个地址

因为routes本身就是个列表,转发多个就直接配置多个路由即可

复制代码
spring:
  cloud:
    gateway:
      routes:
        - id: http1   # 配置第一个路由
          uri: "lb://serviceName" #http转发配置,通过nacos注册中心调用serviceName使用方式2
          predicates: 
            - Path=/**
        - id: websocket1 # 配置第二个路由
          uri: "lb:ws://serviceName" #websocket配置,通过nacos注册中心调用serviceName使用方式2
          predicates: 
            - Path=/websocket
相关推荐
qq_334060211 天前
spring_springmvc_mybatis权限控制+boostrap实现UI
java·spring·mybatis
小江的记录本1 天前
【Spring Boot】Spring Boot 全体系知识结构化拆解(附 Spring Boot 高频面试八股文精简版)
java·spring boot·后端·spring·面试·tomcat·mybatis
eggwyw1 天前
Spring 中使用Mybatis,超详细
spring·tomcat·mybatis
爱吃生蚝的于勒1 天前
【Linux】网络之http协议
linux·运维·服务器·网络·数据结构·c++·http
梵得儿SHI1 天前
SpringCloud 微服务实战:支付全链路生产级落地(接口对接 + 异步通知 + 订单状态闭环)
spring·spring cloud·微服务·微信支付·支付模块·支付集成·支付系统架构设计
爱吃烤鸡翅的酸菜鱼1 天前
Spring Cloud Eureka 服务注册与发现实战详解:从原理到高可用集群搭建
java·spring·spring cloud·eureka
23.1 天前
【分析】HTTP请求端口错误诊断:404、502与连接拒绝的区别
网络·网络协议·http
splage1 天前
Spring Framework 中文官方文档
java·后端·spring
Bear on Toilet1 天前
HTTP + JSON 接口调用清单总结
网络协议·http·json
好多渔鱼好多1 天前
【IPC】HLS(HTTP Live Streaming)技术介绍
网络·网络协议·http·hls