Skip to content

Commit 73c846b

Browse files
authored
fix:nacos client suit (#74)
* fix:nacos client suit * fix:nacos client suit
1 parent 9aea2c6 commit 73c846b

File tree

13 files changed

+264
-286
lines changed

13 files changed

+264
-286
lines changed

conf/sync-config.json

Lines changed: 1 addition & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,5 @@
11
{
2-
"tasks": [
3-
{
4-
"name": "nacos-polaris",
5-
"enable": true,
6-
"source": {
7-
"name": "nacos->polaris",
8-
"type": "nacos",
9-
"server": {
10-
"addresses": [
11-
"127.0.0.1:8848"
12-
]
13-
},
14-
"db": {
15-
"jdbc_url": "jdbc:mysql://127.0.0.1:3306/nacos",
16-
"username": "root",
17-
"password": "root"
18-
}
19-
},
20-
"destination": {
21-
"name": "polaris",
22-
"type": "polaris",
23-
"server": {
24-
"addresses": [
25-
"http://127.0.0.1:8090",
26-
"grpc://127.0.0.1:8091"
27-
],
28-
"token": "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I="
29-
},
30-
"db": {
31-
"jdbc_url": "jdbc:mysql://127.0.0.1:3306/polaris_server",
32-
"username": "root",
33-
"password": "root"
34-
}
35-
},
36-
"match": [
37-
{
38-
"namespace": "empty_ns",
39-
"config_group": "DEFAULT_GROUP"
40-
},
41-
{
42-
"namespace": "springliao",
43-
"config_group": "*"
44-
}
45-
]
46-
},
47-
{
48-
"name": "polaris-nacos",
49-
"enable": true,
50-
"source": {
51-
"name": "polaris->nacos",
52-
"type": "polaris",
53-
"server": {
54-
"token": "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I=",
55-
"addresses": [
56-
"http://127.0.0.1:8090",
57-
"grpc://127.0.0.1:8091"
58-
]
59-
},
60-
"db": {
61-
"jdbc_url": "jdbc:mysql://127.0.0.1:3306/polaris_server",
62-
"username": "root",
63-
"password": "root"
64-
}
65-
},
66-
"destination": {
67-
"name": "nacos",
68-
"type": "nacos",
69-
"server": {
70-
"addresses": [
71-
"127.0.0.1:8848"
72-
],
73-
"user": "nacos",
74-
"password": "nacos"
75-
},
76-
"db": {
77-
"jdbc_url": "jdbc:mysql://127.0.0.1:3306/nacos",
78-
"username": "root",
79-
"password": "root"
80-
}
81-
},
82-
"match": [
83-
{
84-
"namespace": "ns1",
85-
"config_group": "*"
86-
}
87-
]
88-
}
89-
],
2+
"tasks": [],
903
"methods": [
914
{
925
"type": "watch",

conf/sync-registry.json

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,5 @@
11
{
2-
"tasks": [
3-
{
4-
"name": "task1",
5-
"enable": true,
6-
"source": {
7-
"name": "nacos",
8-
"type": "nacos",
9-
"user": "nacos",
10-
"password": "nacos",
11-
"addresses": [
12-
"127.0.0.1:8848"
13-
]
14-
},
15-
"destination": {
16-
"name": "polaris",
17-
"type": "polaris",
18-
"token": "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I=",
19-
"addresses": [
20-
"http://127.0.0.1:8090",
21-
"grpc://127.0.0.1:8091"
22-
]
23-
},
24-
"match": [
25-
{
26-
"namespace": "atom-ns1",
27-
"service": "nacos.test.3"
28-
},
29-
{
30-
"namespace": "atom-ns1",
31-
"service": "nacos.test.4"
32-
}
33-
]
34-
},
35-
{
36-
"name": "task2",
37-
"enable": true,
38-
"source": {
39-
"name": "polaris",
40-
"type": "polaris",
41-
"token": "nu/0WRA4EqSR1FagrjRj0fZwPXuGlMpX+zCuWu4uMqy8xr1vRjisSbA25aAC3mtU8MeeRsKhQiDAynUR09I=",
42-
"addresses": [
43-
"http://127.0.0.1:8090",
44-
"grpc://127.0.0.1:8091"
45-
]
46-
},
47-
"destination": {
48-
"name": "nacos",
49-
"type": "nacos",
50-
"addresses": [
51-
"127.0.0.1:8848"
52-
]
53-
},
54-
"match": [
55-
{
56-
"namespace": "Polaris",
57-
"service": "polaris.checker"
58-
},
59-
{
60-
"namespace": "default",
61-
"service": "EchoServer"
62-
}
63-
]
64-
}
65-
],
2+
"tasks": [],
663
"methods": [
674
{
685
"type": "watch",

polaris-sync-config-plugins/config-nacos/src/main/java/cn/polarismesh/polaris/sync/config/plugins/nacos/NacosConfigCenter.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ public void updateGroups(Collection<ConfigGroup> groups) {
240240
if (namespaceIds.isEmpty()) {
241241
return;
242242
}
243-
244243
ResourceEndpoint endpoint = request.getResourceEndpoint();
245244
AuthResponse authResponse = new AuthResponse();
246245
// 1. 先进行登录
@@ -261,7 +260,7 @@ public void updateGroups(Collection<ConfigGroup> groups) {
261260
List<NacosNamespace> nacosNamespaces = new ArrayList<>();
262261
ResponseProto.DiscoverResponse discoverResponse = NacosRestUtils
263262
.discoverAllNamespaces(authResponse, restOperator, endpoint, nacosNamespaces);
264-
if (null == discoverResponse) {
263+
if (Objects.nonNull(discoverResponse)) {
265264
return;
266265
}
267266
for (NacosNamespace nacosNamespace : nacosNamespaces) {
@@ -307,7 +306,7 @@ public void updateConfigFiles(ConfigGroup group, Collection<ConfigFile> files) {
307306
if (file.isBeta() || !file.isValid()) {
308307
return;
309308
}
310-
309+
file.setNamespace(toNamespaceId(file.getNamespace()));
311310
if (Objects.equals(file.getLabels().get(DefaultValues.META_SYNC), request.getResourceEndpoint()
312311
.getName())) {
313312
return;

polaris-sync-config-plugins/config-nacos/src/main/java/cn/polarismesh/polaris/sync/config/plugins/nacos/NacosRestUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public static void createNamespace(AuthResponse authResponse,
9393
}
9494
HttpMethod method = HttpMethod.POST;
9595
String requestText = String.format("customNamespaceId=%s&namespaceName=%s&namespaceDesc=", namespace, namespace);
96+
namespacesUrl += "&" + requestText;
9697
RestResponse<String> restResponse = restOperator
9798
.curlRemoteEndpoint(namespacesUrl, method, new HttpEntity<>(requestText), String.class);
9899
if (restResponse.hasServerError()) {

polaris-sync-core/src/main/java/cn/polarismesh/polaris/sync/core/tasks/config/AbstractTask.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,23 @@ public interface AbstractTask extends Runnable {
3737
* @param files
3838
* @return
3939
*/
40-
default Collection<ConfigFile> handle(NamedConfigCenter source, NamedConfigCenter destination, Collection<ConfigFile> files) {
41-
if (ResourceType.POLARIS.equals(destination.getConfigCenter().getType()) && ResourceType.NACOS.equals(source.getConfigCenter().getType())) {
40+
default Collection<ConfigFile> handle(NamedConfigCenter source, NamedConfigCenter destination,
41+
Collection<ConfigFile> files) {
42+
if (ResourceType.POLARIS.equals(destination.getConfigCenter().getType()) && ResourceType.NACOS.equals(
43+
source.getConfigCenter().getType())) {
4244
return files.stream().peek(file -> {
43-
if (Objects.equals("", file.getNamespace())) {
45+
if (Objects.equals("", file.getNamespace()) || Objects.equals(DefaultValues.EMPTY_NAMESPACE_HOLDER,
46+
file.getNamespace())) {
4447
file.setNamespace(DefaultValues.DEFAULT_POLARIS_NAMESPACE);
4548
}
4649
}).collect(Collectors.toList());
4750
}
4851

49-
if (ResourceType.POLARIS.equals(source.getConfigCenter().getType()) && ResourceType.NACOS.equals(destination.getConfigCenter().getType())) {
52+
if (ResourceType.POLARIS.equals(source.getConfigCenter().getType()) && ResourceType.NACOS.equals(
53+
destination.getConfigCenter().getType())) {
5054
return files.stream().peek(file -> {
5155
if (Objects.equals(DefaultValues.DEFAULT_POLARIS_NAMESPACE, file.getNamespace())) {
52-
file.setNamespace("");
56+
file.setNamespace(DefaultValues.EMPTY_NAMESPACE_HOLDER);
5357
}
5458
}).collect(Collectors.toList());
5559
}

polaris-sync-core/src/main/java/cn/polarismesh/polaris/sync/core/tasks/config/PullTask.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,16 @@ public void run() {
7171
ConfigGroup configGroup = entry.getKey();
7272
ConfigFilesResponse response = source.getConfigCenter().listConfigFile(configGroup);
7373
if (response.getCode() != StatusCodes.SUCCESS) {
74-
LOG.warn("[Core][Pull] config fail to list service in source {}, type {}, group {}, code is {}",
74+
LOG.warn("[Core][Pull] config fail to list config_files in source {}, type {}, group {}, code is {}",
7575
source.getName(), source.getConfigCenter().getType(), configGroup.getName(),
7676
response.getCode());
7777
return;
7878
}
7979
Collection<ConfigFile> files = handle(source, destination, response.getFiles());
8080
LOG.debug(
81-
"[Core][Pull] config prepare to update from registry {}, type {}, service {}, group {}, instances {}",
82-
source.getName(), source.getConfigCenter().getType(), configGroup, configGroup.getName(), files);
81+
"[Core][Pull] config prepare to update from config_center {}, type {}, service {}, group {}, instances {}",
82+
source.getName(), source.getConfigCenter()
83+
.getType(), configGroup, configGroup.getName(), files);
8384
destination.getConfigCenter().updateConfigFiles(configGroup, files);
8485
}
8586
}

polaris-sync-core/src/main/java/cn/polarismesh/polaris/sync/core/tasks/registry/AbstractTask.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,25 @@ public interface AbstractTask extends Runnable {
3333
* 如果是从 nacos -> polaris, 则需要将 nacos 的默认命名空间转为 polaris 的默认命名空间 default
3434
*
3535
* @param service
36-
* @return
36+
* @return {@link Service}
3737
*/
3838
default Service handle(NamedRegistryCenter source, NamedRegistryCenter destination, Service service) {
39+
// 处理目标是 polaris,源是 nacos
3940
if (ResourceType.POLARIS.equals(destination.getRegistry()
4041
.getType()) && ResourceType.NACOS.equals(source.getRegistry().getType())) {
4142
String ns = service.getNamespace();
42-
if (Objects.equals("", service.getNamespace())) {
43+
if (Objects.equals(DefaultValues.EMPTY_NAMESPACE_HOLDER, service.getNamespace())) {
4344
ns = DefaultValues.DEFAULT_POLARIS_NAMESPACE;
4445
}
4546
return new Service(ns, service.getService());
4647
}
4748

49+
// 处理目标是 nacos,源是 polaris
4850
if (ResourceType.POLARIS.equals(source.getRegistry()
4951
.getType()) && ResourceType.NACOS.equals(destination.getRegistry().getType())) {
5052
String ns = service.getNamespace();
5153
if (Objects.equals(DefaultValues.DEFAULT_POLARIS_NAMESPACE, service.getNamespace())) {
52-
ns = DefaultValues.DEFAULT_POLARIS_NAMESPACE;
54+
ns = DefaultValues.EMPTY_NAMESPACE_HOLDER;
5355
}
5456
return new Service(ns, service.getService());
5557
}

polaris-sync-core/src/main/java/cn/polarismesh/polaris/sync/core/tasks/registry/PullTask.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import java.util.HashMap;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.stream.Collectors;
2627

2728
import cn.polarismesh.polaris.sync.core.tasks.SyncTask;
2829
import cn.polarismesh.polaris.sync.core.utils.ConfigUtils;
2930
import cn.polarismesh.polaris.sync.core.utils.TaskUtils;
3031
import cn.polarismesh.polaris.sync.extension.registry.Service;
3132
import cn.polarismesh.polaris.sync.extension.utils.StatusCodes;
3233
import cn.polarismesh.polaris.sync.model.pb.ModelProto;
34+
import com.google.protobuf.StringValue;
3335
import com.tencent.polaris.client.pb.ResponseProto.DiscoverResponse;
3436
import com.tencent.polaris.client.pb.ServiceProto.Instance;
3537
import org.slf4j.Logger;
@@ -52,12 +54,10 @@ public PullTask(NamedRegistryCenter source, NamedRegistryCenter destination, Lis
5254
if (ConfigUtils.isEmptyMatch(match)) {
5355
continue;
5456
}
55-
serviceToGroups.put(
56-
new Service(match.getNamespace(), match.getName()), TaskUtils.verifyGroups(match.getGroups()));
57+
serviceToGroups.put(new Service(match.getNamespace(), match.getName()), TaskUtils.verifyGroups(match.getGroups()));
5758
}
5859
}
5960

60-
6161
@Override
6262
public void run() {
6363
try {
@@ -72,19 +72,24 @@ public void run() {
7272
// check instances
7373
for (Map.Entry<Service, Collection<ModelProto.Group>> entry : serviceToGroups.entrySet()) {
7474
Service service = entry.getKey();
75-
service = handle(source, destination, service);
7675
for (ModelProto.Group group : entry.getValue()) {
7776
DiscoverResponse srcInstanceResponse = source.getRegistry().listInstances(service, group);
7877
if (srcInstanceResponse.getCode().getValue() != StatusCodes.SUCCESS) {
79-
LOG.warn("[Core][Pull] fail to list service in source {}, type {}, group {}, code is {}",
80-
source.getName(), source.getRegistry().getType(), group.getName(),
81-
srcInstanceResponse.getCode().getValue());
78+
LOG.warn("[Core][Pull] fail to list service in source {}, type {}, group {}, code is {}", source.getName(), source.getRegistry()
79+
.getType(), group.getName(), srcInstanceResponse.getCode().getValue());
8280
return;
8381
}
8482
List<Instance> instances = srcInstanceResponse.getInstancesList();
85-
LOG.info(
86-
"[Core][Pull]prepare to update from registry {}, type {}, service {}, group {}, instances {}",
87-
source.getName(), source.getRegistry().getType(), service, group.getName(), instances);
83+
service = handle(source, destination, service);
84+
Service finalService = service;
85+
instances = instances.stream().map(instance -> {
86+
return Instance.newBuilder(instance)
87+
.setNamespace(StringValue.newBuilder().setValue(finalService.getNamespace()).build())
88+
.setService(StringValue.newBuilder().setValue(finalService.getService()).build())
89+
.build();
90+
}).collect(Collectors.toList());
91+
LOG.info("[Core][Pull]prepare to update from registry {}, type {}, service {}, group {}, instances {}", source.getName(), source.getRegistry()
92+
.getType(), service, group.getName(), instances);
8893
destination.getRegistry().updateInstances(service, group, instances);
8994
}
9095
}

0 commit comments

Comments
 (0)