Apache Thrift之java入门例子
来源:互联网 发布:java stringbuffer类 编辑:程序博客网 时间:2024/06/05 09:08
Apache Thrift 是 Facebook 实现的一种高效的、支持多种编程语言的远程服务调用的框架
一:
首先加入thrift的java的依赖
<dependency><groupId>org.apache.thrift</groupId><artifactId>libthrift</artifactId><version>0.9.3</version></dependency>
二:
本文首先介绍一个简单的 Thrift 实现实例,使读者能够快速直观地了解什么是 Thrift 以及如何使用 Thrift 构建服务。
创建一个简单的服务Calculator。首先根据 Thrift 的语法规范编写脚本文件 Calculator.thrift,代码如下:
namespace java com.pp.calcservice Calculator{i32 add(1:i32 num1, 2:i32 num2)i64 multi(1:i32 num1, 2:i32 num2)}
然后执行thrift命令,生成java代码
thrift -r -gen java Calculator.thrift
这样,就会在当前目录下创建了一个gen-java目录,目录里面是java代码,把目录里面的东西放到项目的src目录中
编写Calculator.Iface(thrift自动生成的,这里就不贴出来了)接口的实现类
package com.pp.calc.impl;import org.apache.thrift.TException;import com.pp.calc.Calculator;public class CalculatorImpl implements Calculator.Iface{public int add(int num1, int num2) throws TException{return num1 + num2;}public long multi(int num1, int num2) throws TException{return Long.valueOf(num1 * num2);}}
三:编写服务端
package com.pp.server;import org.apache.thrift.TMultiplexedProcessor;import org.apache.thrift.protocol.TCompactProtocol;import org.apache.thrift.protocol.TProtocolFactory;import org.apache.thrift.server.TServer;import org.apache.thrift.server.TThreadPoolServer;import org.apache.thrift.transport.TServerSocket;import com.pp.calc.Calculator;import com.pp.calc.impl.CalculatorImpl;public class Server{public static final int PORT = 9988;public static void main(String[] args)throws Exception{TServerSocket serverSocket = new TServerSocket(PORT); TProtocolFactory protocolFactory = new TCompactProtocol.Factory(); TMultiplexedProcessor mp = new TMultiplexedProcessor();mp.registerProcessor("Calculator", new Calculator.Processor<>(new CalculatorImpl()));final TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverSocket).protocolFactory(protocolFactory).processor(mp)); new Thread(() -> {//这个方法会阻塞线程server.serve();}).start();System.out.println("Start server on port " + PORT + " ..."); }}
四:编写客户端
package com.pp.client;import org.apache.thrift.protocol.TCompactProtocol;import org.apache.thrift.protocol.TMultiplexedProtocol;import org.apache.thrift.protocol.TProtocol;import org.apache.thrift.transport.TSocket;import org.apache.thrift.transport.TTransport;import com.pp.calc.Calculator;public class Client { public static void main( String[] args)throws Exception { TTransport transport = new TSocket("127.0.0.1", 9988); transport.open(); TProtocol protocol = new TCompactProtocol(transport); TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "Calculator"); Calculator.Client calc = new Calculator.Client(mp); System.out.println(calc.add(1, 222)); System.out.println(calc.multi(22, 33)); transport.close(); }}
7 0
- Apache Thrift之java入门例子
- Apache Thrift之java入门
- [Thrift]Apache Thrift入门Java实例
- thrift java 例子入门(hello world)
- Apache Thrift入门简单实战(Java)
- Thrift 入门例子
- [转] Apache Thrift入门
- [转] Apache Thrift入门
- Apache Thrift入门学习
- 【thrift】java 例子
- Apache Thrift入门(安装、测试与java程序编写)
- Apache Thrift with Java Quickstart(thrift入门及Java实例)
- apache thrift C++入门实践
- thrift入门java演示
- Apache Thrift - java开发详解
- Apache Thrift - java开发详解
- Apache Thrift 官方JAVA教程
- Apache Thrift - java开发详解
- 代理、nsnotification和kvo的区别和用法
- UILabel使用不同的大小和字体、颜色
- JAVA发送邮件代码
- mybatis 使用经验小结,batch批量提交大量数据
- 栈的数组实现
- Apache Thrift之java入门例子
- 跟我一起写 Makefile(十三)
- 密码加密
- 前端移动端页面开发
- OpenCV for Android Studio 学习笔记(一):NDK及OpenCV的安装配置
- noetpad++ 使用svn
- .NET MVC Iframe 'X-Frame-Options' to 'SAMEORIGIN' 解决办法
- SSH框架整合开发详解(个人笔记)
- iOS 陀螺仪 加速计