本篇,不考虑细节问题和为什么,先照葫芦画瓢写一个Thrift版本的Hello World,了解Thrift RPC服务开发的基本流程
1. 在Intellij中创建一个Maven模块,加入对Thrift的依赖,同时还要加上slf4j依赖,如果不加slf4j依赖,在后面启动Thrift Server时会报错
<dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.9.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.7</version> </dependency>
2. 在resources目录下创建接口定义文件helloworld.thrift文件,内容如下
namespace java com.tom.thrift.hello service IHelloWorldService { string sayHello(1:string username) }
3.在命令号终端cd到helloworld.thrift所在的目录,执行如下命令
thrift -r -gen java helloworld.thrift
4. 命令执行完成后,在当前目录下生成一个gen-java目录,其目录结构为 gen-java/com/tom/thrift/hello/IHelloWorldService.java,这么一个简单的接口声明,自动生成了957行代码。。复杂!!!
5.使用cp -r命令将com/tom/thrift/hello/IHelloWorldService.java目录连同文件copy到Maven模块的src/java/main下面
6.在Maven模块中,自定义IHelloWorldService的实现类HelloWorldService,需要实现IHelloWorldService.Iface接口,不出意料,果然仅仅需要实现sayHello方法,类定义如下:
package com.tom.thrift.hello;
import org.apache.thrift.TException;
public class HelloWorldService implements IHelloWorldService.Iface {
@Override
public String sayHello(String username) throws TException {
return "Hi, " + username + ", Welcome to the Thrift world, enjoy it!";
}
}
7.自定义单线程模式的ThriftServer用于响应客户端的rpc请求,注意一点,IHelloWorldService.Iface的实现类在服务器端进行了实例化
package com.tom.thrift.hello;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
public class HelloWorldSimpleServer {
public static final int SERVER_PORT = 8090;
public void startServer() {
try {
System.out.println("HelloWorld TSimpleServer start ....");
TProcessor tprocessor = new IHelloWorldService.Processor<IHelloWorldService.Iface>(
new HelloWorldService());
// 简单的单线程服务模型,一般用于测试
TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
TServer.Args tArgs = new TServer.Args(serverTransport);
tArgs.processor(tprocessor);
tArgs.protocolFactory(new TBinaryProtocol.Factory());
// tArgs.protocolFactory(new TCompactProtocol.Factory());
// tArgs.protocolFactory(new TJSONProtocol.Factory());
TServer server = new TSimpleServer(tArgs);
server.serve();
} catch (Exception e) {
System.out.println("Server start error!!!");
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
HelloWorldSimpleServer server = new HelloWorldSimpleServer();
server.startServer();
}
}
8.自定义HelloWorld的客户端,用于远程调用第7步Thrift Server提供的IHelloWorldService服务
package com.tom.thrift.hello;
import org.apache.thrift.TException;
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 org.apache.thrift.transport.TTransportException;
public class HelloWorldSimpleClient {
public static final String SERVER_IP = "localhost";
public static final int SERVER_PORT = 8090;//Thrift server listening port
public static final int TIMEOUT = 30000;
/**
*
* @param userName
*/
public void startClient(String userName) {
TTransport transport = null;
try {
transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
// 协议要和服务端一致
TProtocol protocol = new TBinaryProtocol(transport);
// TProtocol protocol = new TCompactProtocol(transport);
// TProtocol protocol = new TJSONProtocol(transport);
IHelloWorldService.Client client = new IHelloWorldService.Client(protocol);
transport.open();
String result = client.sayHello(userName);
System.out.println("Thrift client result =: " + result);
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} finally {
if (null != transport) {
transport.close();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
HelloWorldSimpleClient client = new HelloWorldSimpleClient();
client.startClient("Tom");
}
}
9.启动HelloWorldSimpleServer,控制台输出HelloWorld TSimpleServer start ....,同时,HelloWorldSimpleServer作为Server一直处于运行过程中
10.启动HelloWorldSimpleClient,控制台输出Thrift client result =: Hi, Tom, Welcome to the Thrift world, enjoy it! 执行完后,客户端进程结束
总结
前面一步一步的完成了Thrift版本的Hello World,对Thrift开发RPC服务开发流程,有了一个基本的了解。Thrift本身的细节和更深入的学习,将在后面继续。
本文参考:http://www.micmiu.com/soa/rpc/thrift-sample/