记录SpringCloudGateway无法完成转发Websocket的问题

项目场景:

使用SpringCloudGateway作为网关转发Websocket链接到微服务。


问题描述

SpringCloudGateway无法完成Websocket的转发,表现为无法链接。


原因分析:

我遇到的问题具体有两个原因导致。

  1. 跨域问题

我其实已经配置了,但是少加了一个s,allowedOrigins写成了allowedOrigin
花了我八个小时看源码 自闭

因为SpringGateway有一个默认的跨域Filter:CorsWebFilter。这个过滤器使用DefaultCorsProcessor检查了跨域问题。当调用方的域名非同源并且不在允许列表中时会拒绝访问。

	@Override
	public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
		ServerHttpRequest request = exchange.getRequest();
		CorsConfiguration corsConfiguration = this.configSource.getCorsConfiguration(exchange);
		boolean isValid = this.processor.process(corsConfiguration, exchange); // 调用进行跨域验证
		if (!isValid || CorsUtils.isPreFlightRequest(request)) {
			// 非同源且不在允许范围内
			// 不处理(不转发)
			return Mono.empty();
		}
		return chain.filter(exchange);
	}
  1. 网关微服务引入了tomcat的依赖
    SpringGateway使用了Netty作为容器,而Netty天生支持长连接所以可以进行转发。而项目中如果引入了Tomcat的相关依赖则会将容器替换为Tomcat导致无法转发。具体原因是ServerHttpResponseDecorator这个类中的getNativeResponse这个方法会去对响应体进行强制转换,但是Tomcat的Response和Netty的Response不一样,从而导致抛出ClassCastException。
	/**
	 * Return the native response of the underlying server API, if possible,
	 * also unwrapping {@link ServerHttpResponseDecorator} if necessary.
	 * @param response the response to check
	 * @param <T> the expected native response type
	 * @throws IllegalArgumentException if the native response can't be obtained
	 * @since 5.3.3
	 */
	public static <T> T getNativeResponse(ServerHttpResponse response) {
		if (response instanceof AbstractServerHttpResponse) {
			// 这一段会导致抛出ClassCastException
			return ((AbstractServerHttpResponse) response).getNativeResponse();
		}
		else if (response instanceof ServerHttpResponseDecorator) {
			return getNativeResponse(((ServerHttpResponseDecorator) response).getDelegate());
		}
		else {
			throw new IllegalArgumentException(
					"Can't find native response in " + response.getClass().getName());
		}
	}

解决方案:

  1. 跨域问题
    在配置文件中加入以下配置:
spring:
  cloud:
    gateway:
      globalcors: # 全局的跨域处理
        add-to-simple-url-handler-mapping: true # 解决options请求被拦截问题
        cors-configurations:
          # 这里可以自定义哪些url会匹配跨域定义
          '[/**]':
            allowedOrigins: "*"	# 这里定义允许的跨域host,如果为*则全部允许
            allowedMethods: "*"	# 同上
            allowedHeaders: "*"	# 同上
  1. 引入了tomcat的依赖
    检查依赖,如果存在下列依赖需要去除。
<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>javax.servlet-api</artifactId>
</dependency>
 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
 <dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>jstl</artifactId>
</dependency>
<dependency>
	<groupId>org.apache.tomcat.embed</groupId>
	<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
当使用Spring Cloud Gateway转发WebSocket请求时,需要进行一些特殊配置。下面是一个简单的教程,演示了如何配置Spring Cloud Gateway转发WebSocket请求。 1. 首先,确保你已经有一个基本的Spring Cloud Gateway项目,并且已经添加了必要的依赖。 2. 在你的Gateway配置类中,添加一个`@Bean`方法来创建一个`WebSocketHandlerMapping`的实例。这个实例将用于将WebSocket请求转发到相应的目标服务。 ```java @Configuration public class GatewayConfig { @Bean public WebSocketHandlerMapping webSocketHandlerMapping() { Map<String, WebSocketHandler> handlerMap = new HashMap<>(); // 添加需要转发WebSocket处理器 handlerMap.put("/ws-endpoint", new MyWebSocketHandler()); // 创建WebSocketHandlerMapping实例,并设置handlerMap SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping(); mapping.setOrder(Ordered.HIGHEST_PRECEDENCE); mapping.setUrlMap(handlerMap); return mapping; } } ``` 请替换`MyWebSocketHandler`为你自己实现的WebSocket处理器。 3. 在Gateway配置文件中,添加以下配置来启用WebSocket支持和设置路由规则。 ```yaml spring: cloud: gateway: routes: - id: websocket_route uri: lb://websocket-service predicates: - Path=/ws-endpoint/** filters: - WebSocket=ws-endpoint ``` 这里的`websocket_route`是路由的ID,`lb://websocket-service`是目标WebSocket服务的地址,`/ws-endpoint/**`是需要转发WebSocket请求路径。请根据你的实际情况进行修改。 4. 在你的WebSocket服务项目中,实现一个WebSocket处理器作为目标处理器。例如: ```java @Component public class MyWebSocketHandler extends TextWebSocketHandler { @Override protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { // 处理WebSocket消息 session.sendMessage(new TextMessage("Hello, WebSocket!")); } } ``` 这里的`handleTextMessage`方法用于处理收到的WebSocket消息。 5. 最后,启动你的Gateway服务和WebSocket服务,并尝试发送一个WebSocket消息到`/ws-endpoint`路径。如果一切配置正确,Gateway应该会将该消息转发WebSocket服务,并返回处理结果。 希望这个简单的教程能帮助到你实现Spring Cloud GatewayWebSocket转发功能。如有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dis2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值