不多说,先看项目结构
首先先编写一个hello.thrift的文件
hello.thrift
namespace java sawshaw service HelloService { string hello(1:string method, 2:string param) }
注意了,这个namespace是 thrif 根目录下tutorial目录的gen-java目录下的,如果没有这个目录,先cmd到tutorial目录,执行thrift -r --gen java tutorial.thrif。就会看到一个gen-java目录了,而这个sawshaw是我自定义的目录,把这个hello.thrift文件放到和tutorial目录同级,cmd到该目录后执行thrift -r --gen hello.thrift
可以看到生成了一个HelloService的java类
把这个类复制到项目下面改下包名就可以了
再写个实现类HelloImpl对客户端的请求作响应
HelloImpl
package com.sawshaw.thrift; import org.apache.thrift.TException; import com.sawshaw.thrift.HelloService.Iface; public class HelloImpl implements Iface{ public String hello(String method, String param) throws TException { return "method:"+method+",param:"+param; } }
编写服务端代码以启动server
HelloServer
package com.sawshaw.thrift; 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.TServerTransport; import org.apache.thrift.transport.TTransportException; import com.sawshaw.thrift.HelloService.Iface; public class HelloServer { public static void main(String[] args) throws TTransportException { HelloService.Iface impl=new HelloImpl(); HelloService.Processor<Iface> processor = new HelloService.Processor<Iface>(impl); TServerTransport serverTransport = new TServerSocket(8080); TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport); System.out.println("server has start..."); tArgs.processor(processor); tArgs.protocolFactory(new TBinaryProtocol.Factory()); TServer server = new TThreadPoolServer(tArgs); server.serve(); } }
编写客户端HelloClient调用服务端代码
HelloClient
package com.sawshaw.thrift; 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 HelloClient { public static void main(String[] args) { TTransport transport; transport = new TSocket("localhost", 8080); try { transport.open(); } catch (TTransportException e1) { e1.printStackTrace(); } TProtocol protocol = new TBinaryProtocol(transport); HelloService.Iface client = new HelloService.Client(protocol); // 调用服务的 hello 方法 String resp = null; try { resp = client.hello("method","param"); } catch (TException e) { e.printStackTrace(); } System.out.println("resp:"+resp); transport.close(); } }
当然pom.xml要引用thrift的包,会自动加载thrift的依赖包
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.sawshaw</groupId> <artifactId>thrift</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>thrift</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.11.0</version> </dependency> </dependencies> </project>
服务端启动后,客户端运行结果:
resp:method:method,param:param