环境
ubuntu 12.0
thrift-0.8.0
1. 编写Hello.thrift文件
#!/usr/bin/thrift --gen java
namespace java com.cuijh.hello
service Hello{
i32 helloInt(1:i32 para)
// string helloString(1:string para)
// bool helloBoolean(1:bool para)
// void helloVoid()
// string helloNull()
}
2. 在Hello.thrift目录下执行命令:自动生成java代码
thrift -gen java Hell.thrift
执行后会在当前目录下生成一个目录gen-java,里面有Hello.java
3.创建maven 工程
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cuijh.hello</groupId>
<artifactId>hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hello</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>
</dependencies>
</project>
4.编写接口:
接口实现Thrift定义文件中的服务。
package com.cuijh.hello;
import org.apache.thrift.TException;
public class HelloServiceImpl implements Hello.Iface{
public int helloInt(int para) throws TException{
public boolean helloBoolean(boolean para) throws TException{
return para;
}
public int helloInt(int para) throws TException{
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return para;
}
public String helloNull() throws TException{
return null;
}
public String helloString(String para) throws TException{
return para;
}
public void helloVoid() throws TException{
System.out.println("Hello World!");
}
}
5.编写服务器端
package com.cuijh.hello;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.server.TThreadPoolServer.Args;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;
public class HelloServiceServer {
/**
* 启动thrift服务器
* @param args
*/
public static void main(String[] args) {
try{
//设置服务器端口为7911
TServerSocket serverTransport = new TServerSocket(7911);
//设置协议工厂为TBinaryProtocol.Factory
Factory proFactory = new TBinaryProtocol.Factory();
//关联处理器与Hello服务的实现
TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
TServer.Args tArgs = new TServer.Args(serverTransport);
tArgs.processor(processor);
tArgs.protocolFactory(proFactory);
//使用TSimpleServer
TServer server = new TSimpleServer(tArgs);
System.out.println("Start server on port 7911....");
server.serve();
}catch(TTransportException e){
e.printStackTrace();
}
/*
try{
//设置服务器端口为7911
TServerSocket serverTransport = new TServerSocket(7911);
//设置协议工厂为TBinaryProtocol.Factory
Factory proFactory = new TBinaryProtocol.Factory();
//关联处理器与Hello服务的实现
TProcessor processor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
Args tArgs = new Args(serverTransport);
tArgs.processor(processor);
tArgs.protocolFactory(proFactory);
TServer server = new TThreadPoolServer(tArgs);
System.out.println("Start server on port 7911....");
server.serve();
TServerTransport s = new TServerSocket(11);
}catch(TTransportException e){
e.printStackTrace();
}
*/
}
}
6. 编写客户端
package com.cuijh.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 HelloServiceClient {
/**
* 调用Hello服务
* @param args
*/
public static void main(String[] args) {
try {
//设置调用的服务器为本地,端口为7911
TTransport transport = new TSocket("localhost", 7911);
transport.open();
//设置传输协议为TBinaryProtocol
TProtocol protocol = new TBinaryProtocol(transport);
Hello.Client client = new Hello.Client(protocol);
client.helloInt(32);
transport.close();
} catch (TTransportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
7、运行
先运行服务器端,再运行客户端。
得到:
- Start server on port 7911....