go 集成nacos注册中心、设置中心

[复制链接]
发表于 2025-12-10 00:02:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
使用限定

Go>=v1.15
Nacos>2.x
安装

使用go get安装SDK:
  1. go get -u github.com/nacos-group/nacos-sdk-go/v2
复制代码
快速使用


      
  • 初始化客户端设置ClientConfig
  1. constant.ClientConfig{
  2.   TimeoutMs            uint64 // 请求Nacos服务端的超时时间,默认是10000ms
  3.   NamespaceId          string // Nacos的命名空间Id
  4.   Endpoint             string // 当使用地址服务器时,需要该配置. https://help.aliyun.com/document_detail/130146.html
  5.   RegionId             string // Nacos&KMS的regionId,用于配置中心的鉴权
  6.   AccessKey            string // Nacos&KMS的AccessKey,用于配置中心的鉴权
  7.   SecretKey            string // Nacos&KMS的SecretKey,用于配置中心的鉴权
  8.   OpenKMS              bool   // 是否开启kms,默认不开启,kms可以参考文档 https://help.aliyun.com/product/28933.html
  9.                               // 同时DataId必须以"cipher-"作为前缀才会启动加解密逻辑
  10.   CacheDir             string // 缓存service信息的目录,默认是当前运行目录
  11.   UpdateThreadNum      int    // 监听service变化的并发数,默认20
  12.   NotLoadCacheAtStart  bool   // 在启动的时候不读取缓存在CacheDir的service信息
  13.   UpdateCacheWhenEmpty bool   // 当service返回的实例列表为空时,不更新缓存,用于推空保护
  14.   Username             string // Nacos服务端的API鉴权Username
  15.   Password             string // Nacos服务端的API鉴权Password
  16.   LogDir               string // 日志日志存储路径
  17.   RotateTime           string // 日志日志轮转周期,比如:30m, 1h, 24h, 默认是24h
  18.   MaxAge               int64  // 日志日志最大文件数,默认3
  19.   LogLevel             string // 日志默认级别,值必须是:debug,info,warn,error,默认值是info
  20. }
复制代码

      
  • ServerConfig
  1. constant.ServerConfig{
  2.   ContextPath string // Nacos的ContextPath,默认/nacos,在2.0中不需要设置
  3.   IpAddr      string // Nacos的服务地址
  4.   Port        uint64 // Nacos的服务端口
  5.   Scheme      string // Nacos的服务地址前缀,默认http,在2.0中不需要设置
  6.   GrpcPort    uint64 // Nacos的 grpc 服务端口, 默认为 服务端口+1000, 不是必填
  7. }
复制代码
Note:我们可以设置多个ServerConfig,客户端会对这些服务端做轮询哀求
Create client
  1. // 创建clientConfig
  2. clientConfig := constant.ClientConfig{
  3.   NamespaceId:         "e525eafa-f7d7-4029-83d9-008937f9d468", // 如果需要支持多namespace,我们可以创建多个client,它们有不同的NamespaceId。当namespace是public时,此处填空字符串。
  4.   TimeoutMs:           5000,
  5.   NotLoadCacheAtStart: true,
  6.   LogDir:              "/tmp/nacos/log",
  7.   CacheDir:            "/tmp/nacos/cache",
  8.   LogLevel:            "debug",
  9. }
  10. // 创建clientConfig的另一种方式
  11. clientConfig := *constant.NewClientConfig(
  12.     constant.WithNamespaceId("e525eafa-f7d7-4029-83d9-008937f9d468"), //当namespace是public时,此处填空字符串。
  13.     constant.WithTimeoutMs(5000),
  14.     constant.WithNotLoadCacheAtStart(true),
  15.     constant.WithLogDir("/tmp/nacos/log"),
  16.     constant.WithCacheDir("/tmp/nacos/cache"),
  17.     constant.WithLogLevel("debug"),
  18. )
  19. // 至少一个ServerConfig
  20. serverConfigs := []constant.ServerConfig{
  21.     {
  22.         IpAddr:      "console1.nacos.io",
  23.         ContextPath: "/nacos",
  24.         Port:        80,
  25.         Scheme:      "http",
  26.     },
  27.     {
  28.       IpAddr:      "console2.nacos.io",
  29.       ContextPath: "/nacos",
  30.       Port:        80,
  31.         Scheme:      "http",
  32.     },
  33. }
  34. // 创建serverConfig的另一种方式
  35. serverConfigs := []constan
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表