diff --git a/conf/conf.yaml b/conf/conf.yaml index 8dd9ee9..199a24d 100644 --- a/conf/conf.yaml +++ b/conf/conf.yaml @@ -1,16 +1,16 @@ etcd: - host: # it's possible to define multiple etcd hosts addresses of the same etcd cluster. - - "http://127.0.0.1:2379" # multiple etcd address, if your etcd cluster enables TLS, please use https scheme, - prefix: /apisix # apisix configurations prefix - timeout: 30 # 30 seconds - #user: root # root username for etcd - #password: 5tHkHhYkjr6cQY # root password for etcd - tls: - #cert: /path/to/cert # path of certificate used by the etcd client - #key: /path/to/key # path of key used by the etcd client + - host: # it's possible to define multiple etcd hosts addresses of the same etcd cluster. + - "http://127.0.0.1:2379" # multiple etcd address, if your etcd cluster enables TLS, please use https scheme, + prefix: /apisix # apisix configurations prefix + timeout: 30 # 30 seconds + #user: root # root username for etcd + #password: 5tHkHhYkjr6cQY # root password for etcd + tls: + #cert: /path/to/cert # path of certificate used by the etcd client + #key: /path/to/key # path of key used by the etcd client - verify: true # whether to verify the etcd endpoint certificate when setup a TLS connection to etcd, - # the default value is true, e.g. the certificate will be verified strictly. + verify: true # whether to verify the etcd endpoint certificate when setup a TLS connection to etcd, + # the default value is true, e.g. the certificate will be verified strictly. log: level: warn path: apisix-seed.log # path is the file to write logs to. Backup log files will be retained in the same directory diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 0f5543f..2e3397a 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -14,7 +14,7 @@ type DisBuilder func([]byte) (interface{}, error) var ( WorkDir = "." - ETCDConfig *Etcd + ETCDConfigs []*Etcd = make([]*Etcd, 0) LogConfig *Log DisConfigs = make(map[string]interface{}) DisBuilders = make(map[string]DisBuilder) @@ -44,7 +44,7 @@ type Log struct { } type Config struct { - Etcd Etcd + Etcd []Etcd Log Log Discovery map[string]interface{} } @@ -80,9 +80,10 @@ func InitConf() { } initLogConfig(config.Log) - - if len(config.Etcd.Host) > 0 { - initEtcdConfig(config.Etcd) + for _, singleEtcd := range config.Etcd { + if len(singleEtcd.Host) > 0 { + initEtcdConfig(singleEtcd) + } } } } @@ -99,13 +100,13 @@ func initEtcdConfig(conf Etcd) { prefix = conf.Prefix } - ETCDConfig = &Etcd{ + ETCDConfigs = append(ETCDConfigs, &Etcd{ Host: host, User: conf.User, Password: conf.Password, TLS: conf.TLS, Prefix: prefix, - } + }) } func initLogConfig(conf Log) { diff --git a/internal/core/storer/storehub.go b/internal/core/storer/storehub.go index 03442bd..1c918a6 100644 --- a/internal/core/storer/storehub.go +++ b/internal/core/storer/storehub.go @@ -22,26 +22,26 @@ func InitStore(key string, opt GenericStoreOption, stg Interface) error { return nil } -func InitStores(stg Interface) (err error) { +func InitStores(stg Interface, conf *conf.Etcd) (err error) { err = InitStore("routes", GenericStoreOption{ - BasePath: conf.ETCDConfig.Prefix + "/routes", - Prefix: conf.ETCDConfig.Prefix, + BasePath: conf.Prefix + "/routes", + Prefix: conf.Prefix, }, stg) if err != nil { return } err = InitStore("services", GenericStoreOption{ - BasePath: conf.ETCDConfig.Prefix + "/services", - Prefix: conf.ETCDConfig.Prefix, + BasePath: conf.Prefix + "/services", + Prefix: conf.Prefix, }, stg) if err != nil { return } err = InitStore("upstreams", GenericStoreOption{ - BasePath: conf.ETCDConfig.Prefix + "/upstreams", - Prefix: conf.ETCDConfig.Prefix, + BasePath: conf.Prefix + "/upstreams", + Prefix: conf.Prefix, }, stg) if err != nil { return diff --git a/main.go b/main.go index d48078e..44d93ca 100644 --- a/main.go +++ b/main.go @@ -53,37 +53,44 @@ func main() { log.Fatal(err) } - etcdClient, err := storer.NewEtcd(conf.ETCDConfig) - if err != nil { - panic(err) - } + rewriters := make([]components.Rewriter, 0) - wg := sync.WaitGroup{} - wg.Add(2) - go func() { - defer wg.Done() - err := storer.InitStores(etcdClient) + for _, config := range conf.ETCDConfigs { + etcdClient, err := storer.NewEtcd(config) if err != nil { panic(err) } - }() - go func() { - defer wg.Done() - err := discoverer.InitDiscoverers() - if err != nil { - panic(err) + + wg := sync.WaitGroup{} + wg.Add(2) + go func() { + defer wg.Done() + err := storer.InitStores(etcdClient, config) + if err != nil { + panic(err) + } + }() + go func() { + defer wg.Done() + err := discoverer.InitDiscoverers() + if err != nil { + panic(err) + } + }() + wg.Wait() + + rewriter := components.Rewriter{ + Prefix: config.Prefix, } - }() - wg.Wait() - - rewriter := components.Rewriter{ - Prefix: conf.ETCDConfig.Prefix, + rewriter.Init() + rewriters = append(rewriters, rewriter) + } + for _, rewriter := range rewriters { + defer rewriter.Close() } - rewriter.Init() - defer rewriter.Close() watcher := components.Watcher{} - err = watcher.Init() + err := watcher.Init() if err != nil { log.Error(err.Error()) return