• thrift服务端到客户端开发简单示例


    (1)首先我们在服务器端写个helloworld.thrift文件,如下所示:

    service HelloWorld{
    string ping(1: string name),
    string getpng(),
    }

    (2)在服务器端编译helloworld.thrift
    编译helloworld.thrift文件,会产生服务器端和客户端相应语言的接口源码。
    /usr/local/thrift/bin/thrift -r --gen py helloworld.thrift
    /usr/local/thrift/bin/thrift -r --gen php helloworld.thrift
    #会在当前目录下生成 gen-* 目录。
    产生的gen-py目录放在服务器上,产生的gen-php目录放在客户端上。


    (3)编写服务器端代码

    import sys
    sys.path.append('./gen-py')

    from helloworld import HelloWorld
    from helloworld.ttypes import *

    from thrift.transport import TSocket
    from thrift.transport import TTransport
    from thrift.protocol import TBinaryProtocol
    from thrift.server import TServer

    class HellowordHandler:
    def __init__ (self):
    pass

    def ping (self, name):
    print name + ' from server.'
    return "%s from server." % name
    def getpng (self):
    f = open("./logo.png", "rb")
    c = f.read()
    f.close()
    return c
    handler = HellowordHandler()
    processor = HelloWorld.Processor(handler)
    transport = TSocket.TServerSocket(9090)
    tfactory = TTransport.TBufferedTransportFactory()
    pfactory = TBinaryProtocol.TBinaryProtocolFactory()

    server = TServer.TSimpleServer(processor, transport, tfactory, pfactory)

    # You could do one of these for a multithreaded server
    #server = TServer.TThreadedServer(processor, transport, tfactory, pfactory)
    #server = TServer.TThreadPoolServer(processor, transport, tfactory, pfactory)

    print 'Starting the server...'
    server.serve()
    print 'done.'




    (4)编写客户端代码
    先将gen-php目录拷贝到客户端上。

    <?php
    try{
    //包含thrift客户端库文件
    $GLOBALS['THRIFT_ROOT'] = '/root/thrift-0.9.0/lib/php/lib/Thrift';
    require_once '/root/thrift-0.9.0/lib/php/src/Thrift.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Protocol/TBinaryProtocol.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Transport/TSocket.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Transport/THttpClient.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Transport/TBufferedTransport.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Type/TType.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Type/TMessageType.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Factory/TStringFuncFactory.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/StringFunc/TStringFunc.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/StringFunc/Core.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Exception/TException.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Exception/TProtocolException.php';
    require_once $GLOBALS['THRIFT_ROOT'].'/Exception/TTransportException.php';

    error_reporting(E_NONE);

    //包含helloworld接口文件
    $GEN_DIR = './gen-php';
    require_once $GEN_DIR.'/helloworld/HelloWorld.php';

    error_reporting(E_ALL);

    $socket = new TSocket('*.*.*.*', 9090);
    $transport = new TBufferedTransport($socket, 1024, 1024);
    $protocol = new TBinaryProtocol($transport);
    $client = new HelloWorldClient($protocol);

    $transport->open();

    $a = $client->ping('xyq ');
    echo $a;

    $transport->close();

    }catch(TException $tx){
    print 'TException: '.$tx->getMessage()."/n";
    }

    ?>

  • 相关阅读:
    路径
    aspx.cs调用js方法
    计算字符串中每种字母出现的次数
    [转载]获取2个集合List<T>的共同元素
    [转载]行列转换
    [转载]UNION合并查询
    [转载]Case When Then
    [转载]CAST-CONVERT两个函数进行类型转换时的异同点
    [转载]404错误页面设置
    [转载]JQ小知识
  • 原文地址:https://www.cnblogs.com/wangxusummer/p/4118708.html
Copyright © 2020-2023  润新知