Thrift java服务器与客户端示例

简单的实现一个PING的功能

1.安装thrift

http://thrift.apache.org/download/

人人网镜像下载:

http://labs.renren.com/apache-mirror/thrift/0.6.1/thrift-0.6.1.exe

2.编写Thrift文件(定义接口,结构,异常等),保存为test.thrift

namespace java net.johnc.thrift
service Test{ 
    void ping(1: i32 length)
}

3.生成接口代码

把thrift-0.6.1.exe和test.thrift文件放在同一个目录,当然也可以把thrift-0.6.1.exe文件放进环境变量

进入DOS命令执行:thrift-0.6.1.exe --gen java test.thrift

生成文件 gen-java/net/johnc/thrift/Test.java

4.编写服务端接口实现类

在POM.xml文件加入以下依赖:

<dependency>
      <groupId>org.apache.thrift</groupId>
      <artifactId>libthrift</artifactId>
      <version>0.6.1</version>
</dependency>

 把生成的Test.java复制到项目下

package net.johnc.thrift;
import org.apache.thrift.TException;
 
public class TestImpl implements Test.Iface {
    public void ping(int length) throws TException {
        System.out.println("calling ping ,length=" + length);
    }
}

5.编写启动服务代码

package net.johnc.thrift;

import net.johnc.thrift.Test.Processor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
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.TTransportException;

public class Server {
    public void startServer() {
        try {

            TServerSocket serverTransport = new TServerSocket(1234);

            Test.Processor process = new Processor(new TestImpl());

            Factory portFactory = new TBinaryProtocol.Factory(true, true);

            Args args = new Args(serverTransport);
            args.processor(process);
            args.protocolFactory(portFactory);

            TServer server = new TThreadPoolServer(args);
            server.serve();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.startServer();
    }
}

6.编写客户端代码

package net.johnc.thrift;
 
  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 org.apache.thrift.transport.TTransportException;
 
  public class Client {
 
     public void startClient() {
         TTransport transport;
         try {
             transport = new TSocket("localhost", 1234);
             TProtocol protocol = new TBinaryProtocol(transport);
             Test.Client client = new Test.Client(protocol);
             transport.open();
             client.ping(2012);
             transport.close();
         } catch (TTransportException e) {
             e.printStackTrace();
         } catch (TException e) {
             e.printStackTrace();
         }
     }
 
     public static void main(String[] args) {
         Client client = new Client();
         client.startClient();
     }
 }

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/lihaozy/archive/2013/04/26/3045254.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值