马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
使用限定
Go>=v1.15
Nacos>2.x
安装
使用go get安装SDK:
- go get -u github.com/nacos-group/nacos-sdk-go/v2
复制代码 快速使用
- constant.ClientConfig{
- TimeoutMs uint64 // 请求Nacos服务端的超时时间,默认是10000ms
- NamespaceId string // Nacos的命名空间Id
- Endpoint string // 当使用地址服务器时,需要该配置. https://help.aliyun.com/document_detail/130146.html
- RegionId string // Nacos&KMS的regionId,用于配置中心的鉴权
- AccessKey string // Nacos&KMS的AccessKey,用于配置中心的鉴权
- SecretKey string // Nacos&KMS的SecretKey,用于配置中心的鉴权
- OpenKMS bool // 是否开启kms,默认不开启,kms可以参考文档 https://help.aliyun.com/product/28933.html
- // 同时DataId必须以"cipher-"作为前缀才会启动加解密逻辑
- CacheDir string // 缓存service信息的目录,默认是当前运行目录
- UpdateThreadNum int // 监听service变化的并发数,默认20
- NotLoadCacheAtStart bool // 在启动的时候不读取缓存在CacheDir的service信息
- UpdateCacheWhenEmpty bool // 当service返回的实例列表为空时,不更新缓存,用于推空保护
- Username string // Nacos服务端的API鉴权Username
- Password string // Nacos服务端的API鉴权Password
- LogDir string // 日志
存储路径 - RotateTime string // 日志
轮转周期,比如:30m, 1h, 24h, 默认是24h - MaxAge int64 // 日志
最大文件数,默认3 - LogLevel string // 日志默认级别,值必须是:debug,info,warn,error,默认值是info
- }
复制代码
- constant.ServerConfig{
- ContextPath string // Nacos的ContextPath,默认/nacos,在2.0中不需要设置
- IpAddr string // Nacos的服务地址
- Port uint64 // Nacos的服务端口
- Scheme string // Nacos的服务地址前缀,默认http,在2.0中不需要设置
- GrpcPort uint64 // Nacos的 grpc 服务端口, 默认为 服务端口+1000, 不是必填
- }
复制代码 Note:我们可以设置多个ServerConfig,客户端会对这些服务端做轮询哀求
Create client
- // 创建clientConfig
- clientConfig := constant.ClientConfig{
- NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468", // 如果需要支持多namespace,我们可以创建多个client,它们有不同的NamespaceId。当namespace是public时,此处填空字符串。
- TimeoutMs: 5000,
- NotLoadCacheAtStart: true,
- LogDir: "/tmp/nacos/log",
- CacheDir: "/tmp/nacos/cache",
- LogLevel: "debug",
- }
- // 创建clientConfig的另一种方式
- clientConfig := *constant.NewClientConfig(
- constant.WithNamespaceId("e525eafa-f7d7-4029-83d9-008937f9d468"), //当namespace是public时,此处填空字符串。
- constant.WithTimeoutMs(5000),
- constant.WithNotLoadCacheAtStart(true),
- constant.WithLogDir("/tmp/nacos/log"),
- constant.WithCacheDir("/tmp/nacos/cache"),
- constant.WithLogLevel("debug"),
- )
- // 至少一个ServerConfig
- serverConfigs := []constant.ServerConfig{
- {
- IpAddr: "console1.nacos.io",
- ContextPath: "/nacos",
- Port: 80,
- Scheme: "http",
- },
- {
- IpAddr: "console2.nacos.io",
- ContextPath: "/nacos",
- Port: 80,
- Scheme: "http",
- },
- }
- // 创建serverConfig的另一种方式
- serverConfigs := []constan
复制代码 |