用户登录
用户注册

分享至

一文详解配置中心nacos的安装使用方法

  • 作者: 亖呉?盀
  • 来源: 51数据库
  • 2022-11-15
本文由golang教程栏目给大家介绍关于配置中心“nacos ”,下面将以golang的角度来了解配置中心,naco 的安装以及如何用 nacos做配置,读取nacos中配置文件等等,希望对需要的朋友有所帮助!

什么是配置中心

用来统一管理项目中所有配置的系统。 虽然听起来很简单,但也不要小瞧了这个模块。 如果一个中型互联网项目,不采用配置中心的模式,一大堆的各类配置项,各种不定时的修改需求,一定会让开发同学非常头疼且管理十分混乱。

配置中心的服务流程如下: 用户在配置中心更新配置信息。 服务 A 和服务 B 及时得到配置更新通知,从配置中心获取配置。 总得来说,配置中心就是一种统一管理各种应用配置的基础服务组件。

在系统架构中,配置中心是整个微服务基础架构体系中的一个组件,它的功能看上去并不起眼,无非就是配置的管理和存取,但它是整个微服务架构中不可或缺的一环。

nacos 是什么

Nacos

Nacos 是 Naming and Configuration Service 的缩写,从名字上能看出它重点关注的两个领域是 Naming 即注册中心和Configuration 配置中心。

业务上的配置,功能开关,服务治理上对弱依赖的降级,甚至数据库的密码等,都可能用到动态配置中心。 在没有专门的配置中心组件时,我们使用硬编码、或配置文件、或数据库、缓存等方式来解决问题。 硬编码修改配置时需要重新编译打包,配置文件需要重启应用,数据库受限于性能,缓存丧失了及时性。

Nacos 的配置模型

namespace + group + dataId 唯一确定一个配置

  • namespace:与 client 绑定,一个 clinet 对应到一个 namespace,可用来隔离环境或区分租户

  • group:分组,区分业务

  • dataId:配置的 id

来看一下是如何在实际场景使用的

例如:一个电商网站其中有这几个模块:用户模块、商品模块、订单模块、库存模块

这几个模块都需要进行配置且它们的配置不同,这是我们为每一个模块做一个 namespace, 每一个模块都需要有

开发阶段的配置,和上线后的配置,使用我们使用 dev, 和 pro 两个分组来进行区分,对于 dataId,不管是 dev 还是

pro 都必须填写。

Nacos 的安装

这里我们直接使用 docker 进行安装

Nacos 的安装 (docker)

docker run --name nacos-standalone -e MODE=standalone -e JVM_XMS=512m -e JVM_XMX=512m -e JVM_XMN=256m -p 8848:8848 -d nacos/nacos-server:latest

访问:192.168.1.103:8848/nacos/index.html 用户名 / 密码:nacos/nacos

配置开机启动:

docker container update --restart=always xxx

用 nacos 做配置

nacos 成功启动后访问:192.168.1.103:8848/nacos/index.html

可以创建 namespace, 我们新建一个用户模块 user , 创建成功后可以看到有对应的 id, 例如:7ae18f62-e2b9-48bd-bff2-a49e7443f5bc

然后我们在 user 命名空间下新建一个配置文件,并填写对应的名称 (dataId) 和分组,这里我们新建一个 josn 的配置文件:

{
    "name": "user-web",
    "host": "10.2.106.169",
    "port": 9091,
    "tags":["iceymoss", "goods", "golang", "web"],
    "user_srv":{
        "name": "user-srv",
        "host": "10.2.106.169",
        "port": 8081
    },
    "jwt":{
        "key": "dfijdfjidhfjijdfbdfdFwohPd6XmVCdnQi"
    },
    "sms":{
        "key": "mykey",
        "secret": "mysecret"
    },
    "params":{
        "sign_name": "生鲜小店",
        "code": "**S_244610581"
    },
    "redis":{
        "host": "127.0.0.1",
        "port": 6379,
        "expir": 300
    },
    "verify":{
        "width": 5
    },
    "consul":{
        "host": "10.2.106.169",
        "port": 8500
    },
    "tracing":{
        "host": "127.0.0.1",
        "port": 6831,
        "name": "shopping"
    }
}

这样整个配置文件就配置完成了

读取 nacos 中配置文件

拉取依赖

我们使用 go 来读取配置文件,使用需要拉取 nacos 的 sdk:

go get github.com/nacos-group/nacos-sdk-go/clients
go get github.com/nacos-group/nacos-sdk-go/common/constant
go get github.com/nacos-group/nacos-sdk-go/vo

读取配置

在读取配置之前我们先编写一个用来做配置映射的结构体

目录结构:

nacos_test
├── config
│   └── config.go
└── main.go

编写 config 时需要注意的是我们需要保持 tag 名和配置文件中的名称一致

package config

//UserSerConfig 映射用户配置
type UserSerConfig struct {
    Name string `mapstructure:"name" json:"name"`
    Host string `mapstructure:"host" json:"host"`
    Port int    `mapstructure:"port" json:"port"`
}

//JWTConfig 映射token配置
type JWTConfig struct {
    SigningKey string `mapstructure:"key" json:"key"`
}

//AliSmsConfig 阿里秘钥
type AliSmsConfig struct {
    Apikey    string `mapstructure:"key" json:"key"`
    ApiSecret string `mapstructure:"secret" json:"secret"`
}

//ParamsConfig 短信模板配置
type ParamsConfig struct {
    SignName     string `mapstructure:"sign_name" json:"sign_name"`
    TemplateCode string `mapstructure:"code" json:"code"`
}

//RedisConfig redis数据库配置
type RedisConfig struct {
    Host  string `mapstructure:"host" json:"host"`
    Port  int    `mapstructure:"port" json:"port"`
    Expir int    `mapstructure:"expir" json:"expir"`
}

//Verifier 手机验证长度
type Verifier struct {
    Width int `mapstructure:"width" json:"width"`
}

type ConsulConfig struct {
    Host string `mapstructure:"host" json:"host"`
    Port int    `mapstructure:"port" json:"port"`
}

//ServerConfig  映射服务配置
type ServerConfig struct {
    Name        string        `mapstructure:"name" json:"name"`
    Port        int           `mapstructure:"port" json:"port"`
    UserSerInfo UserSerConfig `mapstructure:"user_srv" json:"user_srv"`
    JWTInfo     JWTConfig     `mapstructure:"jwt" json:"jwt"`
    AliSms      AliSmsConfig  `mapstructure:"sms" json:"sms"`
    Params      ParamsConfig  `mapstructure:"params" json:"params"`
    Redis       RedisConfig   `mapstructure:"redis" json:"redis"`
    Verify      Verifier      `mapstructure:"verify" json:"verify"`
    ConsulInfo  ConsulConfig  `mapstructure:"consul" json:"consul"`
}

下面进行配置文件读取:

package main

import (
    "StudyGin/nacos/config"
    "encoding/json"
    "fmt"

    "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"
)

func main() {
    //服务端配置, nacos运行的socket
    sc := []constant.ServerConfig{
        {
            IpAddr: "10.2.81.102",
            Port:   8848,
        },
    }

    //客服端配置
    cc := constant.ClientConfig{
        NamespaceId:         "7ae18f62-e2b9-48bd-bff2-a49e7443f5bc", // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId
        TimeoutMs:           5000,
        NotLoadCacheAtStart: true,
        LogDir:              "tmp/nacos/log",
        CacheDir:            "tmp/nacos/cache",
        //RotateTime:          "1h",
        //MaxAge:              3,
        LogLevel: "debug",
    }

    configClient, err := clients.CreateConfigClient(map[string]interface{}{
        "serverConfigs": sc,
        "clientConfig":  cc,
    })
    if err != nil {
        panic(err)
    }

    //获取配置
    content, err := configClient.GetConfig(vo.ConfigParam{
        DataId: "user-web.json",
        Group:  "dev"})

    if err != nil {
        panic(err)
    }
    Config := &config.ServerConfig{}

  //将配置信息读取到config.ServerConfig{}对象中
    err = json.Unmarshal([]byte(content), &Config)
    if err != nil {
        panic(err)
    }
    fmt.Println(Config)

}

输出:

&{user-web 9091 {user-srv 10.2.106.169 8081} {dfijdfjidhfjijdfbdfdFwohPd6XmVCdnQi} {mykey mysecret} {生鲜小店 **S_244610581} {127.0.0.1 6379 300} {5} {10.2.106.1600}}

当然配置中心和 viper 都提供实时监控配置

可以这样写:

    //监听配置变化
    err = configClient.ListenConfig(vo.ConfigParam{
        DataId: "user-web",
        Group:  "DEV",
        OnChange: func(namespace, group, dataId, data string) {
            fmt.Println("配置文件变化")
            fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
        },
    })
    time.Sleep(3000 * time.Second)

以上就是一文详解配置中心nacos的安装使用方法的详细内容,更多请关注其它相关文章!

软件
前端设计
程序设计
Java相关