在go语言中使用nacos实现配置中心及读取nacos中配置文件流程步骤
Go  /  管理员 发布于 1年前   809
go + 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)
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
访问:
http://192.168.1.103:8848/nacos/index.html
用户名/密码:nacos/nacos
配置开机启动:
docker container update --restart=always zongscanpz
用nacos做配置
nacos成功启动后访问:
http://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": "SMS_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} {生鲜小店 SMS_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)
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号