用java做thrift服务端,php做thrift客户端简单例子

注意

1).需要的包以及路径问题要注意修改为自己本地的

2)详细情况见  http://pan.baidu.com/s/1qW8xI0k

 

这里以Hello.thrift为例

namespace java hellodemo
namespace php  hellodemo
service Hello{  
    string helloString(1:string para)  
    i32 helloInt(1:i32 para)  
    bool helloBoolean(1:bool para)  
    void helloVoid()  
    string helloNull()  
} 

 

1.通过命令thirft -r --gen php Hello.thrift 生成对应的php文件,通过命令  thirft -r --gen java Hello.thrift生成对应的java文件

2.编写service端(用java两个文件,运行HelloServiceServer
 

import org.apache.thrift.TException;
import hellodemo.Hello;
public class HelloServiceImpl implements Hello.Iface{
    public boolean helloBoolean(boolean para) throws TException{
        return para;
    }
    public int helloInt(int para) throws TException{
        try {
            Thread.sleep(1);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return para;
    }
    
    public String helloNull() throws TException{
        return null;
    }
    
    public String helloString(String para) throws TException{
        return para;
    }
    
    public void helloVoid() throws TException{
        System.out.println("Hello World!");
    }

}
import hellodemo.Hello;

import org.apache.thrift.TProcessor;
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.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;

public class HelloServiceServer {
    /**
     * 启动thrift服务器
     * @param args
     */
    public static void main(String[] args) {        
        try{
        //设置服务器端口为7911
        TServerSocket serverTransport = new TServerSocket(7911);
        //设置协议工厂为TBinaryProtocol.Factory
        Factory proFactory = new TBinaryProtocol.Factory();
        //关联处理器与Hello服务的实现
        TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
        TServer.Args tArgs = new TServer.Args(serverTransport);
        tArgs.processor(processor);
        tArgs.protocolFactory(proFactory);
        //使用TSimpleServer
        TServer server = new TSimpleServer(tArgs);
        System.out.println("Start server on port 7911....");
        server.serve();
        //server.stop();
        }catch(TTransportException e){
            e.printStackTrace();
        }        
        /*
        try{
            //设置服务器端口为7911
            TServerSocket serverTransport = new TServerSocket(7911);
            //设置协议工厂为TBinaryProtocol.Factory
            Factory proFactory = new TBinaryProtocol.Factory();
            //关联处理器与Hello服务的实现
            TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
            Args tArgs = new Args(serverTransport);
            tArgs.processor(processor);
            tArgs.protocolFactory(proFactory);
            TServer server = new TThreadPoolServer(tArgs);
            System.out.println("Start server on port 7911....");
            server.serve();
            TServerTransport s = new TServerSocket(11);            
            }catch(TTransportException e){
                e.printStackTrace();
            }
            */
    }

}

3.编写客户端(用php)

<?php
/**
*修改为对应的目录
*/
error_reporting(E_ALL);
set_time_limit ( 0 );
$root = ‘E:\www\www\test\lib‘;   //# 指定库目录,可以是绝对路径或是相对路径 
require_once $root.‘\Thrift\ClassLoader\ThriftClassLoader.php‘;
require_once ‘E:\www\www\test\gen-php\hellodemo\Hello.php‘;
require_once ‘E:\www\www\test\gen-php\hellodemo\Types.php‘;
 use Thrift\ClassLoader\ThriftClassLoader; 
 use Thrift\Protocol\TBinaryProtocol; 
 use Thrift\Transport\TSocket;
use hellodemo\HelloClient;
use Thrift\Factory\TTransportFactory;
use Thrift\Transport\TBufferedTransport;


$GEN_DIR =$root.‘\gen-php‘;

 $loader = new ThriftClassLoader(); 
$loader->registerNamespace(‘Thrift‘, $root); //# 加载thrift 
$loader->registerDefinition(‘idata\factory\meta_node‘, $GEN_DIR); //# 加载自己写的thrift文件编译的类文件和数据定义 
$loader->register(); 

  
try {  
     $thrif_server_url = ‘localhost‘;  
    //$tt=new  TTransportFactory();
    $transport = new TSocket($thrif_server_url, 7911); 
  
    //$transport = new TBufferedTransport($socket, 1024, 1024);
    $protocol = new TBinaryProtocol($transport);
    $client= new   HelloClient($protocol) ;
   // $protocol = new TBinaryProtocolAccelerated($transport);
     $transport->open();   
     
     //$client= new HelloClient($protocol);//var_dump($client);die();
      //HelloClient($protocol);
     // var_dump($client);
  //$x= $client->helloVoid();
 // $x=$client->helloInt(1);
 $x=$client->helloString("hello world,中国");
  var_dump($x);
     $transport->close();  
} catch (Exception $e) {  
    echo $e->getMessage();  
}  

4.运行服务端与客服端观察结果

 

http://pan.baidu.com/s/1qW8xI0k

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。