《2022年THRIFT开发教程 .pdf》由会员分享,可在线阅读,更多相关《2022年THRIFT开发教程 .pdf(5页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1 编写 thrift 文件(如 aa.thrift)namespacejava com.tv189.uc.thriftnamespacecpp thrift.vdbnamespacerb thrift.vdbnamespaceperl thrift.vdbnamespacecsharp thrift.vdbnamespacejs thrift.vdbnamespacest thrift.vdbnamespacepy thrift.vdbnamespacephp thriftservice UCThriftServicestring ucOperator(1:stringactionType,
2、2:string uid, 3:string data),2 生成 java 文件thrift-0.6.0.exe-r -gen java uc.thriftthrift-0.6.0.exe-r -gen java uc.thriftthrift-0.6.0.exe-r -gen php uc.thriftthrift-0.6.0.exe-r -gen py uc.thrift可以生成 php,py 等等的3 服务端编写1)实现 UCThriftService.Ifacepublic class UCThriftServiceImplimplementsUCThriftService.Ifac
3、epublic static Logger logger = Logger.getLogger(UCThriftServiceImpl.class);Overridepublic String ucOperator(StringactionType,String suid, String data)throws TExceptionSystem.out.println(test);2)运行的 main 编写public class UCServiceServerprivate static Logger logger = Logger.getLogger(UCServiceServer.cla
4、ss);public static void main(String.args) throws Exception /这个是用 properties编写的,可以自行决定String server = PropertiesUtil.getValue(tserver.properties,ucserver.nio,hahs);if (nio.equalsIgnoreCase(server)startNIO(); if (hahs.equalsIgnoreCase(server)名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心
5、整理 - - - - - - - 第 1 页,共 5 页 - - - - - - - - - startHAHS(); else start();private static void start() throws TTransportExceptionString address = PropertiesUtil.getValue(tserver.properties,ucserver.address,0.0.0.0);int port = Integer.parseInt(PropertiesUtil.getValue(tserver.properties,ucserver.port,55
6、55);int clientTimeout=Integer.parseInt(PropertiesUtil.getValue(tserver.properties,ucserver.clientTimeout,30000);int minWorkerThreads=Integer.parseInt(PropertiesUtil.getValue(tserver.properties,ucserver.minWorkerThreads,25);int maxWorkerThreads=Integer.parseInt(PropertiesUtil.getValue(tserver.propert
7、ies,ucserver.maxWorkerThreads,25);String protocol = PropertiesUtil.getValue(tserver.properties,ucserver.protocol,binary);TServerSocketsocket = new TServerSocket(newInetSocketAddress(address,port), clientTimeout);UCThriftService.Processorprocess = new UCThriftService.Processor(UCThriftServiceImpl.ins
8、tance();TThreadPoolServer.Argsarg = new TThreadPoolServer.Args(socket);if(compact.equalsIgnoreCase(protocol)arg.protocolFactory(newTCompactProtocol.Factory();else if(binary.equalsIgnoreCase(protocol)arg.protocolFactory(newTBinaryProtocol.Factory();elsearg.protocolFactory(newTBinaryProtocol.Factory()
9、;arg.transportFactory(newTFramedTransport.Factory();arg.maxWorkerThreads(maxWorkerThreads);arg.minWorkerThreads(minWorkerThreads);/ arg.processor(process);arg.processorFactory(newTProcessorFactory(process);TServer server = new TThreadPoolServer(arg);Logger.getLogger(UCServiceServer.class).info(UCSer
10、viceServer.class.getSimpleName()+ Listen at + port);while(true)try server.serve(); catch (Exception e) logger.error(e.getMessage(),e);server.stop();Logger.getLogger(UCServiceServer.class).info(UCServiceServer.class.getSimpleName()+ Reload);名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精
11、心整理 - - - - - - - 第 2 页,共 5 页 - - - - - - - - - server = new TThreadPoolServer(arg);private static void startNIO()throws TTransportExceptionString address = PropertiesUtil.getValue(tserver.properties,ucserver.address,0.0.0.0);int port = Integer.parseInt(PropertiesUtil.getValue(tserver.properties,ucs
12、erver.port,5555);int clientTimeout=Integer.parseInt(PropertiesUtil.getValue(tserver.properties,ucserver.clientTimeout,30000);String protocol = PropertiesUtil.getValue(tserver.properties,ucserver.protocol,binary);TNonblockingServerSocketsocket = new TNonblockingServerSocket(new InetSocketAddress(addr
13、ess,port), clientTimeout);UCThriftService.Processorprocess = new UCThriftService.Processor(UCThriftServiceImpl.instance();TNonblockingServer.Argsarg = new TNonblockingServer.Args(socket);if(compact.equalsIgnoreCase(protocol)arg.protocolFactory(newTCompactProtocol.Factory();else if(binary.equalsIgnor
14、eCase(protocol)arg.protocolFactory(newTBinaryProtocol.Factory();elsearg.protocolFactory(newTBinaryProtocol.Factory();arg.transportFactory(newTFramedTransport.Factory();/ arg.processor(process);arg.processorFactory(newTProcessorFactory(process);TServer server = new TNonblockingServer(arg);Logger.getL
15、ogger(UCServiceServer.class).info(NIO + UCServiceServer.class.getSimpleName()+ Listen at + port);while(true)try server.serve(); catch (Exception e) server.stop();Logger.getLogger(UCServiceServer.class).info(NIO + UCServiceServer.class.getSimpleName()+ Reload);server = new TNonblockingServer(arg);pri
16、vate static void startHAHS()throws TTransportExceptionString address = PropertiesUtil.getValue(tserver.properties,ucserver.address,0.0.0.0);名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 5 页 - - - - - - - - - int port = Integer.parseInt(PropertiesUtil.getValue(
17、tserver.properties,ucserver.port,5555);int clientTimeout=Integer.parseInt(PropertiesUtil.getValue(tserver.properties,ucserver.clientTimeout,30000);int minWorkerThreads=Integer.parseInt(PropertiesUtil.getValue(tserver.properties,ucserver.minWorkerThreads,25);String protocol = PropertiesUtil.getValue(
18、tserver.properties,ucserver.protocol,binary);TNonblockingServerSocketsocket = new TNonblockingServerSocket(new InetSocketAddress(address,port), clientTimeout);UCThriftService.Processorprocess = new UCThriftService.Processor(UCThriftServiceImpl.instance();THsHaServer.Argsarg = new THsHaServer.Args(so
19、cket);if(compact.equalsIgnoreCase(protocol)arg.protocolFactory(newTCompactProtocol.Factory();else if(binary.equalsIgnoreCase(protocol)arg.protocolFactory(newTBinaryProtocol.Factory();elsearg.protocolFactory(newTBinaryProtocol.Factory();arg.transportFactory(newTFramedTransport.Factory();arg.workerThr
20、eads(minWorkerThreads);/ arg.processor(process);arg.processorFactory(newTProcessorFactory(process);TServer server = new THsHaServer(arg);Logger.getLogger(UCServiceServer.class).info(HAHS + UCServiceServer.class.getSimpleName()+ Listen at + port);while(true)try server.serve(); catch (Exception e) log
21、ger.error(e.getMessage(),e);server.stop();Logger.getLogger(UCServiceServer.class).info(HAHS + UCServiceServer.class.getSimpleName()+ Reload);server = new TNonblockingServer(arg);4 客户端编写名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 5 页 - - - - - - - - - public
22、void newThread()throws TExceptionString address = 0.0.0.0;int port = 5555;int clientTimeout= 30000;TTransporttransport= new TFramedTransport(newTSocket(address,port,clientTimeout);TProtocol protocol = new TBinaryProtocol(transport);UCThriftService.Clientclient = new UCThriftService.Client(protocol);
23、transport.open();try long bt = System.currentTimeMillis();System.out.println(URLDecoder.decode(client.ucOperator(get,29, ); catch (TApplicationExceptione) System.out.println(e.getMessage()+ + e.getType();transport.close();5 运行先运行 ucserverclient 连接可在控制台看到输出说明:client连服务端有几个要注意的地方:1 服务器的 ip 和端口2 服务端和客户端用的transport和协议一定要一样比如:如果服务端用的TFrameTransport那客户端也要用TFrameTransport如果服务端用的TBinaryProtocol,那客户端也要用TBinaryProtocol否则会出一个好像是TTransportException的一个异常。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 5 页 - - - - - - - - -