-
Notifications
You must be signed in to change notification settings - Fork 63
/
kafka.go
113 lines (99 loc) · 3.07 KB
/
kafka.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kafka
import (
"reflect"
"github.com/confluentinc/confluent-kafka-go/kafka"
"github.com/erda-project/erda-infra/base/logs"
"github.com/erda-project/erda-infra/base/servicehub"
writer "github.com/erda-project/erda-infra/pkg/parallel-writer"
)
// Interface .
type Interface interface {
NewBatchReader(c *BatchReaderConfig, options ...BatchReaderOption) (BatchReader, error)
NewConsumer(c *ConsumerConfig, handler ConsumerFunc, options ...ConsumerOption) error
NewConsumerWitchCreator(c *ConsumerConfig, creator func(i int) (ConsumerFunc, error), options ...ConsumerOption) error
NewProducer(c *ProducerConfig, options ...ProducerOption) (writer.Writer, error)
Servers() string
ProduceChannelSize() int
ProduceEventsChannelSize() int
NewAdminClient() (*kafka.AdminClient, error)
}
// Producer .
type Producer interface {
writer.Writer
ProduceChannelSize() int
EventsChannelSize() int
}
type config struct {
Servers string `file:"servers" env:"BOOTSTRAP_SERVERS" default:"localhost:9092" desc:"kafka servers"`
Producer struct {
Options map[string]interface{} `file:"options"`
} `file:"producer"`
Comsumer struct {
Options map[string]interface{} `file:"options"`
} `file:"comsumer"`
Admin struct {
Options map[string]interface{} `file:"options"`
} `file:"admin"`
}
// provider .
type provider struct {
Cfg *config
Log logs.Logger
producer sharedProducer
}
// Init .
func (p *provider) Init(ctx servicehub.Context) error {
p.producer.log = p.Log
return nil
}
// Provide .
func (p *provider) Provide(ctx servicehub.DependencyContext, options ...interface{}) interface{} {
return &service{
p: p,
log: p.Log.Sub(ctx.Caller()),
name: ctx.Caller(),
}
}
type service struct {
p *provider
log logs.Logger
name string
}
var _ Interface = (*service)(nil)
func (s *service) Servers() string { return s.p.Cfg.Servers }
func (s *service) NewAdminClient() (*kafka.AdminClient, error) {
kc := kafka.ConfigMap{}
if s.p.Cfg.Admin.Options != nil {
for k, v := range s.p.Cfg.Admin.Options {
kc[k] = v
}
}
kc["bootstrap.servers"] = s.p.Cfg.Servers
return kafka.NewAdminClient(&kc)
}
func init() {
servicehub.Register("kafka", &servicehub.Spec{
Services: []string{"kafka", "kafka-producer", "kafka-consumer"},
Types: []reflect.Type{
reflect.TypeOf((*Interface)(nil)).Elem(),
},
Description: "kafka、kafka-producer、kafka-consumer",
ConfigFunc: func() interface{} { return &config{} },
Creator: func() servicehub.Provider {
return &provider{}
},
})
}