cmd 相应目录下安装thrift
Python Scripts>pip install thrift
Python Scripts>conda list
thrift 0.11.0 <pip>
因为conda是0.11.0,所以都安装0.11.0版本
http://thrift.apache.org/download
http://archive.apache.org/dist/thrift/
http://archive.apache.org/dist/thrift/0.11.0/
下载thrift-0.11.0.exe,和test.thrift放在同一个文件夹下
test.thrift
service Transmit {
string sayMsg(1:string msg);
string invoke(1:i32 cmd 2:string token 3:string data)
}
cmd相应目录下执行:
D:\thrift>.\thrift-0.11.0.exe --gen py test.thrift
D:\thrift>.\thrift-0.11.0.exe --gen java test.thrift
D:\thrift
在Python Scripts下新建testthrifttest文件夹,将gen-py下的东西复制到testthrifttest下
python服务端:
import json
from testthrifttest.test import Transmit
from testthrifttest.test.ttypes import *
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
from thrift.server import TServer
import socket
class TransmitHandler:
def __init__(self):
self.log = {}
def sayMsg(self, msg):
msg = json.loads(msg)
print("sayMsg(" + msg + ")")
return "say " + msg + " from " + socket.gethostbyname(socket.gethostname())
def invoke(self,cmd,token,data):
cmd = cmd
token =token
data = data
if cmd ==1:
return json.dumps({token:data})
else:
return 'cmd不匹配'
if __name__=="__main__":
handler = TransmitHandler()
processor = Transmit.Processor(handler)
transport = TSocket.TServerSocket('127.0.0.1', 8000)
tfactory = TTransport.TBufferedTransportFactory()
pfactory = TBinaryProtocol.TBinaryProtocolFactory()
server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)
print("Starting python server...")
server.serve()
python客户端:
import sys
import json
from testthrifttest.test import Transmit
from testthrifttest.test.ttypes import *
from testthrifttest.test.constants import *
from thrift import Thrift
from thrift.transport import TSocket
from thrift.transport import TTransport
from thrift.protocol import TBinaryProtocol
transport = TSocket.TSocket('127.0.0.1', 8000)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Transmit.Client(protocol)
# Connect!
transport.open()
cmd = 1
token = '1111-2222-3333-4444'
data = json.dumps({"name":"zhoujielun"})
msg = client.invoke(cmd,token,data)
print(msg)
transport.close()
java客户端
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.11.0</version>
</dependency>
将gen-java中的Transmit.java放入项目中
顶部加上包名
一些override去掉
thriftclient 依赖 Transmit.java 和 libthrift
java客户端:thriftclient.java
import com.hqwx.baiduAPI.Transmit;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSON;
public class thriftclient {
public static void main(String[] args) {
try {
TTransport transport;
transport = new TSocket("127.0.0.1", 8000);
transport.open();
TProtocol protocol = new TBinaryProtocol(transport);
Transmit.Client client = new Transmit.Client(protocol);
int cmd = 1;
String token = "1111-2222-3333-4444";
String data = "{\"name\":\"zhoujielun\"}";
String msg = client.invoke(cmd,token,data);
for(int i=0;i<1000000;i++ ) {
int cmdd = i%2;
String msg1 = client.invoke(cmdd,token,data);
System.out.println(msg1);
}
//System.out.println(msg);
transport.close();
} catch (TException e) {
e.printStackTrace();
}
}
}