GONETHEN 2021-06-04 16:45 采纳率: 0%
浏览 1211

springcloud gateway websocket 无法正常建立连接,求大佬看看怎么回事?

gateway配置

routes:
        - id: bulletscreen
          uri: lb://tubangUser
          predicates:
          - Path=/ws/info/**
        - id: bulletscreen
          uri: lb:ws://tubangUser
          predicates:
          - Path=/ws/**

vue代码

connection() {
        // 建立连接对象
        let socket = new SockJS('/ws');
        // 获取STOMP子协议的客户端对象
        this.stompClient = Stomp.over(socket);
        // 定义客户端的认证信息,按需求配置
        const token = getToken()
        let headers = {
          'Authorization': '',
          'token': token
        }
        // 向服务器发起websocket连接
        this.stompClient.connect(headers, (frame) => {}
}

gateway 报错

2021-06-04 16:47:46.722 ERROR 19236 --- [nio-8005-exec-4] a.w.r.e.AbstractErrorWebExceptionHandler : [16634efc]  500 Server Error for HTTP GET "/ws/201/1dacfqtz/websocket"

java.lang.ClassCastException: org.apache.catalina.connector.ResponseFacade cannot be cast to reactor.netty.http.server.HttpServerResponse
	at org.springframework.web.reactive.socket.server.upgrade.ReactorNettyRequestUpgradeStrategy.getNativeResponse(ReactorNettyRequestUpgradeStrategy.java:182) ~[spring-webflux-5.2.6.RELEASE.jar:5.2.6.RELEASE]
	Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
	|_ checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
	|_ checkpoint ⇢ org.springframework.boot.actuate.metrics.web.reactive.server.MetricsWebFilter [DefaultWebFilterChain]
	|_ checkpoint ⇢ HTTP GET "/ws/201/1dacfqtz/websocket" [ExceptionHandlingWebHandler]
Stack trace:
		at org.springframework.web.reactive.socket.server.upgrade.ReactorNettyRequestUpgradeStrategy.getNativeResponse(ReactorNettyRequestUpgradeStrategy.java:182) ~[spring-webflux-5.2.6.RELEASE.jar:5.2.6.RELEASE]
		at org.springframework.web.reactive.socket.server.upgrade.ReactorNettyRequestUpgradeStrategy.upgrade(ReactorNettyRequestUpgradeStrategy.java:162) ~[spring-webflux-5.2.6.RELEASE.jar:5.2.6.RELEASE]
		at org.springframework.web.reactive.socket.server.support.HandshakeWebSocketService.lambda$handleRequest$1(HandshakeWebSocketService.java:235) ~[spring-webflux-5.2.6.RELEASE.jar:5.2.6.RELEASE]
		at reactor.core.publisher.FluxFlatMap.trySubscribeScalarMap(FluxFlatMap.java:151) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoFlatMap.subscribeOrReturn(MonoFlatMap.java:53) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:57) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:64) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.Mono.subscribe(Mono.java:4218) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:172) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:64) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.InternalMonoOperator.subscribe(InternalMonoOperator.java:64) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoDefer.subscribe(MonoDefer.java:52) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.Mono.subscribe(Mono.java:4218) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.drain(MonoIgnoreThen.java:172) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoIgnoreThen.subscribe(MonoIgnoreThen.java:56) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:150) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:67) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:76) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.FluxConcatMap$ConcatMapImmediate.innerNext(FluxConcatMap.java:274) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.FluxConcatMap$ConcatMapInner.onNext(FluxConcatMap.java:851) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:114) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.FluxSwitchIfEmpty$SwitchIfEmptySubscriber.onNext(FluxSwitchIfEmpty.java:67) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.Operators$MonoSubscriber.complete(Operators.java:1755) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoFlatMap$FlatMapMain.onNext(MonoFlatMap.java:144) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.FluxMap$MapSubscriber.onNext(FluxMap.java:114) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
		at reactor.core.publisher.MonoNext$NextSubscriber.onNext(MonoNext.java:76) ~[reactor-core-3.3.5.RELEASE.jar:3.3.5.RELEASE]
  • 写回答

4条回答 默认 最新

  • gzywalker 2021-12-24 18:13
    关注

    Gateway默认使用基于Netty容器的RequestUpgradeStrategy和WebSocketClient导致报错,声明Tomcat容器对应的Bean来覆盖它可以解决这个问题

        @Bean
        @Primary
        public RequestUpgradeStrategy requestUpgradeStrategy() {
            return new TomcatRequestUpgradeStrategy();
        }
        
        @Bean
        @Primary
        public WebSocketClient webSocketClient() {
            return new TomcatWebSocketClient();
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding