事件插件部署(Kafka)
本文主要讲解如何部署 Kafka 事件订阅插件,包括插件编译、Kafka 部署以及事件订阅示例。
推荐配置
- CPU/ RAM: 16Core / 32G
- DISK: 500G+
- System: Ubuntu / CentOS 64
1. 编译 Kafka event plugin
git clone https://github.com/tronprotocol/event-plugin.git
cd event-plugin
./gradlew build
编译完成后会生成 plugin-kafka-1.0.0.zip,位于 event-plugin/build/plugins/ 目录下。
2. 部署 Kafka
安装 Kafka
Linux 环境:
cd /usr/local
wget https://downloads.apache.org/kafka/2.8.0/kafka_2.13-2.8.0.tgz
$ tar -xzf kafka_2.13-2.8.0.tgz
运行 Kafka
Linux 环境:
cd /usr/local/kafka_2.13-2.8.0
// start the ZooKeeper service
$ bin/zookeeper-server-start.sh config/zookeeper.properties &
// start the Kafka broker service
$ bin/kafka-server-start.sh config/server.properties &
3. 事件订阅
3.1 event subscribe 配置
为了支持 Kafka 实现事件订阅,需要修改 fullnode 的配置文件,添加 event.subscribe 配置项。
3.1.1 插件配置项
event.subscribe = {
native = {
useNativeQueue = false// if true, use native message queue, else use event plugin.
bindport = 5555 // bind port
sendqueuelength = 1000 //max length of send queue
}
path = "" // absolute path of plugin
server = "" // target server address to receive event triggers
dbconfig = "" // dbname|username|password
contractParse = true,
…………
…………
}
- native 配置项是针对 TRON 的内置消息队列,如果要支持 Kafka 事件订阅,需要确保useNativeQueue字段为 false,如果为 true,Kafka 事件订阅将无法生效。
- path 配置项是 plugin-kafka-1.0.0.zip 的本地路径,确保 zip 包路径正确,否则无法加载。
- server 配置项是 Kafka 服务器地址,使用 ip:port 的格式,Kafka 默认端口号是9092, 请确保端口号正确,并确保 Kafka 服务可访问。
- dbconfig 是针对 Mongodb 插件的配置项,忽略即可。
3.1.2 事件订阅配置项
事件订阅相关的配置项,包括 topics, filter 等。
TRON事件订阅已支持 block, transaction, contractevent, contractlog, solidity, soliditytevent, soliditylog等7种类型的事件订阅。
开发者需要依据业务需求进行配置,建议只订阅1-2种 event,如果开启过多 trigger,会导致性能下降。
以 block 事件订阅为例,如下所示。
topics = [
{
triggerName = "block" // block trigger, the value can't be modified
enable = true
topic = "block" // plugin topic, the value could be modified
}
]
- triggerName 配置项,属于内置字段,不可更改
- enable 配置项,如果设置为true,则开启对block 事件的订阅
- topic 配置项,对应是 Kafka 接收 event 对应的订阅 topic,需要使用 Kafka 提前创建,topic 名字可以自己定义,也可以使用默认的 "block"。
filter 字段用于对所订阅事件进行过滤,可以指定区块范围(fromblock - toblock),可以指定特定合约地址(contractAddress),可以指定特定的合约topic (contractTopic),为开发者提供更高效更精准的事件订阅服务。
filter = {
fromblock = "" // the value could be "", "earliest" or a specified block number as the beginning of the queried range
toblock = "" // the value could be "", "latest" or a specified block number as end of the queried range
contractAddress = [
"" // contract address you want to subscribe, if it's set to "", you will receive contract logs/events with any contract address.
]
contractTopic = [
"" // contract topic you want to subscribe, if it's set to "", you will receive contract logs/events with any contract topic.
]
}
3.2 创建Kafka订阅topic
Kafka 订阅 topic 的名字要与3.1.2中的配置项一致,例如开发者需要订阅 block 事件,就在block trigger中填写topic字段 "block",并在Kafka中创建 "block" topic。用于接收区块事件。
Linux 环境:
bin/kafka-topics.sh --create --topic block --bootstrap-server localhost:9092
3.3 启动事件订阅节点
在完成上述配置后,开发者启动全节点,在启动时需要添加 "--es" 参数,这样才可以开启事件订阅功能。
java -jar FullNode.jar -c config.conf --es
查看 tron.log,检验 Kafka event plugin 是否加载成功:
grep -i eventplugin logs/tron.log
如果出现类似字段,表明事件订阅插件加载成功。
[o.t.c.l.EventPluginLoader] 'your plugin path/plugin-kafka-1.0.0.zip' loaded
3.4 事件订阅查询
执行 kafka-console-consumer.sh 脚本,获取 Kafka 中 topic 为 "block" 的消息。
Linux 环境:
bin/kafka-console-consumer.sh --topic block --from-beginning --bootstrap-server localhost:9092
如果出现类似字段,表明事件订阅成功:
{
"timeStamp": 1539973125000,
"triggerName": "blockTrigger",
"blockNumber": 3341315,
"blockHash": "000000000032fc03440362c3d42eb05e79e8a1aef77fe31c7879d23a750f2a31",
"transactionSize": 16,
"latestSolidifiedBlockNumber": 3341297,
"transactionList": ["8757f846e541b51b5692a2370327f4b8031125f4557f8ad4b1037d4452616d39", "f6adab7814b34e5e756170f93a31a0c3393c5d99eff11e30271916375adc7467", ..., "89bcbcd063a48ef4a5678a033acf5edbb6b17419a3c91eb0479a3c8598774b43"]
}
…………
Updated over 1 year ago