thrift实现java与ruby的交互

thrift在apache中主页 http://thrift.apache.org/

Thrift是一个可以提供跨语言开发的框架,它允许你在一个配置文件中定义数据类型和服务接口 ,把这个文件作为输入,编译器产生的代码将要被用来建立RPC客户端和服务端的跨语言的无缝连接。
thrift0.5的下载地址
http://www.apache.org/dyn/closer.cgi?path=/incubator/thrift/0.5.0-incubating/thrift-0.5.0.tar.gz
下载之后编译安装

开始交互之旅
thrift的数据类型:

1 基本数据类型有
bool: A boolean value (true or false)
byte: An 8-bit signed integer
i16: A 16-bit signed integer
i32: A 32-bit signed integer
i64: A 64-bit signed integer
double: A 64-bit floating point number
string: A text string encoded using UTF-8 encoding

2 特殊类型
binary: a sequence of unencoded bytes

3 结构体
4 容器
list<type>: An ordered list of elements. Translates to an STL vector, Java ArrayList, native arrays in scripting languages, etc.
set<type>: An unordered set of unique elements. Translates to an STL set, Java HashSet, set in Python, etc. This type is not supported in PHP! Use lists instead.
map<type1,type2>: A map of strictly unique keys to values. Translates to an STL map, Java HashMap, PHP associative array, Python/Ruby dictionary, etc.



一、新建thrift文件 user.thrift
struct User {
  1: string id,
  2: string name,
}

service UserStorage {
  void set_user(1: User user),
  User get_user(1: string id)
}


在此目录下运行thrift命令,生成相应的java文件和ruby文件
生成java相关文件的命令:
thrift -r -gen java user.thrift

生成ruby相关文件的命令:
thrift -r -gen rb user.thrift


执行命令之后,会发现此目录下面产生了两个文件夹gen-java 和 gen-rb
gen-java下面有两个文件 User.java(对struct User的实现) 和 UserStorage.java(对service UserStorage的实现)
gen-rb 下面有三个文件 user_storage.rb(对service UserStorage的实现) , user_types.rb ,user_constants.rb(后面两个文件对struct User的实现,后者只有一句话require 'user_types')

二、用java做服务端
新建一个java工程,将两个java文件引入,下面开始实现服务
首先要实现那两个自定一个接口方法
public class UserServiceHandler  implements UserStorage.Iface {

  public void set_user(User user) throws TException {
    System.out.println(user.name + " " + user.id);
  }

  public User get_user(String id) throws TException {
    // 没有逻辑意义,纯粹是为了返回一个user
    User user = new User();
    user.setId(id);
    user.setName("user_one");
    return user;
  }
  
}


启动java这边的服务
  public static void main(String[] args) {
    try {
      UserServiceHandler handler = new UserServiceHandler();
      UserStorage.Processor processor = new UserStorage.Processor(handler);
      TServerTransport serverTransport = new TServerSocket(9090);
      TServer server = new TSimpleServer(processor, serverTransport);
      // Use this for a multithreaded server
      // server = new TThreadPoolServer(processor, serverTransport);
      System.out.println("Starting the server...");
      server.serve();
    } catch (TTransportException tTransportException) {
    }
  }


三 ruby客户端

require 'thrift'
require 'user_constants'
require 'user_storage'

transport = Thrift::BufferedTransport.new(Thrift::Socket.new('localhost', 5555))
protocol = Thrift::BinaryProtocol.new(transport)
client = UserStorage::Client.new(protocol)

transport.open()

user = client.get_user('123')
p user



运行ruby的客户端,即可输出接口所设置的user
属性如下
id 123
name user_one




你可能感兴趣的:(java,apache,PHP,python,Ruby)