🐛 fix a bug.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
李寻欢 2021-08-31 14:14:14 +08:00
parent 33132b3564
commit 2c81e99f4e
2 changed files with 17 additions and 8 deletions

View File

@ -37,13 +37,23 @@ func initNacos() {
if err != nil {
panic(err)
}
ip := "gateway-demo"
if ips := getIps(); ips != nil {
ip = ips[0]
}
success, _ := client.RegisterInstance(vo.RegisterInstanceParam{
//Ip: "172.30.0.90",
Ip: ip,
Port: 8889,
Weight: 10,
Enable: true,
Healthy: true,
ServiceName: "gateway",
Ephemeral: true,
})
log.Println("Nacos注册结果: ", success)
if !success {
log.Fatal("服务注册失败,退出程序")
}
nameClient = client
}

13
main.go
View File

@ -5,27 +5,26 @@ import (
"github.com/gin-gonic/gin"
"net"
"net/http"
"strings"
)
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() {