diff --git a/rpc/user_center.go b/rpc/user_center.go index 744eb8331020b23c08d6dca63e6679fb879e30dc..b218d3e7bf7d695b6db83be058b850d2a06f9ecf 100644 --- a/rpc/user_center.go +++ b/rpc/user_center.go @@ -102,33 +102,33 @@ func init() { } mylogrus.MyLog.Infof("userCenterAddr:%v,addr:%v", userCenterAddr, addrs) } - } - mylogrus.MyLog.Infof("connect userCenterAddr:%v", userCenterAddr) - resolver.Register(bd) - go func() { - address := consulapi.DefaultConfig().Address // 用consul api的default config - if err := consul.RegisterWatcher("services", nil, address, func(serviceStatus map[string]map[string][]string) { - if statusAddrs, ok := serviceStatus[userCenterConsulName]; ok { - healthAddrs, _ := statusAddrs[consulapi.HealthPassing] - l := len(healthAddrs) - if l > 0 { - mylogrus.MyLog.Infof("consul service update state:%v-%v", userCenterConsulName, healthAddrs) - bd.UpdateState(healthAddrs) // 更新新的注册名 - } else { - mylogrus.MyLog.Warnf("consul service update local state:%v-%v", userCenterConsulName, defaultUserCenterAddr) - bd.UpdateState([]string{defaultUserCenterAddr}) // 都没有健康的,使用默认本地回环的 - } - for status := range statusAddrs { - if status == consulapi.HealthPassing { - continue + resolver.Register(bd) // 服务注册 + go func() { + address := consulapi.DefaultConfig().Address // 用consul api的default config + if err := consul.RegisterWatcher("services", nil, address, func(serviceStatus map[string]map[string][]string) { + if statusAddrs, ok := serviceStatus[userCenterConsulName]; ok { + healthAddrs, _ := statusAddrs[consulapi.HealthPassing] + l := len(healthAddrs) + if l > 0 { + mylogrus.MyLog.Infof("consul service update state:%v-%v", userCenterConsulName, healthAddrs) + bd.UpdateState(healthAddrs) // 更新新的注册名 + } else { + mylogrus.MyLog.Warnf("consul service update local state:%v-%v", userCenterConsulName, defaultUserCenterAddr) + bd.UpdateState([]string{defaultUserCenterAddr}) // 都没有健康的,使用默认本地回环的 + } + for status := range statusAddrs { + if status == consulapi.HealthPassing { + continue + } + mylogrus.MyLog.Warnf("consul service wrong state:%v-%v-%v", userCenterConsulName, status, statusAddrs[status]) } - mylogrus.MyLog.Warnf("consul service wrong state:%v-%v-%v", userCenterConsulName, status, statusAddrs[status]) } + }); err != nil { + mylogrus.MyLog.Errorf("启动 consul 的watch监控失败") } - }); err != nil { - mylogrus.MyLog.Errorf("启动 consul 的watch监控失败") - } - }() + }() + } + mylogrus.MyLog.Infof("connect userCenterAddr:%v", userCenterAddr) //userCenterAddr := services[0].Address + ":" + strconv.Itoa(services[0].ServicePort) //mylogrus.MyLog.Printf("Choose userCenter %s, %s, weights: %v\n", services[0].ID, userCenterAddr, services[0].ServiceWeights)