欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

thrift 開發教程 - nick的日志 - 網易博客

系統 1861 0

thrift 開發教程 - nick的日志 - 網易博客

thrift 開發教程 ?? ?

2011-05-27 15:27:29 |??分類: thrift |??標簽: ? | 字號 ? ? 訂閱

1 編寫thrift文件(如aa.thrift)
namespace java? com.tv189.uc.thrift
namespace cpp thrift.vdb
namespace rb thrift.vdb
namespace perl thrift.vdb
namespace csharp thrift.vdb
namespace js thrift.vdb
namespace st thrift.vdb
namespace py thrift.vdb
namespace php thrift

?


service UCThriftService{
?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 php uc.thrift
thrift-0.6.0.exe -r --gen py uc.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 static void 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"));
??int clientTimeout = Integer.parseInt(PropertiesUtil.getValue("tserver.properties","ucserver.clientTimeout","30000"));
??int minWorkerThreads = Integer.parseInt(PropertiesUtil.getValue("tserver.properties","ucserver.minWorkerThreads","25"));
??int maxWorkerThreads = 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.Args arg = 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 static void 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"));
??int clientTimeout = 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.Args arg = 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"));
??int clientTimeout = Integer.parseInt(PropertiesUtil.getValue("tserver.properties","ucserver.clientTimeout","30000"));
??int minWorkerThreads = 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.Args arg = 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? void newThread() throws TException {
??
??
??String address = "0.0.0.0";
??int port = 5555;
??int clientTimeout = 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的一個異常。

thrift 開發教程 - nick的日志 - 網易博客


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: a一级爱做片免费 | 国产va免费精品观看精品 | 一区二区三区回区在观看免费视频 | 91精品国产综合久久婷婷香蕉 | www.日本在线 | 亚洲一级视频在线观看 | 成人亚洲一区二区三区 | 国产精品久久久久免费视频 | 欧美高清性色生活片免费观看 | v片在线免费观看 | 热re91久久精品国产99热 | 91网站入口 | 成人片网址 | 欧美日韩91 | 欧美日韩综合在线视频免费看 | 一区二区三区在线观看视频 | 天天碰天天干 | 色精品一区二区三区 | 日韩精品视频免费在线观看 | 日韩无毛 | 欧美三级网址 | 国产日产在线观看 | 日韩在线免费 | 91短视频免费在线观看 | 久久99精品久久久久久综合 | 性欧美tube 精品 | 亚洲电影免费观看高清完整版在线观 | 可以免费看的黄色片 | 亚洲另类天天更新影院在线观看 | 波多野结衣在线观看网址 | 欧美高清在线视频一区二区 | 亚洲人成在线观看一区二区 | 91中文在线观看 | 91免费大片| 亚洲日韩在线视频 | 亚洲一区国产二区 | 曰韩在线视频 | 亚洲欧美日韩高清一区二区三区 | 无码色情影片视频在线看免费 | 色诱成人免费观看视频 | 久久久99精品免费观看 |