Skip to content

Commit 9db55aa

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

File tree

8 files changed

+621
-302
lines changed

8 files changed

+621
-302
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making polaris-java available.
3+
*
4+
* Copyright (C) 2021 Tencent. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.plugins.connector.common.constant;
19+
20+
public interface NacosConstant {
21+
22+
interface MetadataMapKey {
23+
String NACOS_GROUP_KEY = "nacos.group";
24+
25+
String NACOS_CLUSTER_KEY = "nacos.cluster";
26+
27+
String NACOS_EPHEMERAL_KEY = "nacos.ephemeral";
28+
29+
String NACOS_SERVICE_KEY = "nacos.service";
30+
}
31+
}

polaris-plugins/polaris-plugins-connector/connector-composite/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,10 @@
3232
<artifactId>connector-common</artifactId>
3333
<version>${project.version}</version>
3434
</dependency>
35+
<dependency>
36+
<groupId>com.tencent.polaris</groupId>
37+
<artifactId>connector-nacos</artifactId>
38+
<version>${project.version}</version>
39+
</dependency>
3540
</dependencies>
3641
</project>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import com.tencent.polaris.plugins.connector.composite.zero.InstanceListMeta;
4343
import com.tencent.polaris.plugins.connector.consul.ConsulServiceUpdateTask;
4444
import com.tencent.polaris.plugins.connector.grpc.GrpcServiceUpdateTask;
45+
import com.tencent.polaris.plugins.connector.nacos.NacosServiceUpdateTask;
4546
import com.tencent.polaris.specification.api.v1.model.ModelProto;
4647
import com.tencent.polaris.specification.api.v1.service.manage.ResponseProto.DiscoverResponse;
4748
import com.tencent.polaris.specification.api.v1.service.manage.ServiceProto.Instance;
@@ -54,6 +55,7 @@
5455

5556
import static com.tencent.polaris.api.config.plugin.DefaultPlugins.SERVER_CONNECTOR_CONSUL;
5657
import static com.tencent.polaris.api.config.plugin.DefaultPlugins.SERVER_CONNECTOR_GRPC;
58+
import static com.tencent.polaris.api.config.plugin.DefaultPlugins.SERVER_CONNECTOR_NACOS;
5759
import static com.tencent.polaris.plugins.connector.common.constant.ConnectorConstant.ORDER_LIST;
5860
import static com.tencent.polaris.plugins.connector.common.constant.ConnectorConstant.SERVER_CONNECTOR_TYPE;
5961

@@ -96,6 +98,13 @@ public CompositeServiceUpdateTask(ServiceEventHandler handler, DestroyableServer
9698
ifMainConnectorTypeSet = true;
9799
}
98100
}
101+
if (SERVER_CONNECTOR_NACOS.equals(sc.getName()) && sc.isDiscoveryEnable()) {
102+
subServiceUpdateTaskMap.put(SERVER_CONNECTOR_NACOS, new NacosServiceUpdateTask(serviceEventHandler, sc));
103+
if (!ifMainConnectorTypeSet) {
104+
mainConnectorType = sc.getName();
105+
ifMainConnectorTypeSet = true;
106+
}
107+
}
99108
}
100109
}
101110

polaris-plugins/polaris-plugins-connector/connector-consul/src/main/java/com/tencent/polaris/plugins/connector/consul/ConsulAPIConnector.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class ConsulAPIConnector extends DestroyableServerConnector {
8989
*/
9090
private boolean initialized = false;
9191

92-
private boolean ieRegistered = false;
92+
private boolean isRegistered = false;
9393

9494
private String id;
9595
private boolean isRegisterEnable = true;
@@ -294,7 +294,7 @@ public void deRegisterServiceHandler(ServiceEventKey eventKey) throws PolarisExc
294294
@Override
295295
public CommonProviderResponse registerInstance(CommonProviderRequest req, Map<String, String> customHeader)
296296
throws PolarisException {
297-
if (isRegisterEnable() && !ieRegistered) {
297+
if (isRegisterEnable() && !isRegistered) {
298298
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
299299
try {
300300
LOG.info("Registering service to Consul");
@@ -327,7 +327,7 @@ public CommonProviderResponse registerInstance(CommonProviderRequest req, Map<St
327327
resp.setInstanceID(service.getId());
328328
resp.setExists(false);
329329
LOG.info("Registered service to Consul: " + service);
330-
ieRegistered = true;
330+
isRegistered = true;
331331
// heartbeat when registration is successful.
332332
heartbeat(req);
333333
return resp;
@@ -399,13 +399,13 @@ private NewService buildRegisterInstanceRequest(CommonProviderRequest req) {
399399

400400
@Override
401401
public void deregisterInstance(CommonProviderRequest req) throws PolarisException {
402-
if (ieRegistered) {
402+
if (isRegistered) {
403403
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
404404
try {
405405
LOG.info("Unregistering service to Consul: " + consulContext.getInstanceId());
406406
this.consulClient.agentServiceDeregister(consulContext.getInstanceId(), consulContext.getAclToken());
407407
LOG.info("Unregistered service to Consul: " + consulContext.getInstanceId());
408-
ieRegistered = false;
408+
isRegistered = false;
409409
} catch (ConsulException e) {
410410
throw new RetriableException(ErrorCode.NETWORK_ERROR,
411411
String.format("fail to deregister host %s:%d service %s", req.getHost(), req.getPort(),
@@ -416,7 +416,7 @@ public void deregisterInstance(CommonProviderRequest req) throws PolarisExceptio
416416

417417
@Override
418418
public void heartbeat(CommonProviderRequest req) throws PolarisException {
419-
if (ieRegistered) {
419+
if (isRegistered) {
420420
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
421421
try {
422422
this.ttlConsulClient.agentCheckPass(consulContext.getCheckId(), null, consulContext.getAclToken());

0 commit comments

Comments
 (0)