IT技术博客大学习 共学习 共进步
全部 移动开发 后端 数据库 AI 算法 安全 DevOps 前端 设计 开发者

php实现的thrift socket server

某人的栖息地 2010-09-15 09:48:45 累计浏览 4,849 次
本机暂存

这些天用php写了个thrift的socket server,因为原来thrift的源码里php部分只有基于apache的服务器端代码,再加上前些日子看到php也能直接使用libevent构建web服务器,所以才会想到写这个玩玩。

php-thrift-server源码

代码直接从apache的thrift项目clone过来,托管在github上:

http://github.com/volca/thrift

新增或改动的代码如下:

    lib/php/
    `-- src
        |-- server
        |   |-- TNonblockingServer.php
        |   `-- TServer.php
        `-- transport
            |-- TNonblockingServerSocket.php
            |-- TNonblockingSocket.php
            |-- TServerSocket.php
            |-- TServerTransport.php
    test/php
    |-- TestClient.php
    |-- TestNonblockingServer.php

使用示例

获取thrift的源码,并编译出thrift工具,编译过程请搜索

git clone git://github.com/volca/thrift.git

安装php,以及apc, libevent扩展:

pecl install apc
#需要先libevent-devel之类的包包
pecl install libevent

运行php的socket服务器,我直接从thrift的test代码中修改了一个独立运行的php server,见thrift/test/php/TestNonblockingServer.php,这里也包含一个测试业务代码的实现。

cd thrift/test/php
#用thrift命令行工具生成php的测试类库
make
#启动thrift服务,会监听本机的9090端口
php TestNonblockingServer.php

客户端的代码也一并提供,对各种数据类型比如int, float, string, list等等进行测试。

php TestClient.php

性能测试

apache + php的测试结果
testVoid() = void
testString("Test") = "Test"
testByte(1) = 1
testI32(-1) = -1
testI64(-34359738368) = -34359738368
testDouble(-852.234234234) = -852.234234234
testStruct({"Zero", 1, -3, -5}) = {"Zero", 1, -3, -5}
testNest({1, {"Zero", 1, -3, -5}), 5} = {1, {"Zero", 1, -3, -5}, 5}
testMap({0 => -10, 1 => -9, 2 => -8, 3 => -7, 4 => -6}) = {0 => -10, 1 => -9, 2 => -8, 3 => -7, 4 => -6}
testSet({-2, -1, 0, 1, 2}) = {1, 1, 1, 1, 1}
testList({-2, -1, 0, 1, 2}) = {-2, -1, 0, 1, 2}
testEnum(ONE) = 1
testEnum(TWO) = 2
testEnum(THREE) = 3
testEnum(FIVE) = 5
testEnum(EIGHT) = 8
testTypedef(309858235082523) = 309858235082523
Total time: 41 ms

php + libevent的socket server测试结果
testVoid() = void
testString("Test") = "Test"
testByte(1) = 1
testI32(-1) = -1
testI64(-34359738368) = -34359738368
testDouble(-852.234234234) = -852.234234234
testStruct({"Zero", 1, -3, -5}) = {"Zero", 1, -3, -5}
testNest({1, {"Zero", 1, -3, -5}), 5} = {1, {"Zero", 1, -3, -5}, 5}
testMap({0 => -10, 1 => -9, 2 => -8, 3 => -7, 4 => -6}) = {0 => -10, 1 => -9, 2 => -8, 3 => -7, 4 => -6}
testSet({-2, -1, 0, 1, 2}) = {1, 1, 1, 1, 1}
testList({-2, -1, 0, 1, 2}) = {-2, -1, 0, 1, 2}
testEnum(ONE) = 1
testEnum(TWO) = 2
testEnum(THREE) = 3
testEnum(FIVE) = 5
testEnum(EIGHT) = 8
testTypedef(309858235082523) = 309858235082523
Total time: 8 ms

这个测试中,没有耗时很长的请求,处理逻辑完全一样,php socket server耗时仅为apache + php的五分之一。

thrift是什么?

thrift流传的似乎不是太广泛,而且有被别的技术替代的趋势,所以下面还是引用一下别的文章的介绍:

Thrift由一个软件库和一系列的代码生成工具组成,由 Facebook开发。目的是为了加快软件开发和实现高效和可扩展的后台服务。主要目标是不同程序开语言之间实现高效和可靠的通信,这需要将不同语言之间抽象出一个通用层,然后由不同语言来实现这个通用层。在这里要特别指出的是,Thrift允许开发人员定义数据类型和服务接口(定义在一个中性语言文件里),并通过这个文件生成构建RPC客户端和服务端所需的代码。

简单分析其机理,Thrift就是实现C/S模式,通过代码生成工具将接口定义文件生成服务器端和客户端代码(可以为不同语言),从而实现服务端和客户端跨语言的支持。

Thrift可以分为传输层和协议层:

传输层定义了数据的传输方式,可以为TCP/IP传输,内存共享或者文件共享等形式;
协议层定义了数据的传输格式,可以为二进制流或者XML等形式。
当服务器端使用socket协议时,可以用simple|thread-pool|threaded|nonblocking等方式运行,从而获得更好的性能。

同分类推荐文章

  1. 等了十年的 Go 链式管道,终于来了:seq 让你像写 Scala 一样写 Go (2026-06-25 18:38:18)
  2. Go 实验特性详解 (2026-06-21 10:05:27)
  3. amd64 微架构级别对 Go 程序性能提升多少? (2026-06-21 09:38:49)

查看更多 后端 文章 →

建议继续学习

  1. 使用gettext来支持PHP的多语言 (累计阅读 39,268)
  2. WordPress插件开发 -- 在插件使用数据库存储数据 (累计阅读 29,163)
  3. Linux大棚版Thrift入门教程 (累计阅读 24,504)
  4. Paypal接口详细代码(PHP版,非API接口) (累计阅读 19,407)
  5. 我的PHP,Python和Ruby之路 (累计阅读 13,146)
  6. include(“./file.php”)和include(“file.php”)区别 (累计阅读 12,788)
  7. 15个最好的免费开源电子商务平台 (累计阅读 12,541)
  8. Redis消息队列的若干实现方式 (累计阅读 12,087)
  9. 到底什么是MVC? (累计阅读 11,865)
  10. 整理了一份招PHP高级工程师的面试题 (累计阅读 11,708)