Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ryan1068/yii2-kafka
Yii2 kafka 组件
https://github.com/ryan1068/yii2-kafka
Last synced: 22 days ago
JSON representation
Yii2 kafka 组件
- Host: GitHub
- URL: https://github.com/ryan1068/yii2-kafka
- Owner: ryan1068
- License: bsd-3-clause
- Created: 2021-04-22T07:16:56.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-04-23T07:10:09.000Z (over 3 years ago)
- Last Synced: 2024-10-01T14:24:44.897Z (about 1 month ago)
- Language: PHP
- Homepage:
- Size: 22.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# yii2-kafka
### 通过docker安装kafka,zookeeper服务
#### docker-compose配置:
```php
### ZooKeeper #########################################
zookeeper:
build: ./zookeeper
volumes:
- ${DATA_PATH_HOST}/zookeeper/data:/data
- ${DATA_PATH_HOST}/zookeeper/datalog:/datalog
ports:
- "${ZOOKEEPER_PORT}:2181"
networks:
- backend### kafka ####################################################
kafka:
image: wurstmeister/kafka
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ADVERTISED_HOST_NAME: 192.168.0.1
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.0.1:9092
KAFKA_MESSAGE_MAX_BYTES: 2000000
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
volumes:
- ./kafka:/kafka
- /var/run/docker.sock:/var/run/docker.sock
networks:
- backend
### kafka-manager ####################################################
kafka-manager:
image: sheepkiller/kafka-manager
ports:
- 9020:9000
environment:
ZK_HOSTS: zookeeper:2181
networks:
- backend
```#### Yii2配置:
Config:
```php
'components' => [
'kafka' => [
'class' => yii\kafka\Producer::class,
'as log' => yii\kafka\KafkaBehavior::class,
],
]
```Params:
```php
'params' => [
'kafka' => [
'broker_list' => '192.168.0.1:9092,192.168.0.2:9092,192.168.0.3:9092',
'topic' => [
'auto.commit.interval.ms' => 100,
'offset.store.method' => 'broker',
'auto.offset.reset' => 'earliest',
],
],
]
```#### 使用示例:
```php
// 消费者使用示例
notificationService = $notificationService;
parent::__construct($id, $module, $config);
}/**
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
parent::init();
$this->consumer->attachBehavior('kafka', [
'class' => KafkaBehavior::class,
'tableName' => 'kafka_queue_log'
]);
}/**
* @return string 主题名称
*/
public function getTopicName()
{
return 'notification';
}/**
* @return string 分组id
*/
public function getGroupId()
{
return 'notificationGroup';
}/**
* @param $payload
* @throws \yii\base\UserException
*/
public function consume($payload)
{
$this->notificationService->sendMessage($payload['id'], $payload['scene']);
}
}// 生产者使用示例
\Yii::$app->kafka->produce('notification', ['id' => 1, 'scene' => 0]);```