Skip to content

Commit 8428598

Browse files
authored
feat: support dual-register. (#646)
1 parent 5a3b692 commit 8428598

File tree

12 files changed

+721
-390
lines changed

12 files changed

+721
-390
lines changed

polaris-distribution/polaris-all/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@
167167
<shadedPattern>shade.polaris.org.apache.org.apache.http
168168
</shadedPattern>
169169
</relocation>
170+
<relocation>
171+
<pattern>com.alibaba</pattern>
172+
<shadedPattern>shade.polaris.com.alibaba
173+
</shadedPattern>
174+
</relocation>
170175
<relocation>
171176
<pattern>io.perfmark</pattern>
172177
<shadedPattern>shade.polaris.io.perfmark</shadedPattern>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
String NACOS_WEIGHT_KEY = "nacos.weight";
32+
}
33+
}

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: 12 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

@@ -83,6 +85,7 @@ public class CompositeServiceUpdateTask extends ServiceUpdateTask {
8385
public CompositeServiceUpdateTask(ServiceEventHandler handler, DestroyableServerConnector connector) {
8486
super(handler, connector);
8587
CompositeConnector compositeConnector = (CompositeConnector) connector;
88+
EventType eventType = handler.getServiceEventKey().getEventType();
8689
for (DestroyableServerConnector sc : compositeConnector.getServerConnectors()) {
8790
if (SERVER_CONNECTOR_GRPC.equals(sc.getName()) && sc.isDiscoveryEnable()) {
8891
subServiceUpdateTaskMap.put(SERVER_CONNECTOR_GRPC, new GrpcServiceUpdateTask(serviceEventHandler, sc));
@@ -96,6 +99,15 @@ public CompositeServiceUpdateTask(ServiceEventHandler handler, DestroyableServer
9699
ifMainConnectorTypeSet = true;
97100
}
98101
}
102+
// nacos only support INSTANCE and SERVICE events.
103+
if (SERVER_CONNECTOR_NACOS.equals(sc.getName()) && sc.isDiscoveryEnable()
104+
&& (eventType == EventType.INSTANCE || eventType == EventType.SERVICE)) {
105+
subServiceUpdateTaskMap.put(SERVER_CONNECTOR_NACOS, new NacosServiceUpdateTask(serviceEventHandler, sc));
106+
if (!ifMainConnectorTypeSet) {
107+
mainConnectorType = sc.getName();
108+
ifMainConnectorTypeSet = true;
109+
}
110+
}
99111
}
100112
}
101113

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
@@ -90,7 +90,7 @@ public class ConsulAPIConnector extends DestroyableServerConnector {
9090
*/
9191
private boolean initialized = false;
9292

93-
private boolean ieRegistered = false;
93+
private boolean isRegistered = false;
9494

9595
private String id;
9696
private boolean isRegisterEnable = true;
@@ -295,7 +295,7 @@ public void deRegisterServiceHandler(ServiceEventKey eventKey) throws PolarisExc
295295
@Override
296296
public CommonProviderResponse registerInstance(CommonProviderRequest req, Map<String, String> customHeader)
297297
throws PolarisException {
298-
if (isRegisterEnable() && !ieRegistered) {
298+
if (isRegisterEnable() && !isRegistered) {
299299
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
300300
try {
301301
LOG.info("Registering service to Consul");
@@ -331,7 +331,7 @@ public CommonProviderResponse registerInstance(CommonProviderRequest req, Map<St
331331
resp.setInstanceID(service.getId());
332332
resp.setExists(false);
333333
LOG.info("Registered service to Consul: " + service);
334-
ieRegistered = true;
334+
isRegistered = true;
335335
// heartbeat when registration is successful.
336336
heartbeat(req);
337337
return resp;
@@ -405,13 +405,13 @@ private NewService buildRegisterInstanceRequest(CommonProviderRequest req) {
405405

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

423423
@Override
424424
public void heartbeat(CommonProviderRequest req) throws PolarisException {
425-
if (ieRegistered) {
425+
if (isRegistered) {
426426
ServiceKey serviceKey = new ServiceKey(req.getNamespace(), req.getService());
427427
try {
428428
this.ttlConsulClient.agentCheckPass(consulContext.getCheckId(), null, consulContext.getAclToken());

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@
2727
<groupId>com.alibaba.nacos</groupId>
2828
<artifactId>nacos-client</artifactId>
2929
<version>${nacos.version}</version>
30+
<exclusions>
31+
<exclusion>
32+
<groupId>com.fasterxml.jackson.core</groupId>
33+
<artifactId>jackson-core</artifactId>
34+
</exclusion>
35+
<exclusion>
36+
<groupId>org.yaml</groupId>
37+
<artifactId>snakeyaml</artifactId>
38+
</exclusion>
39+
</exclusions>
3040
</dependency>
3141
<dependency>
3242
<groupId>org.apache.httpcomponents</groupId>

0 commit comments

Comments
 (0)