我有一个简单的 Thrift 服务器,在 scala 中用 finagle 实现:
import com.twitter.util.{ Await, Future }
import com.jakiku.thriftscala.{ RealTimeDatabasePageImpressions, PageImpressions }
import com.twitter.finagle.Thrift
import com.twitter.finagle.thrift.ThriftServerFramedCodec
import com.twitter.finagle.builder.{ ServerBuilder, Server }
object ThriftServer {
def main(args: Array[String]) {
val server = Thrift.serveIface("localhost:9090", new RealTimeDatabasePageImpressions[Future] {
def getByTrackIdAndDay(trackId: Int, day: Int) = {
Future(Seq(PageImpressions.apply(123, 3, 4, 3, 2000L, 2000L)))
}
})
Await.ready(server)
}
}
这是我的节俭文件:
namespace java com.jakiku.thriftjava
#@namespace scala com.jakiku.thriftscala
typedef i64 long
typedef i32 int
struct PageImpressionsSum{
1: required int trackId;
2: required int day;
3: required int hour;
4: required int minute;
5: required string pageId;
6: required long uniqueImpressions;
7: required long sumImpressions;
}
struct PageImpressions{
1: required int trackId;
2: required int day;
3: required int hour;
4: required int minute;
6: required long uniqueImpressions;
7: required long sumImpressions;
}
service RealTimeDatabase_pageImpressions{
list<PageImpressions> getByTrackIdAndDay(1:int trackId, 2:int day);
}
service RealTimeDatabase_pageImpressionsSum {
list<PageImpressionsSum> getByTrackIdAndDay(1:int trackId, 2:int day);
}
我用 scrooge 生成了 scala thrift 类。添加了 thriftlib、scrooge-core、scrooge-generator 等作为依赖项。我还添加了 scrooge sbt 插件:
addSbtPlugin("com.twitter" % "scrooge-sbt-plugin" % "4.5.0")
我用 sudo sbt 'run' 启动了服务器。控制台的输出:
[信息] Running ThriftServer Mai 25, 2016 4:18:39 AM com.twitter.finagle.Init$$anonfun$1 apply$mcV$sp 信息:Finagle 版本 6.34.0 (rev=44f444f606b10582c2da8d5770b7879ddd961211) 建于 20160310-155851
在这一刻,一切看起来都很好。我在nodejs中测试了服务器。不工作。我还使用了 thrift python 测试套件。每次检查都会超时。
这是 nodejs 客户端:
var thrift = require('thrift');
var Realtime_pageImpressions = require('./gen-nodejs/RealTimeDatabase_pageImpressions.js');
var ttypes = require('./gen-nodejs/RealTimeDatabase_types.js');
var connection = thrift.createConnection("localhost", 9090);
connection.on('error', function(err) {
console.log(err);
});
var client = thrift.createClient(Realtime_pageImpressions, connection);
client.getByTrackIdAndDay(123124, 4, function(err, response) {
if (err) {
console.error(err);
} else {
console.log(response);
}
});
我真的不知道我做错了什么。