首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

Thrift开发范例

2012-09-29 
Thrift开发实例Thrift开发实例 准备工作Thrift主页:http://thrift.apache.org/Thrift下载:http://thrift.a

Thrift开发实例
Thrift开发实例


准备工作
Thrift主页:http://thrift.apache.org/

Thrift下载:http://thrift.apache.org/download/

下载thrift-0.8.0.tar.gz和Thrift compiler for Windows (thrift-0.8.0.exe)

创建工作区../thrift;

将thrift-0.8.0.tar.gz和thrift-0.8.0.exe拷贝到../thrift下;

将thrift-0.8.0.tar.gz解压到当前目录;

创建start.bat,编辑其内容为:

cd

thrift-0.8.0 --gen java *.thrift

pause




简单实例
创建thrift脚本文件:

namespace java com.test.rpc

service TestService{

string getUserName(1:i64 id)

}


执行start.bat,thrift会根据脚本生成java代码../thrift/gen-java/com/test/rpc/ TestService.java;

创建java project;

创建package:com.test.rpc,将生成的TestService.java拷入;

创建接口实现类TestImpl

package com.test.rpc;



import org.apache.log4j.Logger;

import org.apache.thrift.TException;



import com.test.rpc.TestService.Iface;

import com.test.user.IUserService;

import com.test.user.UserServiceImpl;





public class TestImpl implements Iface {

    private static final Logger LOG = Logger.getLogger(TestImpl.class);

   

    @Override

    public String getUserName(long id) throws TException {

        LOG.info("TestImpl server get rpc msg :"+id);

        IUserService userService = new UserServiceImpl();

        String username = userService.getUser(id);

        return username;

    }



}


创建RPC server启动线程

package com.test.rpc;



import javax.xml.ws.Endpoint;



import org.apache.log4j.Logger;

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.TThreadPoolServer;

import org.apache.thrift.server.TThreadPoolServer.Args;

import org.apache.thrift.transport.TServerSocket;

import org.apache.thrift.transport.TTransportException;



import com.test.service.CommonService;











public class TestServer extends Thread  {

    private static final Logger LOG = Logger.getLogger(TestServer.class);

   

    @Override

    public void run() {

        try {

            TestImpl testimpl = new TestImpl();

            TServerSocket serverTransport = new TServerSocket(7911);

            Factory proFactory = new TBinaryProtocol.Factory();

            TProcessor processor = new TestService.Processor<TestImpl>(testimpl);

            Args rpcArgs = new Args(serverTransport);

            rpcArgs.processor(processor);

            rpcArgs.protocolFactory(proFactory);

            TServer server = new TThreadPoolServer(rpcArgs);

            LOG.info("Start TestService on port 7911..." + Thread.currentThread().getId() + "["

                + Thread.currentThread().getName() + "]");

            server.serve();

        } catch (TTransportException e) {

            e.printStackTrace();

            LOG.info("TestService.simpleRun", e);

        }

    }

    /**

     * @param args

     */

    public static void main(String[] args)   {

        TestServer server = new TestServer();

        server.start();

    }



}








复杂对象实例
与简单实例基本相同,只是脚本不同,参数以对象方式传递,自动生成的java类多一些;

创建thrift脚本文件:

namespace java com.test.rpc

struct User {  

  1: i64 id  

  2: string name  

}

service TestService{

User getUser(1:i64 id)

}


执行start.bat,thrift会根据脚本生成java代码../thrift/gen-java/com/test/rpc/目录下TestService.java、User.Java;

创建java project;

创建package:com.test.rpc,将生成的TestService.java、User.Java拷入;

其他步骤类同上例;



 

客户端代码



package com.test.rpc;



import org.apache.log4j.Logger;

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.TTransportException;

import com.test.rpc.TestService.Client;



public class TestClient {

    private static final Logger LOG = Logger.getLogger(TestClient.class);

   

    public static void main(String[] args){

        try{

            TSocket tsocket = new TSocket("localhost", 7911);

tsocket.open();

            TProtocol protocol = new TBinaryProtocol(tsocket);

            Client client = new Client(protocol);

            User user = client.getUser(id);

            LOG.info(user.getName());

        }catch (Exception e) {

            LOG.error(e.getMessage(), e);

        }

    }

   

}




热点排行