thrift使用(2):代码生成和接口调用
来源:互联网 发布:seo网络推广专员 编辑:程序博客网 时间:2024/06/01 07:20
1. 编写接口thrift
namespace java com.wzz.thrift.resultstruct Result { 1: string result, 2: map<string,string> value }
namespace java com.wzz.thriftinclude "Result.thrift" service FuzaServ{ Result.Result helloWorld(1:string para)}
2. 使用thrift.exe生成代码,
thrift-0.10.0.exe -gen java FuzaServ.thrift
thrift-0.10.0.exe -gen java Result.thrift
3. 看到已经生成了代码,在文件夹下
将代码复制到eclipse中,开始写实现类
public class FuzaServImpl implements Iface{@Overridepublic Result helloWorld(String para) throws TException {System.out.println(para);Result r = new Result();return r;}}
public class Server {public static void main(String[] args) throws TTransportException {TServerSocket serverTransport = new TServerSocket(7911);//设置服务器端口 Factory proFactory = new TBinaryProtocol.Factory();//设置协议工厂 TProcessor processor = new FuzaServ.Processor<FuzaServ.Iface>(new FuzaServImpl());//关联处理器与Hello.thrift文件中定义的服务的实现 TServer.Args tArgs = new TServer.Args(serverTransport); tArgs.processor(processor); tArgs.protocolFactory(proFactory); TServer server = new TSimpleServer(tArgs); System.out.println("Start server on port 7911"); server.serve();}}
5. 接口调用
public class Client {public static void main(String[] args) throws TException {TTransport transport = new TSocket("localhost", 7911);// 建立连接transport.open();TProtocol protocol = new TBinaryProtocol(transport);FuzaServ.Client client = new FuzaServ.Client(protocol);// 生成客户端实例对象Result helloWorld = client.helloWorld("hello");}}
另附:
pom文件,引入包
<dependency><groupId>org.apache.thrift</groupId><artifactId>libthrift</artifactId><version>0.10.0</version></dependency>
0 0
- thrift使用(2):代码生成和接口调用
- Thrift 接口生成和jar包缩减
- Thrift源码分析(三)-- IDL和生成代码分析
- 使用NetBeans IDE自动生成代码调用netweaver PI 接口
- thrift生成php代码不完整问题(thrift接口定义文件中含include)
- scala远程调用thrift接口
- 用jdk生成webservice客户端代码(调用免费接口)
- 在windows上使用Thrift生成PHP代码
- win下使用thrift生成gen-php文件代码
- 使用Python通过Thrift接口访问HBase和Hive
- 使用thrift做c++,java和python的相互调用
- Thrift服务搭建和调用
- Thrift compiler代码生成类解析
- spring配置xfire生成webservice接口 和 调用webservice接口
- 使用wsimport生成本地调用代码
- 通过Python使用HBase Thrift接口 url
- hiveserver2 java thrift api 接口使用事例
- Impala thrift API接口使用事例
- ABAP通过OLEDB读取本地Access(MDB)文件
- 欢迎使用CSDN-markdown编辑器
- ubuntu16.04 swoole 安装
- Universal Link(iOS)踩坑
- csu1779——错误的算法(模拟)
- thrift使用(2):代码生成和接口调用
- Oracle 时间修改
- 关于 MPAndroidChart 中的 LineChart 的使用小结
- VUEJS滚动插件BetterScroll使用时 无法滚动,但是已经捕获 demo
- c++作业四
- JavaScript之命名表达式
- IP回流问题分析
- 如果两个程序员差不多,选写作能力更好的那个
- 微信小程序开发常见问题FAQ之三