feat: code cleanup
This commit is contained in:
20
consumer.go
20
consumer.go
@@ -15,10 +15,6 @@ import (
|
||||
)
|
||||
|
||||
// InitConsumer 初始化消费者
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 8:45 下午 2021/9/22
|
||||
func InitConsumer(topic string, consumerGroup string, hostList []string, cfg *sarama.Config) (*Consumer, error) {
|
||||
if nil == cfg {
|
||||
cfg = sarama.NewConfig()
|
||||
@@ -36,10 +32,6 @@ func InitConsumer(topic string, consumerGroup string, hostList []string, cfg *sa
|
||||
}
|
||||
|
||||
// Consumer ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 8:52 下午 2021/9/22
|
||||
type Consumer struct {
|
||||
topic string
|
||||
host []string
|
||||
@@ -50,10 +42,6 @@ type Consumer struct {
|
||||
}
|
||||
|
||||
// init ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 8:55 下午 2021/9/22
|
||||
func (c *Consumer) init() error {
|
||||
var (
|
||||
err error
|
||||
@@ -68,10 +56,6 @@ func (c *Consumer) init() error {
|
||||
}
|
||||
|
||||
// Consume 消费消息
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 9:04 下午 2021/9/22
|
||||
func (c *Consumer) Consume(dataHandler IConsumeDataHandler) {
|
||||
ctx := context.Background()
|
||||
for {
|
||||
@@ -106,10 +90,6 @@ func (h consumerGroupHandler) ConsumeClaim(sess sarama.ConsumerGroupSession, cla
|
||||
}
|
||||
|
||||
// Destroy ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 8:59 下午 2021/9/22
|
||||
func (c *Consumer) Destroy() {
|
||||
_ = c.consumerGroupInstance.Close()
|
||||
}
|
||||
|
||||
28
producer.go
28
producer.go
@@ -16,10 +16,6 @@ import (
|
||||
)
|
||||
|
||||
// InitProducer 初始化生产者
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:52 下午 2021/9/21
|
||||
func InitProducer(hostList []string, topic string, cfg *sarama.Config) (*Producer, error) {
|
||||
if cfg == nil {
|
||||
cfg = sarama.NewConfig()
|
||||
@@ -43,10 +39,6 @@ func InitProducer(hostList []string, topic string, cfg *sarama.Config) (*Produce
|
||||
}
|
||||
|
||||
// Producer 生产者
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:57 下午 2021/9/21
|
||||
type Producer struct {
|
||||
cfg *sarama.Config
|
||||
hostList []string
|
||||
@@ -57,10 +49,6 @@ type Producer struct {
|
||||
}
|
||||
|
||||
// init kafka初始化
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:58 下午 2021/9/21
|
||||
func (p *Producer) init() error {
|
||||
var (
|
||||
err error
|
||||
@@ -85,20 +73,12 @@ func (p *Producer) init() error {
|
||||
}
|
||||
|
||||
// Destroy 断开生产者连接时,触发的逻辑
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 3:58 下午 2021/9/21
|
||||
func (p *Producer) Destroy() {
|
||||
// 断开客户端链接
|
||||
_ = p.client.Close()
|
||||
}
|
||||
|
||||
// Sync 同步生产数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:08 下午 2021/9/21
|
||||
func (p *Producer) Sync(data ProducerData) ProducerResult {
|
||||
mes := p.buildMessage(data)
|
||||
result := ProducerResult{}
|
||||
@@ -107,10 +87,6 @@ func (p *Producer) Sync(data ProducerData) ProducerResult {
|
||||
}
|
||||
|
||||
// Async 异步生产数据
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 4:09 下午 2021/9/21
|
||||
func (p *Producer) Async(data ProducerData) ProducerResult {
|
||||
result := ProducerResult{}
|
||||
p.asyncProducer.Input() <- p.buildMessage(data)
|
||||
@@ -129,10 +105,6 @@ func (p *Producer) Async(data ProducerData) ProducerResult {
|
||||
}
|
||||
|
||||
// buildMessage 构建消息
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 7:32 下午 2021/9/21
|
||||
func (p *Producer) buildMessage(data ProducerData) *sarama.ProducerMessage {
|
||||
mes := &sarama.ProducerMessage{
|
||||
Topic: p.topic,
|
||||
|
||||
Reference in New Issue
Block a user