Introduction
Dubbo Gateway是一个基于Java语言的Dubbo网关实现。基于Dubbo注册中心的元数据,通过将HTTP请求转换为Dubbo协议,泛化调用的方式返回请求结果。它具有如下的特性:
- 使用简单,开箱即用,非常适用于一些接口测试场景;
- 灵活可拓展。通过Interceptor的实现,可非常方便实现熔断、限流、路由、定制响应、用户授权等功能。系统也内置了部分Interceptor实现;
- 支持仅调用声明为对外开放的Dubbo服务,安全保证。(基于Feature:apache/dubbo#7660 );
- 文档自动化生成。基于Javadoc标准的dubbo对外接口生成系统,API写好即可生成文档,同时集成了阿里云OSS,全程仅需几步参数配置,即可可视化预览文档。
Requirements
服务提供者Dubbo 版本:Dubbo 2.7.14+ 或 Dubbo 3.x
Java 版本:1.8+
Guides
以Springboot项目方式,启动Dubbo Gateway网关:
1、添加依赖
新建Springboot工程,添加Dubbo Gateway的核心依赖:
<dependency>
<groupId>com.kalman03groupId>
<artifactId>gateway-coreartifactId>
<version>1.2.0version>
dependency>
当然,还需要添加Dubbo的注册中心依赖,以Zookeeper为例:
<dependency>
<groupId>org.apache.zookeepergroupId>
<artifactId>zookeeperartifactId>
<version>${zookeeper_version}version>
<exclusions>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-frameworkartifactId>
<version>${curator_version}version>
dependency>
<dependency>
<groupId>org.apache.curatorgroupId>
<artifactId>curator-recipesartifactId>
<version>${curator_version}version>
dependency>
2、参数配置
gateway.netty.server.port=80
gateway.netty.server.host=127.0.0.1
gateway.netty.server.connect-timeout=3000
gateway.netty.business.thread-count=50
gateway.dubbo.registry.address=zookeeper://127.0.0.1:2181
gateway.dubbo.openservice=true
3、启动服务
@SpringBootApplication
@EnableAutoConfiguration
public class DubboGatewayTest {
public static void main(String[] args) {
try {
SpringApplication.run(DubboGatewayTest.class, args);
} catch (Throwable e) {
e.printStackTrace();
}
}
//可选自定义拦截器
@Component
@Order(100)
@InterceptorRule(routeRuleType = RouteRuleType.PATH, excludePatterns = { "/api/**" })
class CustomInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(GatewayHttpRequest request, GatewayHttpResponse response) throws Exception {
System.out.println("preHandle");
return true;
}
@Override
public void afterCompletion(GatewayHttpRequest request, GatewayHttpResponse response, Exception ex)
throws Exception {
System.out.println("afterCompletion");
}
}
}
4、访问目标服务
系统内置了PATH路由
与MIX路由
,也同时支持自定义路由实现。下面演示如何通过不同的路由规则访问对应的服务。
-
PATH路由(推荐)
PATH路由是系统内置默认的路由,也是系统推荐的路由规则,其拥有较多适用性(既可正常调用,也可以满足一些第三方系统的调用要求,比如支付消息回调等),路由规则如下
HTTP URL:
http(s)://{domain}:{port}/{appName}/{interfaceName}/{method}/{group}/{version}
HTTP Body (payload or form-data):
{ "id": 23, "username": "testUser" }
-
MIX路由
考虑到有些参数相对于固定不变,且暴露在URL中不尽友善,系统内置了一种MIX的路由规则,将部分路由参数以Header的参数形式进行传递。
HTTP URL:
http(s)://{domain}:{port}/{interfaceName}/{method}
HTTP Header:
x-app-name={appName} x-group={group} x-version={version} x-route-rule=mix
HTTP Body (payload or form-data):
{ "id": 23, "username": "testUser" }
-
CUSTOM自定义路由
CUSTOM路由
专为一些对PATH路由
与MIX路由
都不满意的开发者准备,只需要继承AbstractRouteHandlerInterceptor 类,即可轻松实现自定义的路由规则。
Param Transmission
用户端通用参数(诸如请求UA/Referer/IP等)以及Token用户信息等,需要传递到服务提供方。Dubbo Gateway与服务提供者之间内置的参数传递走Dubbo的RpcContext。
Dubbo网关内置了gatewayConsumerFilter
与gatewayProviderFilter
,可作为Dubbo服务提供者的默认Filter实现,通过该Filter可获取用户端请求的一些参数以及自定义拦截器封装的参数。当然,开发者也可自行在服务提供方读取RpcContext中的传递参数。
更多使用方式,参考:gateway-samples 与 gateway-samples-provider
Attention
Dubbo Gateway对Dubbo服务提供者提供的对外服务有一条要求:只能有一个服务入参,且为对象类型(非普通Java数据类型)。
TODO
- 更丰富的使用案例
欢迎贡献pull request......
Contacts
QQ group:601173814
License
Dubbo Gateway is released under the Apache License Version 2.0. See the LICENSE file for details.