🐛 fix a bug.
Some checks are pending
continuous-integration/drone/push Build is running

This commit is contained in:
李寻欢 2021-08-31 13:53:48 +08:00
parent 5c718886bd
commit b4d60b891c

21
main.go
View File

@ -42,8 +42,13 @@ func initNacos() {
} }
portNum, _ := strconv.Atoi(port) portNum, _ := strconv.Atoi(port)
pp := uint64(portNum) pp := uint64(portNum)
ip := "api-demo"
if ips := getIps(); ips != nil {
ip = ips[0]
}
success, err := client.RegisterInstance(vo.RegisterInstanceParam{ success, err := client.RegisterInstance(vo.RegisterInstanceParam{
Ip: "api-demo", Ip: ip,
Port: pp, Port: pp,
Weight: 10, Weight: 10,
Enable: true, Enable: true,
@ -70,24 +75,24 @@ func initNacos() {
//log.Println("监听配置失败: ", err.Error()) //log.Println("监听配置失败: ", err.Error())
} }
func getIps() string { func getIps() []string {
addrs, err := net.InterfaceAddrs() addrs, err := net.InterfaceAddrs()
if err != nil { if err != nil {
return "" return nil
} }
var aaaa []string var ips []string
for _, address := range addrs { for _, address := range addrs {
// 检查ip地址判断是否回环地址 // 检查ip地址判断是否回环地址
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil { if ipnet.IP.To4() != nil {
ip := ipnet.IP.String() ip := ipnet.IP.String()
fmt.Println(ip) fmt.Println(ip)
aaaa = append(aaaa, ip) ips = append(ips, ip)
} }
} }
} }
ipStr := strings.Join(aaaa, ",") //ipStr := strings.Join(aaaa, ",")
return ipStr return ips
} }
func main() { func main() {
@ -107,7 +112,7 @@ func main() {
}) })
app.GET("/ip", func(context *gin.Context) { app.GET("/ip", func(context *gin.Context) {
context.String(http.StatusOK, getIps()) context.String(http.StatusOK, strings.Join(getIps(), ","))
}) })
app.Run(fmt.Sprintf(":%v", port)) app.Run(fmt.Sprintf(":%v", port))
} }