package service import ( "context" "strings" "git.apinb.com/bsm-sdk/engine/etcd" "git.apinb.com/bsm-sdk/engine/print" "git.apinb.com/bsm-sdk/engine/types" "git.apinb.com/bsm-sdk/engine/vars" "github.com/zeromicro/go-zero/zrpc" ) func RegisterAnonymous(c *zrpc.RpcServerConf, anonCfg *types.AnonymousConf) { // etcd is has if !c.HasEtcd() { return } else { print.Info("[Blocks Register] %s ETCD Endpoints: %s", vars.ServiceKey, c.Etcd.Hosts) print.Success("[Blocks Register] Service: %s", c.Etcd.Key) } // anonymous slice is zero if len(anonCfg.Urls) == 0 || anonCfg.Key == "" { return } // connect etcd var client *etcd.Etcd if c.Etcd.HasTLS() { client = etcd.NewEtcd(c.Etcd.Hosts, &types.EtcdTls{ Ca: c.Etcd.CACertFile, Cert: c.Etcd.CertFile, CertKey: c.Etcd.CertKeyFile, }) } else { client = etcd.NewEtcd(c.Etcd.Hosts, nil) } defer client.Client.Close() // remove reppeat, clear service all anonymous uri. anonymous, _, _ := client.Get(anonCfg.Key) as := strings.Split(anonymous, ",") as = append(clearService(as), anonCfg.Urls...) newAnonymous := strings.Join(as, ",") // put anonymous to etcd _, err := client.Client.Put(context.Background(), anonCfg.Key, newAnonymous) if err != nil { print.Fail("[Blocks Register] Anonymous Fail.") } else { print.Success("[Blocks Register] Anonymous: %s", newAnonymous) } } func clearService(as []string) (out []string) { for _, v := range as { if len(v) >= len(vars.ServiceKey) { if v[0:len(vars.ServiceKey)] != vars.ServiceKey { out = append(out, v) } } } return }