etcdc.go 670 B

1234567891011121314151617181920212223242526272829303132
  1. package comp
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. pb "git.wanbits.io/joe/franklin/protos"
  6. "git.wanbits.io/joe/kettle/etcd"
  7. "time"
  8. )
  9. var (
  10. GEtcdc *etcd.EtcdClient
  11. )
  12. func ConnectEtcd(c *pb.ConfConf) (*etcd.EtcdClient, error) {
  13. GEtcdc, err := etcd.New(c.Addrs, c.Username, c.Password, time.Duration(c.Timeout)*time.Second)
  14. return GEtcdc, err
  15. }
  16. func RegisterSelf(path string, c *pb.AppConf, conf *pb.AppConfConf) error {
  17. sconf, err := json.Marshal(conf)
  18. if err != nil {
  19. return err
  20. }
  21. fullpath := fmt.Sprintf("%s/%v", path, c.Id)
  22. leaseId, err := GEtcdc.PutWithLife(fullpath, string(sconf), 2)
  23. if err != nil {
  24. return err
  25. }
  26. return GEtcdc.KeepAlive(leaseId)
  27. }