This commit is contained in:
as2252258@163.com
2021-07-20 01:56:04 +08:00
parent d5cc4c2d64
commit 46bcbb4f4b
2 changed files with 180 additions and 197 deletions
+12 -27
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 Server\SInterface\CustomProcess;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Runtime; use Snowflake\Runtime;
@@ -23,55 +24,41 @@ use Throwable;
* Class Queue * Class Queue
* @package Queue * @package Queue
*/ */
class Kafka extends \Snowflake\Process\Process class Kafka implements CustomProcess
{ {
protected Channel $channel; protected Channel $channel;
private int $maxLength = 5000;
/** /**
* @var array * Kafka constructor.
* @param array $kafkaConfig
*/ */
private array $kafkaConfig = []; public function __construct(public array $kafkaConfig)
{
}
/** /**
* @return string * @return string
* @throws ConfigException * @throws ConfigException
*/ */
public function getProcessName(): string public function getProcessName(Process $process): string
{ {
$this->kafkaConfig = swoole_unserialize($this->read()); $name = Config::get('id', 'system') . '[' . $process->pid . ']';
$name = Config::get('id', 'system') . '[' . $this->pid . ']';
return $name . '.' . 'Kafka Consumer ' . $this->kafkaConfig['topic']; return $name . '.' . 'Kafka Consumer ' . $this->kafkaConfig['topic'];
} }
/**
* @param Process $process
* @throws \Exception
*/
public function before(Process $process): void
{
$content = System::readFile(storage(Runtime::CACHE_NAME));
$annotation = Snowflake::app()->getAnnotation();
$annotation->setLoader(unserialize($content));
$annotation->runtime(KAFKA_PATH);
}
/** /**
* @param Process $process * @param Process $process
* @throws \Exception * @throws \Exception
*/ */
public function onHandler(Process $process): void public function onHandler(Process $process): void
{ {
$this->waite($this->kafkaConfig); $this->waite($process, $this->kafkaConfig);
} }
@@ -79,11 +66,9 @@ class Kafka extends \Snowflake\Process\Process
* @param array $kafkaServer * @param array $kafkaServer
* @throws \Exception * @throws \Exception
*/ */
private function waite(array $kafkaServer) private function waite(Process $process, array $kafkaServer)
{ {
try { try {
name($this->pid, 'Kafka Consumer ' . $kafkaServer['topic']);
[$config, $topic, $conf] = $this->kafkaConfig($kafkaServer); [$config, $topic, $conf] = $this->kafkaConfig($kafkaServer);
if (empty($config) && empty($topic) && empty($conf)) { if (empty($config) && empty($topic) && empty($conf)) {
return; return;
+2 -4
View File
@@ -35,14 +35,12 @@ class KafkaProviders extends Providers
if (!extension_loaded('rdkafka')) { if (!extension_loaded('rdkafka')) {
return; return;
} }
$kafkaServers = Config::get('kafka.consumers', []); $kafkaServers = Config::get('kafka.consumers', []);
if (empty($kafkaServers)) { if (empty($kafkaServers)) {
return; return;
} }
foreach ($kafkaServers as $kafkaServer) {
foreach ($kafkaServers as $index => $kafkaServer) { $server->addProcess(new Kafka($kafkaServer));
$server->addProcess('kafka_' . $index, Kafka::class, $kafkaServer);
} }
} }