大蒜/bus

消息总线捆绑包用于通过消息总线(默认为RabbitMQ)在微服务之间进行通信

安装次数: 2,242

依赖项: 3

建议者: 0

安全: 0

星标: 6

关注者: 8

分支: 0

开放问题: 1

类型:symfony-bundle


README

此捆绑包通过提供消息总线,允许微服务通过RabbitMQ传输相互通信。

为了正确使用,此捆绑包必须在通信的两端安装(当前和目标服务)

安装

此捆绑包工作所需的只是一些东西。

将大蒜/bus捆绑包添加到您的composer.json

composer require garlic/bus

作为守护进程运行处理器(将配置添加到您的supervisor.conf)

[program:garlic_communication]
command=/var/www/bin/console --env=prod --no-debug enqueue:consume --setup-broker
process_name=%(program_name)s_%(process_num)02d
numprocs=4
autostart=true
autorestart=true
tartsecs=0
user=www-data
redirect_stderr=true

现在您可以使用大蒜总线

可选:您可以为等待响应设置超时

设置变量 REQUEST_TIMEOUT=30,其中30是秒数

用法

常用的方法

如果您想从当前服务获取响应,您必须使用“request”方法,如下所述

$data = $this->get('communicator') // Or you can call by class name. Example: $this->get(GarlicBus:class)
    ->request('targetServiceName') // Type of message. So far you can use ->request() or ->command() methods. Command provide mesage type that not need response. 
    ->post()                       // Set one of REST methods (post, put, delete). Bu default set GET 
    ->targetServiceAction(         // CamelCased route where slashes vere changed to upper letter by magic (example: getUser will changed to /get/user)
        array $path = [],          // Path parameters to query (example: ['user' => 1])
        array $query = [],         // Post or Get parameters to the query
        array $headers = []        // Additional headers
    );
    

或相同的,但使用直接路由作为方法参数

$data = $this->get('communicator')
    ->request('targetServiceName')
    ->send(
        string $route, // Route to the target service action (example: /user/get)
        array $path = [], 
        array $query = [],
        array $headers = [] 
    );
    

使用并行处理进行异步批处理请求

$data = $this->get('communicator')
    ->pool(
        'service1', // Target service name
        '/',        // Route to the target service action (example: /user/get)
        [],         // Path parameters to query
        [],         // Post or Get parameters to the query
        []          // Request headers
    )
    ->pool(
        'service1', // Target service name
        '/',        // Route to the target service action (example: /user/get)
        [],         // Path parameters to query
        [],         // Post or Get parameters to the query
        []          // Request headers
        )
    ->fetch();      // Get response from async queries pool

处理文件

确保您向.env添加变量

### host url, will be used by another service to get files from current service
HOST_URL=172.18.1.14

###files will be uploaded to this dir
UPLOAD_DIR = public/upload

###should be same for every service using same bus
SCP_USERNAME=www-data
SCP_PASSWORD=KJLgbJ32PIHDJU4

从请求上传文件

 $handler = $this->get('Garlic\Bus\Service\File\FileHandlerService');
 $handler->handleFiles($_FILES['pictures']);

从另一个服务获取文件

 $uploader = $this->get('Garlic\Bus\Service\File\ScpFileUploadService');
 $uploader->getFile(['host_url' => '172.18.0.1','origin_name' => '1.jpg','path' => 'public/upload/fsdljkahb.jpg']);