This commit is contained in:
parent
5c718886bd
commit
b4d60b891c
21
main.go
21
main.go
@ -42,8 +42,13 @@ func initNacos() {
|
||||
}
|
||||
portNum, _ := strconv.Atoi(port)
|
||||
pp := uint64(portNum)
|
||||
ip := "api-demo"
|
||||
if ips := getIps(); ips != nil {
|
||||
ip = ips[0]
|
||||
}
|
||||
|
||||
success, err := client.RegisterInstance(vo.RegisterInstanceParam{
|
||||
Ip: "api-demo",
|
||||
Ip: ip,
|
||||
Port: pp,
|
||||
Weight: 10,
|
||||
Enable: true,
|
||||
@ -70,24 +75,24 @@ func initNacos() {
|
||||
//log.Println("监听配置失败: ", err.Error())
|
||||
}
|
||||
|
||||
func getIps() string {
|
||||
func getIps() []string {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
var aaaa []string
|
||||
var ips []string
|
||||
for _, address := range addrs {
|
||||
// 检查ip地址判断是否回环地址
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
ip := ipnet.IP.String()
|
||||
fmt.Println(ip)
|
||||
aaaa = append(aaaa, ip)
|
||||
ips = append(ips, ip)
|
||||
}
|
||||
}
|
||||
}
|
||||
ipStr := strings.Join(aaaa, ",")
|
||||
return ipStr
|
||||
//ipStr := strings.Join(aaaa, ",")
|
||||
return ips
|
||||
}
|
||||
|
||||
func main() {
|
||||
@ -107,7 +112,7 @@ func main() {
|
||||
})
|
||||
|
||||
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))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user