改名
This commit is contained in:
+30
-4
@@ -9,8 +9,11 @@ use RdKafka\ProducerTopic;
|
|||||||
use RdKafka\TopicConf;
|
use RdKafka\TopicConf;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Snowflake\Abstracts\Component;
|
use Snowflake\Abstracts\Component;
|
||||||
|
use Snowflake\Abstracts\Config;
|
||||||
|
use Snowflake\Core\Json;
|
||||||
use Snowflake\Event;
|
use Snowflake\Event;
|
||||||
use Snowflake\Exception\ComponentException;
|
use Snowflake\Exception\ComponentException;
|
||||||
|
use Snowflake\Exception\ConfigException;
|
||||||
use Snowflake\Exception\NotFindClassException;
|
use Snowflake\Exception\NotFindClassException;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
@@ -37,6 +40,10 @@ class Producer extends Component
|
|||||||
private ?\RdKafka\Producer $producer = null;
|
private ?\RdKafka\Producer $producer = null;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Producer constructor.
|
||||||
|
* @param array $config
|
||||||
|
*/
|
||||||
public function __construct($config = [])
|
public function __construct($config = [])
|
||||||
{
|
{
|
||||||
parent::__construct($config);
|
parent::__construct($config);
|
||||||
@@ -81,13 +88,33 @@ class Producer extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $topic
|
||||||
|
* @param array $params
|
||||||
|
* @param string|null $groupId
|
||||||
|
* @throws NotFindClassException
|
||||||
|
* @throws ReflectionException
|
||||||
|
* @throws ConfigException
|
||||||
|
*/
|
||||||
|
public function dispatch(string $topic, array $params = [], string $groupId = null)
|
||||||
|
{
|
||||||
|
if ($groupId === null || empty($groupId)) {
|
||||||
|
$consumers = Config::get('kafka.consumers.' . $topic);
|
||||||
|
if (empty($consumers)) {
|
||||||
|
$consumers = ['groupId' => $topic . ':' . Snowflake::localhost()];
|
||||||
|
}
|
||||||
|
$groupId = $consumers['groupId'];
|
||||||
|
}
|
||||||
|
$this->setGroupId($groupId)->setTopic($topic)->delivery(Json::encode($params));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $message
|
* @param $message
|
||||||
* @param null $key
|
* @param null $key
|
||||||
* @param bool $isAck
|
* @param bool $isAck
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException
|
* @throws ReflectionException
|
||||||
* @throws ComponentException
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function delivery($message, $key = null, $isAck = false)
|
public function delivery($message, $key = null, $isAck = false)
|
||||||
@@ -114,18 +141,17 @@ class Producer extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** @var ProducerTopic[] $topics */
|
/** @var ProducerTopic[] $topics */
|
||||||
private array $topics = [];
|
private array $topics = [];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $message
|
* @param $message
|
||||||
* @param $key
|
* @param $key
|
||||||
*/
|
*/
|
||||||
private function push($message, $key)
|
private function push($message, $key)
|
||||||
{
|
{
|
||||||
if(!isset($this->topics[$this->_topic])){
|
if (!isset($this->topics[$this->_topic])) {
|
||||||
$this->topics[$this->_topic] = $this->producer->newTopic($this->_topic, $this->topicConf);
|
$this->topics[$this->_topic] = $this->producer->newTopic($this->_topic, $this->topicConf);
|
||||||
}
|
}
|
||||||
$this->topics[$this->_topic]->produce(RD_KAFKA_PARTITION_UA, 0, $message, $key);
|
$this->topics[$this->_topic]->produce(RD_KAFKA_PARTITION_UA, 0, $message, $key);
|
||||||
|
|||||||
Reference in New Issue
Block a user