nacos 配置中心 & 服務發現 使用
一: 安裝步驟
-
從
-
解壓, 進入bin目錄執行:
sh startup.sh -m standalone
-
看到
nacos is starting with standalone
表面城管
二: 配置中心的使用
-
使用 go-sdk
-
測試代碼
-
進入nacos頁面 鏈接: http://127.0.0.1:8848/nacos/
-
創建用戶 => 創建角色&綁定用戶 => 創建namespace => 角色賦權限
具體操作如下
默認用戶名,密碼: nacos , nacos
-
登錄后的頁面
-
創建用戶:
-
創建namespace (類似php的namespace , golang的package ; 主要用戶隔離環境)
-
創建角色 & 綁定用戶
-
分配權限 (給角色分配權限)
使用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",