标签:
/** * thrift中的数据类型 * bool 布尔类型 * byte 同java里的byte * i16 短整型,同short int * i32 整型,同int * i64 长整型,同long * double 64位浮点数 * string 同java的string * binary 字节数组 * map<t1,t2> 同java的hashmap泛型 * list<t1> 同java的arraylist,存储的数据是已排序的 * set<t1> 同java的hashset,存储不重覆的数据 */ /* *这里定义各语言的namespace */ namespace java com.lzf.thrift.demo1 namespace php Demo /** *定义服务,格式为 service 服务名{},这里也可以继承其它的thrift文件里的service,格式如下 * service 服务名 extends aaa.aaaservice{},需要在当前文件头部加上 * include "aaa.thrift" * 这里定义一个简单的服务 */ /* *可以定义结构体,用于交换对象等数据 */ struct User{ 1:string name, 2:i32 age } service HelloServe{ /* *在服务里面可以定义方法 *格式:返回值 方法名(参数位置:参数类型 参数名) */ i32 adduser(1:i32 age,2:string name), map<string,i32> getusers() }
package com.lzf.thrift.demo1; import org.apache.thrift.TException; import java.util.HashMap; public class DemoHandler implements HelloServe.Iface{ public DemoHandler(){} private static HashMap<String,Integer> users = new HashMap<>(); public int adduser(int age, String name) throws TException{ users.put(name,age); return 1001; } public HashMap<String,Integer> getusers(){ return users; } }
package com.lzf.thrift.demo1; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TServer.Args; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.server.TThreadPoolServer; import org.apache.thrift.transport.TSSLTransportFactory; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TServerTransport; import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters; import java.lang.Exception; import java.lang.Override; import java.lang.Runnable; public class DemoServer{ public static DemoHandler handler; public static HelloServe.Processor processor; public static void main(String[] args){ try{ handler = new DemoHandler(); processor = new HelloServe.Processor(handler); simple(processor); }catch (Exception x){ x.printStackTrace(); } } public static void simple(HelloServe.Processor processor) { try { TServerTransport serverTransport = new TServerSocket(9090); TServer server = new TSimpleServer(new Args(serverTransport).processor(processor)); System.out.println("Starting the simple server..."); server.serve(); } catch (Exception e) { e.printStackTrace(); } } }服务端代码编写完成。
<?php /** * author: lzf * createTime: 15/8/3 22:13 * description: */ require_once 'Thrift/ClassLoader/ThriftClassLoader.php'; use Thrift\ClassLoader\ThriftClassLoader; $GEN_DIR = realpath(dirname(__FILE__)).'/gen-php'; $loader = new ThriftClassLoader(); $loader->registerNamespace('Thrift', __DIR__); $loader->registerDefinition('Demo', $GEN_DIR); $loader->register(); use Thrift\Protocol\TBinaryProtocol; use Thrift\Transport\TSocket; use Thrift\Transport\THttpClient; use Thrift\Transport\TBufferedTransport; use Thrift\Exception\TException; try { $socket = new TSocket('localhost', 9090); $transport = new TBufferedTransport($socket, 1024, 1024); $protocol = new TBinaryProtocol($transport); $client = new \Demo\HelloServeClient($protocol); $transport->open(); echo $client->adduser(23,"lzf"); echo $client->adduser(23,"lzf001"); echo $client->adduser(23,"lzf002"); echo '执行了<br />'; $userlist = $client->getusers(); print_r($userlist); $transport->close(); } catch (TException $tx) { print 'TException: '.$tx->getMessage()."\n"; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/luzhengfei/article/details/47265313