Dubbo3.x-43-java sdk 之 rpc Triple 3.3 新特性
2016年9月25日大约 6 分钟
回顾
大家好,我是老马。
最近 dubbo3.x 在公司内部分享,于是想系统梳理一下。
总体思路是官方文档入门+一些场景的问题思考+源码解析学习。
本文介绍 triple 协议在 3.3 版本中的新特性
全新的 Rest 支持
特性
在3.3版本中,基于现有 HTTP 协议栈,triple实现了全面的 REST 风格服务导出能力,无需使用泛化或网关层协议转换,无需配置,用户即可通过 HTTP 协议去中心化直接访问后端的 Triple 协议服务。
同时,针对高级 REST 用法,如路径定制、输出格式定制和异常处理,提供了丰富的注解和 SPI 扩展支持。其主要特性包括:
- Triple协议融合
重用Triple原有HTTP协议栈, 无需额外配置或新增端口,即可同时支持 HTTP/1、HTTP/2 和 HTTP/3 协议的访问。 - 去中心化
可直接对外暴露 Rest API,不再依赖网关应用进行流量转发,从而提升性能,并降低因网关引发的稳定性风险。安全问题可通过应用内部扩展解决,这一实践已在淘宝内部的 MTOP 中得到验证。 - 支持已有servlet设施
支持 Servlet API 和 Filter,用户可以重用现有基于 Servlet API 的安全组件。通过实现一个 Servlet Filter,即可集成 OAuth 和 Spring Security 等安全框架。 - 多种方言
考虑到大部分用户习惯使用 SpringMVC 或 JAX-RS 进行 REST API 开发,Triple Rest 允许继续沿用这些方式定义服务,并支持大部分扩展和异常处理机制(具备原框架 80% 以上的功能)。对于追求轻量级的用户,可使用 Basic 方言,Triple 的开箱即用 REST 访问能力即基于此方言导出服务。 - 扩展能力强
提供超过 20 个 扩展点,用户不仅可以轻松实现自定义方言,还能灵活定制参数获取、类型转换、错误处理等逻辑。 - 开箱即用
REST 能力开箱即用,只需启用 Triple 协议,即具备 REST 直接访问服务能力。 - 高性能路由
路由部分采用优化的 Radix Tree 和 Zero Copy 技术,提升路由性能。 - OpenAPI无缝集成(TBD)
即将完成 OpenAPI 集成,开箱即用支持导出 OpenAPI Schema, 引入 Swagger 依赖可直接使用 Web UI 来进行服务测试。有了 OpenAPI Schema 可使用 Postman、Apifox 等API工具来管理和测试 API,利用 OpenAPI 生态可轻松实现跨语言调用。未来会进一步支持 Schema First 的方式,先和前端团队一起定义 OpenAPI, 前端基于 OpenAPI 来生成调用代码和 Mock,后端基于 OpenAPI 来生成 Stub 来开发服务,极大提升协同效率。
示例
示例代码
package org.apache.dubbo.rest.demo;
import org.apache.dubbo.remoting.http12.rest.Mapping;
import org.apache.dubbo.remoting.http12.rest.Param;
// 服务接口
public interface DemoService {
String hello(String name);
@Mapping(path = "/hi", method = HttpMethods.POST)
String hello(User user, @Param(value = "c", type = ParamType.Header) int count);
}
// 服务实现
@DubboService
public class DemoServiceImpl implements DemoService {
@Override
public String hello(String name) {
return "Hello " + name;
}
@Override
public String hello(User user, int count) {
return "Hello " + user.getTitle() + ". " + user.getName() + ", " + count;
}
}
// 模型
@Data
public class User {
private String title;
private String name;
}
下载运行示例
# 获取示例代码
git clone --depth=1 [https://github.com/apache/dubbo-samples.git ](https://github.com/apache/dubbo-samples.git )
cd dubbo-samples/2-advanced/dubbo-samples-triple-rest/dubbo-samples-triple-rest-basic
# 运行
mvn spring-boot:run
curl测试
curl -v "[http://127.0.0.1:8081/org.apache.dubbo.rest.demo.DemoService/hello ](http://127.0.0.1:8081/org.apache.dubbo.rest.demo.DemoService/hello )?name=world"
# 输出如下
#> GET /org.apache.dubbo.rest.demo.DemoService/hello?name=world HTTP/1.1
#>
# POST /org.apache.dubbo.rest.demo.DemoService/hi.txt?title=Mr HTTP/1.1
#> c: 3
#> Content-Length: 9
#> Content-Type: application/x-[www-form-urlencoded](https://www-form-urlencoded)
#>
#
#* Request completely sent off
#
#* Request completely sent off
#< HTTP/3 200
#< content-type: application/json
#<
#"Hello world"
性能对比
丢包率对 QPS 的影响

丢包率对 RT 的影响

架构图

详情文档
请访问:how-to-enable-http3-support-for-triple 了解如何配置并启用 HTTP/3 支持
参考资料
贡献者
binbin.hou