在探索未知的程序之前,我们往往会使用“Hello World”这个经典的输出作为测试,为了遵循这个惯例,作为thrift菜鸟都不算的一员,决定跑一下“Hello world”正式进入菜鸟的行列。
thrift通过一个跨语言的定义文件的方式定义数据类型和服务接口,这个文件作为RPC客户端和服务器通信的标准,所谓跨语言,就是客户端和服务器端的语言是没有限制的,可以相同也可以不同,本质上定义的服务和接口都是一样的。
下面是我用intellij跑的java的例子:
环境配置:
1. ubuntu 13
2. thrift 0.5.0
3. Intellij 13
1.在Intellij中创建maven项目,在pom.xml中添加项目需要的依赖:
<dependencies>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.5.0-cdh</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
</dependencies>
2.在resources目录下新建helloworld.thrift文件,内容如下:
namespace java com.qiao.thrift.helloservice IHelloWorldService {
string sayHello(1:string username)
}
3.在命令端打开helloworld.thrift文件所在的目录,执行下面的命令:
thrift -r --gen java hello_world.thrift
这时目录下会产生gen-java目录,目录下就是thrift自动生成的代码。
4. 使用cp -r命令将com/tom/thrift/hello/IHelloWorldService.java目录连同文件copy到Maven模块的src/java/main下面。
5.在Maven模块中,自定义IHelloWorldService的实现类HelloWorldService,需要实现IHelloWorldService.Iface接口,不出意料,果然仅仅需要实现sayHello方法,类定义如下:
public class HelloWorldService implements IHelloWorldService.Iface {
@Override
public String sayHello(String username) throws TException {
return "Hi,"+username+",Welcome to the thrift world";
}
}
6.自定义单线程模式的ThriftServer用于响应客户端的rpc请求,注意一点,IHelloWorldService.Iface的实现类在服务器端进行了实例化:
public class HelloWorldSimpleServer {
public static final int SERVER_PORT=8090;
public void startServer(){
try{
System.out.println("Hello World TSimpleServer start..."); TProcessor tp=new IHelloWorldService.Processor(new HelloWorldService());
TServerTransport sT=new TServerSocket(SERVER_PORT);
TServer server=new TSimpleServer(tp,sT);
System.out.println("Starting the server"); server.serve(); }catch (Exception e){
System.out.println("Server start error!!");
e.printStackTrace();
}
System.out.println("done.");
} public static void main(String[] args){
HelloWorldSimpleServer server=new HelloWorldSimpleServer();
server.startServer();
}
}
7.自定义HelloWorld的客户端,用于远程调用第6步Thrift Server提供的IHelloWorldService服务
public class HelloWorldSimpleClient { public void startClient(String userName) { TTransport transport = null;
try { transport = new TSocket("localhost", 8090);
TProtocol protocol = new TBinaryProtocol(transport);
IHelloWorldService.Client client = new IHelloWorldService.Client(protocol); transport.open(); String res = client.sayHello(userName);
System.out.println("Thrift result =:" + res); } catch (Exception x) {
System.out.println("Start client Error!");
x.printStackTrace();
} finally {
if (transport != null) {
transport.close();
}
} }
public static void main(String[] args){
HelloWorldSimpleClient client=new HelloWorldSimpleClient();
client.startClient("qiao");
}}
8.启动HelloWorldSimpleServer,控制台输出HelloWorld TSimpleServer start ….,同时,HelloWorldSimpleServer作为Server一直处于运行过程中;启动HelloWorldSimpleClient,控制台输出Thrift client result =: Hi, Tom, Welcome to the Thrift world, enjoy it! 执行完后,客户端进程结束。
另外在运行过程中出现下面的警告:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
原因是org.slf4j.impl.StaticLoggerBinder
无法载入到内存,原因是没有找到合适的绑定SLF4J,需要添加所列举的包中的某一个。解决方案是手动下载一个slf4j-nop.jar,然后添加到路径中就可以了。
另外比较全的代码例子为:https://github.com/mariusae/thrift-0.5.0-finagle.git