thrift-超快速建立client与server的通信
关键字
thrift windows 新手入门教程
废话几句
建立client与server的通信,之前一直采用的最常见的http+xml/jscn的方式。
也用过php-rpc,生成jar的步骤稍有繁琐,执行效率也不是很让人满意。
webservice做过几个项目,写wsdl文件是个体力活,不过有了wsdl后还是很快的。
有了thrift,一切就太简单了。
使用方法
下面笔者简单研究了下,windows下最简单的方法(详细请见http://thrift.apache.org/):
1.搞一个 Apache Thrift compiler
http://thrift.apache.org/download/
直接下exe最快。
2.Writing a .thrift file
struct UserProfile {
1: i32 uid,
2: string name,
3: string blurb
}
service UserStorage {
void store(1: UserProfile user),
UserProfile retrieve(1: i32 uid)
}
3.生成代码
thrift --gen <language> <Thrift filename>
thrift-0.8.0.exe --gen java ./test.thrift 我是这样生成java代码的。
gen-java里生成UserProfile.java与UserStorage.java两个文件。
4.实现代码
UserStorage是一个由服务生成的类,里面有2个子类 Iface 与 AsyncIface 分别是同步与异步的接口类。
自己写一个UserStorageImpl来实现吧。
(1) Python Client
# Make an object
up = UserProfile(uid=1,
name="Test User",
blurb="Thrift is great")
# 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)
(2)C++ Server
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 handler(new UserStorageHandler());
shared_ptr processor(new UserStorageProcessor(handler));
shared_ptr serverTransport(new TServerSocket(port));
shared_ptr transportFactory(new TBufferedTransportFactory());
shared_ptr protocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
return 0;
}
(2)Java Server
还是实现一下吧。
UserStorageImpl.java
import org.apache.thrift.TException;
public class UserStorageImpl implements UserStorage.Iface{
public void store(UserProfile user) throws TException {
// TODO Auto-generated method stub
System.out.println("store...." + user.toString());
}
public UserProfile retrieve(int uid) throws TException {
// TODO Auto-generated method stub
System.out.println("retrieve...." + uid);
return null;
}
}
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import foo.UserStorage.Processor;
public class Server {
static private void start() {
try {
TServerSocket serverTransport = new TServerSocket(7777);
UserStorage.Processor processor = new UserStorage.Processor(new UserStorageImpl());
Factory protFactory = new TBinaryProtocol.Factory(true, true);
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor) );
System.out.println("Server start on port:" + 7777);
server.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
start();
}
}
Client.java
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class Client {
public static void main(String[] args) {
try {
TTransport transport = new TSocket("localhost", 7777);
TProtocol protocol = new TBinaryProtocol(transport);
UserStorage.Client client = new UserStorage.Client(protocol);
transport.open();
System.out.println("client call by thrift");
client.retrieve(111);
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

浙公网安备 33010602011771号