Skip to content

Commit

Permalink
[ISSUE #3949] support Tencent Polaris Registration Center (#4897)
Browse files Browse the repository at this point in the history
* Support Polaris service registration

* merge upstream master.

* check style.

* init module.

* Polaris instance register repository.

* unit test 1.

* integrated test add type + 4.

* unit test + 1.

* switch httpclient to configPublishService API.

* add some unit test.

* add license.

---------

Co-authored-by: fengbiao <fengbiao@wondersgroup.com>
Co-authored-by: dragon-zhang <zhangzicheng@apache.org>
  • Loading branch information
3 people committed Jul 25, 2023
1 parent 39db11b commit 2a416d5
Show file tree
Hide file tree
Showing 21 changed files with 1,366 additions and 3 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
<eureka.version>1.10.17</eureka.version>
<javatuples.version>1.2</javatuples.version>
<k8s-client.version>17.0.2</k8s-client.version>
<polaris.version>1.13.0</polaris.version>
<!--maven plugin version-->
<exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
<jacoco-maven-plugin.version>0.8.7</jacoco-maven-plugin.version>
Expand Down Expand Up @@ -391,6 +392,12 @@
<version>${apollo.version}</version>
</dependency>

<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-all</artifactId>
<version>${polaris.version}</version>
</dependency>

<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
Expand Down
6 changes: 6 additions & 0 deletions shenyu-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-register-client-server-polaris</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-register-client-server-zookeeper</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ The text of each license is also included at licenses/LICENSE-[project].txt.
protobuf-java 3.12.0: https://github.com/protocolbuffers/protobuf/, BSD 3-Clause
protobuf-java-util 3.21.1: https://github.com/protocolbuffers/protobuf/, BSD 3-Clause
openGauss-connector-jdbc 5.0.0-og https://github.com/opengauss-mirror/openGauss-connector-jdbc
polaris-all 1.13.0: https://github.com/polarismesh/polaris-java, BSD 3-Clause

========================================================================
CC0 licenses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ The text of each license is also included at licenses/LICENSE-[project].txt.
hamcrest-core 2.2: https://github.com/hamcrest/JavaHamcrest ,BSD 3-Clause
asm 9.2: https://github.com/llbit/ow2-asm, BSD 3-Clause
asm-tree 9.2: https://asm.ow2.io/, BSD 3-Clause
polaris-all 1.13.0: https://github.com/polarismesh/polaris-java, BSD 3-Clause

========================================================================
CC0 licenses
Expand Down
3 changes: 2 additions & 1 deletion shenyu-register-center/shenyu-register-client-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>shenyu-register-client-server</artifactId>
<packaging>pom</packaging>

<modules>
<module>shenyu-register-client-server-api</module>
<module>shenyu-register-client-server-zookeeper</module>
<module>shenyu-register-client-server-etcd</module>
<module>shenyu-register-client-server-consul</module>
<module>shenyu-register-client-server-nacos</module>
<module>shenyu-register-client-server-apollo</module>
<module>shenyu-register-client-server-polaris</module>
</modules>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-register-client-server</artifactId>
<version>2.6.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shenyu-register-client-server-polaris</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-register-client-server-api</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.tencent.polaris</groupId>
<artifactId>polaris-all</artifactId>
</dependency>

<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-common</artifactId>
<version>${project.version}</version>
</dependency>


</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shenyu.register.client.server.polaris;

import com.tencent.polaris.api.config.Configuration;
import com.tencent.polaris.api.core.ConsumerAPI;
import com.tencent.polaris.api.pojo.Instance;
import com.tencent.polaris.api.rpc.WatchServiceRequest;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.configuration.api.core.ConfigFile;
import com.tencent.polaris.configuration.api.core.ConfigFileService;
import com.tencent.polaris.configuration.factory.ConfigFileServiceFactory;
import com.tencent.polaris.factory.ConfigAPIFactory;
import com.tencent.polaris.factory.api.DiscoveryAPIFactory;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import static org.apache.shenyu.common.constant.Constants.NAMESPACE;
import org.apache.shenyu.common.constant.PolarisPathConstants;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.register.client.server.api.ShenyuClientServerRegisterPublisher;
import org.apache.shenyu.register.client.server.api.ShenyuClientServerRegisterRepository;
import org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig;
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
import org.apache.shenyu.register.common.dto.URIRegisterDTO;
import org.apache.shenyu.register.common.enums.EventType;
import org.apache.shenyu.register.common.path.RegisterPathConstants;
import org.apache.shenyu.spi.Join;

/**
* Polaris register server.
*/
@Join
public class PolarisClientServerRegisterRepository implements ShenyuClientServerRegisterRepository {

private ConsumerAPI consumerAPI;

private ConfigFileService configFileService;

private ShenyuClientServerRegisterPublisher publisher;

private Properties props;

@Override
public void init(final ShenyuClientServerRegisterPublisher publisher, final ShenyuRegisterCenterConfig config) {
Configuration configuration = buildConfiguration(config);
SDKContext sdkContext = SDKContext.initContextByConfig(configuration);

this.consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext);
this.publisher = publisher;
this.configFileService = ConfigFileServiceFactory.createConfigFileService(sdkContext);
this.props = config.getProps();
subscribe();
}

private Configuration buildConfiguration(final ShenyuRegisterCenterConfig config) {
String serverLists = config.getServerLists();
return ConfigAPIFactory.createConfigurationByAddress(serverLists);
}

private void subscribe() {
RpcTypeEnum.acquireSupportMetadatas().forEach(this::subscribe);
}

private void subscribe(final RpcTypeEnum rpcTypeEnum) {
final String namespace = props.getProperty(NAMESPACE, PolarisPathConstants.NAMESPACE);
final String serviceName = RegisterPathConstants.buildServiceInstancePath(rpcTypeEnum.getName());
final String group = props.getProperty("fileGroup", PolarisPathConstants.FILE_GROUP);
WatchServiceRequest watchServiceRequest = WatchServiceRequest.builder()
.namespace(namespace)
.service(serviceName)
.listeners(Collections.singletonList(event -> {
List<Instance> allInstances = event.getAllInstances();
for (Instance instance : allInstances) {
if (!instance.isHealthy()) {
continue;
}
Map<String, String> metaMap = instance.getMetadata();
URIRegisterDTO uriRegisterDTO = GsonUtils.getInstance().fromJson(GsonUtils.getInstance().toJson(metaMap), URIRegisterDTO.class);
uriRegisterDTO.setPort(instance.getPort());
uriRegisterDTO.setEventType(EventType.REGISTER);
publisher.publish(uriRegisterDTO);

String fileName = RegisterPathConstants.buildServiceConfigPath(uriRegisterDTO.getRpcType(), uriRegisterDTO.getContextPath());
ConfigFile configFile = configFileService.getConfigFile(namespace, group, fileName);
List<MetaDataRegisterDTO> registerMetadataList = GsonUtils.getInstance().fromList(configFile.getContent(), MetaDataRegisterDTO.class);
for (MetaDataRegisterDTO metaDataDto : registerMetadataList) {
publisher.publish(metaDataDto);
}
}
})).build();
consumerAPI.watchService(watchServiceRequest);
}

@Override
public void close() {
this.consumerAPI.close();
this.publisher.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

polaris=org.apache.shenyu.register.client.server.polaris.PolarisClientServerRegisterRepository
Loading

0 comments on commit 2a416d5

Please sign in to comment.