nacos 配置中心 & 服務發現 使用


 

nacos 配置中心 & 服務發現 使用

一: 安裝步驟

  • github下載連接

  • 解壓, 進入bin目錄執行: sh startup.sh -m standalone

  • 看到 nacos is starting with standalone 表面城管

二: 配置中心的使用

具體操作如下

默認用戶名,密碼: nacos , nacos

  • 登錄后的頁面

image-20210116220725920

 

  • 創建用戶:

image-20210116221011641

 

  • 創建namespace (類似php的namespace , golang的package ; 主要用戶隔離環境)

image-20210116221036477

 

  • 創建角色 & 綁定用戶

image-20210116221220754

 

  • 分配權限 (給角色分配權限)

image-20210116221256896

 

使用golang執行操作 (使用 nacos-group/nacos-sdk-go 客戶端來操作)
func main() {
// 服務端配置
sc := []constant.ServerConfig{
{
IpAddr: "127.0.0.1", // nacos服務端的地址, 集群版配置多個
Port:   8848,        // nacos 的端口
},
}
// 客戶端配置
cc := constant.ClientConfig{
NamespaceId:         "09f4588a-ca0e-4ab6-9911-549703764e39", //namespace_id 剛才配置添加的id
TimeoutMs:           5000,                                   // 超時時間
NotLoadCacheAtStart: true,
LogDir:              "/tmp/nacos/log",
CacheDir:            "/tmp/nacos/cache",
RotateTime:          "1h",
MaxAge:              3,
LogLevel:            "debug",
}

// a more graceful way to create config client
// 創建配置中心client
client, err := clients.NewConfigClient(
vo.NacosClientParam{
ClientConfig:  &cc,
ServerConfigs: sc,
},
)

if err != nil {
panic(err)
}

//publish config
//config key=dataId+group+namespaceId
_, err = client.PublishConfig(vo.ConfigParam{
DataId:  "test-data",
Group:   "test-group",
Content: "hello world123!",
})
_, err = client.PublishConfig(vo.ConfigParam{
DataId:  "test-data-2",
Group:   "test-group",
Content: "hello world001!",
})
if err != nil {
fmt.Printf("PublishConfig err:%+v \n", err)
}

//get config
content, err := client.GetConfig(vo.ConfigParam{
DataId: "test-data",
Group:  "test-group",
})
fmt.Println("GetConfig,config :" + content)
}  

輸出: GetConfig,config :hello world123!

 

還可以監聽key的變更

 //Listen config change,key=dataId+group+namespaceId.
// 監聽配置的變更
err = client.ListenConfig(vo.ConfigParam{
DataId: "test-data",
Group:  "test-group",
OnChange: func(namespace, group, dataId, data string) {
fmt.Println("config changed group:" + group + ", dataId:" + dataId + ", content:" + data)
},
})
  • 登錄nacos客戶端查看

image-20210116221921951

 

注意:

  • nacos 只需要知道namespace , client不需要輸入username, password就可以訪問

  • 但是如果輸入了, username 與 password 不匹配就好報錯

二: 服務發現的使用

 

image-20210116224914402

 

具體代碼

func main() {
// 服務端配置
sc := []constant.ServerConfig{
{
IpAddr: "127.0.0.1",
Port:   8848,
},
}
// 客戶端配置
cc := constant.ClientConfig{
NamespaceId:         "09f4588a-ca0e-4ab6-9911-549703764e39", //namespace id
TimeoutMs:           5000,
NotLoadCacheAtStart: true,
LogDir:              "/tmp/nacos/log",
CacheDir:            "/tmp/nacos/cache",
RotateTime:          "1h",
MaxAge:              3,
LogLevel:            "debug",
}
// a more graceful way to create naming client
client, err := clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig:  &cc,
ServerConfigs: sc,
},
)

if err != nil {
panic(err)
}

//Register with default cluster and group
//ClusterName=DEFAULT,GroupName=DEFAULT_GROUP
ExampleServiceClient_RegisterServiceInstance(client, vo.RegisterInstanceParam{
Ip:          "192.168.0.11",
Port:        8848,
ServiceName: "zbs_test_service",
Weight:      10,
Enable:      true,
Healthy:     true,
Ephemeral:   true,
Metadata:    map[string]string{"idc": "shanghai"},
})

//Register with cluster name
//GroupName=DEFAULT_GROUP
ExampleServiceClient_RegisterServiceInstance(client, vo.RegisterInstanceParam{
Ip:          "192.168.0.12",
Port:        8848,
ServiceName: "zbs_test_service",
Weight:      10,
ClusterName: "cluster-a",
Enable:      true,
Healthy:     true,
Ephemeral:   true,
})
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM