Thrift 入门例子
来源:互联网 发布:为什么要探索宇宙 知乎 编辑:程序博客网 时间:2024/06/11 17:35
pom.xml
<dependency><groupId>org.apache.thrift</groupId><artifactId>libthrift</artifactId><version>0.10.0</version></dependency>
下载Thrift compiler for Windows (thrift-0.10.0.exe) : http://thrift.apache.org/download
新建文件Thrift 服务描述文件 HelloService.thrift
namespace java thriftservice HelloService { string sayHello(1:string name)}
运行下面命令生成HelloService.java
thrift-0.10.0.exe --gen java HelloService.thrift
编写HelloService 服务实现代码:
import org.apache.thrift.TException;public class HelloServiceHandler implements HelloService.Iface {@Overridepublic String sayHello(String name) throws TException {return "Hello " + name;}}
服务器端代码
import org.apache.thrift.TProcessor;import org.apache.thrift.server.TServer;import org.apache.thrift.server.TSimpleServer;import org.apache.thrift.transport.TServerSocket;import org.apache.thrift.transport.TServerTransport;public class ThriftServer {public static void main(String[] args) throws Exception {TServerTransport transport = new TServerSocket(1001);TProcessor processor = new HelloService.Processor<HelloServiceHandler>(new HelloServiceHandler());TServer server = new TSimpleServer(new TServer.Args(transport).processor(processor));server.serve();}}
客户端代码:
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;public class ThriftClient {public static void main(String[] args) throws TException {TTransport transport = new TSocket("127.0.0.1", 1001);transport.open();TProtocol prot = new TBinaryProtocol(transport);HelloService.Client client = new HelloService.Client(prot );System.out.println(client.sayHello("Andy"));}}
运行 ThriftServer, 然后运行ThriftClient。
阅读全文
0 0
- Thrift 入门例子
- Apache Thrift之java入门例子
- thrift java 例子入门(hello world)
- 【thrift】 thrift入门
- thrift 入门
- Thrift入门
- thrift入门
- Thrift 入门
- Thrift入门
- Thrift 入门
- Thrift入门
- Thrift 入门
- Thrift入门
- Thrift入门
- Thrift CPP例子
- thrift一个例子
- thrift完整例子
- thrift一个例子
- 15. Yii 2.0 页面缓存
- spring事务管理UnexpectedRollbackException
- 以客户为中心进行数据挖掘
- 为什么Nginx的性能要比Apache高很多?
- HDU
- Thrift 入门例子
- 清晰理解堆排序
- Spring DI依赖注入,自动装配
- iframe跨域
- Codeforces Round #433 (Div. 2) Planning (贪心+并查集)
- cnpm 安装vue
- jdk安装
- 第二十四节:兼容所有浏览器滚动条滚动的js
- μcos-II任务堆栈、任务控制块和任务代码