Skip to content

Commit 20e4dbc

Browse files
author
fishtailfu
committed
feat: support dual-discovery
1 parent 9db55aa commit 20e4dbc

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

polaris-plugins/polaris-plugins-connector/connector-composite/src/main/java/com/tencent/polaris/plugins/connector/composite/CompositeServiceUpdateTask.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public class CompositeServiceUpdateTask extends ServiceUpdateTask {
8585
public CompositeServiceUpdateTask(ServiceEventHandler handler, DestroyableServerConnector connector) {
8686
super(handler, connector);
8787
CompositeConnector compositeConnector = (CompositeConnector) connector;
88+
EventType eventType = handler.getServiceEventKey().getEventType();
8889
for (DestroyableServerConnector sc : compositeConnector.getServerConnectors()) {
8990
if (SERVER_CONNECTOR_GRPC.equals(sc.getName()) && sc.isDiscoveryEnable()) {
9091
subServiceUpdateTaskMap.put(SERVER_CONNECTOR_GRPC, new GrpcServiceUpdateTask(serviceEventHandler, sc));
@@ -98,7 +99,9 @@ public CompositeServiceUpdateTask(ServiceEventHandler handler, DestroyableServer
9899
ifMainConnectorTypeSet = true;
99100
}
100101
}
101-
if (SERVER_CONNECTOR_NACOS.equals(sc.getName()) && sc.isDiscoveryEnable()) {
102+
// nacos仅支持服务事件和实例事件
103+
if (SERVER_CONNECTOR_NACOS.equals(sc.getName()) && sc.isDiscoveryEnable()
104+
&& (eventType == EventType.INSTANCE || eventType == EventType.SERVICE)) {
102105
subServiceUpdateTaskMap.put(SERVER_CONNECTOR_NACOS, new NacosServiceUpdateTask(serviceEventHandler, sc));
103106
if (!ifMainConnectorTypeSet) {
104107
mainConnectorType = sc.getName();

polaris-plugins/polaris-plugins-connector/connector-nacos/src/main/java/com/tencent/polaris/plugins/connector/nacos/NacosService.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
package com.tencent.polaris.plugins.connector.nacos;
2323

24-
import static com.tencent.polaris.api.config.plugin.DefaultPlugins.SERVER_CONNECTOR_CONSUL;
2524
import static com.tencent.polaris.api.config.plugin.DefaultPlugins.SERVER_CONNECTOR_NACOS;
2625
import static com.tencent.polaris.plugins.connector.common.constant.ConnectorConstant.SERVER_CONNECTOR_TYPE;
2726

@@ -41,8 +40,6 @@
4140
import com.tencent.polaris.api.exception.ServerCodes;
4241
import com.tencent.polaris.api.exception.ServerErrorResponseException;
4342
import com.tencent.polaris.api.plugin.server.ServerEvent;
44-
import com.tencent.polaris.api.pojo.ServiceInfo;
45-
import com.tencent.polaris.api.pojo.Services;
4643
import com.tencent.polaris.api.utils.CollectionUtils;
4744
import com.tencent.polaris.api.utils.StringUtils;
4845
import com.tencent.polaris.api.utils.ThreadPoolUtils;
@@ -51,10 +48,8 @@
5148
import com.tencent.polaris.plugins.connector.common.ServiceUpdateTask;
5249
import com.tencent.polaris.specification.api.v1.service.manage.ResponseProto;
5350
import com.tencent.polaris.specification.api.v1.service.manage.ServiceProto;
54-
import com.tencent.polaris.specification.api.v1.service.manage.ServiceProto.Service;
5551
import java.nio.charset.StandardCharsets;
5652
import java.util.ArrayList;
57-
import java.util.HashMap;
5853
import java.util.LinkedHashSet;
5954
import java.util.List;
6055
import java.util.Map;
@@ -67,7 +62,7 @@ public class NacosService extends Destroyable {
6762

6863
private static final Logger LOG = LoggerFactory.getLogger(NacosService.class);
6964

70-
private NamingService namingService;
65+
private final NamingService namingService;
7166

7267
private NacosContext nacosContext;
7368

@@ -83,7 +78,22 @@ public NacosService(NamingService namingService, NacosContext nacosContext) {
8378
}
8479

8580

86-
public void doInstanceSubscribe(ServiceUpdateTask serviceUpdateTask) {
81+
public void sendInstanceRequest(ServiceUpdateTask serviceUpdateTask) {
82+
83+
refreshExecutor.submit(() -> {
84+
try {
85+
asyncGetInstances(serviceUpdateTask);
86+
} catch (Exception e) {
87+
LOG.error("Get nacos service instances of {} failed. ",
88+
serviceUpdateTask.getServiceEventKey().getService(), e);
89+
throw new RuntimeException(e);
90+
}
91+
});
92+
93+
}
94+
95+
public void asyncGetInstances(ServiceUpdateTask serviceUpdateTask) {
96+
8797
// 通过namingService订阅服务监听,当服务有变化时,回调serviceListener,将结果notify给polaris
8898
EventListener serviceListener = event -> {
8999
try {
@@ -154,7 +164,7 @@ public void doInstanceSubscribe(ServiceUpdateTask serviceUpdateTask) {
154164
String.format("Get service instances of %s sync failed.",
155165
serviceUpdateTask.getServiceEventKey().getServiceKey()));
156166
ServerEvent serverEvent = new ServerEvent(serviceUpdateTask.getServiceEventKey(), null, error,
157-
SERVER_CONNECTOR_CONSUL);
167+
SERVER_CONNECTOR_NACOS);
158168
serviceUpdateTask.notifyServerEvent(serverEvent);
159169
serviceUpdateTask.retry();
160170
}
@@ -233,7 +243,7 @@ private void syncGetService(ServiceUpdateTask serviceUpdateTask) {
233243
newDiscoverResponseBuilder.setCode(UInt32Value.of(code));
234244

235245
ServerEvent serverEvent = new ServerEvent(serviceUpdateTask.getServiceEventKey(),
236-
newDiscoverResponseBuilder.build(), null, SERVER_CONNECTOR_CONSUL);
246+
newDiscoverResponseBuilder.build(), null, SERVER_CONNECTOR_NACOS);
237247
boolean svcDeleted = serviceUpdateTask.notifyServerEvent(serverEvent);
238248

239249
if (!svcDeleted) {

polaris-plugins/polaris-plugins-connector/connector-nacos/src/main/java/com/tencent/polaris/plugins/connector/nacos/NacosServiceUpdateTask.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package com.tencent.polaris.plugins.connector.nacos;
1919

20-
import com.alibaba.nacos.api.naming.NamingService;
2120
import com.tencent.polaris.api.plugin.server.ServerEvent;
2221
import com.tencent.polaris.api.plugin.server.ServiceEventHandler;
2322
import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
@@ -63,7 +62,7 @@ public void execute(ServiceUpdateTask serviceUpdateTask) {
6362
if (serviceUpdateTask.getServiceEventKey().getEventType() == EventType.SERVICE) {
6463
nacosService.sendServiceRequest(serviceUpdateTask);
6564
} else if (serviceUpdateTask.getServiceEventKey().getEventType() == EventType.INSTANCE) {
66-
nacosService.doInstanceSubscribe(serviceUpdateTask);
65+
nacosService.sendInstanceRequest(serviceUpdateTask);
6766
}
6867
}
6968
}

0 commit comments

Comments
 (0)