1. 程式人生 > >thrift C++做server, C++,python, java做Client例子

thrift C++做server, C++,python, java做Client例子

cloud1:~/test/thrift/demo # vi PythonClient.py    1 #!/usr/bin/env python   2    3 #   4 # Licensed to the Apache Software Foundation (ASF) under one   5 # or more contributor license agreements. See the NOTICE file #!/usr/bin/env python # # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # #   http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # import sys, glob sys.path.append('gen-py') #sys.path.insert(0, glob.glob('../../lib/py/build/lib.*')[0]) from demo import UserStorage  from demo.ttypes import * from thrift import Thrift from thrift.transport import TSocket from thrift.transport import TTransport from thrift.protocol import TBinaryProtocol try:     # Make socket     transport = TSocket.TSocket('localhost', 9090)     # Buffering is critical. Raw sockets are very slow     transport = TTransport.TBufferedTransport(transport)     # Wrap in a protocol     protocol = TBinaryProtocol.TBinaryProtocol(transport)     # Create a client to use the protocol encoder     client = UserStorage.Client(protocol)     # Connect!     transport.open()     try:         u1 = UserProfile()           u1.id=456           u1.name='nick'           u1.blurb='test bbb'           client.store(u1)          print 'store done!'     except InvalidOperation, io:          print 'InvalidOperation: %r' % io     u = client.getUser(123)       print 'id=%s name=%s blurb=%s' %(u.id,u.name,u.blurb)     # Close!     transport.close() except Thrift.TException, tx:     print '%s' % (tx.message) cloud1:~/test/thrift/demo # python PythonClient.py  store done! id=123 name=jason blurb=test aaa cloud1:~/test/thrift/demo #  Java 版客戶端: thrift-0.9.1/tutorial/java/src/ 
下拷:JavaClient.java 做修改: 重新命名為:UserProfileClient.java cloud1:~/test/thrift/demo # vi UserProfileClient.java  import org.apache.thrift.TException; import org.apache.thrift.transport.TSSLTransportFactory; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; public class UserProfileClient {   public static void main(String [] args) {     /*if (args.length != 1) {       System.out.println("Please enter 'simple' or 'secure'");       System.exit(0);     }  */      try {
      TTransport transport;       //if (args[0].contains("simple")) {         transport = new TSocket("localhost", 9090);         transport.open();       //}          //else {         /*            * Similar to the server, you can use the parameters to setup client parameters or          * use the default settings. On the client side, you will need a TrustStore which          * contains the trusted certificate along with the public key.           * For this example it's a self-signed cert.           */         //TSSLTransportParameters params = new TSSLTransportParameters();         //params.setTrustStore("../../lib/java/test/.truststore", "thrift", "SunX509", "JKS");         /*            * Get a client transport instead of a server transport. The connection is opened on          * invocation of the factory method, no need to specifically call open()          */         //transport = TSSLTransportFactory.getClientSocket("localhost", 9091, 0, params);       //}          TProtocol protocol = new  TBinaryProtocol(transport);       UserStorage.Client client = new UserStorage.Client(protocol);       int uid=123;       System.out.println(client.getUser(uid));       UserProfile u = new UserProfile();       u.id=999;       u.name="kaining";       u.blurb="test 999";        client.store(u);       transport.close();     } catch (TException x) {       x.printStackTrace();     }      } } cloud1:~/test/thrift/demo # cat compile.sh  #!/bin/sh export CLASS_PATH_JAVA="" for i in /usr/local/lib/*.jar do         if [ "x${CLASS_PATH_JAVA}" == "x" ]         then                 export CLASS_PATH_JAVA=$i                 continue         fi         export CLASS_PATH_JAVA=${CLASS_PATH_JAVA}:$i #echo $i done echo "$CLASS_PATH_JAVA" javac -classpath $CLASS_PATH_JAVA UserProfileClient.java ./gen-java/*.java cloud1:~/test/thrift/demo #  cloud1:~/test/thrift/demo # cat run.sh  #!/bin/sh export CLASS_PATH_JAVA="" for i in /usr/local/lib/*.jar do         if [ "x${CLASS_PATH_JAVA}" == "x" ]         then                 export CLASS_PATH_JAVA=.:$i                 continue         fi         export CLASS_PATH_JAVA=${CLASS_PATH_JAVA}:$i done #echo "$CLASS_PATH_JAVA" java -cp $CLASS_PATH_JAVA:./gen-java UserProfileClient cloud1:~/test/thrift/demo #  編譯,執行compile.sh cloud1:~/test/thrift/demo # ./compile.sh  /usr/local/lib/commons-codec-1.6.jar:/usr/local/lib/commons-lang3-3.1.jar:/usr/local/lib/commons-logging-1.1.1.jar:/usr/local/lib/httpclient-4.2.5.jar:/usr/local/lib/httpcore-4.2.4.jar:/usr/local/lib/junit-4.4.jar:/usr/local/lib/libthrift-0.9.1-javadoc.jar:/usr/local/lib/libthrift-0.9.1.jar:/usr/local/lib/log4j-1.2.14.jar:/usr/local/lib/servlet-api-2.5.jar:/usr/local/lib/slf4j-api-1.5.8.jar:/usr/local/lib/slf4j-log4j12-1.5.8.jar Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. cloud1:~/test/thrift/demo # 執行,執行run.sh cloud1:~/test/thrift/demo # ./run.sh  UserProfile(id:123, name:jason, blurb:test aaa) cloud1:~/test/thrift/demo #