Thrift简介与实践

Thrift是一个支持多种语言的远程服务调用框架,最初由Facebook开发,目前由Apache基金会负责维护。

安装

tar -zxvf thrift-0.9.1.tar.gz
cd thrift-0.9.1/
./configure
make
make install

安装成功后,可以使用“thrift”命令。

定义远程服务接口

Thrift提供了接口描述语言,用于定义远程服务接口,例如,“TestService.thrift”中定义了“greet”方法:

1
2
3
4
namespace java com.magicwt.service
service TestService {
string greet(1:string name)
}

使用“thrift”命令根据“TestService.thrift”生成相应的Java代码文件:

thrift -r -gen java TestService.thrift

执行成功后,会在gen-java/com/magicwt/service目录下生成TestService.java。

远程服务调用测试

创建Maven工程,增加对Thrift的依赖:

1
2
3
4
5
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.9.1</version>
</dependency>

导入TestService.java,创建TestServiceImpl类实现TestService中的Iface接口,实现其中的greet方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.magicwt.service.impl;

import com.magicwt.service.TestService;
import org.apache.thrift.TException;

public class TestServiceImpl implements TestService.Iface {

@Override
public String greet(String name) throws TException {
return "hi, " + name;
}

}

创建TestServer和TestClient类分别作为测试服务端和测试客户端。

  • TestServer:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    package com.magicwt;

    import com.magicwt.service.TestService;
    import com.magicwt.service.impl.TestServiceImpl;
    import org.apache.thrift.TProcessor;
    import org.apache.thrift.protocol.TBinaryProtocol;
    import org.apache.thrift.server.TServer;
    import org.apache.thrift.server.TThreadPoolServer;
    import org.apache.thrift.server.TThreadPoolServer.Args;
    import org.apache.thrift.transport.TServerSocket;
    import org.apache.thrift.transport.TServerTransport;

    import java.net.InetSocketAddress;

    public class TestServer {

    public static void main(String[] array) throws Exception {
    TProcessor processor = new TestService.Processor(new TestServiceImpl());
    TServerTransport transport = new TServerSocket(new InetSocketAddress("127.0.0.1", 9001));
    Args args = new Args(transport);
    args.processor(processor);
    args.protocolFactory(new TBinaryProtocol.Factory());
    TServer server = new TThreadPoolServer(args);
    server.serve();
    }
    }
  • TestClient:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    package com.magicwt;

    import com.magicwt.service.TestService.Client;
    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 TestClient {

    public static void main(String[] args) throws Exception {
    TTransport transport = new TSocket("127.0.0.1", 9001);
    TProtocol protocol = new TBinaryProtocol(transport);
    Client client = new Client(protocol);
    transport.open();
    System.out.println(client.greet("admin"));
    transport.close();
    }
    }

先后启动TestServer和TestClient,TestClient的控制台输出如下:
1
说明远程服务调用成功。