This commit is contained in:
as2252258@163.com
2021-04-11 17:18:54 +08:00
parent 364c638d81
commit 6fbb0dea76
4 changed files with 245 additions and 134 deletions
+11 -1
View File
@@ -4,6 +4,9 @@
namespace Annotation; namespace Annotation;
use Kafka\ConsumerInterface;
use Kafka\TaskContainer;
/** /**
* Class Kafka * Class Kafka
* @package Annotation * @package Annotation
@@ -16,7 +19,7 @@ namespace Annotation;
* Kafka constructor. * Kafka constructor.
* @param string $topic * @param string $topic
*/ */
public function __construct(public string $topic, public string $groupId, public ?string $brokers = null) public function __construct(public string $topic)
{ {
} }
@@ -28,6 +31,13 @@ namespace Annotation;
*/ */
public function execute(array $handler): mixed public function execute(array $handler): mixed
{ {
if (!($handler[0] instanceof ConsumerInterface)) {
return false;
}
$container = TaskContainer::getInstance();
$container->addConsumer($this->topic, [$handler[0], 'onHandler']);
return parent::execute($handler); // TODO: Change the autogenerated stub return parent::execute($handler); // TODO: Change the autogenerated stub
} }
+44 -2
View File
@@ -7,6 +7,9 @@ namespace HttpServer\Events;
use Annotation\Loader; use Annotation\Loader;
use Exception; use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use Kafka\ConsumerInterface;
use Kafka\Struct;
use Kafka\TaskContainer;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\ComponentException; use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -28,8 +31,47 @@ class OnPipeMessage extends Callback
*/ */
public function onHandler(Server $server, int $src_worker_id, $message) public function onHandler(Server $server, int $src_worker_id, $message)
{ {
$events = Snowflake::app()->getEvent(); try {
$events->trigger(Event::PIPE_MESSAGE, [$server, $src_worker_id, $message]); $swoole_unserialize = swoole_unserialize($message);
match ($swoole_unserialize['action'] ?? null) {
'kafka' => $this->onKafkaWorker($swoole_unserialize),
default => $this->onMessageWorker($server, $src_worker_id, $message)
};
} catch (\Throwable $exception) {
$this->addError($exception);
} finally {
fire(Event::SYSTEM_RESOURCE_RELEASES);
}
} }
/**
* @param $server
* @param $src_worker_id
* @param $message
* @throws \Exception
*/
private function onMessageWorker($server, $src_worker_id, $message)
{
fire(Event::PIPE_MESSAGE, [$server, $src_worker_id, $message]);
return 'success';
}
/**
* @param array $message
* @throws \ReflectionException
* @throws \Snowflake\Exception\NotFindClassException
*/
private function onKafkaWorker(array $message)
{
[$topic, $message] = $message['body'];
$container = TaskContainer::getInstance();
$container->process($topic, new Struct($topic, $message));
return 'success';
}
} }
+7 -10
View File
@@ -10,6 +10,7 @@ use RdKafka\ConsumerTopic;
use RdKafka\Exception; use RdKafka\Exception;
use RdKafka\KafkaConsumer; use RdKafka\KafkaConsumer;
use RdKafka\TopicConf; use RdKafka\TopicConf;
use Snowflake\Core\Json;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Coroutine\Channel; use Swoole\Coroutine\Channel;
use Swoole\Process; use Swoole\Process;
@@ -99,17 +100,13 @@ class Kafka extends \Snowflake\Process\Process
{ {
go(function () use ($topic, $message) { go(function () use ($topic, $message) {
try { try {
$topic = str_replace('-', '_', $topic); $server = Snowflake::app()->getSwoole();
$namespace = 'App\\Kafka\\' . ucfirst($topic) . 'Consumer'; $setting = $server->setting['worker_num'];
if (!class_exists($namespace)) {
return; $message = swoole_serialize(['action' => 'kafka', 'body' => [$topic, $message]]);
}
$class = Snowflake::createObject($namespace); $server->sendMessage($message, random_int(0, $setting));
if (!($class instanceof ConsumerInterface)) {
return;
}
$class->onHandler(Snowflake::createObject(Struct::class, [$topic, $message]));
} catch (Throwable $exception) { } catch (Throwable $exception) {
logger()->addError($exception, 'throwable'); logger()->addError($exception, 'throwable');
} }
+62
View File
@@ -0,0 +1,62 @@
<?php
namespace Kafka;
use Snowflake\Abstracts\BaseObject;
/**
* Class TaskContainer
* @package Kafka
*/
class TaskContainer extends BaseObject
{
private array $_topics = [];
private static TaskContainer $container;
/**
* @return \Kafka\TaskContainer
*/
public static function getInstance(): TaskContainer
{
if (!(static::$container instanceof TaskContainer)) {
static::$container = new TaskContainer();
}
return static::$container;
}
/**
* @param $topic
* @param $handler
*/
public function addConsumer($topic, $handler)
{
if (isset($this->_topics[$topic])) {
return;
}
$this->_topics[$topic] = $handler;
}
/**
* @param $topic
* @param \Kafka\Struct $struct
*/
public function process($topic, Struct $struct)
{
$handler = $this->_topics[$topic] ?? null;
if (empty($handler)) {
return;
}
call_user_func($handler, $struct);
}
}