Thrift是一個軟件框架,用來進行可擴展且跨語言的服務開發協議。它擁有強大的代碼生成引擎,支持C++,Java,Python,PHP,Ruby等編程語言。Thrift允許定義一個簡單的定義文件(以.thirft結尾),文件中包含數據類型和服務接口。用以作為輸入文件,編譯器生成代碼用來方便的生成RPC客戶端和服務端通信的編程語言。具體Thrift安裝過程請參考《Mac OS X 下搭建thrift環境》。
package cn.rpc.main;import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import cn.rpc.service.StatQueryService;
import cn.rpc.service.impl.StatQueryServiceImpl;/*** @Date Mar 23, 2015** @Author dengjie*/publicclass StatsServer {privatestatic Logger logger = LoggerFactory.getLogger(StatsServer.class);privatefinalint PORT = 9090;@SuppressWarnings({ "rawtypes", "unchecked" })privatevoid start() {try {TNonblockingServerSocket socket = new TNonblockingServerSocket(PORT);final StatQueryService.Processor processor = new StatQueryService.Processor(new StatQueryServiceImpl());THsHaServer.Args arg = new THsHaServer.Args(socket);/** Binary coded format efficient, intensive data transmission, The* use of non blocking mode of transmission, according to the size* of the block, similar to the Java of NIO*/arg.protocolFactory(new TCompactProtocol.Factory());arg.transportFactory(new TFramedTransport.Factory());arg.processorFactory(new TProcessorFactory(processor));TServer server = new THsHaServer(arg);server.serve();} catch (Exception ex) {ex.printStackTrace();}}publicstaticvoid main(String[] args) {try {logger.info("start thrift server...");StatsServer stats = new StatsServer();stats.start();} catch (Exception ex) {ex.printStackTrace();logger.error(String.format("run thrift server has error,msg is %s", ex.getMessage()));}}}
package org.apache.hadoop.ipc;import java.io.IOException;/*** Superclass of all protocols that use Hadoop RPC.* Subclasses of this interface are also supposed to have* a static final long versionID field.*/publicinterface VersionedProtocol {/*** Return protocol version corresponding to protocol interface.* @param protocol The classname of the protocol interface* @param clientVersion The version of the protocol that the client speaks* @return the version that the server will speak* @throws IOException if any IO error occurs*/publiclong getProtocolVersion(String protocol,long clientVersion) throws IOException;/*** Return protocol version corresponding to protocol interface.* @param protocol The classname of the protocol interface* @param clientVersion The version of the protocol that the client speaks* @param clientMethodsHash the hashcode of client protocol methods* @return the server protocol signature containing its version and* a list of its supported methods* @see ProtocolSignature#getProtocolSignature(VersionedProtocol, String, * long, int) for a default implementation*/public ProtocolSignature getProtocolSignature(String protocol, long clientVersion,int clientMethodsHash) throws IOException;
}