CentOS7.0安装thrift

  1. 安装前的准备1

     yum install \
     flex \
     bison \
     libtool \
     gcc-c++ \
     automake \
     pkgconfig \
     zlib-devel \
     ruby-devel \
     boost-devel \
     python-devel \
     openssl-devel \
     libevent-devel \
     -y \
    
  2. 安装前的准备2

     cd /root
     wget http://sourceforge.net/projects/boost/files/boost/1.53.0/boost_1_53_0.tar.gz
     tar -zxvf boost_1_53_0.tar.gz
     cd /root/boost_1_53_0/
     /root/boost_1_53_0/bootstrap.sh
     /root/boost_1_53_0/bz2
     cp -r /root/boost_1_53_0 /usr/local/boost
     cd /usr/local/boost/
     ln -s /usr/local/boost/stage/lib/libboost_unit_test_framework.a /usr/lib64/libboost_unit_test_framework.a
    
  3. 安装thrift

     wget http://mirror.bit.edu.cn/apache/thrift/0.10.0/thrift-0.10.0.tar.gz
     tar -zxvf thrift-0.10.0.tar.gz
     cd thrift-0.10.0/
     ./configure
     make
     make install
    
  4. 检查安装是否成功

     thrift -version
    
  5. 创建一个目录

     mkdir test
     mkdir test/Server
     mkdir test/Client
     cd test
    
  6. 创建thrift文件源码

     # 为了体现跨语言的特性,这里使用php和js分别实现客户端
     vim services.thrift
     namespace js  TestThrift
     namespace php TestThrift
     service TestService {
         string index(1:string content)
     }
     ESC
     :wq
    
  7. 生成客户端、服务端文件

     thrift -r --gen php:psr4        -out Client services.thrift
     thrift -r --gen php:psr4,server -out Server services.thrift
    
  8. 初始化composer并安装apache/thrift

     # 客户端
     cd Client
     composer init
     composer config repo.packagist composer https://packagist.phpcomposer.com
     composer require apache/thrift
     # 服务端
     cd Server
     composer init
     composer config repo.packagist composer https://packagist.phpcomposer.com
     composer require apache/thrift
    
  9. 修改客户端、服务端composer.json

     # 客户端根花括号添加以下代码
     cd Client
     "autoload":{
         "psr-4":{
             "TestThrift\\":["TestThrift/"]
         }
     },
     # 服务端根花括号添加以下代码
     cd Server
     "autoload":{
         "psr-4":{
             "TestThrift\\":["TestThrift/"]
         }
     },
    
  10. 客户端、服务端自动加载生成的类文件

     cd Client
     composer dump-autoload -o
     cd Server
     composer dump-autoload -o
    
  11. 实现接口Interface

     cd Server/TestThrift;
     vim TestService.php
     <?php
     namespace TestThrift;
     class TestService implements TestServiceIf
     {
         public function index($content)
         {
             return $content;
         }
     }
     ESC
     :wq
    
  12. 现在开始写服务端代码server_http.php

     cd Server
     vim server.php
     <?php
     require_once __DIR__ . '/vendor/autoload.php';
     use TestThrift\TestService;
     use TestThrift\TestServiceProcessor;
     use Thrift\Transport\TPhpStream;
     use Thrift\Transport\TBufferedTransport;
     use Thrift\Protocol\TBinaryProtocol;
     header('Content-Type', 'application/x-thrift');
     $handler = new TestService();
     $processor = new TestServiceProcessor($handler);
     $transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
     $protocol = new TBinaryProtocol($transport, true, true);
     $transport->open();
     $processor->process($protocol, $protocol);
     $transport->close();
     ESC
     :wq
    
  13. 现在开始写客户端代码client_http.php

     cd Client
     vim client.php
     <?php
     require_once __DIR__ . '/vendor/autoload.php';
     use Thrift\Transport\TSocket;
     use Thrift\Transport\THttpClient;
     use Thrift\Transport\TBufferedTransport;
     use Thrift\Protocol\TBinaryProtocol;
     use TestThrift\TestServiceClient;
     if (array_search('--http', $argv)) {
         $socket = new THttpClient('127.0.0.1', 8080, '/server_http.php');
     } else {
         $socket = new TSocket('localhost', 9090);
     }
     $transport = new TBufferedTransport($socket, 1024, 1024);
     $protocol = new TBinaryProtocol($transport);
     $client = new TestServiceClient($protocol);
     $content = $argv[1];
     $transport->open();
     echo $client->index($content);
     $transport->close();
     ESC
     :wq
    
  14. 以web形式运行服务端代码

     cd Server
     php -S 0.0.0.0:8080 -t .
     # 如需在node客户端运行需要配置真实web环境
    
  15. 通过客户端访问服务

     cd Client
     php client_http.php
    
  16. 现在开始写服务端代码server_socket.php

    参考链接 ``` cd Server vim server.php

    <?php requireonce _DIR . '/vendor/autoload.php'; use TestThrift\TestService; use TestThrift\TestServiceProcessor; use Transport\TSwooleFramedTransport; //use Transport\TSwooleBufferedTransport; use Thrift\Protocol\TBinaryProtocol; $server = new \swoole_server("0.0.0.0", 9999); $server_config = [

     'worker_num' => 1,          //worker数量
     'daemonize' => false,      //worker数量
    

    ]; $server->set($server_config); $server->on('workerStart', function () {

     echo "ThriftServer Start\n";
    

    }); $server->on('receive', function ($server, $fd, $from_id, $data) {

     $handler = new TestService();
     $socket = new TSwooleFramedTransport();
     $socket->setHandle($fd);
     $socket->buffer = $data;
     $socket->server = $server;
     $processor = new TestServiceProcessor($handler);
     $protocol = new TBinaryProtocol($socket, false, false);
     try {
         $processor->process($protocol, $protocol);
     } catch (\Exception $e) {
         echo 'CODE:' . $e->getCode() . ' MESSAGE:' . $e->getMessage() . "\n" . $e->getTraceAsString();
     }
    

    }); $server->start();

    ESC :wq ```

  17. 现在开始写客户端代码client_socket.php

     cd Client
     vim client.php
    
     <?php
     require_once __DIR__ . '/vendor/autoload.php';
     use TestThrift\TestServiceClient;
     use Thrift\Transport\TSocket;
     use Thrift\Transport\TFramedTransport;
     use Thrift\Protocol\TBinaryProtocol;
     $socket = new TSocket('127.0.0.1', 9999);
     $transport = new TFramedTransport($socket);
     $protocol = new TBinaryProtocol($transport);
     $client = new TestServiceClient($protocol);
     $transport->open();
     $result = $client->index('这是内容');
     echo $result;
     $transport->close();
    
     ESC
     :wq
    
  18. 运行服务端代码

     cd Server
     php server_socket.php
    
  19. 通过客户端访问服务

     cd Client
     php client_socket
    

通过nodejs实现客户端

  1. 安装node.js 8.9.x

  2. 创建运行目录

     mkdir test/JsClient
     thrift -r --gen js:node -out JsClient services.thrift
    
  3. 初始化

     cd JsClient
     npm init
     npm install thrift
    
  4. 以websocket形式访问的代码jsclient_socket.js

     cd JsClient
     vim jsclient_http.js
     let thrift = require("thrift");
     let ttypes = require("./services_types.js");
     let test =  require("./TestService.js");
     let transport = thrift.TBufferedTransport;
     let protocol = thrift.TStringProtocol;
     let connection = thrift.createConnection('127.0.0.1', 8080, {
         transport: transport,
         protocol: protocol
     });
     let client = thrift.createClient(test, connection);
     client.index('这是参数content呦',function(err, msg){
         if(err){
             console.log(err);
         } else {
             console.log(msg);
         }
         connection.end();
     });
     ESC
     :wq
    

    参考链接

@耿志环 2012-∞ 冀ICP备17033181号, powered by Gitbook修订: 2019-07-08 16:49:59

results matching ""

    No results matching ""