Thrift安装与使用

Thrift结合IntellijIDEA

1.  pom.xml:<build><plugin>中需要指定相关配置

<plugin>
    <groupId>org.apache.thrift.tools</groupId>
    <artifactId>maven-thrift-plugin</artifactId>
    <version>0.1.12-SNAPSHOT</version>
    <configuration>
        <!-- <thriftExecutable>D:\thrift\thrift-0.9.2.exe</thriftExecutable>-->
        <!--<thriftExecutable>/usr/local/bin/thrift</thriftExecutable>-->
        <thriftExecutable>D:\Code\Thrift-gen\thrift-0.9.2.exe</thriftExecutable>
    </configuration>
    <executions>
        <execution>
            <id>thrift-sources</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

2. 在 thrift文件正确下,直接用IDEA的maven插件install


基本使用

代码示例

  • 在Linux下安装thrift0.9.2:

                tar -zxvf thrift-0.9.1.tar.gz

                ./configure
                make  //编译
                sudo make install
                thrift -version

            使用which thrift命令查看thrift安装到哪个目录,默认是:/usr/local/bin/thrift

  • thrift转Java: thrift-0.9.2.exe -r -gen java ./×××××.thrift

  • 实现服务端接口( 接口实现类 )

public class HelloWorldImpl implements HelloWorldService.Iface{
    private static final Logger logger = Logger.getLogger(HelloWorldImpl.class);
    @Override
    public String sayHello(String username) throws TException {
        return "Hi,"+username+" welcome to my Server....";
    }
}
  • 编写服务端

public class HelloServerDemo {
    public static final int SERVER_PORT = 8090;
    private static final Logger logger = Logger.getLogger(HelloServerDemo.class);

    public static void main(String[] args){
        HelloServerDemo serverDemo = new HelloServerDemo();
        serverDemo.startServer();
    }
    public void startServer(){
        // TServer --> TServer.Args: 1. TServerSocket( port )
        //                           2. interface
        //                           3. TBinaryProtocol.Factory()
        try {
            System.out.println("HelloWorld TSimpleServer start....");
            //TProcessor tProcessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldImpl());
            HelloWorldService.Processor<HelloWorldService.Iface> tprocessor = new HelloWorldService.Processor<HelloWorldService.Iface>(new HelloWorldImpl());

            //简单地单线程服务模型,一般用于测试
            TServerSocket serverTransport = new TServerSocket(SERVER_PORT);
            TServer.Args tArgs = new TServer.Args(serverTransport);
            tArgs.processor(tprocessor);
            tArgs.protocolFactory(new TBinaryProtocol.Factory());
            //tArgs.protocolFactory(new TCompactProtocol.Factory());
            //tArgs.protocolFactory(new TJSONProtocol.Factory());
            TServer server = new TSimpleServer(tArgs);
            server.serve();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }
}
  • 编写Client

public class HelloClientDemo {
    public static final String SERVER_IP = "localhost";
    public static final int SERVER_PORT = 8090;
    public static final int TIME_OUT = 30000;

    public static void main(String[] args){
        try {
            new HelloClientDemo().startClient("Tanzhen");
        } catch (TException e) {
            e.printStackTrace();
        }
    }
    public static void startClient(String usrname) throws TException {
        TTransport transport = null;
        transport = new TSocket(SERVER_IP,SERVER_PORT,TIME_OUT);
        TProtocol protocol = new TBinaryProtocol(transport);
        HelloWorldService.Client client = new HelloWorldService.Client(protocol);
        transport.open();
        String result = client.sayHello(usrname);
        System.out.println("Thrift client result="+result);
    }
}



基本类型

  • bool: 布尔值,true或false,对应Java的boolean

  • byte:8位有符号数,对应Java的byte

  • i16:16位有符号数,对应Java的short

  • i32:32位有符号数,对应Java的int

  • i64:对应Java的long

  • double:对应Java的double

  • string:utf-8编码的字符串,对应Java的String

  • list:对应Java中ArrayList

  • set:对应Java的HashSet

  • map:对应Java的HashMap

  • exception:对应Java的Exception

  • service:对应服务的类

结构体

  • struct:定义公共对象,类似于C语言中的结构体定义,在Java中是一个JavaBean


原理详解

http://www.cnblogs.com/brucewoo/archive/2012/06/03/2532788.html

是什么:

为什么:

怎么做:


转载于:https://my.oschina.net/supersonic/blog/480088

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值