PHP code example of songyz / php-rabbit-client
1. Go to this page and download the library: Download songyz/php-rabbit-client library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
songyz / php-rabbit-client example snippets
namespace App\Providers;
use App\Services\RabbitConfig;
use Illuminate\Support\ServiceProvider;
use Songyz\Rabbit\RabbitManager;
class RabbitMqProvider extends ServiceProvider
{
public function boot()
{
$this->app->singleton(RabbitManager::class, function () {
return RabbitManager::getInstance(new RabbitConfig(), app('log'));
});
}
/**
* 获取提供者提供的服务
*
* @return array
*/
public function provides()
{
return [RabbitManager::class];
}
}
namespace App\Services;
use Songyz\Rabbit\Conf\Config;
class RabbitConfig extends Config
{
public function __construct()
{
$this->hosts = $this->initEnvironment();
}
/**
* 根据环境变量 设置不同的配置
* 此配置本身需要放在env中去维护,但考虑到env不支持数组,需要特定的格式去配置 较为繁琐
* 因此放在这里去做配置
* initEnvironment
* @return array|array[]
*
*/
private function initEnvironment()
{
$environment = app()->environment();
switch ($environment) {
case "development":
return [
[
'host' => "192.168.1.196",
'port' => 5672,
'user' => 'guest',
'password' => 'guest',
'vhost' => '/'
],
];
break;
case "test":
return [];
break;
case "pre-production":
return [
[
'host' => "192.168.1.198",
'port' => 5672,
'user' => 'guest',
'password' => 'guest',
'vhost' => '/'
],
];
break;
case "production":
return [
[
'host' => "192.168.1.199",
'port' => 5672,
'user' => 'guest',
'password' => 'guest',
'vhost' => '/'
],
];
break;
default:
return [];
break;
}
}
}
namespace App\Services;
use Songyz\Rabbit\Exchange\Exchange;
class PhpFirstExchange extends Exchange
{
/** @var string 交换机名称 */
protected $name = "php-f-exchange";
/** @var string
* 交换机路由key 可以为空 直连模式
* 交换机路由key topic 及 fanout 可指定对对应的key
*/
protected $routingKey = "";
}
$rabbitManager = app(RabbitManager::class);
$rabbit = $rabbitManager->rabbit();
$exchange = new PhpFirstExchange();
$message = new Message("test-php 2021-05-03 14:19:03");
$rabbit1 = $rabbit->publish($message, $exchange, '');
$rabbitManager = app(RabbitManager::class);
$rabbit = $rabbitManager->rabbit();
$exchange = new PhpFirstExchange();
$message = new Message("test-php 2021-05-03 14:19:03");
$rabbit1 = $rabbit->publish($message, $exchange, '', true);
namespace App\Services;
use Songyz\Rabbit\Message\Message;
use Songyz\Rabbit\RabbitManager;
class MqService
{
public function mq()
{
$rabbitManager = app(RabbitManager::class);
$rabbit = $rabbitManager->rabbit();
$exchange = new PhpFirstExchange();
$result = [];
for ($i = 0; $i <= 30; $i++) {
array_push($result, new Message(json_encode(['content' => "test-php" . microtime(true)])));
}
$rabbit->publishMulti($result, $exchange, '');
}
}
namespace App\Services;
use Songyz\Rabbit\Message\Message;
use Songyz\Rabbit\RabbitManager;
class MqService
{
public function mq()
{
$rabbitManager = app(RabbitManager::class);
$rabbit = $rabbitManager->rabbit();
$exchange = new PhpFirstExchange();
$result = [];
for ($i = 0; $i <= 30; $i++) {
array_push($result, new Message(json_encode(['content' => "test-php" . microtime(true)])));
}
$rabbit->publishMulti($result, $exchange, '', true);
}
}
namespace App\Console\Commands;
use App\Models\HealthRecord;
use Songyz\Rabbit\Command\RabbitCommand;
use Songyz\Rabbit\Message\ResponseMessage;
class MqConsumer extends RabbitCommand
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'mq:xf';
/**
* The console command description.
*
* @var string
*/
protected $description = '脚本消费';
/** @var string 要消费的队列 */
protected $queue = 'php-f-queue3';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
parent::handle();
return 0;
}
public function consume(ResponseMessage $message): bool
{
$this->line("消费者标签:{$message->getDeliveryTag()} | 信息:" . $message->getBody());
$body = json_decode($message->getBody(), true);
// if (! empty($body) && is_array($body)) {
return false;
// }
//处理你的业务逻辑
//如果成功 则返回 true 失败就返回 false 默认会重试三次
return true;
}
}