# 1.拉取镜像
docker pull nacos/nacos-server
1
# 2.启动nacos容器
# 2.1 单机部署
docker run -d --name nacos -p 8848:8848 --restart=always --link=mysql8.0 -e JVM_XMS=256m -e JVM_XMX=256m -e MODE=standalone -e SPRING_DATASOURCE_PLATFORM=mysql -e MYSQL_SERVICE_HOST=mysql8.0 -e MYSQL_SERVICE_PORT=3306 -e MYSQL_SERVICE_DB_NAME=nacos -e MYSQL_SERVICE_USER=root -e MYSQL_SERVICE_PASSWORD=123456 -e MYSQL_SERVICE_DB_PARAM="characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC" nacos/nacos-server
1
mysql8.0
是我docker里面现有的,直接使用即可。
# 2.2 集群部署
docker run -d --name nacos -p 8848:8848 --restart=always --link=mysql8.0 -e JVM_XMS=256m -e JVM_XMX=256m -e MODE=cluster -e NACOS_APPLICATION_PORT=8848 -e NACOS_SERVERS=127.0.0.1:8848,127.0.0.1:8849,127.0.0.1:8850 -e SPRING_DATASOURCE_PLATFORM=mysql -e MYSQL_SERVICE_HOST=mysql8.0 -e MYSQL_SERVICE_PORT=3306 -e MYSQL_SERVICE_DB_NAME=nacos -e MYSQL_SERVICE_USER=root -e MYSQL_SERVICE_PASSWORD=123456 -e MYSQL_SERVICE_DB_PARAM="characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC" nacos/nacos-server
1
或者直接使用 官方 nacos-docker (opens new window) 里面的 docker-compose
文件直接运行即可。
cluster-hostname.yaml
version: "3"
services:
nacos1:
hostname: nacos1
container_name: nacos1
image: nacos/nacos-server:v2.0.4
volumes:
- ./cluster-logs/nacos1:/home/nacos/logs
- ./init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8848:8848"
- "9848:9848"
- "9555:9555"
env_file:
- ../env/nacos-hostname.env
restart: always
depends_on:
- mysql
nacos2:
hostname: nacos2
image: nacos/nacos-server:v2.0.4
container_name: nacos2
volumes:
- ./cluster-logs/nacos2:/home/nacos/logs
- ./init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8849:8848"
- "9849:9848"
env_file:
- ../env/nacos-hostname.env
restart: always
depends_on:
- mysql
nacos3:
hostname: nacos3
image: nacos/nacos-server:v2.0.4
container_name: nacos3
volumes:
- ./cluster-logs/nacos3:/home/nacos/logs
- ./init.d/custom.properties:/home/nacos/init.d/custom.properties
ports:
- "8850:8848"
- "9850:9848"
env_file:
- ../env/nacos-hostname.env
restart: always
depends_on:
- mysql
mysql:
container_name: mysql
image: nacos/nacos-mysql:5.7
env_file:
- ../env/mysql.env
volumes:
- ./mysql:/var/lib/mysql
ports:
- "3306:3306"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
安装成功后的效果如下:
# 3.快速使用
# 3.1 安装包
使用go get
安装SDK:
go get -u github.com/nacos-group/nacos-sdk-go
1
# 3.2 配置项
constant.ClientConfig{
TimeoutMs uint64 // 请求Nacos服务端的超时时间,默认是10000ms
NamespaceId string // ACM的命名空间Id
AppName string // App名称
Endpoint string // 当使用ACM时,需要该配置. https://help.aliyun.com/document_detail/130146.html
RegionId string // ACM&KMS的regionId,用于配置中心的鉴权
AccessKey string // ACM&KMS的AccessKey,用于配置中心的鉴权
SecretKey string // ACM&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 // 日志存储路径
LogLevel string // 日志默认级别,值必须是:debug,info,warn,error,默认值是info
LogSampling *ClientLogSamplingConfig // 日志采样配置
LogRollingConfig *ClientLogRollingConfig // 日志归档配置
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
- ServerConfig
constant.ServerConfig{
ContextPath string // Nacos的ContextPath
IpAddr string // Nacos的服务地址
Port uint64 // Nacos的服务端口
Scheme string // Nacos的服务地址前缀
}
1
2
3
4
5
6
2
3
4
5
6
Note:我们可以配置多个ServerConfig,客户端会对这些服务端做轮询请求
# 3.3 创建连接
// 创建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 := []constant.ServerConfig{
*constant.NewServerConfig(
"console1.nacos.io",
80,
constant.WithScheme("http"),
constant.WithContextPath("/nacos")
),
*constant.NewServerConfig(
"console2.nacos.io",
80,
constant.WithScheme("http"),
constant.WithContextPath("/nacos")
),
}
// 创建服务发现客户端
_, _ := clients.CreateNamingClient(map[string]interface{}{
"serverConfigs": serverConfigs,
"clientConfig": clientConfig,
})
// 创建动态配置客户端
_, _ := clients.CreateConfigClient(map[string]interface{}{
"serverConfigs": serverConfigs,
"clientConfig": clientConfig,
})
// 创建服务发现客户端的另一种方式 (推荐)
namingClient, err := clients.NewNamingClient(
vo.NacosClientParam{
ClientConfig: &clientConfig,
ServerConfigs: serverConfigs,
},
)
// 创建动态配置客户端的另一种方式 (推荐)
configClient, err := clients.NewConfigClient(
vo.NacosClientParam{
ClientConfig: &clientConfig,
ServerConfigs: serverConfigs,
},
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
Create client for ACM
https://help.aliyun.com/document_detail/130146.html (opens new window)
cc := constant.ClientConfig{
Endpoint: "acm.aliyun.com:8080",
NamespaceId: "e525eafa-f7d7-4029-83d9-008937f9d468",
RegionId: "cn-shanghai",
AccessKey: "LTAI4G8KxxxxxxxxxxxxxbwZLBr",
SecretKey: "n5jTL9YxxxxxxxxxxxxaxmPLZV9",
OpenKMS: true,
TimeoutMs: 5000,
LogLevel: "debug",
}
// a more graceful way to create config client
client, err := clients.NewConfigClient(
vo.NacosClientParam{
ClientConfig: &cc,
},
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 3.4 服务发现
- 注册实例:RegisterInstance
success, _ := namingClient.RegisterInstance(vo.RegisterInstanceParam{
Ip: "127.0.0.1",
Port: 9501,
ServiceName: "user-service",
Weight: 10,
Enable: true,
Healthy: true,
Ephemeral: false,
Metadata: map[string]string{"idc": "shanghai"},
ClusterName: "default", // 默认值DEFAULT
GroupName: "rx", // 默认值DEFAULT_GROUP
})
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
- 注销实例:DeregisterInstance
success, err := namingClient.DeregisterInstance(vo.DeregisterInstanceParam{
Ip: "127.0.0.1",
Port: 9501,
ServiceName: "user-service",
Ephemeral: true,
Cluster: "default", // 默认值DEFAULT
GroupName: "rx", // 默认值DEFAULT_GROUP
})
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- 获取服务信息:GetService
services, err := namingClient.GetService(vo.GetServiceParam{
ServiceName: "user-service",
Clusters: []string{"default"}, // 默认值DEFAULT
GroupName: "rx", // 默认值DEFAULT_GROUP
})
1
2
3
4
5
2
3
4
5
- 获取所有的实例列表:SelectAllInstances
// SelectAllInstance可以返回全部实例列表,包括healthy=false,enable=false,weight<=0
instances, err := namingClient.SelectAllInstances(vo.SelectAllInstancesParam{
ServiceName: "user-service",
GroupName: "rx", // 默认值DEFAULT_GROUP
Clusters: []string{"default"}, // 默认值DEFAULT
})
1
2
3
4
5
6
2
3
4
5
6
- 获取实例列表 :SelectInstances
// SelectInstances 只返回满足这些条件的实例列表:healthy=${HealthyOnly},enable=true 和weight>0
instances, err := namingClient.SelectInstances(vo.SelectInstancesParam{
ServiceName: "user-service",
GroupName: "rx", // 默认值DEFAULT_GROUP
Clusters: []string{"default"}, // 默认值DEFAULT
HealthyOnly: true,
})
1
2
3
4
5
6
7
2
3
4
5
6
7
- 获取一个健康的实例(加权随机轮询):SelectOneHealthyInstance
// SelectOneHealthyInstance将会按加权随机轮询的负载均衡策略返回一个健康的实例
// 实例必须满足的条件:health=true,enable=true and weight>0
instance, err := namingClient.SelectOneHealthyInstance(vo.SelectOneHealthInstanceParam{
ServiceName: "user-service",
GroupName: "rx", // 默认值DEFAULT_GROUP
Clusters: []string{"default"}, // 默认值DEFAULT
})
1
2
3
4
5
6
7
2
3
4
5
6
7
- 监听服务变化:Subscribe
// Subscribe key=serviceName+groupName+cluster
// 注意:我们可以在相同的key添加多个SubscribeCallback.
err := namingClient.Subscribe(vo.SubscribeParam{
ServiceName: "user-service",
GroupName: "rx", // 默认值DEFAULT_GROUP
Clusters: []string{"default"}, // 默认值DEFAULT
SubscribeCallback: func(services []model.SubscribeService, err error) {
log.Printf("\n\n callback return services:%s \n\n", util.ToJsonString(services))
},
})
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 取消服务监听:Unsubscribe
err := namingClient.Unsubscribe(vo.SubscribeParam{
ServiceName: "user-service",
GroupName: "rx", // 默认值DEFAULT_GROUP
Clusters: []string{"default"}, // 默认值DEFAULT
SubscribeCallback: func(services []model.SubscribeService, err error) {
log.Printf("\n\n callback return services:%s \n\n", utils.ToJsonString(services))
},
})
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
- 获取服务名列表:GetAllServicesInfo
serviceInfos, err := namingClient.GetAllServicesInfo(vo.GetAllServiceInfoParam{
NameSpace: "default",
GroupName: "rx",
PageNo: 1,
PageSize: 10,
}),
1
2
3
4
5
6
2
3
4
5
6
# 3.5 动态配置
- 发布配置:PublishConfig
success, err := configClient.PublishConfig(vo.ConfigParam{
DataId: "dataId",
Group: "group",
Content: "hello world!"})
1
2
3
4
2
3
4
- 删除配置:DeleteConfig
success, err = configClient.DeleteConfig(vo.ConfigParam{
DataId: "dataId",
Group: "group"})
1
2
3
4
5
2
3
4
5
- 获取配置:GetConfig
content, err := configClient.GetConfig(vo.ConfigParam{
DataId: "dataId",
Group: "group"})
1
2
3
4
5
2
3
4
5
- 监听配置变化:ListenConfig
err := configClient.ListenConfig(vo.ConfigParam{
DataId: "dataId",
Group: "group",
OnChange: func(namespace, group, dataId, data string) {
fmt.Println("group:" + group + ", dataId:" + dataId + ", data:" + data)
},
})
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- 取消配置监听:CancelListenConfig
err := configClient.CancelListenConfig(vo.ConfigParam{
DataId: "dataId",
Group: "group",
})
1
2
3
4
5
6
2
3
4
5
6
- 搜索配置: SearchConfig
configPage,err := configClient.SearchConfig(vo.SearchConfigParam{
Search: "blur",
DataId: "",
Group: "",
PageNo: 1,
PageSize: 10,
})
1
2
3
4
5
6
7
2
3
4
5
6
7
# 参考
- https://github.com/nacos-group/nacos-docker (opens new window)
- https://blog.csdn.net/Tianwen55/article/details/123863217 (opens new window)
- http://events.jianshu.io/p/31575a674b30 (opens new window)
- https://blog.csdn.net/u011374856/article/details/109204466 (opens new window)
- https://github.com/alibaba/nacos/blob/master/distribution/conf/nacos-mysql.sql (opens new window)
- https://developer.aliyun.com/article/931380 (opens new window)
- https://nacos.io/en-us/docs/open-api.html (opens new window)