下载thrift window编译器
需要先下载编译器,本文用的版本是:
thrift-0.10.0.exe
对应的下载链接:thrift编译器
将下载好后的thrift-0.10.0.exe重命名成thrift.exe,并配置到window path路径上,假设thrift.exe是放置在如下目录:
D:\test\software\lib\thrift
那么直接将D:\test\software\lib\thrift配置到path上。
使用cmd命令打开一个窗口,执行:
thrift -version
正常情况下会输出thrift的版本号:
Thrift version 0.10.0
下载thrift java依赖包
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>
使用maven命令下载即可,下载成功后会有四个依赖包:
libthrift-0.10.0.jar
slf4j-api-1.7.12.jar
httpclient-4.4.1.jar
httpcore-4.4.1.jar
下载thrift eclipse 插件
直接使用Eclipse的MarketPlace找不到thrift support这个插件,得使用Eclipse的Install new software的方式。
点击Eclipse的install new software按钮,输入地址
下载thrift support 插件,这个插件的高亮功能还是相当完备的,建议开发者安装。
编写thrift idl文件
编写animal.thrift
namespace java thrift.generated
typedef i16 short
typedef i32 int
typedef i64 long
typedef bool boolean
typedef string String
struct Animal {
1: optional String username,
2: optional int age,
3: optional boolean married
}
exception DataException {
1: optional String message,
2: optional String callStack,
3: optional boolean date
}
service AnimalService {
Animal getAnimalByUsername(1: required String name) throws (1: DataException dataException),
void saveAnimal(1: required Animal animal) throws (1: DataException dataException)
}
animal.thrift位于:
src/main/java/thrift
完整路径是:
src/main/java/thrift/animal.thrift
其中的
typedef i16 short
typedef i32 int
typedef i64 long
typedef bool boolean
typedef string String
是做类型定义,因为thrift默认的类型定义不太符合java程序员的使用习惯.
使用thrift编译器生成java类
执行命令:
thrift –gen java src/main/java/thrift/animal.thrift
注意是gen前面是两个-,不是一个- .
执行成功后会生成gen-java目录:
gen-java
thrift
generated
Animal.java
AnimalService.java
DataException.java
这些文件不是在java source目录下的,我们可以将其拷贝到source目录下,本文中将其拷贝到如下source目录:
thrift.generated
实现Animal业务层服务类
package thrift.firstdemo;
import org.apache.thrift.TException;
import thrift.generated.Animal;
import thrift.generated.AnimalService;
import thrift.generated.DataException;
/**
* 业务层服务类
*
*/
public class AnimalBizService implements AnimalService.Iface{
@Override
public Animal getAnimalByUsername(String name) throws DataException, TException {
System.out.println("client name:"+name);
Animal animal = new Animal();
animal.setUsername(name);
animal.setAge(34);
animal.setMarried(true);
return animal;
}
@Override
public void saveAnimal(Animal animal) throws DataException, TException {
System.out.println("saveAnimal");
System.out.println(animal.getUsername());
System.out.println(animal.getAge());
System.out.println(animal.isMarried());
}
}
通常用thrift生成service后,会用一个业务实现类来实现thrift servce接口,完成业务逻辑.
Thrift Server端代码
package thrift.firstdemo;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.THsHaServer.Args;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
import thrift.generated.AnimalService;
public class ThriftServer {
public static void main(String[] args) throws TTransportException {
TNonblockingServerSocket socket = new TNonblockingServerSocket(8899);
Args arg = new THsHaServer.Args(socket).minWorkerThreads(2).maxWorkerThreads(4);
AnimalService.Processor<AnimalBizService> processors = new AnimalService.Processor<>(new AnimalBizService());
arg.protocolFactory(new TCompactProtocol.Factory());
arg.transportFactory(new TFramedTransport.Factory());
arg.processorFactory(new TProcessorFactory(processors));
TServer server = new THsHaServer(arg);
server.serve();
}
}
Thrift Client端代码
package thrift.firstdemo;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import thrift.generated.Animal;
import thrift.generated.AnimalService;
import thrift.generated.AnimalService.Client;
import thrift.generated.DataException;
public class ThriftClient {
public static void main(String[] args) throws DataException, TException {
TTransport transport = new TFramedTransport(new TSocket("localhost", 8899),600);
TCompactProtocol tCompactProtocol = new TCompactProtocol(transport);
Client client = new AnimalService.Client(tCompactProtocol);
transport.open();
Animal animal = client.getAnimalByUsername("sam");
System.out.println(animal.getUsername());
System.out.println(animal.getAge());
System.out.println(animal.isMarried());
Animal animal2 = new Animal();
animal2.setUsername("sam2");
animal2.setAge(35);
animal2.setMarried(false);
client.saveAnimal(animal2);
}
}
运行代码
分别运行ThriftServer类和ThriftClient类的main方法,启动服务端和客户端,正常情况下会打印如下日志:
client name:sam
saveAnimal
sam2
35
false
csdn code 路径
这个项目的源代码放置在csdn code上,欢迎访问。