From 2c81e99f4e4b8d71fb0cdf6afb19012feb9e32b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Tue, 31 Aug 2021 14:14:14 +0800 Subject: [PATCH] :bug: fix a bug. --- feign.go | 12 +++++++++++- main.go | 13 ++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/feign.go b/feign.go index 131cf07..e23fe52 100644 --- a/feign.go +++ b/feign.go @@ -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 } diff --git a/main.go b/main.go index 716bca9..9812609 100644 --- a/main.go +++ b/main.go @@ -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() {