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);

}

}



}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值