springcloud gateway+websocket使用的坑

这个问题用一天的时间解决了,网上一直没有搜到答案

问题描述:websocket 请求通过网关时报错,

2022-02-25 14:41:28,117 [boundedElastic-2] o.s.c.l.CompositeLog ERROR [262f41de-1] 500 Server Error for HTTP GET “/stt/streaming” java.lang.ClassCastException: class io.undertow.server.HttpServerExchange cannot be cast to class reactor.netty.http.server.HttpServerResponse (io.undertow.server.HttpServerExchange and reactor.netty.http.server.HttpServerResponse are in unnamed module of loader ‘app’) at org.springframework.web.reactive.socket.server.upgrade.ReactorNettyRequestUpgradeStrategy.upgrade(ReactorNettyRequestUpgradeStrategy.java:163) ~[spring-webflux-5.3.9.jar:5.3.9] Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: … Stack trace: at org.springframework.web.reactive.socket.server.upgrade.ReactorNettyRequestUpgradeStrategy.upgrade(ReactorNettyRequestUpgradeStrategy.java:163) ~[spring-webflux-5.3.9.jar:5.3.9] at org.springframework.web.reactive.socket.server.support.HandshakeWebSocketService.lambda$handleRequest$1(HandshakeWebSocketService.java:243) ~[spring-webflux-5.3.9.jar:5.3.9]

核心点:

class io.undertow.server.HttpServerExchange cannot be cast to class reactor.netty.http.server.HttpServerResponse

这里可以看出是 undertow类型cast to netty类型出错。。。cloud gateway 是基于webflux的,不需要使用undertow或者 tomcat 等容器,查找项目gateway模块的pom文件,引入umdertow依赖,以及在spring-boot-starter-web 下还包含tomcat依赖 都需要除去

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${
          
   spring-boot.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
                
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
            <version>${
          
   spring-boot.version}</version>
            
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
                
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
            <version>${
          
   spring-boot.version}</version>
            
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
    
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
经验分享 程序员 微信小程序 职场和发展