thrift 搭建

在这里插入图片描述

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();
        }
		
	}

}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值