java和php使用thrift通讯

2014-05-24 11:00 来源:www.chinab4c.com 作者:ecshop专家

     java和php使用thrift通讯,我们使用java thrift server,然后使用php thrift client,这个方法还是很好的,通过thrift方式去实现。下面我们将结合例子,来谈谈具体的操作方式。

1:首先配置thrift环境

2:配置好java环境

3:将以下thrift所需要运行的4个jar放到java的 jre/lib/ext扩展目录去

4:编写testJava.thrift

 namespace java Test

service Something{

    i32 ping();

    i32 getsum(1:i32 num1,2:i32 num2);

5:testJava.thrift目录执行如下命令: thrift -gen java testJava.thrift ,生成的源文件在./gen-java/目录下

6:进入gen-java/Test目录,编写SomethingImpl.java Server.java Client.java
   SomethingImpl.java
   package Test;
import org.apache.thrift.TException;
class SomethingImpl implements Something.Iface {
 
    public SomethingImpl() {
 
    }
    public int ping() throws TException {
 
        System.out.println("Recieve ping from client...");
 
        return 0;
 
    }
 
public int getsum(int num1,int num2) throws TException {
int x ;
x = num1+num2;
System.out.println(x);
return x;
}
}
 
   编写java的server
  package Test;
import java.io.IOException;
 
import org.apache.thrift.protocol.TBinaryProtocol;
 
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
 
import org.apache.thrift.server.TServer;
 
import org.apache.thrift.server.TSimpleServer;
 
import org.apache.thrift.server.TThreadPoolServer;
 
import org.apache.thrift.transport.TServerSocket;
 
import org.apache.thrift.transport.TTransportException;
public class Server {
 
    private void start() {
 
        try {
 
            TServerSocket serverTransport = new TServerSocket(7911);
 
            Something.Processor processor = new Something.Processor(new SomethingImpl());
 
            Factory protFactory = new TBinaryProtocol.Factory(true, true);
 
            //TServer server = new TThreadPoolServer(processor, serverTransport,protFactory);
 
            //TServer server = new TSimpleServer(new Args(serverTransport).processor(processor));
 
            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
 
            System.out.println("Starting server on port 7911 ...");
 
            server.serve();
 } catch (TTransportException e) {
 
            e.printStackTrace();
 
        } catch (Exception e) {
 
            e.printStackTrace();
 
        }
 
    }
public static void main(String args[]) {
 
        Server srv = new Server();
 
        srv.start();
 
    }
}
 
7:编写Client.java
package Test;
import java.io.IOException;
import org.apache.thrift.*;
import org.apache.thrift.protocol.*;
import org.apache.thrift.transport.*;
public class Client {
 
      public static void main(String [] args) {
 
           try {
 
                    TTransport transport = new TSocket("localhost", 7911);
 
                    TProtocol protocol = new TBinaryProtocol(transport);
 
                    Something.Client client = new Something.Client(protocol);
 
                    transport.open();
 
                    System.out.println("Client calls ping()");
 
                    client.ping();
client.getsum(1,2);
System.out.println(client.getsum(1,2));
                    transport.close();
 
               } catch (TException x) {
 
                    x.printStackTrace();
 
               }   
 
        }
}
 
8:回到java的gen-java目录
javac  -d E:\java\thrift\gen-java -s E:\java\thrift\gen-java\Test *.java 执行以下命令
9:启动java的服务
 java Test/Server
 
10:运行java的client
java Test/Client
看到运行结果了。
 
11:把thrift_trunk里面的 thrift-trunk\lib\php\src中的程序,拷贝到php运行目录
12:testJava.thrift目录执行如下命令: thrift -gen php testJava.thrift ,生成的源文件在./gen-php/目录
14:把里面的gen-php目录拷贝到php运行目录
15:比如php运行目录在D:\AppServ815\www\273\utf\daili\thrift
形成 D:\AppServ815\www\273\utf\daili\thrift\src
   D:\AppServ815\www\273\utf\daili\thrift\gen-php
   我们在D:\AppServ815\www\273\utf\daili\thrift\目录编写index.php测试程序
  
<?php
/*
 * 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.
 */
 
$GLOBALS['THRIFT_ROOT'] = 'src';
require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/THttpClient.php';
require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
$GEN_DIR = 'gen-php';
require_once $GEN_DIR.'/Something.php';
require_once $GEN_DIR.'/Types.php';
 
error_reporting(E_ALL);
 
try {
  $socket = new TSocket('localhost', 7911);
  $transport = new TBufferedTransport($socket, 1024, 1024);
  $protocol = new TBinaryProtocol($transport);
  $client = new SomethingClient($protocol);
 
  $transport->open();
 
  $client->ping();
  print "ping()\n";
 
  $sum = $client->getsum(1,77);
  print "1+1=$sum\n";
 $transport->close();
 
} catch (TException $tx) {
  print 'TException: '.$tx->getMessage()."\n";
}
 
?>
运行发现,能得到正确结果。
 
来源:http://www.chinab4c.com