eclipse 配置thrift

  1. In Eclipse: File --> New --> Other... (Ctrl+N) --> Dynamic Web Project...

  2. Project name: ThriftExample

  3. 讲项目转换成maven项目

  4. 在pom.xml中添加

     <url>http://maven.apache.org</url>
      <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>  
                <groupId>org.apache.thrift</groupId>  
                <artifactId>libthrift</artifactId>  
                <version>0.9.3</version>  
            </dependency>  
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-api</artifactId>  
                <version>1.7.5</version>  
            </dependency>  
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-simple</artifactId>  
                <version>1.7.5</version>  
            </dependency>  
      
            <dependency>  
                <groupId>org.slf4j</groupId>  
                <artifactId>slf4j-log4j12</artifactId>  
                <version>1.7.5</version>  
            </dependency>  
            <dependency>  
                <groupId>log4j</groupId>  
                <artifactId>log4j</artifactId>  
                <version>1.2.17</version>  
            </dependency>  
      </dependencies>

5.编辑文件example.thrift

namespace java example

struct BeanExample {
        1: bool booleanPrimive;
        2: byte bytePrimive;
        3: i16 shortPrimive;
        4: i32 intPrimive;
        5: i64 longPrimive;
        6: double doublePrimive;
        7: string stringObject; 
        8: binary byteArray; //ByteBuffer
}

service ServiceExample {
  BeanExample getBean(1: i32 anArg; 2: string anOther)
}

下载thrift-0.9..3.exe

thrift-0.9.3 --gen java -out . example.thrift

将生成的包放到项目下面

编辑ServiceExampleImpl.java

 package example;
   
   import java.nio.ByteBuffer;
   import org.apache.thrift.TException;
    
   public class ServiceExampleImpl implements ServiceExample.Iface {
           public BeanExample getBean(int anArg, String anOther) throws TException {
                   return new BeanExample(true, (byte) 2, (short) 3, 4, 5, 6.0,
                      "OK", ByteBuffer.wrap(new byte[] { 3, 1, 4 }));
          }
  }

ClientExample.java

 package example;

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;

// import org.apache.thrift... etc.;
   
   public class ClientExample {
           private static final int PORT = 7911;
   
           public static void main(String[] args) {
                   try {
                           TTransport transport = new TSocket("localhost", PORT);
                           TProtocol protocol = new TBinaryProtocol(transport);
                           ServiceExample.Client client = new ServiceExample.Client(protocol);
                           transport.open();
                           BeanExample bean = client.getBean(1, "string");
                           transport.close();
                           System.out.println(bean.getStringObject());
                   } catch (TTransportException e) {
                           e.printStackTrace();
                   } catch (TException e) {
                           e.printStackTrace();
                   }
           }
   }
 

ServerExample.java

package example;

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

// import org.apache.thrift... etc.;

public class ServerExample implements Runnable {
    private static final int PORT = 7911;

    public void run() {
        try {
            TServerSocket serverTransport = new TServerSocket(PORT);
            ServiceExample.Processor processor = new ServiceExample.Processor(new ServiceExampleImpl());
            TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(processor));
            System.out.println("Starting server on port " + PORT);
            server.serve();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new Thread(new ServerExample()).run();
    }
}

  1. Execute the server (right-click ServerExample.java --> Run as --> Java Application) to see the message: Starting server on port 7911

  2. Execute the client (right-click ClientExample.java --> Run as --> Java Application) to see the message: OK

转载于:https://my.oschina.net/u/2510243/blog/678025

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值