linkis实现的spingcloudgateway解析

linkis是一个连接执行引擎与上层应用之间的大数据组件。其中防火墙模块按照Gateway的多WebSocket请求转发实现来重写的。

对此进行了剖析,看它是如何利用scala语言重写了多websocket转发。

SpringCloudGatewayConfiguration.scala定义了配置文件,其中加载了两个GlobalFilter,一个为GatewayAuthorizationFilter,一个为SpringCloudGatewayWebsocketFilter。

还有定义了路由转发RouteLocator 。

根据上面的定义,可以深入去理解SpringCloudGatewayWebsocketFilter,来找到其实现多websocket转发的解决方案。

 

一、搭建eureka注册服务中心

首先运行,com.webank.wedatasphere.linkis.eureka.SpringCloudEurekaApplication

搭建eureka注册服务中心。linkis设置的端口为:20303

二、启动gateway-ujes-support模块

配置的main class为:com.webank.wedatasphere.linkis.DataWorkCloudApplication

模块为:linkis-gateway-ujes-support.

修改配置文件中:application.yml的eureka服务地址

eureka:
  client:
    serviceUrl:
#      defaultZone: http://localhost:port/eureka/
      defaultZone: http://localhost:20303/eureka/
    registry-fetch-interval-seconds: 5
  instance:
    metadata-map:
      test: wedatasphere

调试程序

然后启动。

三、gateway的谓语predicates分析

1、配置文件中的application.yml,定义了几个基本的路由

比方/dws/   /dws/vg/  /dws/easyide/

server:
  port: 9001
spring:
  application:
    name: DataWorkCloud-Gateway
  cloud:
    gateway:
      routes:
        - id: dws
          uri: http://localhost:port/
          predicates:
          - Path=/dws/
        - id: dws/vg
          uri: http://localhost:port/
          predicates:
          - Path=/dws/vg/
        - id: dws/easyide
          uri: http://localhost:port/
          predicates:
          - Path=/dws/easyide/

2、com.webank.wedatasphere.linkis.gateway.springcloud.SpringCloudGatewayConfiguration#createRouteLocator定义的route

有:/api  /dws  /ws  /ws_http

其中websocket路由为:lb:ws://

微服务路由为:lb://

具体的要看其它微服务怎么去调用来实现

四、gateway统一进行的登录验证:com.webank.wedatasphere.linkis.gateway.springcloud.SpringCloudGatewayConfiguration#authorizationFilter

其中调用了该方法:com.webank.wedatasphere.linkis.gateway.springcloud.http.GatewayAuthorizationFilter#filter

最终验证方法为:com.webank.wedatasphere.linkis.gateway.security.SecurityFilter#doFilter

如果失败,则在response里面修改了context,从而得到显示的数据:

{
method: "/dws",
status: -1,
message: "You are not logged in, please login first(您尚未登录,请先登录)!",
data: { },
}

五、注释权限验证,再次请求

com.webank.wedatasphere.linkis.gateway.springcloud.http.GatewayAuthorizationFilter#gatewayDeal

将SecurityFilter.doFilter注释

//        if(!SecurityFilter.doFilter(gatewayContext)) {
//            return gatewayHttpResponse.getResponseMono();
//        } else
            if(gatewayContext.isWebSocketRequest()) {
            return chain.filter(exchange);
        }

再次请求http://192.168.88.102:9001/api

追踪到处理该请求的为:com.webank.wedatasphere.linkis.gateway.parser.DefaultGatewayParser#parse

返回 sendErrorResponse(s"Cannot find a service to deal $path, ignore it.", gatewayContext)

{
method: "/api",
status: 1,
message: "Cannot find a service to deal /api, ignore it.",
data: { },
}

最本质的是该parse方法:com.webank.wedatasphere.linkis.gateway.parser.DefaultGatewayParser

看还有这些定义好的接口:/api/rest....          /dws

  private val COMMON_REGEX = "/api/rest_[a-zA-Z]+/(v\\d+)/([^/]+)/.+".r
  private val CLIENT_HEARTBEAT_REGEX = s"/api/rest_[a-zA-Z]+/(v\\d+)/${AbstractGatewayParser.GATEWAY_HEART_BEAT_URL.mkString("/")}".r

再次参考官方文档:http接入文档

发现了确实是这样过滤请求的。接下来就是发现服务,转发服务。现在需要搭建其它的模块了

 

 

 

参考:芋道 Spring Cloud 网关 Spring Cloud Gateway 入门

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值