安装thrift
mac
brew install thrift
安装完成检查
thrift --version
新建maven项目
pom.xml
1 <dependencies> 2 <dependency> 3 <groupId>org.apache.thrift</groupId> 4 <artifactId>libthrift</artifactId> 5 <version>0.11.0</version> 6 </dependency> 7 </dependencies> 8 9 <build> 10 <plugins> 11 <plugin> 12 <groupId>org.apache.thrift</groupId> 13 <artifactId>thrift-maven-plugin</artifactId> 14 <version>0.10.0</version> 15 <configuration> 16 <thriftExecutable>/usr/local/bin/thrift</thriftExecutable> <!--thrift安装路径--> 17 <thriftSourceRoot>src/main/resources</thriftSourceRoot> <!--thrift配置文件路径--> 18 </configuration> 19 <executions> 20 <execution> 21 <id>thrift-sources</id> 22 <phase>generate-sources</phase> 23 <goals> 24 <goal>compile</goal> 25 </goals> 26 </execution> 27 </executions> 28 </plugin> 29 </plugins> 30 </build>
定义服务
新建文件 src/main/resources/service.thrift
1namespace java com.acupt.thritf.service 2service HelloService{ 3 string hello(1:string name) 4}
构建
使用maven插件根据.proto文件生成Java代码,插件已在pom.xml中配置,只需执行命令:
mvn install
构建完成后可以在target中找到生成的Java代码,用这些代码可以实现thrift远程调用。
target/generated-sources/thrift/com/acupt/thritf/service/HelloService.java
如果在项目中无法直接引用上面的类,IDEA右键thrift文件夹 -> Mark Directory as -> Generated Sources Root
现在就可以在项目中引用了
代码
3个类
1package com.acupt.thrift; 2 3import com.acupt.thritf.service.HelloService; 4import org.apache.thrift.TException; 5 6/** 7 * 服务实现类 8 */ 9public class HelloServiceImpl implements HelloService.Iface { 10 11 @Override 12 public String hello(String name) throws TException { 13 return "hello," + name; 14 } 15} 16 17 18 19package com.acupt.thrift; 20 21import com.acupt.thritf.service.HelloService; 22import org.apache.thrift.TProcessor; 23import org.apache.thrift.protocol.TBinaryProtocol; 24import org.apache.thrift.server.TServer; 25import org.apache.thrift.server.TSimpleServer; 26import org.apache.thrift.transport.TServerSocket; 27 28/** 29 * 服务提供方 30 */ 31public class MyServer { 32 public static void main(String args[]) { 33 try { 34 TProcessor tprocessor = new HelloService.Processor<HelloService.Iface>(new HelloServiceImpl()); 35 TServerSocket serverTransport = new TServerSocket(50005); 36 TServer.Args tArgs = new TServer.Args(serverTransport); 37 tArgs.processor(tprocessor); 38 tArgs.protocolFactory(new TBinaryProtocol.Factory()); 39 TServer server = new TSimpleServer(tArgs); 40 System.out.println("server starting"); 41 //定时关闭 42 new Thread(() -> { 43 try { 44 System.out.println("server wait stop"); 45 Thread.sleep(30000); 46 } catch (InterruptedException e) { 47 e.printStackTrace(); 48 } 49 System.out.println("server stopping"); 50 server.stop(); 51 System.out.println("server stop"); 52 }).start(); 53 server.serve();//会阻塞 54 System.out.println("server finish"); 55 } catch (Exception e) { 56 e.printStackTrace(); 57 } 58 } 59 60} 61 62 63 64package com.acupt.thrift; 65 66import com.acupt.thritf.service.HelloService; 67import org.apache.thrift.protocol.TBinaryProtocol; 68import org.apache.thrift.protocol.TProtocol; 69import org.apache.thrift.transport.TSocket; 70import org.apache.thrift.transport.TTransport; 71 72/** 73 * 服务调用方 74 */ 75public class MyClient { 76 77 public static void main(String[] args) { 78 TTransport transport = null; 79 try { 80 transport = new TSocket("localhost", 50005); 81 TProtocol protocol = new TBinaryProtocol(transport); 82 HelloService.Client client = new HelloService.Client(protocol); 83 transport.open(); 84 String result = client.hello("tom"); 85 System.out.println(result); 86 } catch (Exception e) { 87 e.printStackTrace(); 88 } finally { 89 if (null != transport) { 90 transport.close(); 91 } 92 } 93 } 94 95} 96
先启动MyServer,成功启动后再启动MyClient。
和grpc用法差不多