Thrift

安装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用法差不多

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )