thrift java eclipse

1. 下载 thrift

https://thrift.apache.org/download

我是windows环境

http://www.apache.org/dyn/closer.cgi?path=/thrift/0.9.3/thrift-0.9.3.exe

2.  eclipse  建  maven  工程

pom.xml

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



3.  project 下建 thrift目录,建hello.thrift文件

namespace java com.stone.thrift

service Hello{

  string helloString(1:string para)
  i32 helloInt(1:i32 para)
  bool helloBoolean(1:bool para)
  void helloVoid()
  string helloNull()
 
 }



4. 根据  thrift  文件生成 java 代码

在当前目录下  生成了gen-java目录

将目录拷贝到project 下


拷贝进来后报错,是override的原因。没有父类或接口,却使用了override,不知是工具的原因,还是我理解不到位。

remove 所有的  override注解



5. 编写接口的实现

package com.stone.thrift;

import org.apache.thrift.TException;

public class HelloServiceImpl implements Hello.Iface{

    public String helloString(String para) throws TException {
        // TODO Auto-generated method stub
        return "这是我的一个thrift  hello java "+ para;
    }

    public int helloInt(int para) throws TException {
        // TODO Auto-generated method stub
        return 0;
    }

    public boolean helloBoolean(boolean para) throws TException {
        // TODO Auto-generated method stub
        return false;
    }

    public void helloVoid() throws TException {
        // TODO Auto-generated method stub
        
    }

    public String helloNull() throws TException {
        // TODO Auto-generated method stub
        return null;
    }

}

这里只具体实现了一个方法,其他方法都是自动生成的,没任何功能实现



6. 编写服务端,并启动

package com.stone.thrift.server;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

import com.stone.thrift.Hello;
import com.stone.thrift.HelloServiceImpl;

public class HelloServiceServer {
   /**
    * 启动 Thrift 服务器
    * @param args
    */
   public static void main(String[] args) {
       try {
           
           // 设置服务监听端口为 1688
           TServerSocket serverTransport = new TServerSocket(1688);
           //创建自己具体的processor
           TProcessor tprocessor = new Hello.Processor<Hello.Iface>(new HelloServiceImpl());
           
           TServer.Args tArgs = new TServer.Args(serverTransport);
           tArgs.processor(tprocessor);
           // 设置协议工厂为 TBinaryProtocol.Factory
           tArgs.protocolFactory(new TBinaryProtocol.Factory());
           
           TServer server = new TSimpleServer(tArgs);
           // 关联处理器与 Hello 服务的实现
           System.out.println("Start server on port 1699...");
           
           server.serve();
           
           
       } catch (TTransportException e) {
           e.printStackTrace();
       }
   }
}




7. 编写客户端,并启动


package com.stone.thrift.client;

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 com.stone.thrift.Hello;

public class HelloClient {
    
        public static void main(String[] s){
            TTransport transport = null;
            try {
                transport = new TSocket("127.0.0.1", 1688, 30000);
                // 协议要和服务端一致
                TProtocol protocol = new TBinaryProtocol(transport);
                // TProtocol protocol = new TCompactProtocol(transport);
                // TProtocol protocol = new TJSONProtocol(transport);
                Hello.Client client = new Hello.Client(protocol);
                transport.open();
                String result = client.helloString("THRIFT JAVA CLIENT ");
                System.out.println("Thrify client result =: " + result);
            } catch (TTransportException e) {
                e.printStackTrace();
            } catch (TException e) {
                e.printStackTrace();
            } finally {
                if (null != transport) {
                    transport.close();
                }
            }
        }
        
        
}

服务端启动信息


客户端启动信息


至此,一个简单的thrift hello  完成了。


示例代码   http://download.csdn.net/detail/stonexmx/9546638


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值