当前 dubbo 支持 1的 thrift 协议是对 thrift 原生协议 2 的扩展,在原生协议的基础上添加了一些额外的头信息,比如 service name,magic number 等。
使用 dubbo thrift 协议同样需要使用 thrift 的 idl compiler 编译生成相应的 java 代码,后续版本中会在这方面做一些增强。
依赖
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.8.0</version>
</dependency>
配置
所有服务共用一个端口 3:
<dubbo:protocol name="thrift" port="3030" />
使用
可以参考 dubbo 项目中的示例代码
常见问题
- Thrift 不支持 null 值,即:不能在协议中传递 null 值
1.
2.3.0
以上版本支持
↩
2. Thrift 是 Facebook 捐给 Apache 的一个 RPC 框架 ↩
3. 与原生Thrift不兼容 ↩
ThriftProtocol也是提供了export和refer对外暴露服务和引用服务两个功能
(1)export:暴露服务,通过netty、mina或者grizaly对外暴露tcp服务
//通过dubboExport中提供的netty、mina或grizaly对外暴露tcp协议
public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException {
// can use thrift codec only
URL url = invoker.getUrl().addParameter(Constants.CODEC_KEY, ThriftCodec.NAME);
// find server.
String key = url.getAddress();
// client can expose a service for server to invoke only.
boolean isServer = url.getParameter(Constants.IS_SERVER_KEY, true);
if (isServer && !serverMap.containsKey(key)) {
serverMap.put(key, getServer(url));
}
// export service.
key = serviceKey(url);
DubboExporter<T> exporter = new DubboExporter<T>(invoker, key, exporterMap);
exporterMap.put(key, exporter);
return exporter;
}
(2)refer:引用服务,与服务提供者建立长连接进行服务引用
//通过ThriftInvoker中提供的netty、mina或grizaly通过tcp协议与服务提供者建立连接
public <T> Invoker<T> refer(Class<T> type, URL url) throws RpcException {
ThriftInvoker<T> invoker = new ThriftInvoker<T>(type, url, getClients(url), invokers);
invokers.add(invoker);
return invoker;
}