overblog/thrift-bundle

OverBlog Thrift Bundle

安装次数: 18,532

依赖者: 0

建议者: 0

安全性: 0

星标: 45

关注者: 31

分支: 14

开放问题: 1

类型:symfony-bundle

0.9.19 2019-05-09 06:15 UTC

README

Build Status

这个仓库是什么?

这是PHP的Thrift协议的定制版本。

有用的链接?

https://github.com/yuxel/thrift-examples

http://svn.apache.org/repos/asf/thrift/trunk/

获取包

Composer方式

将这个新依赖项添加到“require”部分更新你的composer json。

// composer.json
"require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.1.*",
    ...
    "overblog/thrift-bundle": "*" // insert this line
}

然后运行 composer update

设置包

让我们首先将包注册到AppKernel中

// app/AppKernel.php
public function registerBundles()
{
    $bundles = array(
        ...
        new Overblog\ThriftBundle\OverblogThriftBundle(),
        ...
    );

    return $bundles;
}

你现在可以创建你的Service.thrift文件,并将其放置在你自己的包中的ThriftDefinition目录中。

例如:MyNameSpace/MyBundle/ThriftDefinition/Service.thrift

你需要配置编译器以构建正确的文件。

#app/config/config.yml
  overblog_thrift:
    services:
      *service_name*:
        definition: Service
        namespace: ThriftModel\Service
        definitionPath: /PATH/ # Path of the definition
        server: true    # Define if server class will be generated

你现在可以使用 php app/console thrift:compile CompleteBundleName Service 生成模型。

模型将在缓存预热时自动生成(在 php app/console cache:warmup)你的缓存目录。

你还可以设置“protocol”选项

要使用服务器

创建你的处理器,它扩展 Overblog\ThriftBundle\Api\Extensions\BaseExtension 并实现 ThriftModel\Service\ServiceIf。然后将其注册到你的包中

#Bundle/Ressources/config/services.yml
    services:
      thrift.handler.service:
        class: BundleName\Handler\Service
        arguments: [@service_container]

将服务器配置添加到你的项目的config.yml中

#app/config/config.yml
    overblog_thrift:
      servers:
        *service_name*:
          service: *service_name*
          handler: thrift_api.processor.service

如果你想使用通过HTTP传输的Thrift,你必须将包的路由添加到你的项目中。

#app/config/routing.yml
    OverblogThriftBundle:
      resource: "@OverblogThriftBundle/Resources/config/routing.yml"
      prefix:   /

或者你可以使用命令启动套接字版本

php app/console thrift:server *service_name*

要使用客户端

HTTP客户端:将此添加到你的项目的config.yml

#app/config/config.yml
  overblog_thrift:
    clients:
      *client_name*:
        service: *service_name*:
        type: http
        hosts:
          comment:
            host: domain/thrift
            port: 80

套接字客户端:将此添加到你的项目的config.yml

#app/config/config.yml
  overblog_thrift:
    clients:
      *client_name*:
        service: *service_name*:
        type: socket
        hosts:
          *host_name*:
            host: localhost
            port: 9090

多套接字客户端:将此添加到你的项目的config.yml

#app/config/config.yml
  overblog_thrift:
    clients:
      *client_name*:
        service: *service_name*:
        type: socket
        hosts:
          *host_name*:
            host: localhost
            port: 9090
          *host_name_2*:
            host: localhost
            port: 9091

然后你可以调用客户端

// your_controller.php

$service = $this->getContainer()->get('thrift.client.*client_name*');
$client = $service->getClient();

$service = $service->getFactory('ThriftModel\Service\Service');
$service->property = 121354984651354647;
$service->name = 'Name 1';

$id = $client->execMethod($service);

贡献

测试

安装 phpunit

在包目录下

phpunit