add document
This commit is contained in:
parent
953c2e844e
commit
d662701d62
|
@ -0,0 +1,10 @@
|
|||
### Contribution flow
|
||||
|
||||
This is a rough outline of what a contributor's workflow looks like:
|
||||
|
||||
* Fork the current repository
|
||||
* Create a topic branch from where to base the contribution. This is usually develop.
|
||||
* Make commits of logical units.
|
||||
* Make sure commit messages are in the proper format (see below).
|
||||
* Push changes in a topic branch to your forked repository.
|
||||
* Before you sending out the pull request, please sync your forked repository with remote repository, this will make your pull request simple and clear. See guide below:
|
210
README.md
210
README.md
|
@ -1,51 +1,78 @@
|
|||
## nacos-go
|
||||
go语言版本的nacos client,支持服务发现和配置管理
|
||||
# Nacos-sdk-go [中文](./README_CN.md) #
|
||||
|
||||
### 客户端配置
|
||||
[](https://travis-ci.org/nacos-group/nacos-sdk-go) [](https://goreportcard.com/report/github.com/nacos-group/nacos-sdk-go) 
|
||||
|
||||
---
|
||||
|
||||
## Nacos-sdk-go
|
||||
|
||||
Nacos-sdk-go for Go client allows you to access Nacos service,it supports service discovery and dynamic configuration.
|
||||
|
||||
## Requirements
|
||||
Supported Go version over 1.12
|
||||
Supported Nacos version over 1.x
|
||||
|
||||
## Installation
|
||||
Use `go get` to install SDK:
|
||||
```sh
|
||||
$ go get -u github.com/nacos-group/nacos-sdk-go
|
||||
```
|
||||
## Quick Examples
|
||||
* ClientConfig
|
||||
|
||||
* ClientConfig 客户端配置参数
|
||||
|
||||
```go
|
||||
constant.ClientConfig{
|
||||
TimeoutMs: 10 * 1000, //http请求超时时间,单位毫秒
|
||||
ListenInterval: 30 * 1000, //监听间隔时间,单位毫秒(仅在ConfigClient中有效)
|
||||
BeatInterval: 5 * 1000, //心跳间隔时间,单位毫秒(仅在ServiceClient中有效)
|
||||
NamespaceId: "public", //nacos命名空间
|
||||
Endpoint: "", //获取nacos节点ip的服务地址
|
||||
CacheDir: "/data/nacos/cache", //缓存目录
|
||||
LogDir: "/data/nacos/log", //日志目录
|
||||
UpdateThreadNum: 20, //更新服务的线程数
|
||||
NotLoadCacheAtStart: true, //在启动时不读取本地缓存数据,true--不读取,false--读取
|
||||
UpdateCacheWhenEmpty: true, //当服务列表为空时是否更新本地缓存,true--更新,false--不更新
|
||||
TimeoutMs uint64 //timeout for requesting Nacos server, default value is 10000ms
|
||||
ListenInterval uint64 //the time interval for pulling config change,default value is 30000ms
|
||||
BeatInterval int64 //the time interval for sending beat to server,default value is 5000ms
|
||||
NamespaceId string //the namespaceId of Nacos
|
||||
Endpoint string //the endpoint for get Nacos server addresses
|
||||
RegionId string //the regionId for kms
|
||||
AccessKey string //the AccessKey for kms
|
||||
SecretKey string //the SecretKey for kms
|
||||
OpenKMS bool //it's to open kms,default is false. https://help.aliyun.com/product/28933.html
|
||||
CacheDir string //the directory for persist nacos service info,default value is current path
|
||||
UpdateThreadNum int //the number of gorutine for update nacos service info,default value is 20
|
||||
NotLoadCacheAtStart bool //not to load persistent nacos service info in CacheDir at start time
|
||||
UpdateCacheWhenEmpty bool //update cache when get empty service instance from server
|
||||
Username string //the username for nacos auth
|
||||
Password string //the password for nacos auth
|
||||
LogDir string // the directory for log, default is current path
|
||||
RotateTime string //the rotate time for log, eg: 30m, 1h, 24h, default is 24h
|
||||
MaxAge int64 //the max age of a log file, default value is 3
|
||||
LogLevel string //the level of log, it's must be debug,info,warn,error, default value is info
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
* ServerConfig nacos服务信息配置参数
|
||||
* ServerConfig
|
||||
|
||||
```go
|
||||
constant.ServerConfig{
|
||||
IpAddr: "console.nacos.io", //nacos服务的ip地址
|
||||
ContextPath: "/nacos", //nacos服务的上下文路径,默认是“/nacos”
|
||||
Port: 80, //nacos服务端口
|
||||
ContextPath string //the nacos server contextpath
|
||||
IpAddr string //the nacos server address
|
||||
Port uint64 //the nacos server port
|
||||
}
|
||||
```
|
||||
|
||||
<b>注:ServerConfig支持配置多个,在请求出错时,自动切换</b>
|
||||
<b>Note:We can config multiple ServerConfig,the client will rotate request the servers</b>
|
||||
|
||||
### 构造客户端
|
||||
### Create client
|
||||
|
||||
```go
|
||||
// 可以没有,采用默认值
|
||||
clientConfig := constant.ClientConfig{
|
||||
TimeoutMs: 10 * 1000,
|
||||
ListenInterval: 30 * 1000,
|
||||
BeatInterval: 5 * 1000,
|
||||
LogDir: "/nacos/logs",
|
||||
CacheDir: "/nacos/cache",
|
||||
NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468", //we can create mutilple clients with different namespaceId to support multiple namespace
|
||||
TimeoutMs: 5000,
|
||||
ListenInterval: 10000,
|
||||
NotLoadCacheAtStart: true,
|
||||
LogDir: "/tmp/nacos/log",
|
||||
CacheDir: "/tmp/nacos/cache",
|
||||
RotateTime: "1h",
|
||||
MaxAge: 3,
|
||||
LogLevel: "debug",
|
||||
}
|
||||
|
||||
// 至少一个
|
||||
// At least one serverconfig
|
||||
serverConfigs := []constant.ServerConfig{
|
||||
{
|
||||
IpAddr: "console1.nacos.io",
|
||||
|
@ -59,11 +86,13 @@ serverConfigs := []constant.ServerConfig{
|
|||
},
|
||||
}
|
||||
|
||||
//create naming client for service discovery
|
||||
namingClient, err := clients.CreateNamingClient(map[string]interface{}{
|
||||
"serverConfigs": serverConfigs,
|
||||
"clientConfig": clientConfig,
|
||||
})
|
||||
|
||||
//create config client for dynamic configuration
|
||||
configClient, err := clients.CreateConfigClient(map[string]interface{}{
|
||||
"serverConfigs": serverConfigs,
|
||||
"clientConfig": clientConfig,
|
||||
|
@ -72,91 +101,102 @@ configClient, err := clients.CreateConfigClient(map[string]interface{}{
|
|||
```
|
||||
|
||||
|
||||
### 服务发现
|
||||
### Service Discovery
|
||||
|
||||
* 注册服务实例:RegisterInstance
|
||||
* Register instance:RegisterInstance
|
||||
|
||||
```go
|
||||
|
||||
success, _ := namingClient.RegisterInstance(vo.RegisterInstanceParam{
|
||||
success, err := namingClient.RegisterInstance(vo.RegisterInstanceParam{
|
||||
Ip: "10.0.0.11",
|
||||
Port: 8848,
|
||||
ServiceName: "demo.go",
|
||||
Weight: 10,
|
||||
ClusterName: "a",
|
||||
Enable: true,
|
||||
Healthy: true,
|
||||
Ephemeral: true,
|
||||
Metadata: map[string]string{"idc":"shanghai"},
|
||||
ClusterName: "cluster-a", //default value is DEFAULT
|
||||
GroupName: "group-a", //default value is DEFAULT_GROUP
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 注销服务实例:DeregisterInstance
|
||||
* Deregister instance:DeregisterInstance
|
||||
|
||||
```go
|
||||
|
||||
success, _ := namingClient.DeregisterInstance(vo.DeregisterInstanceParam{
|
||||
success, err := namingClient.DeregisterInstance(vo.DeregisterInstanceParam{
|
||||
Ip: "10.0.0.11",
|
||||
Port: 8848,
|
||||
ServiceName: "demo.go",
|
||||
ClusterName: "a",
|
||||
Ephemeral: true,
|
||||
Cluster: "cluster-a", //default value is DEFAULT
|
||||
GroupName: "group-a", //default value is DEFAULT_GROUP
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 获取服务:GetService
|
||||
* Get service:GetService
|
||||
|
||||
```go
|
||||
|
||||
service, _ := namingClient.GetService(vo.GetServiceParam{
|
||||
services, err := namingClient.GetService(vo.GetServiceParam{
|
||||
ServiceName: "demo.go",
|
||||
Clusters: []string{"a"},
|
||||
Clusters: []string{"cluster-a"}, //default value is DEFAULT
|
||||
GroupName: "group-a", //default value is DEFAULT_GROUP
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 获取所有的实例列表:SelectAllInstances
|
||||
* Get all instances:SelectAllInstances
|
||||
|
||||
```go
|
||||
|
||||
//SelectAllInstance return all instances,include healthy=false,enable=false,weight<=0
|
||||
instances, err := namingClient.SelectAllInstances(vo.SelectAllInstancesParam{
|
||||
ServiceName: "demo.go",
|
||||
Clusters: []string{"a"},
|
||||
GroupName: "group-a", //default value is DEFAULT_GROUP
|
||||
Clusters: []string{"cluster-a"}, //default value is DEFAULT
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 获取实例列表:SelectInstances
|
||||
* Get instances :SelectInstances
|
||||
|
||||
```go
|
||||
|
||||
//SelectInstances only return the instances of healthy=${HealthyOnly},enable=true and weight>0
|
||||
instances, err := namingClient.SelectInstances(vo.SelectInstancesParam{
|
||||
ServiceName: "demo.go",
|
||||
Clusters: []string{"a"},
|
||||
GroupName: "group-a", //default value is DEFAULT_GROUP
|
||||
Clusters: []string{"cluster-a"}, //default value is DEFAULT
|
||||
HealthyOnly: true,
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 获取一个健康的实例(加权轮训负载均衡):SelectOneHealthyInstance
|
||||
* Get one healthy instance(WRR):SelectOneHealthyInstance
|
||||
|
||||
```go
|
||||
|
||||
//SelectOneHealthyInstance return one instance by WRR strategy for load balance
|
||||
//And the instance should be health=true,enable=true and weight>0
|
||||
instance, err := namingClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
|
||||
ServiceName: "demo.go",
|
||||
Clusters: []string{"a"},
|
||||
GroupName: "group-a", //default value is DEFAULT_GROUP
|
||||
Clusters: []string{"cluster-a"}, //default value is DEFAULT
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 服务监听:Subscribe
|
||||
* Listen service change event:Subscribe
|
||||
|
||||
```go
|
||||
|
||||
namingClient.Subscribe(vo.SubscribeParam{
|
||||
//Subscribe key=serviceName+groupName+cluster
|
||||
//Note:We call add multiple SubscribeCallback with the same key.
|
||||
err:=namingClient.Subscribe(vo.SubscribeParam{
|
||||
ServiceName: "demo.go",
|
||||
Clusters: []string{"a"},
|
||||
GroupName: "group-a", //default value is DEFAULT_GROUP
|
||||
Clusters: []string{"cluster-a"}, //default value is DEFAULT
|
||||
SubscribeCallback: func(services []model.SubscribeService, err error) {
|
||||
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
|
||||
},
|
||||
|
@ -164,13 +204,14 @@ namingClient.Subscribe(vo.SubscribeParam{
|
|||
|
||||
```
|
||||
|
||||
* 取消服务监听:Unsubscribe
|
||||
* Cancle the listen of service change event:Unsubscribe
|
||||
|
||||
```go
|
||||
|
||||
namingClient.Unsubscribe(vo.SubscribeParam{
|
||||
err:=namingClient.Unsubscribe(vo.SubscribeParam{
|
||||
ServiceName: "demo.go",
|
||||
Clusters: []string{"a"},
|
||||
GroupName: "group-a", //default value is DEFAULT_GROUP
|
||||
Clusters: []string{"cluster-a"}, //default value is DEFAULT
|
||||
SubscribeCallback: func(services []model.SubscribeService, err error) {
|
||||
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
|
||||
},
|
||||
|
@ -178,9 +219,20 @@ namingClient.Unsubscribe(vo.SubscribeParam{
|
|||
|
||||
```
|
||||
|
||||
### 配置管理
|
||||
* Get all services name:GetAllServicesInfo
|
||||
```go
|
||||
|
||||
* 发布配置:PublishConfig
|
||||
serviceInfos, err := client.GetAllServicesInfo(vo.GetAllServiceInfoParam{
|
||||
NameSpace: "0e83cc81-9d8c-4bb8-a28a-ff703187543f",
|
||||
PageNo: 1,
|
||||
ageSize: 10,
|
||||
})),
|
||||
|
||||
```
|
||||
|
||||
### Dynamic configuration
|
||||
|
||||
* publish config:PublishConfig
|
||||
|
||||
```go
|
||||
|
||||
|
@ -191,7 +243,7 @@ success, err := configClient.PublishConfig(vo.ConfigParam{
|
|||
|
||||
```
|
||||
|
||||
* 删除配置:DeleteConfig
|
||||
* delete config:DeleteConfig
|
||||
|
||||
```go
|
||||
|
||||
|
@ -201,7 +253,7 @@ success, err = configClient.DeleteConfig(vo.ConfigParam{
|
|||
|
||||
```
|
||||
|
||||
* 获取配置:GetConfig
|
||||
* get config info:GetConfig
|
||||
|
||||
```go
|
||||
|
||||
|
@ -211,11 +263,11 @@ content, err := configClient.GetConfig(vo.ConfigParam{
|
|||
|
||||
```
|
||||
|
||||
* 监听配置:ListenConfig
|
||||
* Listen config change event:ListenConfig
|
||||
|
||||
```go
|
||||
|
||||
configClient.ListenConfig(vo.ConfigParam{
|
||||
err:=configClient.ListenConfig(vo.ConfigParam{
|
||||
DataId: "dataId",
|
||||
Group: "group",
|
||||
OnChange: func(namespace, group, dataId, data string) {
|
||||
|
@ -224,13 +276,47 @@ configClient.ListenConfig(vo.ConfigParam{
|
|||
})
|
||||
|
||||
```
|
||||
* 取消监听配置:CancelListenConfig
|
||||
* Cancel the listening of config change event:CancelListenConfig
|
||||
|
||||
```go
|
||||
|
||||
configClient.CancelListenConfig(vo.ConfigParam{
|
||||
err:=configClient.CancelListenConfig(vo.ConfigParam{
|
||||
DataId: "dataId",
|
||||
Group: "group",
|
||||
})
|
||||
|
||||
```
|
||||
```
|
||||
|
||||
* Search config: SearchConfig
|
||||
```go
|
||||
configPage,err:=configClient.SearchConfig(vo.SearchConfigParam{
|
||||
Search: "blur",
|
||||
DataId: "",
|
||||
Group: "",
|
||||
PageNo: 1,
|
||||
PageSize: 10,
|
||||
})
|
||||
```
|
||||
## Example
|
||||
We can run example to learn how to use nacos go client.
|
||||
* [Config Example](./example/config)
|
||||
* [Naming Example](./example/service)
|
||||
|
||||
## Documentation
|
||||
You can view the open-api documentaion from the [Nacos open-api wepsite](https://nacos.io/en-us/docs/open-api.html).
|
||||
You can view the full documentation from the [Nacos website](https://nacos.io/en-us/docs/what-is-nacos.html).
|
||||
|
||||
## Contributing
|
||||
Contributors are welcomed to join Nacos-sdk-go project. Please check [CONTRIBUTING](./CONTRIBUTING.md) about how to contribute to this project.
|
||||
|
||||
## Contact
|
||||
* Join us from DingDing Group(23191211).
|
||||
* [Gitter](https://gitter.im/alibaba/nacos): Nacos's IM tool for community messaging, collaboration and discovery.
|
||||
* [Twitter](https://twitter.com/nacos2): Follow along for latest nacos news on Twitter.
|
||||
* [Weibo](https://weibo.com/u/6574374908): Follow along for latest nacos news on Weibo (Twitter of China version).
|
||||
* [Nacos Segmentfault](https://segmentfault.com/t/nacos): Get latest notice and prompt help from Segmentfault.
|
||||
* Email Group:
|
||||
* users-nacos@googlegroups.com: Nacos usage general discussion.
|
||||
* dev-nacos@googlegroups.com: Nacos developer discussion (APIs, feature design, etc).
|
||||
* commits-nacos@googlegroups.com: Commits notice, very high frequency.
|
||||
|
||||
|
|
|
@ -0,0 +1,322 @@
|
|||
# Nacos-sdk-go [English](./README.md) #
|
||||
|
||||
[](https://travis-ci.org/nacos-group/nacos-sdk-go) [](https://goreportcard.com/report/github.com/nacos-group/nacos-sdk-go) 
|
||||
|
||||
---
|
||||
|
||||
## Nacos-sdk-go
|
||||
|
||||
Nacos-sdk-go是Nacos的Go语言客户端,它实现了服务发现和动态配置的功能
|
||||
|
||||
## 使用限制
|
||||
支持Go>v1.12版本
|
||||
支持Nacos>1.x版本
|
||||
|
||||
## 安装
|
||||
Use `go get` to install SDK:
|
||||
```sh
|
||||
$ go get -u github.com/nacos-group/nacos-sdk-go
|
||||
```
|
||||
## 快速使用
|
||||
* ClientConfig
|
||||
|
||||
```go
|
||||
constant.ClientConfig{
|
||||
TimeoutMs uint64 //请求Nacos服务端的超时时间,默认是10000ms
|
||||
ListenInterval uint64 //监听配置变更的轮训间隔时间,默认是30000ms
|
||||
NamespaceId string //Nacos的命名空间
|
||||
Endpoint string //获取Nacos服务列表的endpoint地址
|
||||
RegionId string //kms的regionId,用于配置中心的鉴权
|
||||
AccessKey string //kms的AccessKey,用于配置中心的鉴权
|
||||
SecretKey string //kms的SecretKey,用于配置中心的鉴权
|
||||
OpenKMS bool //是否开启kms,默认不开启,kms可以参考文档 https://help.aliyun.com/product/28933.html
|
||||
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
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
* ServerConfig
|
||||
|
||||
```go
|
||||
constant.ServerConfig{
|
||||
ContextPath string //Nacos的ContextPath
|
||||
IpAddr string //Nacos的服务地址
|
||||
Port uint64 //Nacos的服务端口
|
||||
}
|
||||
```
|
||||
|
||||
<b>Note:我们可以配置多个ServerConfig,客户端会对这些服务端做轮训请求</b>
|
||||
|
||||
### Create client
|
||||
|
||||
```go
|
||||
clientConfig := constant.ClientConfig{
|
||||
NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468", //如果需要支持多namespace,我们可以场景多个client,它们有不同的NamespaceId
|
||||
TimeoutMs: 5000,
|
||||
ListenInterval: 10000,
|
||||
NotLoadCacheAtStart: true,
|
||||
LogDir: "/tmp/nacos/log",
|
||||
CacheDir: "/tmp/nacos/cache",
|
||||
RotateTime: "1h",
|
||||
MaxAge: 3,
|
||||
LogLevel: "debug",
|
||||
}
|
||||
|
||||
//至少一个ServerConfig
|
||||
serverConfigs := []constant.ServerConfig{
|
||||
{
|
||||
IpAddr: "console1.nacos.io",
|
||||
ContextPath: "/nacos",
|
||||
Port: 80,
|
||||
},
|
||||
{
|
||||
IpAddr: "console2.nacos.io",
|
||||
ContextPath: "/nacos",
|
||||
Port: 80,
|
||||
},
|
||||
}
|
||||
|
||||
//创建服务发现客户端
|
||||
namingClient, err := clients.CreateNamingClient(map[string]interface{}{
|
||||
"serverConfigs": serverConfigs,
|
||||
"clientConfig": clientConfig,
|
||||
})
|
||||
|
||||
//创建动态配置客户端
|
||||
configClient, err := clients.CreateConfigClient(map[string]interface{}{
|
||||
"serverConfigs": serverConfigs,
|
||||
"clientConfig": clientConfig,
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
|
||||
### 服务发现
|
||||
|
||||
* 注册实例:RegisterInstance
|
||||
|
||||
```go
|
||||
|
||||
success, err := namingClient.RegisterInstance(vo.RegisterInstanceParam{
|
||||
Ip: "10.0.0.11",
|
||||
Port: 8848,
|
||||
ServiceName: "demo.go",
|
||||
Weight: 10,
|
||||
Enable: true,
|
||||
Healthy: true,
|
||||
Ephemeral: true,
|
||||
Metadata: map[string]string{"idc":"shanghai"},
|
||||
ClusterName: "cluster-a", //默认值DEFAULT
|
||||
GroupName: "group-a", //默认值DEFAULT_GROUP
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 注销实例:DeregisterInstance
|
||||
|
||||
```go
|
||||
|
||||
success, err := namingClient.DeregisterInstance(vo.DeregisterInstanceParam{
|
||||
Ip: "10.0.0.11",
|
||||
Port: 8848,
|
||||
ServiceName: "demo.go",
|
||||
Ephemeral: true,
|
||||
Cluster: "cluster-a", //默认值DEFAULT
|
||||
GroupName: "group-a", //默认值DEFAULT_GROUP
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 获取服务信息:GetService
|
||||
|
||||
```go
|
||||
|
||||
services, err := namingClient.GetService(vo.GetServiceParam{
|
||||
ServiceName: "demo.go",
|
||||
Clusters: []string{"cluster-a"}, //默认值DEFAULT
|
||||
GroupName: "group-a", //默认值DEFAULT_GROUP
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 获取所有的实例列表:SelectAllInstances
|
||||
|
||||
```go
|
||||
//SelectAllInstance可以返回全部实例列表,包括healthy=false,enable=false,weight<=0
|
||||
instances, err := namingClient.SelectAllInstances(vo.SelectAllInstancesParam{
|
||||
ServiceName: "demo.go",
|
||||
GroupName: "group-a", //默认值DEFAULT_GROUP
|
||||
Clusters: []string{"cluster-a"}, //默认值DEFAULT
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 获取实例列表 :SelectInstances
|
||||
|
||||
```go
|
||||
//SelectInstances 只返回满足这些条件的实例列表:healthy=${HealthyOnly},enable=true 和weight>0
|
||||
instances, err := namingClient.SelectInstances(vo.SelectInstancesParam{
|
||||
ServiceName: "demo.go",
|
||||
GroupName: "group-a", //默认值DEFAULT_GROUP
|
||||
Clusters: []string{"cluster-a"}, //默认值DEFAULT
|
||||
HealthyOnly: true,
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 获取一个健康的实例(加权随机轮训):SelectOneHealthyInstance
|
||||
|
||||
```go
|
||||
//SelectOneHealthyInstance将会按加权随机轮训的负载均衡策略返回一个健康的实例
|
||||
//实例必须满足的条件:health=true,enable=true and weight>0
|
||||
instance, err := namingClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
|
||||
ServiceName: "demo.go",
|
||||
GroupName: "group-a", //默认值DEFAULT_GROUP
|
||||
Clusters: []string{"cluster-a"}, //默认值DEFAULT
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 监听服务变化:Subscribe
|
||||
|
||||
```go
|
||||
|
||||
//Subscribe key=serviceName+groupName+cluster
|
||||
//注意:我们可以在相同的key添加多个SubscribeCallback.
|
||||
err:=namingClient.Subscribe(vo.SubscribeParam{
|
||||
ServiceName: "demo.go",
|
||||
GroupName: "group-a", //默认值DEFAULT_GROUP
|
||||
Clusters: []string{"cluster-a"}, //默认值DEFAULT
|
||||
SubscribeCallback: func(services []model.SubscribeService, err error) {
|
||||
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
|
||||
},
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 取消服务监听:Unsubscribe
|
||||
|
||||
```go
|
||||
|
||||
err:=namingClient.Unsubscribe(vo.SubscribeParam{
|
||||
ServiceName: "demo.go",
|
||||
GroupName: "group-a", //默认值DEFAULT_GROUP
|
||||
Clusters: []string{"cluster-a"}, //默认值DEFAULT
|
||||
SubscribeCallback: func(services []model.SubscribeService, err error) {
|
||||
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
|
||||
},
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* Get all services name:GetAllServicesInfo
|
||||
```go
|
||||
|
||||
serviceInfos, err := client.GetAllServicesInfo(vo.GetAllServiceInfoParam{
|
||||
NameSpace: "0e83cc81-9d8c-4bb8-a28a-ff703187543f",
|
||||
PageNo: 1,
|
||||
ageSize: 10,
|
||||
})),
|
||||
|
||||
```
|
||||
|
||||
### 动态配置
|
||||
|
||||
* 发布配置:PublishConfig
|
||||
|
||||
```go
|
||||
|
||||
success, err := configClient.PublishConfig(vo.ConfigParam{
|
||||
DataId: "dataId",
|
||||
Group: "group",
|
||||
Content: "hello world!222222"})
|
||||
|
||||
```
|
||||
|
||||
* 删除配置:DeleteConfig
|
||||
|
||||
```go
|
||||
|
||||
success, err = configClient.DeleteConfig(vo.ConfigParam{
|
||||
DataId: "dataId",
|
||||
Group: "group"})
|
||||
|
||||
```
|
||||
|
||||
* 获取配置:GetConfig
|
||||
|
||||
```go
|
||||
|
||||
content, err := configClient.GetConfig(vo.ConfigParam{
|
||||
DataId: "dataId",
|
||||
Group: "group"})
|
||||
|
||||
```
|
||||
|
||||
* 监听配置变化:ListenConfig
|
||||
|
||||
```go
|
||||
|
||||
err:=configClient.ListenConfig(vo.ConfigParam{
|
||||
DataId: "dataId",
|
||||
Group: "group",
|
||||
OnChange: func(namespace, group, dataId, data string) {
|
||||
fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
|
||||
},
|
||||
})
|
||||
|
||||
```
|
||||
* 取消配置监听:CancelListenConfig
|
||||
|
||||
```go
|
||||
|
||||
err:=configClient.CancelListenConfig(vo.ConfigParam{
|
||||
DataId: "dataId",
|
||||
Group: "group",
|
||||
})
|
||||
|
||||
```
|
||||
|
||||
* 搜索配置: SearchConfig
|
||||
```go
|
||||
configPage,err:=configClient.SearchConfig(vo.SearchConfigParam{
|
||||
Search: "blur",
|
||||
DataId: "",
|
||||
Group: "",
|
||||
PageNo: 1,
|
||||
PageSize: 10,
|
||||
})
|
||||
```
|
||||
## 例子
|
||||
我们能从例子中学习如何使用Nacos go客户端
|
||||
* [动态配置例子](./example/config)
|
||||
* [服务发现例子](./example/service)
|
||||
|
||||
## 文档
|
||||
Nacos open-api相关信息可以查看文档 [Nacos open-api wepsite](https://nacos.io/en-us/docs/open-api.html).
|
||||
Nacos产品了解可以查看 [Nacos website](https://nacos.io/en-us/docs/what-is-nacos.html).
|
||||
|
||||
## 贡献代码
|
||||
我们非常欢迎大家为Nacos-sdk-go贡献代码. 贡献前请查看[CONTRIBUTING](./CONTRIBUTING.md)
|
||||
|
||||
## 联系我们
|
||||
* 加入Nacos-sdk-go钉钉群(23191211).
|
||||
* [Gitter](https://gitter.im/alibaba/nacos): Nacos即时聊天工具.
|
||||
* [Twitter](https://twitter.com/nacos2): 在Twitter上关注Nacos的最新动态.
|
||||
* [Weibo](https://weibo.com/u/6574374908): 在微博上关注Nacos的最新动态.
|
||||
* [Nacos Segmentfault](https://segmentfault.com/t/nacos): Segmentfault可以获得最新的推送和帮助.
|
||||
* Email Group:
|
||||
* users-nacos@googlegroups.com: Nacos用户讨论组.
|
||||
* dev-nacos@googlegroups.com: Nacos开发者讨论组 (APIs, feature design, etc).
|
||||
* commits-nacos@googlegroups.com: Nacos commit提醒.
|
||||
|
||||
|
|
@ -23,23 +23,23 @@ type ServerConfig struct {
|
|||
}
|
||||
|
||||
type ClientConfig struct {
|
||||
TimeoutMs uint64 //timeout for requesting Nacos server, default is 10000ms
|
||||
ListenInterval uint64 //the time interval for pulling config change,default is 30000ms
|
||||
BeatInterval int64 //the time interval for sending beat to server,default is 5000ms
|
||||
TimeoutMs uint64 //timeout for requesting Nacos server, default value is 10000ms
|
||||
ListenInterval uint64 //the time interval for pulling config change,default value is 30000ms
|
||||
BeatInterval int64 //the time interval for sending beat to server,default value is 5000ms
|
||||
NamespaceId string //the namespaceId of Nacos
|
||||
Endpoint string //the endpoint for get Nacos server addresses
|
||||
RegionId string //the regionId for kms
|
||||
AccessKey string //the AccessKey for kms
|
||||
SecretKey string //the SecretKey for kms
|
||||
OpenKMS bool //it's to open kms,default is false. https://help.aliyun.com/product/28933.html
|
||||
CacheDir string //the directory for persist nacos service info,default is current path
|
||||
UpdateThreadNum int //the number of gorutine for update nacos service info
|
||||
CacheDir string //the directory for persist nacos service info,default value is current path
|
||||
UpdateThreadNum int //the number of gorutine for update nacos service info,default value is 20
|
||||
NotLoadCacheAtStart bool //not to load persistent nacos service info in CacheDir at start time
|
||||
UpdateCacheWhenEmpty bool //update cache when get empty service instance from server
|
||||
Username string //the username for nacos auth
|
||||
Password string //the password for nacos auth
|
||||
LogDir string // the directory for log, default is current path
|
||||
RotateTime string //the rotate time for log, eg: 30m, 1h, 24h, default is 24h
|
||||
MaxAge int64 //the max age of a log file, default is 3
|
||||
LogLevel string //the level of log, it's must be debug,info,warn,error, default is info
|
||||
MaxAge int64 //the max age of a log file, default value is 3
|
||||
LogLevel string //the level of log, it's must be debug,info,warn,error, default value is info
|
||||
}
|
||||
|
|
|
@ -123,4 +123,13 @@ func main() {
|
|||
})
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
searchPage, _ := client.SearchConfig(vo.SearchConfigParm{
|
||||
Search: "blur",
|
||||
DataId: "",
|
||||
Group: "",
|
||||
PageNo: 1,
|
||||
PageSize: 10,
|
||||
})
|
||||
fmt.Printf("Search config:%+v \n", searchPage)
|
||||
|
||||
}
|
||||
|
|
|
@ -66,6 +66,7 @@ func main() {
|
|||
Enable: true,
|
||||
Healthy: true,
|
||||
Ephemeral: true,
|
||||
Metadata: map[string]string{"idc": "shanghai"},
|
||||
})
|
||||
|
||||
//Register with cluster name
|
||||
|
@ -193,7 +194,7 @@ func main() {
|
|||
ServiceName: "demo.go",
|
||||
})
|
||||
|
||||
//SelectInstances return one instance by WRR strategy for load balance
|
||||
//SelectOneHealthyInstance return one instance by WRR strategy for load balance
|
||||
//And the instance should be health=true,enable=true and weight>0
|
||||
//ClusterName=DEFAULT,GroupName=DEFAULT_GROUP
|
||||
ExampleServiceClient_SelectOneHealthyInstance(client, vo.SelectOneHealthInstanceParam{
|
||||
|
|
|
@ -49,30 +49,10 @@ type GetServiceParam struct {
|
|||
}
|
||||
|
||||
type GetAllServiceInfoParam struct {
|
||||
NameSpace string `param:"nameSpace"`
|
||||
GroupName string `param:"groupName"`
|
||||
PageNo uint32 `param:"pageNo"`
|
||||
PageSize uint32 `param:"pageSize"`
|
||||
}
|
||||
|
||||
type GetServiceListParam struct {
|
||||
StartPage uint32 `param:"startPg"`
|
||||
PageSize uint32 `param:"pgSize"`
|
||||
Keyword string `param:"keyword"`
|
||||
NamespaceId string `param:"namespaceId"`
|
||||
}
|
||||
|
||||
type GetServiceInstanceParam struct {
|
||||
Tenant string `param:"tenant"`
|
||||
HealthyOnly bool `param:"healthyOnly"`
|
||||
Cluster string `param:"cluster"`
|
||||
ServiceName string `param:"serviceName"`
|
||||
Ip string `param:"ip"`
|
||||
Port uint64 `param:"port"`
|
||||
}
|
||||
|
||||
type GetServiceDetailParam struct {
|
||||
ServiceName string `param:"serviceName"`
|
||||
NameSpace string `param:"nameSpace"` //optional,default:public
|
||||
GroupName string `param:"groupName"` //optional,default:DEFAULT_GROUP
|
||||
PageNo uint32 `param:"pageNo"` //optional,default:1
|
||||
PageSize uint32 `param:"pageSize"` //optional,default:10
|
||||
}
|
||||
|
||||
type SubscribeParam struct {
|
||||
|
@ -89,14 +69,14 @@ type SelectAllInstancesParam struct {
|
|||
}
|
||||
|
||||
type SelectInstancesParam struct {
|
||||
Clusters []string `param:"clusters"`
|
||||
ServiceName string `param:"serviceName"`
|
||||
GroupName string `param:"groupName"`
|
||||
HealthyOnly bool `param:"healthyOnly"`
|
||||
Clusters []string `param:"clusters"` //optional,default:DEFAULT
|
||||
ServiceName string `param:"serviceName"` //required
|
||||
GroupName string `param:"groupName"` //optional,default:DEFAULT_GROUP
|
||||
HealthyOnly bool `param:"healthyOnly"` //optional,return only healthy instance
|
||||
}
|
||||
|
||||
type SelectOneHealthInstanceParam struct {
|
||||
Clusters []string `param:"clusters"`
|
||||
ServiceName string `param:"serviceName"`
|
||||
GroupName string `param:"groupName"`
|
||||
Clusters []string `param:"clusters"` //optional,default:DEFAULT
|
||||
ServiceName string `param:"serviceName"` //required
|
||||
GroupName string `param:"groupName"` //optional,default:DEFAULT_GROUP
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue