【Apache Thrift】Thrift的使用和编译(二)
Thrift的使用和编译2016-07-20
本文参考了http://www.jianshu.com/p/0f4113d6ec4b这篇文章,感谢作者提供的学习资料。
上一篇:【Apache Thrift】thrift入门教程(一)windows下thrift的安装
thrift不支持无符号类型,因为很多编程语言不存在无符号类型,比如java
集合中的元素可以是除了service之外的任何类型,包括exception。
struct NPC { 1:i32 id; 2:string name; }
枚举的定义形式和Java的Enum定义差不多,例如:
enum Action { Idle, Attack, Run }
exception RequestException { 1: i32 code; 2: string reason; }
service HelloWordService { // service中定义的函数,相当于Java interface中定义的函数 string doAction(1: string name, 2: i32 age); }
typedef i32 Integer typedef i64 Long
thrift也支持常量定义,使用const关键字,例如:
const i32 MAX_RETRIES_TIME = 10 const string MY_WEBSITE = "http://qifuguang.me";
thrift的命名空间相当于Java中的package的意思,主要目的是组织代码。thrift使用关键字namespace定义命名空间,例如:
namespace java com.game.lll.thrift
thrift也支持文件包含,相当于C/C++中的include,Java中的import,C#中的using。使用关键字include定义,例 如:
include "global.thrift"
thrift注释方式支持shell风格的注释,支持C/C++风格的注释,即#和//开头的语句都单当做注释,/**/包裹的语句也是注释。
struct People { 1: required string name; 2: optional i32 age; }表示name是必填的,age是可选的。
namespace java com.game.lll.thrift struct Request { 1: string username; 2: string password; } exception RequestException { 1: required i32 code; 2: optional string reason; } // 服务名 service LoginService { string doAction(1: Request request) throws (1:RequestException qe); // 可能抛出异常。 }
3.服务器端准备工作
1>下载jar包
package com.game.lll.login; import org.apache.thrift.TException; import com.game.lll.thrift.LoginService; import com.game.lll.thrift.Request; import com.game.lll.thrift.RequestException; public class LoginServiceImpl implements LoginService.Iface{ @Override public String doAction(Request request) throws RequestException, TException { // TODO Auto-generated method stub System.out.println("username:"+request.getUsername()); System.out.println("password:"+request.getPassword()); return "aaa"; } }
package com.game.lll.login; import java.net.ServerSocket; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TServerSocket; import com.game.lll.thrift.LoginService; import com.game.lll.thrift.LoginService.Processor; public class LoginMain { public static void main(String[] args) throws Exception { ServerSocket socket = new ServerSocket(8888); TServerSocket serverTransport = new TServerSocket(socket); LoginService.Processor processor = new Processor(new LoginServiceImpl()); TServer.Args tServerArgs = new TServer.Args(serverTransport); tServerArgs.processor(processor); TServer server = new TSimpleServer(tServerArgs); System.out.println("Starting the simple server..."); server.serve(); } }
package com.game.lll.login; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import com.game.lll.thrift.LoginService; import com.game.lll.thrift.Request; public class ClientMain { public static void main(String[] args) throws Exception { TTransport transport = new TSocket("localhost", 8888); TProtocol protocol = new TBinaryProtocol(transport); // 创建client LoginService.Client client = new LoginService.Client(protocol); transport.open(); // 建立连接 // 第一种请求类型 Request request = new Request().setUsername("liulongling").setPassword("123456"); System.out.println(client.doAction(request)); transport.close(); // 请求结束,断开连接 } }