
| 功能点 | apollo | nacos |
|---|---|---|
| 开源时间 | 2016.5 | 2018.6 |
| 配置实时推送 | 支持(http长轮询) | 支持(http长轮询) |
| 配置回滚 | 支持 | 支持 |
| 灰度发布 | 支持 | 待支持 |
| 权限管理 | 支持 | 支持 |
| 多集群 | 支持 | 支持 |
| 监听查询 | 支持 | 支持 |
| 多语言 | 主流语言 | 主流语言(官方支持) |
| 通讯协议 | http | http |



package main
import (
"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"
"time"
)
func main() {
sc := []constant.ServerConfig{
{
IpAddr: "192.168.124.51",
Port: 8848,
},
}
// 创建clientConfig
cc := constant.ClientConfig{
NamespaceId: "90dec033-6f9a-4ab2-97e5-fd60de1743c9", // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId。当namespace是public时,此处填空字符串。
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "tmp/nacos/log", //去掉tmp前面的/,这样就会默认保存到当前项目目录下
CacheDir: "tmp/nacos/cache",
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.yaml",
Group: "dev"})
if err != nil {
panic(err)
}
fmt.Println(content) //字符串 - yaml
//监听配置修改
err = configClient.ListenConfig(vo.ConfigParam{
DataId: "user_web.yaml",
Group: "dev",
OnChange: func(namespace, group, dataId, data string) {
fmt.Println("配置文件变化")
fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
},
})
time.Sleep(3000 * time.Second)
}




{
"name": "user_web",
"port": 8081,
"user_srv": {
"host": "127.0.0.1",
"port": 50051,
"name": "user_srv"
},
"jwt": {
"key": "VYLDYq3&hGWjWqF$K1ih"
},
"sms": {
"key": "",
"secrect": ""
},
"redis": {
"host": "192.168.124.51",
"port": 6379,
"expire": 300
},
"consul": {
"host": "192.168.124.51",
"port": 8500
}
}
package config
type UserSrvConfig struct {
Host string `mapstructure:"host" json:"host"`
Port int `mapstructure:"port" json:"port"`
Name string `mapstructure:"name" json:"name"`
}
type JWTConfig struct {
SigningKey string `mapstructure:"key" json:"key"`
}
type AliSmsConfig struct {
ApiKey string `mapstructure:"key" json:"key"`
ApiSecrect string `mapstructure:"secrect" json:"secrect"`
}
type ConsulConfig struct {
Host string `mapstructure:"host" json:"host"`
Port int `mapstructure:"port" json:"port"`
}
type RedisConfig struct {
Host string `mapstructure:"host" json:"host"`
Port int `mapstructure:"port" json:"port"`
Expire int `mapstructure:"expire" json:"expire"`
}
type ServerConfig struct {
Name string `mapstructure:"name" json:"name"`
Port int `mapstructure:"port" json:"port"`
UserSrvInfo UserSrvConfig `mapstructure:"user_srv" json:"user_srv"`
JWTInfo JWTConfig `mapstructure:"jwt" json:"jwt"`
AliSmsInfo AliSmsConfig `mapstructure:"sms" json:"sms"`
RedisInfo RedisConfig `mapstructure:"redis" json:"redis"`
ConsulInfo ConsulConfig `mapstructure:"consul" json:"consul"`
}
type NacosConfig struct {
Host string `mapstructure:"host"`
Port uint64 `mapstructure:"port"`
Namespace string `mapstructure:"namespace"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DataId string `mapstructure:"dataid"`
Group string `mapstructure:"group"`
}
package global
import (
ut "github.com/go-playground/universal-translator"
"web_api/user_web/config"
"web_api/user_web/proto"
)
var (
Trans ut.Translator
ServerConfig *config.ServerConfig = &config.ServerConfig{}
UserSrvClient proto.UserClient
NacosConfig *config.NacosConfig = &config.NacosConfig{}
)
//config_debug.yaml
host: '192.168.124.51'
port: 8848
namespace: '90dec033-6f9a-4ab2-97e5-fd60de1743c9'
user: 'nacos'
password: 'nacos'
dataid: 'user_web.json'
group: 'dev'
//config_pro.yaml
host: '192.168.124.51'
port: 8848
namespace: '90dec033-6f9a-4ab2-97e5-fd60de1743c9'
user: 'nacos'
password: 'nacos'
dataid: 'user_web.json'
group: 'pro'
package initialize
import (
"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"
"github.com/spf13/viper"
"go.uber.org/zap"
"web_api/user_web/global"
)
func GetEnvInfo(env string) bool {
viper.AutomaticEnv()
return viper.GetBool(env)
//刚才设置的环境变量 想要生效 我们必须得重启goland
}
func InitConfig() {
debug := GetEnvInfo("DEV_CONFIG")
configFilePrefix := "config"
configFileName := fmt.Sprintf("user_web/%s_pro.yaml", configFilePrefix)
if debug {
configFileName = fmt.Sprintf("user_web/%s_debug.yaml", configFilePrefix)
}
v := viper.New()
//文件的路径如何设置
v.SetConfigFile(configFileName)
if err := v.ReadInConfig(); err != nil {
panic(err)
}
//这个对象如何在其他文件中使用 - 全局变量
if err := v.Unmarshal(&global.NacosConfig); err != nil {
panic(err)
}
zap.S().Infof("配置信息: %v", global.NacosConfig)
//从nacos中读取配置信息
sc := []constant.ServerConfig{
{
IpAddr: global.NacosConfig.Host,
Port: global.NacosConfig.Port,
},
}
cc := constant.ClientConfig{
NamespaceId: global.NacosConfig.Namespace, // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "tmp/nacos/log",
CacheDir: "tmp/nacos/cache",
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: global.NacosConfig.DataId,
Group: global.NacosConfig.Group})
if err != nil {
panic(err)
}
//fmt.Println(content) //字符串 - yaml
//想要将一个json字符串转换成struct,需要去设置这个struct的tag
err = json.Unmarshal([]byte(content), &global.ServerConfig)
if err != nil {
zap.S().Fatalf("读取nacos配置失败: %s", err.Error())
}
fmt.Println(&global.ServerConfig)
}


//config_debug.yaml
host: '192.168.124.51'
port: 8848
namespace: '90dec033-6f9a-4ab2-97e5-fd60de1743c9'
user: 'nacos'
password: 'nacos'
dataid: 'user_srv.json'
group: 'dev'
//config_pro.yaml
host: '192.168.124.51'
port: 8848
namespace: '90dec033-6f9a-4ab2-97e5-fd60de1743c9'
user: 'nacos'
password: 'nacos'
dataid: 'user_srv.json'
group: 'pro'
package config
type MysqlConfig struct {
Host string `mapstructure:"host" json:"host"`
Port int `mapstructure:"port" json:"port"`
Name string `mapstructure:"db" json:"db"`
User string `mapstructure:"user" json:"user"`
Password string `mapstructure:"password" json:"password"`
}
type ConsulConfig struct {
Host string `mapstructure:"host" json:"host"`
Port int `mapstructure:"port" json:"port"`
}
type ServerConfig struct {
Name string `mapstructure:"name" json:"name"`
MysqlInfo MysqlConfig `mapstructure:"mysql" json:"mysql"`
ConsulInfo ConsulConfig `mapstructure:"consul" json:"consul"`
}
type NacosConfig struct {
Host string `mapstructure:"host"`
Port uint64 `mapstructure:"port"`
Namespace string `mapstructure:"namespace"`
User string `mapstructure:"user"`
Password string `mapstructure:"password"`
DataId string `mapstructure:"dataid"`
Group string `mapstructure:"group"`
}
package global
import (
"gorm.io/gorm"
"nd/user_srv/config"
)
var (
DB *gorm.DB
ServerConfig config.ServerConfig
NacosConfig config.NacosConfig
)
package initialize
import (
"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"
"github.com/spf13/viper"
"go.uber.org/zap"
"nd/user_srv/global"
)
func GetEnvInfo(env string) bool {
viper.AutomaticEnv()
return viper.GetBool(env)
//刚才设置的环境变量 想要生效 我们必须得重启goland
}
func InitConfig() {
//从配置文件中读取出对应的配置
debug := GetEnvInfo("DEV_CONFIG")
configFilePrefix := "config"
configFileName := fmt.Sprintf("%s_pro.yaml", configFilePrefix)
if debug {
configFileName = fmt.Sprintf("%s_debug.yaml", configFilePrefix)
}
v := viper.New()
//文件的路径如何设置
v.SetConfigFile(configFileName)
if err := v.ReadInConfig(); err != nil {
panic(err)
}
//这个对象如何在其他文件中使用 - 全局变量
if err := v.Unmarshal(&global.NacosConfig); err != nil {
panic(err)
}
zap.S().Infof("配置信息: %v", global.NacosConfig)
//从nacos中读取配置信息
sc := []constant.ServerConfig{
{
IpAddr: global.NacosConfig.Host,
Port: global.NacosConfig.Port,
},
}
cc := constant.ClientConfig{
NamespaceId: global.NacosConfig.Namespace, // 如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId
TimeoutMs: 5000,
NotLoadCacheAtStart: true,
LogDir: "tmp/nacos/log",
CacheDir: "tmp/nacos/cache",
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: global.NacosConfig.DataId,
Group: global.NacosConfig.Group})
if err != nil {
panic(err)
}
//fmt.Println(content) //字符串 - yaml
//想要将一个json字符串转换成struct,需要去设置这个struct的tag
err = json.Unmarshal([]byte(content), &global.ServerConfig)
if err != nil {
zap.S().Fatalf("读取nacos配置失败: %s", err.Error())
}
fmt.Println(&global.ServerConfig)
}




