博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
eclipse 配置thrift
阅读量:7062 次
发布时间:2019-06-28

本文共 3853 字,大约阅读时间需要 12 分钟。

hot3.png

  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 examplestruct 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

你可能感兴趣的文章
Android全局代理软件ProxyDroid和TransProxy源码分享
查看>>
C# WinForm开发系列 - ADO.NET
查看>>
SQL Server误区30日谈-Day23-有关锁升级的误区
查看>>
人人都是 DBA(XV)锁信息收集脚本汇编
查看>>
将不确定变为确定~MVC3的ValidateInput属性失灵了
查看>>
[LeetCode] Paint House II 粉刷房子之二
查看>>
[LeetCode] Number Complement 补数
查看>>
设计一个有getMin功能的栈
查看>>
[LintCode] Maximum Gap 求最大间距
查看>>
RegeX的早期版本
查看>>
Redis 小白指南(二)- 聊聊五大类型:字符串、散列、列表、集合和有序集合...
查看>>
零元学Expression Blend 4 - Chapter 23 Deep Zoom Composer与Deep Zoom功能
查看>>
C#~异步编程再续~async异步方法与同步方法的并行
查看>>
Windows下的字体美化
查看>>
13.9. Health Status
查看>>
Unable to execute dex: Multiple dex files define Lcom/kenai/jbosh/AbstractAttr
查看>>
微信小程序明星开发者博卡君专访
查看>>
什么是 Help Desk?
查看>>
【MySQL】Tokudb安装测试初探
查看>>
12C打补丁的简单尝试
查看>>