-
Notifications
You must be signed in to change notification settings - Fork 26.5k
support nacos metadata #4025
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
support nacos metadata #4025
Changes from 5 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5aab4ef
nacos metadata report
moriadry b17e3f6
Merge remote-tracking branch 'apache/master' into feature/nacos-metadata
moriadry afa44ee
nacos metadata and code clean
moriadry 2ff7688
add license
moriadry 5fa3b6b
fix conflicts
moriadry e9d6aec
remove NacosUtils
moriadry 42ad95f
Merge remote-tracking branch 'apache/master' into feature/nacos-metadata
moriadry 5bda7cd
revert
moriadry 8e480f0
constants from another class
moriadry 9e2a03d
fix
moriadry 566a5b1
code clean
moriadry e24ba38
PROVIDER_SIDE and CONSUMER_SIDE
moriadry 0cdb031
Merge remote-tracking branch 'apache/master' into feature/nacos-metadata
moriadry 53e16b2
merge
moriadry File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
dubbo-common/src/main/java/org/apache/dubbo/common/utils/NacosUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| /* | ||
| * 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.dubbo.common.utils; | ||
|
|
||
| import org.apache.dubbo.common.URL; | ||
|
|
||
| import java.util.Properties; | ||
| import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR; | ||
| import static com.alibaba.nacos.api.PropertyKeyConst.SECRET_KEY; | ||
| import static com.alibaba.nacos.api.PropertyKeyConst.ACCESS_KEY; | ||
| import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT; | ||
| import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE; | ||
| import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME; | ||
| import static com.alibaba.nacos.client.naming.utils.UtilAndComs.NACOS_NAMING_LOG_NAME; | ||
|
|
||
| import static org.apache.dubbo.common.Constants.BACKUP_KEY; | ||
|
|
||
| /** | ||
| * NacosUtils | ||
| */ | ||
| public class NacosUtils { | ||
|
Moriadry-zz marked this conversation as resolved.
Outdated
|
||
|
|
||
| public static Properties buildNacosProperties(URL url) { | ||
| Properties properties = new Properties(); | ||
| setServerAddr(url, properties); | ||
| setProperties(url, properties); | ||
| return properties; | ||
| } | ||
|
|
||
| public static void setServerAddr(URL url, Properties properties) { | ||
| StringBuilder serverAddrBuilder = | ||
| new StringBuilder(url.getHost()) // Host | ||
| .append(":") | ||
| .append(url.getPort()); // Port | ||
| // Append backup parameter as other servers | ||
| String backup = url.getParameter(BACKUP_KEY); | ||
| if (backup != null) { | ||
| serverAddrBuilder.append(",").append(backup); | ||
| } | ||
| String serverAddr = serverAddrBuilder.toString(); | ||
| properties.put(SERVER_ADDR, serverAddr); | ||
| } | ||
|
|
||
| public static void setProperties(URL url, Properties properties) { | ||
| putPropertyIfAbsent(url, properties, NAMESPACE); | ||
| putPropertyIfAbsent(url, properties, NACOS_NAMING_LOG_NAME); | ||
| putPropertyIfAbsent(url, properties, ENDPOINT); | ||
| putPropertyIfAbsent(url, properties, ACCESS_KEY); | ||
| putPropertyIfAbsent(url, properties, SECRET_KEY); | ||
| putPropertyIfAbsent(url, properties, CLUSTER_NAME); | ||
| } | ||
|
|
||
|
|
||
| public static void putPropertyIfAbsent(URL url, Properties properties, String propertyName) { | ||
| String propertyValue = url.getParameter(propertyName); | ||
| if (StringUtils.isNotEmpty(propertyValue)) { | ||
| properties.setProperty(propertyName, propertyValue); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?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> | ||
| <artifactId>dubbo-metadata-report</artifactId> | ||
| <groupId>org.apache.dubbo</groupId> | ||
| <version>${revision}</version> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
|
|
||
| <artifactId>dubbo-metadata-report-nacos</artifactId> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>org.apache.dubbo</groupId> | ||
| <artifactId>dubbo-metadata-report-api</artifactId> | ||
| <version>${project.parent.version}</version> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.alibaba.nacos</groupId> | ||
| <artifactId>nacos-client</artifactId> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> |
81 changes: 81 additions & 0 deletions
81
...report-nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReport.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * 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.dubbo.metadata.store.nacos; | ||
|
|
||
| import com.alibaba.nacos.api.NacosFactory; | ||
| import com.alibaba.nacos.api.config.ConfigService; | ||
| import com.alibaba.nacos.api.exception.NacosException; | ||
| import org.apache.dubbo.common.URL; | ||
| import org.apache.dubbo.common.logger.Logger; | ||
| import org.apache.dubbo.common.logger.LoggerFactory; | ||
| import org.apache.dubbo.common.utils.NacosUtils; | ||
| import org.apache.dubbo.metadata.identifier.MetadataIdentifier; | ||
| import org.apache.dubbo.metadata.support.AbstractMetadataReport; | ||
| import org.apache.dubbo.rpc.RpcException; | ||
|
|
||
| import java.util.Properties; | ||
|
|
||
|
|
||
| /** | ||
| * metadata report impl for nacos | ||
| */ | ||
| public class NacosMetadataReport extends AbstractMetadataReport { | ||
| private static final Logger logger = LoggerFactory.getLogger(NacosMetadataReport.class); | ||
| private ConfigService configService; | ||
|
|
||
| public NacosMetadataReport(URL url) { | ||
| super(url); | ||
| this.configService = buildConfigService(url); | ||
| } | ||
|
|
||
| public ConfigService buildConfigService(URL url) { | ||
| Properties nacosProperties = NacosUtils.buildNacosProperties(url); | ||
| try { | ||
| configService = NacosFactory.createConfigService(nacosProperties); | ||
| } catch (NacosException e) { | ||
| if (logger.isErrorEnabled()) { | ||
| logger.error(e.getErrMsg(), e); | ||
| } | ||
| throw new IllegalStateException(e); | ||
| } | ||
| return configService; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) { | ||
| this.storeMetadata(providerMetadataIdentifier, serviceDefinitions); | ||
| } | ||
|
|
||
| @Override | ||
| protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String value) { | ||
| this.storeMetadata(consumerMetadataIdentifier, value); | ||
| } | ||
|
|
||
| private void storeMetadata(MetadataIdentifier identifier, String value) { | ||
| try { | ||
| boolean publishResult = configService.publishConfig(identifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), identifier.getGroup(), value); | ||
| if (!publishResult) { | ||
| throw new RuntimeException("publish nacos metadata failed"); | ||
| } | ||
| } catch (Throwable t) { | ||
| logger.error("Failed to put " + identifier + " to nacos " + value + ", cause: " + t.getMessage(), t); | ||
| throw new RpcException("Failed to put " + identifier + " to nacos " + value + ", cause: " + t.getMessage(), t); | ||
| } | ||
| } | ||
| } |
32 changes: 32 additions & 0 deletions
32
...nacos/src/main/java/org/apache/dubbo/metadata/store/nacos/NacosMetadataReportFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * 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.dubbo.metadata.store.nacos; | ||
|
|
||
| import org.apache.dubbo.common.URL; | ||
| import org.apache.dubbo.metadata.store.MetadataReport; | ||
| import org.apache.dubbo.metadata.support.AbstractMetadataReportFactory; | ||
|
|
||
| /** | ||
| * metadata report factory impl for nacos | ||
| */ | ||
| public class NacosMetadataReportFactory extends AbstractMetadataReportFactory { | ||
| @Override | ||
| protected MetadataReport createMetadataReport(URL url) { | ||
| return new NacosMetadataReport(url); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
...n/resources/META-INF/dubbo/internal/org.apache.dubbo.metadata.store.MetadataReportFactory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| consul=org.apache.dubbo.metadata.store.consul.ConsulMetadataReportFactory | ||
|
Moriadry-zz marked this conversation as resolved.
Outdated
|
||
28 changes: 28 additions & 0 deletions
28
...t-nacos/src/test/java/org/apache/dubbo/metadata/store/nacos/NacosMetadata4TstService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /* | ||
| * 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.dubbo.metadata.store.nacos; | ||
|
|
||
| /** | ||
| * Test interface for Nacos metadata report | ||
| */ | ||
| public interface NacosMetadata4TstService { | ||
|
|
||
| int getCounter(); | ||
|
|
||
| void printResult(String var); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.