博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
thrift 开发教程 - nick的日志 - 网易博客
阅读量:5778 次
发布时间:2019-06-18

本文共 7194 字,大约阅读时间需要 23 分钟。

thrift 开发教程   

2011-05-27 15:27:29|  分类: |  标签:  |字号  

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的一个异常。

转载地址:http://zduyx.baihongyu.com/

你可能感兴趣的文章
前端日拱一卒D6——字符编码与浏览器解析
查看>>
python学习笔记- 多线程
查看>>
换一种思维看待PHP VS Node.js
查看>>
举重若轻的人人车移动端数据平台
查看>>
Oracle回应用户锁定,自治数据库是更好选择
查看>>
深入理解浏览器的缓存机制
查看>>
使用 Swift 3.0 操控日期
查看>>
微软向Linux社区开放60000多项专利:对开源微软是认真的
查看>>
版本控制、Git及其在企业中的应用
查看>>
Ruby开发者已可通过Fog管理Microsoft Azure服务
查看>>
《Doing It - Management 3.0 Experiences》作者访谈
查看>>
基于 Bitbucket Pipeline + Amazon S3 的自动化运维体系
查看>>
Hoshin Kanri在丰田的应用
查看>>
又拍云沈志华:如何打造一款安全的App
查看>>
Nginx 学习书单整理
查看>>
克服大数据集群的挑战
查看>>
PostgreSQL并发控制(MVCC, 事务,事务隔离级别)
查看>>
12月19日一周一次【Python基础语法】
查看>>
DM***的第二阶段OSPF
查看>>
C# 列表 - List
查看>>