golang實(shí)現(xiàn)nacos獲取配置和服務(wù)注冊(cè)-支持集群詳解
更新時(shí)間:2025年11月17日 14:42:14 作者:一條閑魚_mytube
文章介紹了如何在Go語言中使用Nacos獲取配置和服務(wù)注冊(cè),支持集群初始化,客戶端結(jié)構(gòu)體中的IpAddresses可以配置多個(gè)地址,新客戶端支持集群配置,文章還討論了如何將Nacos獲取到的配置與項(xiàng)目中的Viper無縫對(duì)接,以便進(jìn)行Viper的正常操作
golang nacos獲取配置和服務(wù)注冊(cè)-支持集群
初始化客戶端
結(jié)構(gòu)體中IpAddres 可以配置多個(gè)地址
package utils
import (
"github.com/nacos-group/nacos-sdk-go/clients/config_client"
"github.com/nacos-group/nacos-sdk-go/clients/naming_client"
"github.com/nacos-group/nacos-sdk-go/vo"
"time"
)
type NacosConfigClient struct {
//ConfigInput
//"ip1,ip2,ip3"
IpAddrs string
Port int
NaocesNameSpace string
DataId string
Group string
NacosLogDir string
NacosCacheDir string
//NamingInput
NamingServiceName string
NamingServiceIP string
NamingOwner string
NamingPort int
//Output
ConfigClient config_client.IConfigClient
NamingClient naming_client.INamingClient
}
可選參數(shù)配置
type OptionNacos func(msg *NacosConfigClient)
//config naming
func OptionNacosConfigWithServiceName(serviceName string) OptionNacos {
return func(msg *NacosConfigClient) {
msg.NamingServiceName = serviceName
}
}
func OptionNacosConfigWithServiceIP(serviceIP string) OptionNacos {
return func(msg *NacosConfigClient) {
msg.NamingServiceIP = serviceIP
}
}
func OptionNacosConfigWithOwner(owner string) OptionNacos {
return func(msg *NacosConfigClient) {
msg.NamingOwner = owner
}
}
//config client
func OptionNacosConfigWithLogDir(logDir string) OptionNacos {
return func(msg *NacosConfigClient) {
msg.NacosLogDir = logDir
}
}
func OptionNacosConfigWithCacheDir(cacheDir string) OptionNacos {
return func(msg *NacosConfigClient) {
msg.NacosCacheDir = cacheDir
}
}
func OptionNacosConfigWithIpaddr(ipAddrr string) OptionNacos {
return func(msg *NacosConfigClient) {
msg.IpAddrs = ipAddrr
}
}
func OptionNacosConfigWithNameSpace(nameSpace string) OptionNacos {
return func(msg *NacosConfigClient) {
msg.NaocesNameSpace = nameSpace
}
}
func OptionNacosConfigWithPort(port int) OptionNacos {
return func(msg *NacosConfigClient) {
msg.Port = port
}
}
func OptionNacosConfigDataId(dataId string) OptionNacos {
return func(msg *NacosConfigClient) {
msg.DataId = dataId
}
}
func OptionNacosConfigGroup(group string) OptionNacos {
return func(msg *NacosConfigClient) {
msg.Group = group
}
}
func OptionNacosWithNamingPort(port int) OptionNacos {
return func(msg *NacosConfigClient) {
msg.NamingPort = port
}
}
new一個(gè)客戶端 支持集群配置
func NewNacosClient(ops ...OptionNacos) *NacosConfigClient{
nacosConfigClient := new(NacosConfigClient)
for _, o := range ops {
o(nacosConfigClient)
}
serverConfigs:=make([]constant.ServerConfig,0)
address:=strings.Split(nacosConfigClient.IpAddrs,",")
for i:=range address{
serverConfigs=append(serverConfigs,constant.ServerConfig{
IpAddr: address[i],
Port: uint64(nacosConfigClient.Port),
})
}
clientConfig := constant.ClientConfig{
NamespaceId: nacosConfigClient.NaocesNameSpace, //
NotLoadCacheAtStart: false,
TimeoutMs: 5 * 1000,
LogDir: nacosConfigClient.NacosLogDir,
CacheDir: nacosConfigClient.NacosCacheDir,
}
configClient, err := clients.NewConfigClient(
vo.NacosClientParam{
ClientConfig: &clientConfig,
ServerConfigs: serverConfigs,
},
)
if err != nil {
panic(err)
}
nacosConfigClient.ConfigClient = configClient
// Naming client
namingClient, err := clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig: &clientConfig,
ServerConfigs: serverConfigs,
},
)
if err != nil {
panic(err)
}
nacosConfigClient.NamingClient = namingClient
return nacosConfigClient
}
獲取配置信息
func (client *NacosConfigClient) GetConfig() (string, error) {
content, err := client.ConfigClient.GetConfig(vo.ConfigParam{
DataId: client.DataId,
Group: client.Group,
})
return content, err
}
服務(wù)注冊(cè)
func (client *NacosConfigClient) Register() (bool, error) {
param := vo.RegisterInstanceParam{
Ip: client.NamingServiceIP,
Port: uint64(client.NamingPort),
ServiceName: client.NamingServiceName,
Weight: 10,
Enable: true,
Healthy: true,
Ephemeral: true,
Metadata: map[string]string{"owenr": client.NamingOwner, "uptime": time.Now().Format("2006-01-02 15:04:05")},
}
success, err := client.NamingClient.RegisterInstance(param)
return success, err
}
獲取配置后如何和項(xiàng)目中的vipper如何無縫對(duì)接?
func InitConfig() {
config = viper.New()
config.SetConfigType("yaml")
config.ReadConfig(bytes.NewReader([]byte(GetNacosConfig())))
}
func GetNacosConfig() string {
content,err:=GetNacosClient().GetConfig()
if err!=nil{
panic(err)
}
return content
}
這樣就可以進(jìn)行vipper正常操作了
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vscode中安裝Go插件和配置Go環(huán)境詳細(xì)步驟
要在VSCode中配置Go語言插件,首先需要確保你的電腦已經(jīng)安裝了Go環(huán)境和最新版本的VSCode,這篇文章主要給大家介紹了關(guān)于vscode中安裝Go插件和配置Go環(huán)境的相關(guān)資料,需要的朋友可以參考下2024-01-01
Golang處理gRPC請(qǐng)求/響應(yīng)元數(shù)據(jù)的示例代碼
前段時(shí)間實(shí)現(xiàn)內(nèi)部gRPC框架時(shí),為了實(shí)現(xiàn)在服務(wù)端攔截器中打印請(qǐng)求及響應(yīng)的頭部信息,便查閱了部分關(guān)于元數(shù)據(jù)的資料,因?yàn)橹形木W(wǎng)絡(luò)上對(duì)于該領(lǐng)域的信息較少,于是在這做了一些簡(jiǎn)單的總結(jié),需要的朋友可以參考下2024-03-03
golang使用viper加載配置文件實(shí)現(xiàn)自動(dòng)反序列化到結(jié)構(gòu)
這篇文章主要為大家介紹了golang使用viper加載配置文件實(shí)現(xiàn)自動(dòng)反序列化到結(jié)構(gòu)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
golang程序進(jìn)度條實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了golang程序?qū)崿F(xiàn)進(jìn)度條示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

