如何将Spring Cloud Gateway路由到WebSocket服务?

时间:2019-05-27 00:52:17

标签: spring-boot websocket spring-cloud-gateway

我想在Spring Cloud Gateway上路由我的Websocket服务

作为Spring Cloud Gateway文档,我试图做到这一点并且有效:

spring:
  cloud:
    gateway:
      routes:
      - id: websocket-sockjs-route
        uri: http://localhost:8085
        predicates:
        - Path=/websocket/info/**
      - id: websocket_route
        uri: ws://localhost:8085
        predicates:
        - Path=/websocket/**

但是当我尝试这样做时,它不会起作用:

spring:
  cloud:
    gateway:
      routes:
      - id: websocket-sockjs-route
        uri: http://websocket-service:8085
        predicates:
        - Path=/websocket/info/**
      - id: websocket_route
        uri: ws://websocket-service:8085
        predicates:
        - Path=/websocket/**

我想让我的websocket路由变成这样:

spring:
  cloud:
    gateway:
      routes:
      - id: hello-world-route
        uri: lb://hello-world:8081
        predicates:
        - Path=/hello

1 个答案:

答案 0 :(得分:1)

您可以查看此问题:gateway

,它应该通过以下方式工作:

spring:
  cloud:
    gateway:
      routes:
      - id: websocket-sockjs-route
        uri: http://websocket-service
        predicates:
        - Path=/websocket/info/**
      - id: websocket_route
        uri: lb:ws://websocket-service
        predicates:
        - Path=/websocket/**

如果使用发现客户端,则无需设置端口,只需使用服务名称即可。