forked from 0xPolygonHermez/zkevm-node
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support nacos * reorgnize nacos configuration * modify the nacos configure name * add example configure file
- Loading branch information
Showing
7 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package nacos | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"strconv" | ||
"strings" | ||
"time" | ||
|
||
"github.com/nacos-group/nacos-sdk-go/clients" | ||
"github.com/nacos-group/nacos-sdk-go/common/constant" | ||
"github.com/nacos-group/nacos-sdk-go/vo" | ||
|
||
"github.com/0xPolygonHermez/zkevm-node/log" | ||
) | ||
|
||
// StartNacosClient start nacos client and register rest service in nacos | ||
func StartNacosClient(urls string, namespace string, name string, externalAddr string) { | ||
ip, port, err := ResolveIPAndPort(externalAddr) | ||
if err != nil { | ||
log.Error(fmt.Sprintf("failed to resolve %s error: %s", externalAddr, err.Error())) | ||
return | ||
} | ||
|
||
serverConfigs, err := getServerConfigs(urls) | ||
if err != nil { | ||
log.Error(fmt.Sprintf("failed to resolve nacos server url %s: %s", urls, err.Error())) | ||
return | ||
} | ||
client, err := clients.CreateNamingClient(map[string]interface{}{ | ||
"serverConfigs": serverConfigs, | ||
"clientConfig": constant.ClientConfig{ | ||
TimeoutMs: 5000, | ||
ListenInterval: 10000, | ||
NotLoadCacheAtStart: true, | ||
NamespaceId: namespace, | ||
LogDir: "/dev/null", | ||
LogLevel: "error", | ||
}, | ||
}) | ||
if err != nil { | ||
log.Error(fmt.Sprintf("failed to create nacos client. error: %s", err.Error())) | ||
return | ||
} | ||
|
||
_, err = client.RegisterInstance(vo.RegisterInstanceParam{ | ||
Ip: ip, | ||
Port: uint64(port), | ||
ServiceName: name, | ||
Weight: 10, | ||
ClusterName: "DEFAULT", | ||
Enable: true, | ||
Healthy: true, | ||
Ephemeral: true, | ||
Metadata: map[string]string{ | ||
"preserved.register.source": "GO", | ||
"app_registry_tag": strconv.FormatInt(time.Now().Unix(), 10), | ||
}, | ||
}) | ||
if err != nil { | ||
log.Error(fmt.Sprintf("failed to register instance in nacos server. error: %s", err.Error())) | ||
return | ||
} | ||
log.Info("register application instance in nacos successfully") | ||
} | ||
|
||
func ResolveIPAndPort(addr string) (string, int, error) { | ||
laddr := strings.Split(addr, ":") | ||
ip := laddr[0] | ||
if ip == "127.0.0.1" { | ||
return GetLocalIP(), 26659, nil | ||
} | ||
port, err := strconv.Atoi(laddr[1]) | ||
if err != nil { | ||
return "", 0, err | ||
} | ||
return ip, port, nil | ||
} | ||
|
||
// GetLocalIP get local ip | ||
func GetLocalIP() string { | ||
addrs, err := net.InterfaceAddrs() | ||
if err != nil { | ||
return "" | ||
} | ||
for _, address := range addrs { | ||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { | ||
if ipnet.IP.To4() != nil { | ||
return ipnet.IP.String() | ||
} | ||
} | ||
} | ||
return "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package nacos | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/nacos-group/nacos-sdk-go/clients" | ||
"github.com/nacos-group/nacos-sdk-go/common/constant" | ||
"github.com/nacos-group/nacos-sdk-go/model" | ||
"github.com/nacos-group/nacos-sdk-go/vo" | ||
) | ||
|
||
func GetOneInstance(urls string, nameSpace string, param vo.SelectOneHealthInstanceParam) (instance *model.Instance, err error) { | ||
serverConfigs, err := getServerConfigs(urls) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to resolve nacos server url %s: %s", urls, err.Error()) | ||
} | ||
|
||
namingClient, err := clients.CreateNamingClient(map[string]interface{}{ | ||
"serverConfigs": serverConfigs, | ||
"clientConfig": constant.ClientConfig{ | ||
NamespaceId: nameSpace, | ||
TimeoutMs: 5000, | ||
NotLoadCacheAtStart: true, | ||
LogDir: "/dev/null", | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create nacos client when getting one service. error: %s", err.Error()) | ||
} | ||
|
||
instance, err = namingClient.SelectOneHealthyInstance(param) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get %s service in [%s, %s]. error: %s", param, urls, nameSpace, err.Error()) | ||
} | ||
return instance, nil | ||
} | ||
|
||
func getServerConfigs(urls string) ([]constant.ServerConfig, error) { | ||
// nolint | ||
var configs []constant.ServerConfig | ||
for _, url := range strings.Split(urls, ",") { | ||
laddr := strings.Split(url, ":") | ||
serverPort, err := strconv.Atoi(laddr[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
configs = append(configs, constant.ServerConfig{ | ||
IpAddr: laddr[0], | ||
Port: uint64(serverPort), | ||
}) | ||
} | ||
return configs, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters