Thrift官方网站: http://thrift.apache.org。
目前对Thrift的学习和理解都是HelloWorld级别的,所以就贴一个HelloWorld吧,作为学习的第一步。
首先看一下需要建立的文件目录结构。
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.mt</groupId>
<artifactId>thrift-test</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<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>
Hello.thrift
namespace java service.demo
service Hello{
string helloString(1:string para)
i32 helloInt(1:i32 para)
bool helloBoolean(1:bool para)
void helloVoid()
string helloNull()
}
Hello.java 文件系统可以自动生成。 链接:https://pan.baidu.com/s/1UV7XXzKlasSzh5RL-iMoMg 密码:ccgo
本文使用本地安装的Thrift进行生成。 进入Hello.thrift目录,执行thrift –gen java Hello.thrift。,代码为自动生成所以不再展示。
实现自定义的Hello.thrift目标接口。
HelloServiceImpl.java(实际处理业务的实现类)
package service.demo;
import org.apache.thrift.TException;
public class HelloServiceImpl implements Hello.Iface {
@Override
public boolean helloBoolean(boolean para) throws TException {
return para;
}
@Override
public int helloInt(int para) throws TException {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return para;
}
@Override
public String helloNull() throws TException {
return null;
}
@Override
public String helloString(String para) throws TException {
System.out.println("参数:" + para);
return "hello " + para;
}
@Override
public void helloVoid() throws TException {
System.out.println("Hello World");
}
}
提供服务的服务端。
HelloServiceServer.java
package server;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
import service.demo.Hello;
import service.demo.HelloServiceImpl;
public class HelloServiceServer {
/**
* 启动 Thrift 服务器
*
* @param args
*/
public static void main(String[] args) {
try {
// 设置服务端口为 1234
TServerSocket serverTransport = new TServerSocket(1234);
// 设置协议工厂为 TBinaryProtocol.Factory
TBinaryProtocol.Factory proFactory = new TBinaryProtocol.Factory();
// 关联处理器与 Hello 服务的实现
TProcessor processor = new Hello.Processor(new HelloServiceImpl());
TThreadPoolServer.Args args1 = new TThreadPoolServer.Args(serverTransport);
args1.processor(processor);
args1.protocolFactory(proFactory);
TServer server = new TThreadPoolServer(args1);
System.out.println("Start server on port 1234...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
客户端。
HelloServiceClient.java
package client;
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;
import service.demo.Hello;
public class HelloServiceClient {
/**
* 调用 Hello 服务
*
* @param args
*/
public static void main(String[] args) throws TException {
try {
//
TTransport transport = new TSocket("localhost", 1234);
transport.open();
// 设置传输协议为 TBinaryProtocol
TProtocol protocol = new TBinaryProtocol(transport);
Hello.Client client = new Hello.Client(protocol);
// 调用服务的 helloString 方法
String resp = client.helloString("it's a beautiful day");
System.out.println(resp);
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
先运行服务端:HelloServiceServer.java.
启动客户端:HelloServiceClient.java.
结果截图:
附上一个源码链接。demo很简单,但是初学可能也会遇到各种小问题。参考一下吧。
链接:https://pan.baidu.com/s/1UV7XXzKlasSzh5RL-iMoMg 密码:ccgo