最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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插件和配置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ù)的示例代碼

    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)

    這篇文章主要為大家介紹了golang使用viper加載配置文件實(shí)現(xiàn)自動(dòng)反序列化到結(jié)構(gòu)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • Go語言中database/sql的用法介紹

    Go語言中database/sql的用法介紹

    Go語言中的database/sql包定義了對(duì)數(shù)據(jù)庫的一系列操作,database/sql/driver包定義了應(yīng)被數(shù)據(jù)庫驅(qū)動(dòng)實(shí)現(xiàn)的接口,這些接口會(huì)被sql包使用,本文將詳細(xì)給大家介紹Go的database/sql的使用方法,需要的朋友可以參考下
    2023-05-05
  • Go設(shè)計(jì)模式之中介者模式講解和代碼示例

    Go設(shè)計(jì)模式之中介者模式講解和代碼示例

    中介者是一種行為設(shè)計(jì)模式,讓程序組件通過特殊的中介者對(duì)象進(jìn)行間接溝通,達(dá)到減少組件之間依賴關(guān)系的目的,因此本文就給大家詳細(xì)介紹一下Go中介者模式,需要的朋友可以參考下
    2023-06-06
  • golang程序進(jìn)度條實(shí)現(xiàn)示例詳解

    golang程序進(jìn)度條實(shí)現(xiàn)示例詳解

    這篇文章主要為大家介紹了golang程序?qū)崿F(xiàn)進(jìn)度條示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Goland和IDEA換行符的設(shè)置方式

    Goland和IDEA換行符的設(shè)置方式

    這篇文章主要介紹了Goland和IDEA換行符的設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • GoLang分布式鎖與snowflake雪花算法

    GoLang分布式鎖與snowflake雪花算法

    這篇文章主要介紹了GoLang分布式鎖與snowflake雪花算法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2022-12-12
  • Golang控制協(xié)程執(zhí)行順序方法詳解

    Golang控制協(xié)程執(zhí)行順序方法詳解

    這篇文章主要介紹了Golang控制協(xié)程執(zhí)行順序的方法,Golang的語法和運(yùn)行時(shí)直接內(nèi)置了對(duì)并發(fā)的支持。Golang里的并發(fā)指的是能讓某個(gè)函數(shù)獨(dú)立于其他函數(shù)運(yùn)行的能力
    2022-11-11
  • Golang熔斷器的開發(fā)過程詳解

    Golang熔斷器的開發(fā)過程詳解

    Golang熔斷器是一種用于處理分布式系統(tǒng)中服務(wù)調(diào)用的故障保護(hù)機(jī)制,它可以防止故障服務(wù)的連鎖反應(yīng),提高系統(tǒng)的穩(wěn)定性和可靠性,本文將給大家詳細(xì)的介紹一下Golang熔斷器的開發(fā)過程,需要的朋友可以參考下
    2023-09-09

最新評(píng)論

福贡县| 瑞金市| 岳普湖县| 娄烦县| 电白县| 巩义市| 阿克| 江北区| 苏尼特右旗| 武邑县| 永嘉县| 万山特区| 庆元县| 靖安县| 邯郸县| 澄江县| 陆川县| 广饶县| 特克斯县| 米泉市| 高州市| 井陉县| 大足县| 交口县| 恩施市| 道孚县| 河间市| 沂水县| 阿勒泰市| 马龙县| 顺义区| 临猗县| 松江区| 扎赉特旗| 即墨市| 福安市| 如皋市| 巴东县| 鸡东县| 宁化县| 古交市|