Thrift开发教程

1 编写thrift文件(aa.thrift)

namespace java com.tv189.uc.thrift
namespace cppthrift.vdb
namespace rbthrift.vdb
namespace perlthrift.vdb
namespace csharpthrift.vdb
namespace jsthrift.vdb
namespace stthrift.vdb
namespace pythrift.vdb
namespace php thrift

serviceUCThriftService{
    string ucOperator(1:string actionType, 2:string uid, 3:string data),
}

2 生成java文件

thrift-0.6.0.exe -r --gen java uc.thrift
thrift-0.6.0.exe -r --gen java uc.thrift
thrift-0.6.0.exe -r --gen phpuc.thrift
thrift-0.6.0.exe -r --gen pyuc.thrift
可以生成 php py 等等的

3 服务端编写

1)实现UCThriftService.Iface

public class UCThriftServiceImpl implements UCThriftService.Iface {
    public static Logger logger = Logger.getLogger(UCThriftServiceImpl.class);

    @Override
    public String ucOperator(String actionType, String suid, String data)
    throws TException {
        System.out.println("test");
    }
}

2) 运行的main编写

public class UCServiceServer {
	private static Logger logger = Logger.getLogger(UCServiceServer.class);

	public static void main(String... args) throws Exception {
		// 这个是用properties编写的,可以自行决定
		String server = PropertiesUtil.getValue("tserver.properties",
				"ucserver.nio", "hahs");
		if ("nio".equalsIgnoreCase(server)) {
			startNIO();
		}
		if ("hahs".equalsIgnoreCase(server)) {
			startHAHS();
		} else {
			start();
		}

	}

	private staticvoid start() throws TTransportException {
		String address = PropertiesUtil.getValue("tserver.properties",
				"ucserver.address", "0.0.0.0");
		int port = Integer.parseInt(PropertiesUtil.getValue(
				"tserver.properties", "ucserver.port", "5555"));
		intclientTimeout = Integer.parseInt(PropertiesUtil.getValue(
				"tserver.properties", "ucserver.clientTimeout", "30000"));
		intminWorkerThreads = Integer.parseInt(PropertiesUtil.getValue(
				"tserver.properties", "ucserver.minWorkerThreads", "25"));
		intmaxWorkerThreads = Integer.parseInt(PropertiesUtil.getValue(
				"tserver.properties", "ucserver.maxWorkerThreads", "25"));
		String protocol = PropertiesUtil.getValue("tserver.properties",
				"ucserver.protocol", "binary");
		TServerSocket socket = new TServerSocket(new InetSocketAddress(address,
				port), clientTimeout);
		UCThriftService.Processor process = new UCThriftService.Processor(
				UCThriftServiceImpl.instance());
		TThreadPoolServer.Argsarg = new TThreadPoolServer.Args(socket);
		if ("compact".equalsIgnoreCase(protocol)) {
			arg.protocolFactory(new TCompactProtocol.Factory());
		} else if ("binary".equalsIgnoreCase(protocol)) {
			arg.protocolFactory(new TBinaryProtocol.Factory());
		} else {
			arg.protocolFactory(new TBinaryProtocol.Factory());
		}
		arg.transportFactory(new TFramedTransport.Factory());
		arg.maxWorkerThreads(maxWorkerThreads);
		arg.minWorkerThreads(minWorkerThreads);
		// arg.processor(process);
		arg.processorFactory(new TProcessorFactory(process));
		TServer server = new TThreadPoolServer(arg);
		Logger.getLogger(UCServiceServer.class).info(
				UCServiceServer.class.getSimpleName() + " Listen at " + port);
		while (true) {
			try {
				server.serve();
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				server.stop();
				Logger.getLogger(UCServiceServer.class).info(
						UCServiceServer.class.getSimpleName() + " Reload");
				server = new TThreadPoolServer(arg);
			}
		}
	}

	private staticvoid startNIO() throws TTransportException {
		String address = PropertiesUtil.getValue("tserver.properties",
				"ucserver.address", "0.0.0.0");
		int port = Integer.parseInt(PropertiesUtil.getValue(
				"tserver.properties", "ucserver.port", "5555"));
		intclientTimeout = Integer.parseInt(PropertiesUtil.getValue(
				"tserver.properties", "ucserver.clientTimeout", "30000"));
		String protocol = PropertiesUtil.getValue("tserver.properties",
				"ucserver.protocol", "binary");
		TNonblockingServerSocket socket = new TNonblockingServerSocket(
				new InetSocketAddress(address, port), clientTimeout);
		UCThriftService.Processor process = new UCThriftService.Processor(
				UCThriftServiceImpl.instance());
		TNonblockingServer.Argsarg = new TNonblockingServer.Args(socket);
		if ("compact".equalsIgnoreCase(protocol)) {
			arg.protocolFactory(new TCompactProtocol.Factory());
		} else if ("binary".equalsIgnoreCase(protocol)) {
			arg.protocolFactory(new TBinaryProtocol.Factory());
		} else {
			arg.protocolFactory(new TBinaryProtocol.Factory());
		}
		arg.transportFactory(new TFramedTransport.Factory());
		// arg.processor(process);
		arg.processorFactory(new TProcessorFactory(process));
		TServer server = new TNonblockingServer(arg);
		Logger.getLogger(UCServiceServer.class).info(
				"NIO " + UCServiceServer.class.getSimpleName() + " Listen at"
						+ port);
		while (true) {
			try {
				server.serve();
			} catch (Exception e) {

				server.stop();
				Logger.getLogger(UCServiceServer.class).info(
						"NIO " + UCServiceServer.class.getSimpleName()
								+ "Reload");
				server = new TNonblockingServer(arg);
			}
		}
	}

	private static void startHAHS() throws TTransportException {
		String address = PropertiesUtil.getValue("tserver.properties",
				"ucserver.address", "0.0.0.0");
		int port = Integer.parseInt(PropertiesUtil.getValue(
				"tserver.properties", "ucserver.port", "5555"));
		intclientTimeout = Integer.parseInt(PropertiesUtil.getValue(
				"tserver.properties", "ucserver.clientTimeout", "30000"));
		intminWorkerThreads = Integer.parseInt(PropertiesUtil.getValue(
				"tserver.properties", "ucserver.minWorkerThreads", "25"));
		String protocol = PropertiesUtil.getValue("tserver.properties",
				"ucserver.protocol", "binary");

		TNonblockingServerSocket socket = new TNonblockingServerSocket(
				new InetSocketAddress(address, port), clientTimeout);
		UCThriftService.Processor process = new UCThriftService.Processor(
				UCThriftServiceImpl.instance());
		THsHaServer.Argsarg = new THsHaServer.Args(socket);
		if ("compact".equalsIgnoreCase(protocol)) {
			arg.protocolFactory(new TCompactProtocol.Factory());
		} else if ("binary".equalsIgnoreCase(protocol)) {
			arg.protocolFactory(new TBinaryProtocol.Factory());
		} else {
			arg.protocolFactory(new TBinaryProtocol.Factory());
		}
		arg.transportFactory(new TFramedTransport.Factory());
		arg.workerThreads(minWorkerThreads);
		// arg.processor(process);
		arg.processorFactory(new TProcessorFactory(process));
		TServer server = new THsHaServer(arg);
		Logger.getLogger(UCServiceServer.class).info(
				"HAHS " + UCServiceServer.class.getSimpleName() + " Listen at"
						+ port);
		while (true) {
			try {
				server.serve();
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
				server.stop();
				Logger.getLogger(UCServiceServer.class).info(
						"HAHS " + UCServiceServer.class.getSimpleName()
								+ "Reload");
				server = new TNonblockingServer(arg);
			}
		}
	}

}

4 客户端编写

public voidnewThread() throws TException {
	String address = "0.0.0.0";
	int port = 5555;
	intclientTimeout = 30000;
	TTransport transport = new TFramedTransport(new TSocket(address, port,
	clientTimeout));
	TProtocol protocol = new TBinaryProtocol(transport);
	UCThriftService.Client client = new UCThriftService.Client(protocol);
	transport.open();
	try {
	    long bt = System.currentTimeMillis();
	    System.out.println(URLDecoder.decode(client.ucOperator("get","29", "")));
	} catch (TApplicationException e) {
	    System.out.println(e.getMessage() + " " + e.getType());
	}
	transport.close();
}

5 运行

先运行ucserver

client连接可在控制台看到输出


说明:

client连服务端有几个要注意的地方:

1 服务器的ip和端口

2 服务端和客户端用的 transport 和协议一定要一样

比如:如果服务端用的TFrameTransport那客户端也要用TFrameTransport

如果服务端用的TBinaryProtocol,那客户端也要用TBinaryProtocol

否则会出一个好像是TTransportException的一个异常。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值