1 Star 0 Fork 33

Hanley / spring-cloud-gateway-dubbo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Spring Cloud Gateway Dubbo

工程简介

Spring Cloud Gateway Dubbo 基于Spring Cloud Alibaba的SpringCloud集成Dubbo的方案之上开发, 用于在SpringCloudGateway中直接调用Dubbo接口,可以减少使用Web接口来实现与gateway对接。

架构

HTTP网关

HTTP网关
基础的SpringCloudGateway需要WebApp对Dubbo接口进行调用

RPC网关

RPC网关
网关直接调用Dubbo后无需WebApp进行中转

转发流程

转发流程

结构

结构

已实现功能

  • Dubbo路由解析及转发
  • Dubbo Consumer缓存
  • Dubbo 参数rewrite
    • 内置header,query,cookie,body变量
    • 支持自定义变量扩展
    • 内置Spel,Velocity引擎渲染变量
  • Dubbo调用结果转换

工程结构

模块 描述
spring-cloud-gateway-dubbo 集成Dubbo调用功能,包括路由解析、转发、参数处理、参数重写、结果转换等功能
spring-cloud-gateway-dubbo-starter 集成dubbo功能自动装配
spring-cloud-gateway-dubbo-sample 集成Dubbo调用示例

示例工程

工程结构

模块 描述
dubbo-provider Dubbo服务提供方
gateway 网关

依赖

  • 注册中心使用nacos,用于Dubbo服务注册,需要本地启动Nacos,如果非本地Nacos则需求修改配置,配置文件: spring-cloud-gateway-dubbo-sample/gateway/src/main/resources/application.yml

启动

分别启动:

com.github.leecho.spring.cloud.dubbo.sample.gateway.GatewayApplication
com.github.leecho.spring.cloud.dubbo.sample.provider.DubboProviderApplication

测试

在idea中执行http文件:gateway.http

配置dubbo路由

在Gateway中定义路由

- id: echo
  uri: dubbo://dubbo-provider/EchoService/echo
  predicates:
  - Path=/echo/**
  metadata:
    dubbo:
      group: default
      version: 1.0
      parameterTypes:
        - java.lang.String

dubbo路由uri结构:dubbo://{serviceName}/{interfaceClass}/{method},dubbo接口其他属性在metadata中进行配置,内置的解析器会识别scheme为dubbo的uri,也可以进行自定义

重写dubbo参数

在Gateway中定义路由

- id: rewrite
  uri: dubbo://dubbo-provider/EchoService/request
  metadata:
    dubbo:
      group: default
      version: 1.0
      parameterTypes:
        - com.github.leecho.spring.cloud.dubbo.sample.provider.Action
        - com.github.leecho.spring.cloud.dubbo.sample.provider.Request
      rewrite:
        request:
          message: "#body['request']['message']"
          target:
            name: "#cookie['JSESSIONID']"
            id: "#header['Authorization']"
          date: 2021-07-01 00:00:00
          #attributes: "#body['request']['attributes']"
          attributes:
            - "#header['Authorization']"
            - attr2
            - attr3
        action:
          name: request
          parameters:
            param1: "#auth['user']"
  predicates:
    - Path=/rewrite/**

如何扩展

将Dubbo结果转成成自定义结构

配置DubboMessageConverter对象,会自动识别并进行装配:

@Bean
public DubboMessageConverter dubboResultConverter() {
    return source -> {
    Map<String, Object> result = new HashMap<>(2);
    result.put("success", true);
    result.put("data", source);
    return result;
};

自定义Consumer缓存

配置DubboGenericServiceCache对象,会自动识别并进行装配:

@Bean
public DubboGenericServiceCache dubboGenericServiceCache() {
    return new GuavaDubboGenericServiceCache(1000, Duration.of(10, ChronoUnit.SECONDS));
}

扩展自定义变量

配置VariableLoader对象

	@Bean
	public VariableLoader authVariableProcessor() {
		return context -> {
			Map<String, Object> auth = new HashMap<>();
			auth.put("user", "user");
			context.setVariable("auth", auth);
		};
	}

已知问题

  • 配置Dubbo重写参数时,参数类型为list时geteway路由会解析成map
  • 尚未经过大规模测试,在生产环境上使用请先进行测试,欢迎大家测试后提供测试结果及改进意见

交流

欢迎大家进行使用及测试,有任何问题可以在平台上提交issue或者站内信联系我,也欢迎大家提交pr,共同为开源社区提供更好的方案。

MIT License Copyright (c) 2021 leecho Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

SpringCloudGateway集成Dubbo,可直接在网关调用Dubbo接口,内置缓存、结果转换、参数处理、参数重写等功能。 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/thanlin/spring-cloud-gateway-dubbo.git
git@gitee.com:thanlin/spring-cloud-gateway-dubbo.git
thanlin
spring-cloud-gateway-dubbo
spring-cloud-gateway-dubbo
master

搜索帮助