github.com/erda-project/erda-infra@v1.0.9/providers/kafkav2/kafka.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package kafkav2 16 17 import ( 18 "reflect" 19 20 "github.com/erda-project/erda-infra/base/logs" 21 "github.com/erda-project/erda-infra/base/servicehub" 22 writer "github.com/erda-project/erda-infra/pkg/parallel-writer" 23 ) 24 25 // Interface . 26 type Interface interface { 27 NewProducer(c ProducerConfig, options ...ProducerOption) (writer.Writer, error) 28 Servers() string 29 } 30 31 // Producer . 32 type Producer interface { 33 writer.Writer 34 } 35 36 type config struct { 37 Servers string `file:"servers" env:"BOOTSTRAP_SERVERS" default:"localhost:9092" desc:"kafka servers"` 38 } 39 40 // provider . 41 type provider struct { 42 Cfg *config 43 Log logs.Logger 44 } 45 46 // Init . 47 func (p *provider) Init(ctx servicehub.Context) error { 48 return nil 49 } 50 51 // Provide . 52 func (p *provider) Provide(ctx servicehub.DependencyContext, options ...interface{}) interface{} { 53 return &service{ 54 p: p, 55 log: p.Log.Sub(ctx.Caller()), 56 name: ctx.Caller(), 57 } 58 } 59 60 type service struct { 61 p *provider 62 log logs.Logger 63 name string 64 } 65 66 var _ Interface = (*service)(nil) 67 68 func (s *service) Servers() string { return s.p.Cfg.Servers } 69 70 func init() { 71 servicehub.Register("kafka-v2", &servicehub.Spec{ 72 Services: []string{"kafka-v2", "kafka-producer-v2"}, 73 Types: []reflect.Type{ 74 reflect.TypeOf((*Interface)(nil)).Elem(), 75 }, 76 ConfigFunc: func() interface{} { return &config{} }, 77 Creator: func() servicehub.Provider { 78 return &provider{} 79 }, 80 }) 81 }