Thrift简单例子
来源:互联网 发布:化学物质数据库 编辑:程序博客网 时间:2024/06/08 19:43
个人博客原文:Thrift 简单例子
摘要:本文简单的举了个例子,怎么用Thrift去实现一个RPC调用。
编写IDL接口
HelloService.thrift
namespace java com.thrift.demo.service service HelloService { i32 sayInt(1:i32 param) string sayString(1:string param) bool sayBoolean(1:bool param) void sayVoid()}
用Thrift编译器编译成对应的类
运行下面命令,生成HelloService.java类
thrift-0.10.0 -gen java HelloService.thrift
生成的HelloService.java内容如下:
package com.thrift.demo01.service;@SuppressWarnings({"cast", "rawtypes", "serial", "unchecked", "unused"})@javax.annotation.Generated(value = "Autogenerated by Thrift Compiler (0.10.0)", date = "2017-02-13")public class HelloService { public interface Iface { public int sayInt(int param) throws org.apache.thrift.TException; public java.lang.String sayString(java.lang.String param) throws org.apache.thrift.TException; public boolean sayBoolean(boolean param) throws org.apache.thrift.TException; public void sayVoid() throws org.apache.thrift.TException; } //......省略了很多代码}
编写真正业务逻辑实现类
public class HelloServiceImpl implements HelloService.Iface { public int sayInt(int param) throws TException { System.out.println("say int :" + param); return param; } public String sayString(String param) throws TException { System.out.println("say string :" + param); return param; } public boolean sayBoolean(boolean param) throws TException { System.out.println("say boolean :" + param); return param; } public void sayVoid() throws TException { System.out.println("say void ..."); }}
编写Server端代码
public class ThriftServer { public static void main(String[] args) { try { // 设置服务器端口 TServerSocket serverTransport = new TServerSocket(9090); // 设置二进制协议工厂 Factory protocolFactory = new TBinaryProtocol.Factory(); // 处理器关联业务实现 Processor<HelloService.Iface> processor = new HelloService.Processor<HelloService.Iface>( new HelloServiceImpl()); //使用单线程标准阻塞I/O模型 TServer.Args simpleArgs = new TServer.Args(serverTransport) .processor(processor) .protocolFactory(protocolFactory); TServer server = new TSimpleServer(simpleArgs); System.out.println("开启thrift服务器,监听端口:9090"); server.serve(); } catch (TTransportException e) { e.printStackTrace(); } }}
编写Client端代码
public class ThriftClient { public static void main(String[] args) { try { // 设置调用的服务地址-端口 TTransport transport = new TSocket("localhost", 9090); // 使用二进制协议 TProtocol protocol = new TBinaryProtocol(transport); // 使用的接口 HelloService.Client client = new HelloService.Client(protocol); // 打开socket transport.open(); client.sayBoolean(true); client.sayString("Hello world"); client.sayInt(20141111); client.sayVoid(); transport.close(); } catch (TTransportException e) { e.printStackTrace(); } catch (TException te) { te.printStackTrace(); } }}
运行结果
先运行Server端,再运行Client端
服务端输出:
开启thrift服务器,监听端口:9090
say boolean :true
say string :Hello world
say int :20141111
say void …
到这里,就实现了一个Thrift的RPC调用例子
0 0
- python thrift简单例子
- Thrift简单例子
- 一个Thrift的简单例子
- Thrift CPP例子
- thrift一个例子
- thrift完整例子
- thrift一个例子
- thrift小例子
- 【Thrift】python 例子
- Thrift 入门例子
- 【thrift】java 例子
- thrift 0.9.0 tutorial例子
- thrift windows vs2013测试例子
- Thrift多接口支持例子
- RPC-Thrift简单应用
- thrift简单使用
- thrift简单实例
- thrift简单使用
- 配置jdk和类源码问题
- atexit 详解
- RecycleView(样例Phone)
- java简单的人机猜拳小游戏
- sourceInsight 查找某个变量或函数 调用了谁,或者被谁调用了
- Thrift简单例子
- Codeforces Round #398 (Div. 2) D. Cartons of milk
- [弦图最小染色] BZOJ1006: [HNOI2008]神奇的国度
- openssl+tomcat7使用简明笔记
- 树莓派Ubuntu mate15.10图形界面 用root账号登录登录
- Please open the about:config page and disable the "security.fileuri.strict_origin_policy" option
- caffe在ubuntu16.04下的配置
- HTML复习
- zynq-7000系列解决部分用户反映MAC地址每次启动都变化的问题