github.com/erda-project/erda-infra@v1.0.9/providers/zk-master-election/examples/main.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 main 16 17 import ( 18 "context" 19 "fmt" 20 "os" 21 22 "github.com/erda-project/erda-infra/base/servicehub" 23 election "github.com/erda-project/erda-infra/providers/zk-master-election" 24 ) 25 26 type provider struct { 27 Election election.Interface // autowired 28 } 29 30 func (p *provider) Init(ctx servicehub.Context) error { 31 p.Election.Watch(p.masterChanged) 32 return nil 33 } 34 35 func (p *provider) Run(ctx context.Context) error { 36 for { 37 select { 38 case <-ctx.Done(): 39 return nil 40 } 41 } 42 } 43 44 func (p *provider) masterChanged(event election.Event) { 45 fmt.Println("is master: ", event.IsMaster(), ", is connected: ", event.IsConnected()) 46 } 47 48 func init() { 49 servicehub.Register("example", &servicehub.Spec{ 50 Services: []string{"example"}, 51 Dependencies: []string{"zk-master-election"}, 52 Description: "example", 53 Creator: func() servicehub.Provider { 54 return &provider{} 55 }, 56 }) 57 } 58 59 func main() { 60 hub := servicehub.New() 61 hub.Run("examples", "", os.Args...) 62 } 63 64 // OUTPUT: 65 // INFO[2021-03-18 15:31:36.589] provider zookeeper initialized 66 // INFO[2021-03-18 15:31:36.589] provider zk-master-election (depends [zookeeper]) initialized 67 // INFO[2021-03-18 15:31:36.589] provider example (depends [zk-master-election]) initialized 68 // INFO[2021-03-18 15:31:36.589] signals to quit:[hangup interrupt terminated quit] 69 // 2021/03/18 15:31:36 connected to 127.0.0.1:2181 70 // INFO[2021-03-18 15:31:36.682] connected to zookeeper successfully module=zk-master-election 71 // 2021/03/18 15:31:36 authenticated: id=105855796925956124, timeout=12000 72 // 2021/03/18 15:31:36 re-submitting `0` credentials after reconnect 73 // INFO[2021-03-18 15:31:36.871] election finish, i am slave module=zk-master-election 74 // is master: true , is connected: true 75 // INFO[2021-03-18 15:31:36.929] start watch path "/example/master-node-key" module=zk-master-election 76 // ^C 77 // INFO[2021-03-18 15:31:44.602] provider example exit 78 // INFO[2021-03-18 15:31:44.602] exit waith path "/example/master-node-key" module=zk-master-election 79 // 2021/03/18 15:31:44 recv loop terminated: EOF 80 // 2021/03/18 15:31:44 send loop terminated: <nil> 81 // INFO[2021-03-18 15:31:44.634] disconnected zookeeper module=zk-master-election 82 // INFO[2021-03-18 15:31:44.634] provider zk-master-election closed