thrift简介:

  thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的、高效的服务。

  thrift允许定义一个简单的定义文件中的数据类型和服务接口,以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝跨编程语言。

windows 环境下thrift 的安装:

  1. 到thrift官网下载相应的exe文件

  2. 将其放到某个目录下,重命名为thrift.exe

  3. 将该目录添加到path环境变量中

  4. 在命令行下运行命令 thrift  -version,如果出现"Thrift version xx.xx.xx",说明安装成功。

thrift的基本用法:

  thrift --gen language Thrift filename

  其中language是要生成的代码的编程语言,Thrift filename为依据的thrift文件。

thrift的Hello World程序:

  0. 依赖的jar包:

    libthrift-0.9.1.jar

    log4j.jar

    slf4j-api.jar

    slf4j-log4j12.jar

  1. 生成一个HelloWorld.thrift文件,内容如下:

    namespace java com.zhang.thrift.server  //定义命名空间

    service HelloWorldService {  //定义服务类

    string helloWorld()  //定义服务的方法

    }

    其中的“java” 为要生成的代码语言(我不明白为什么一定要有这个东西,thrift命令的参数有了生成的代码语言)。

    在该thrift文件所在的目录下运行命令   thrift --gen  java HelloWorld.thrift

    就会在该目录下生成一个gen-java的文件夹,其中有一个类com.zhang.thrift.server.HelloWorldService

2. 新建一个类实现com.zhang.thrift.server.HelloWorldService.Iface接口

public class HelloWorldServiceImpl implements HelloWorldService.Iface{
    @Override
    public String helloWorld() throws TException {
        return "Hello World!";
    }
}

3. 创建一个类,作为服务器端

package com.zhang.thrift.server.java;

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServer.Args;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TSSLTransportFactory;
import org.apache.thrift.transport.TSSLTransportFactory.TSSLTransportParameters;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;

import com.zhang.thrift.server.HelloWorldService;
import com.zhang.thrift.server.impl.HelloWorldServiceImpl;

public class JavaServer {

	public static HelloWorldServiceImpl helloWorldServiceImpl;

	public static HelloWorldService.Processor processor;

	public static void main(String[] args) {
		try {
			helloWorldServiceImpl = new HelloWorldServiceImpl();
			processor = new HelloWorldService.Processor(helloWorldServiceImpl);

			Runnable simple = new Runnable() {
				public void run() {
					simple(processor);
				}
			};

//			Runnable secure = new Runnable() {
//				public void run() {
//					secure(processor);
//				}
//			};

			new Thread(simple).start();
//			new Thread(secure).start();

		} catch (Exception x) {
			x.printStackTrace();
		}
	}

	public static void simple(HelloWorldService.Processor processor) {
		try {
			TServerTransport serverTransport = new TServerSocket(8080);
			TServer server = new TSimpleServer(
					new Args(serverTransport).processor(processor));

			// Use this for a multithreaded server
			// TServer server = new TThreadPoolServer(new
			// TThreadPoolServer.Args(serverTransport).processor(processor));

			System.out.println("Starting the simple server...");
			server.serve();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

4. 创建一个类,作为客户端,对服务进行调用测试。

package com.zhang.thrift.server.java;

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;

import com.zhang.thrift.server.HelloWorldService;

public class JavaSimpleClient {

	public static void main(String[] args) {
		try {
			TTransport transport;

			transport = new TSocket("localhost", 8080);
			transport.open();

			TProtocol protocol = new TBinaryProtocol(transport);
			HelloWorldService.Client client = new HelloWorldService.Client(protocol);

			perform(client);

			transport.close();
		} catch (TException x) {
			x.printStackTrace();
		}
	}

	private static void perform(HelloWorldService.Client client) throws TException {
		String result = client.helloWorld();
		System.out.println(result);
	}
}