Thrift是一个RPC软件框架, 解决各系统间大数据量的传输通信以及系统之间语言环境不同需要跨平台的特性.近期的项目中用到了Thrift,做了一个简单的demo.
环境:
1.eclipse
2.Thrift-0.10.0
新建两个maven项目,thriftServer和thriftClient.其中thriftServer是服务端,thriftClient是客户端
3分别在thriftServer和thriftClient的pom文件中添加下面的引用
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
ThriftServer
HelloWorldService.java是利用thrift命令生成的
- 在本地磁盘上创建HelloWorld.thrift.
Service HelloWorldService{
string sayHello(1:string username)
}
其中1:string username代表sayHello方法的第一个参数为username,参数类型为String.
如果有2个参数,第二个参数为String类型的pwd,写法为:
service HelloWorldService{
string sayHello(1:string username,2:string pwd)
}
(2)利用thrift生成HelloWorldService.java文件,cmd指令下进入thrift当前目录下输入命令
thrift.exe -gen java HelloWorld.thrift
将生成的HelloWorldService.java复制到ThriftServer的src/main/java目录下.
由于HelloWorldService.java生成的代码比较多,在这里不贴出来了,有需要的,留言给我...
HelloWorldImpl.java
HelloWorldImpl.java是HelloWorldService.java的实现类,HelloWorldImpl实现HelloWorldService.IFace.
package com.bee.thrift.service;
import org.apache.thrift.TException;
public class HelloWorldImpl implements HelloWorldService.Iface {
public String sayHello(String username) throws TException {
return "Hi," + username
+ " welcome to my blog ";
}
}
package com.bee.thrift.service;
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 HelloWorldServer {
public final static int SERVER_PORT = 7099;
private static String SERVER_IP = "localhost";
public void startServer() {
try {
System.out.println("HelloWorld Server start...");
TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
TServer.Args args = new TServer.Args(serverTransport);
TProcessor process = new HelloWorldService.Processor(
new HelloWorldImpl());
TBinaryProtocol.Factory portFactory = new TBinaryProtocol.Factory(
true, true);
args.processor(process);
args.protocolFactory(portFactory);
TServer server = new TSimpleServer(args);
server.serve();
} catch (Exception e) {
System.out.println("Server start error");
e.printStackTrace();
}
}
public static void main(String[] args) {
HelloWorldServer server = new HelloWorldServer();
server.startServer();
}
}
package com.bee.thrift.service;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
public class HelloWorldClient {
public static final int SERVER_PORT = 7099;
public static final String SERVER_IP = "localhost";
public void startClient(String username) {
TTransport tTransport = null;
try {
tTransport = new TSocket(SERVER_IP, SERVER_PORT);
// 协议要和服务端一致
TProtocol protocol = new TBinaryProtocol(tTransport);
HelloWorldService.Client client = new HelloWorldService.Client(
protocol);
tTransport.open();
String result = client.sayHello(username);
System.out.println("Thrift client result=" + result);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
HelloWorldClient client = new HelloWorldClient();
client.startClient("chenxiaochan");
}
}