This commit is contained in:
2020-10-09 16:34:33 +08:00
parent c6345a245c
commit 5eaf38b952
6 changed files with 261 additions and 3 deletions
+2 -2
View File
@@ -22,7 +22,7 @@ trait Action
* @return mixed
* @throws Exception
*/
public function restart($socket)
public function restart(\HttpServer\Server $socket)
{
$this->_shutdown($socket);
@@ -34,7 +34,7 @@ trait Action
* @param \HttpServer\Server $socket
* @throws Exception
*/
public function stop($socket)
public function stop(\HttpServer\Server $socket)
{
$this->_shutdown($socket);
}
+29
View File
@@ -0,0 +1,29 @@
<?php
namespace Kafka;
/**
* Interface ConsumerInterface
* @package App\Kafka
*/
interface ConsumerInterface
{
/**
* @param int $offset
* @param $part
* @param int $crc
* @param int $magic
* @param int $attr
* @param int $timestamp
* @param string $key
* @param string $value
* @return mixed
*/
public function onHandler(int $offset, $part, int $crc, int $magic, int $attr, int $timestamp, string $key, string $value);
}
+62
View File
@@ -0,0 +1,62 @@
<?php
namespace Kafka;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
use Swoole\Process;
use Snowflake\Abstracts\Config as SConfig;
/**
* Class Queue
* @package Queue
*/
class Kafka extends \Snowflake\Process\Process
{
/**
* @param Process $process
* @throws ConfigException
*/
public function onHandler(Process $process)
{
$kafka = SConfig::get('kafka');
$config = \Kafka\ConsumerConfig::getInstance();
$config->setMetadataRefreshIntervalMs(
$kafka['metadataRefreshIntervalMs'] ?? 1000
);
$config->setMetadataBrokerList($kafka['brokers']);
$config->setGroupId($kafka['groupId']);
$config->setBrokerVersion($kafka['version']);
$config->setTopics($kafka['topics']);
$consumer = new \Kafka\Consumer();
$consumer->setLogger(new Logger());
$consumer->start(function ($topic, $part, $message) {
$namespace = 'App\Kafka\\' . ucfirst($topic);
if (!class_exists($namespace) || !($namespace instanceof ConsumerInterface)) {
return;
}
try {
/** @var ConsumerInterface $class */
$class = Snowflake::createObject($namespace);
$class->onHandler(
$message['offset'],
$part,
$message['message']['crc'],
$message['message']['magic'],
$message['message']['attr'],
$message['message']['timestamp'],
$message['message']['key'],
$message['message']['value']
);
} catch (\Throwable $exception) {
$this->application->error($exception->getMessage());
}
});
}
}
+52
View File
@@ -0,0 +1,52 @@
<?php
namespace Kafka;
use Exception;
use HttpServer\Server;
use Snowflake\Abstracts\Config as SConfig;
use Snowflake\Abstracts\Providers;
use Snowflake\Application;
/**
* Class QueueProviders
* @package Queue
*/
class KafkaProviders extends Providers
{
/**
* @param Application $application
* @throws Exception
*/
public function onImport(Application $application)
{
/** @var Server $server */
$server = $application->get('server');
$kafka = SConfig::get('kafka');
if (empty($kafka) || !($kafka['enable'] ?? false)) {
return;
}
if (!isset($kafka['topics']) || empty($kafka['topics'])) {
throw new Exception('kafka configure error.');
}
if (!isset($kafka['version']) || empty($kafka['version'])) {
throw new Exception('kafka configure error.');
}
if (!isset($kafka['brokers']) || empty($kafka['brokers'])) {
throw new Exception('kafka configure error.');
}
if (!isset($kafka['groupId']) || empty($kafka['groupId'])) {
throw new Exception('kafka configure error.');
}
if (!is_array($kafka['topics'])) {
throw new Exception('Add kafka topics must is array.');
}
$server->addProcess('kafka', Kafka::class);
}
}
+114
View File
@@ -0,0 +1,114 @@
<?php
namespace Kafka;
use Psr\Log\LoggerInterface;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
/**
* Class Logger
* @package Kafka
*/
class Logger implements LoggerInterface
{
/**
* @param string $message
* @param array $context
*/
public function emergency($message, array $context = array())
{
// TODO: Implement emergency() method.
var_dump(func_get_args());
}
/**
* @param string $message
* @param array $context
* @throws ComponentException
*/
public function alert($message, array $context = array())
{
$logger = Snowflake::app()->getLogger();
$logger->debug($message);
}
public function critical($message, array $context = array())
{
// TODO: Implement critical() method.
var_dump(func_get_args());
}
/**
* @param string $message
* @param array $context
* @throws ComponentException
*/
public function error($message, array $context = array())
{
$logger = Snowflake::app()->getLogger();
$logger->error($message);
}
/**
* @param string $message
* @param array $context
* @throws ComponentException
*/
public function warning($message, array $context = array())
{
$logger = Snowflake::app()->getLogger();
$logger->warning($message);
}
/**
* @param string $message
* @param array $context
* @throws ComponentException
*/
public function notice($message, array $context = array())
{
$logger = Snowflake::app()->getLogger();
$logger->info($message);
}
/**
* @param string $message
* @param array $context
* @throws ComponentException
*/
public function info($message, array $context = array())
{
$logger = Snowflake::app()->getLogger();
$logger->info($message);
}
/**
* @param string $message
* @param array $context
* @throws ComponentException
*/
public function debug($message, array $context = array())
{
$logger = Snowflake::app()->getLogger();
$logger->debug($message);
}
/**
* @param $level
* @param $message
* @param array $context
* @throws ComponentException
*/
public function log($level, $message, array $context = array())
{
$logger = Snowflake::app()->getLogger();
$logger->debug($message);
}
}
+2 -1
View File
@@ -25,7 +25,8 @@
"ext-inotify": "*",
"ext-curl": "*",
"ext-openssl": "*",
"amphp/amp": "v1.2.2"
"amphp/amp": "v1.2.2",
"psr/log": "1.*"
},
"autoload": {
"psr-4": {