An Example
Thrift allows you to define data types and service interfaces in a simple definition file. Taking that file as input, the compiler generates code to be used to easily build RPC clients and servers that communicate seamlessly across programming languages.
For instance, say you would like to write a service to store user objects for your web frontend. You could write a Thrift file as follows:
struct UserProfile {
1: i32 uid,
2: string name,
3: string blurb
}
service UserStorage {
void store(1: UserProfile user),
UserProfile retrieve(1: i32 uid)
}
Thrift does the heavy lifting. Instead of writing a load of boilerplate code to serialize and transport your objects and invoke remote methods, you can get right down to business. Here is some sample Python client code:
# Make an object
up = UserProfile(uid=1,
name="Mark Slee",
blurb="I'll find something to put here.")
# Talk to a server via TCP sockets, using a binary protocol
transport = TSocket.TSocket("localhost", 9090)
transport.open()
protocol = TBinaryProtocol.TBinaryProtocol(transport)
# Use the service we already defined
service = UserStorage.Client(protocol)
service.store(up)
# Retrieve something as well
up2 = service.retrieve(2)
Not much to it. Implementing the server as simple as filling in the blanks:
class UserStorageHandler : virtual public UserStorageIf {
public:
UserStorageHandler() {
// Your initialization goes here
}
void store(const UserProfile& user) {
// Your implementation goes here
printf("store\n");
}
void retrieve(UserProfile& _return, const int32_t uid) {
// Your implementation goes here
printf("retrieve\n");
}
};
int main(int argc, char **argv) {
int port = 9090;
shared_ptr<UserStorageHandler> handler(new UserStorageHandler());
shared_ptr<TProcessor> processor(new UserStorageProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
return 0;
}
分享到:
相关推荐
使用javascript调用thrift进行通信,可以让你了解thrift的完整使用流程。
使用thrift框架的Python事例
Make applications cross-communicate using Apache Thrift! About This Book Leverage Apache Thrift to enable ...Chapter 7: An Example Client-Server Application Chapter 8: Advanced Usage of Apache Thrift
然后你应该运行 Thrift Server: $ sbt "run-main com.acme.Example.ThriftServer"现在我们准备使用 HTTP 客户端进行一些测试调用: $ sbt "run-main com.acme.Example.HttpClient"在 HTTP 客户端你应该得到以下输出...
thrift -o client/ThriftTest --gen cocoa thrift/messaging-service.thrift thrift -o server --gen js:node thrift/messaging-service.thrift 您通常可能不想对生成的代码进行版本控制,而是在构建/编译时生成它...
thrift-clj-example 用法示例。 用法 该示例实现了一个简单的存储服务器,该服务器使用给定的 ID 存储Person对象并在请求时传送它们。 启动服务器 lein run -m person-index.server [] 默认端口为 7007。 启动...
节俭的例子 客户:打字稿 服务器:服务端
thrift --gen py example.thrift ./client.py 提交 节俭文件 一个简单的节俭文件。 一种服务扩展了另一种服务,并且存在异常。 目的是在尽可能小的空间内呈现出许多深奥的案例。 产生节俭服务 注意main.go顶部的//...
example provider Map<String> processorMap = ImmutableMap .<String> of( "blackService", new BlackService.Processor( new BlackServiceImpl()), "userService", new ...
Buck构建的Thrift服务器/客户端示例依存关系愚蠢的: : rev: f0852f333e5574390a8c0498575487b8ed0d3c82 fbthrift: : rev: e71617c04e4426d0706d1c7342de4d2fb543f079 巴克: : rev: 264d12f9fcf4423aeacb2be8e04...
这已经构建为与 [kinesis-example-scala-consumer]。先决条件该项目需要 Java 7 或 8、SBT 0.13.0 和 Thrift: 在 Mac OS X 上,Thrift 可以通过brew install thrift轻松安装。 在 Linux 上, sudo apt-get install...
lua call c++, c++ call lua, c++ call lua with lua call c++ so
要运行示例项目, pod install克隆 repo,然后从 Example 目录运行pod install 。 要求 安装 MSThriftSerializer 可通过。 要安装它,只需将以下行添加到您的 Podfile 中: ##用法 要使用 MSSerializer,只需导入...
build service to client example: final ThriftProtocolService<ThriftClientRequest> serve = ThriftProtocolClientBuilder .safeBuild(ThriftProtocolClientBuilder .get() .codec(FACTORY) ....
nettythrift en: A Java Server IO framework use netty and thrift. You could send thrift json protol ...Server Example public void startServer() { // different from nify,
DataX服务器 为提供远程调用(Thrift服务器,Http服务器)分布式运行(YARN上的DataX)功能 特征 节俭服务器 纱线上的DataX ...单机多线程方式运行 单机多进程方式运行 分散运行(在纱线上) ... cd example/no
实木复合地板先生示例如何在 MapReduce 中使用 Parquet 和 Avro 作为... Parquet 的“默认示例”是 Group,但您也可以使用 Thrift、Google Protocol Buffers、Hive 或 Pig。 但是磁盘上的存储格式仍然是 Parquet。 您仍
我已经包括了两个主要的感兴趣的协议:Http 和 Thrift。 网址 要运行,请打开几个终端并从项目的根目录运行sbt run-main io.artfuldodge.Server 。 这将设置在/zktest下注册的许多服务。 看一看! 在新的终端中运行...
SSH处理程序是在Windows上处理SSH URI(如ssh://user@example.com的程序。 当您在浏览器中单击SSH链接时,它将使用正确的SSH参数启动 。 安装程序 如果需要,安装程序将安装SSH处理程序和.NET Framework 4客户端...