forked from apache/dubbo
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Polish apache#3946 : Dubbo Cloud Native : To Add Dubbo metadata service
- Loading branch information
1 parent
77d802e
commit 5c73899
Showing
16 changed files
with
1,035 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,59 @@ | ||
<!-- | ||
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.dubbo</groupId> | ||
<artifactId>dubbo-parent</artifactId> | ||
<version>${revision}</version> | ||
<relativePath>../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>dubbo-metadata</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<name>dubbo-metadata</name> | ||
<description>The metadata module of Dubbo project</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.dubbo</groupId> | ||
<artifactId>dubbo-config-api</artifactId> | ||
<version>${revision}</version> | ||
<optional>true</optional> | ||
</dependency> | ||
|
||
<!-- Test dependencies --> | ||
<dependency> | ||
<groupId>org.apache.dubbo</groupId> | ||
<artifactId>dubbo-rpc-dubbo</artifactId> | ||
<version>${revision}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.apache.dubbo</groupId> | ||
<artifactId>dubbo-remoting-netty4</artifactId> | ||
<version>${revision}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
157 changes: 157 additions & 0 deletions
157
dubbo-metadata/src/main/java/org/apache/dubbo/metadata/InMemoryLocalMetadataService.java
This file contains 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,157 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.dubbo.common.URL; | ||
|
||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.stream.Collectors; | ||
|
||
import static java.util.Collections.unmodifiableList; | ||
import static org.apache.dubbo.common.URL.buildKey; | ||
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY; | ||
|
||
/** | ||
* The {@link LocalMetadataService} implementation stores the metadata of Dubbo services in memory locally when they | ||
* exported. | ||
* | ||
* @see MetadataService | ||
* @since 2.7.2 | ||
*/ | ||
public class InMemoryLocalMetadataService implements LocalMetadataService { | ||
|
||
/** | ||
* The class name of {@link MetadataService} | ||
*/ | ||
static final String METADATA_SERVICE_CLASS_NAME = MetadataService.class.getName(); | ||
|
||
// =================================== Registration =================================== // | ||
|
||
/** | ||
* All exported {@link URL urls} {@link Map} whose key is the return value of {@link URL#getServiceKey()} method | ||
* and value is the {@link List} of the {@link URL URLs} | ||
*/ | ||
private ConcurrentMap<String, List<URL>> exportedServiceURLs = new ConcurrentHashMap<>(); | ||
|
||
// ==================================================================================== // | ||
|
||
// =================================== Subscription =================================== // | ||
|
||
/** | ||
* All subscribed service names | ||
*/ | ||
private Set<String> subscribedServices = new LinkedHashSet<>(); | ||
|
||
/** | ||
* The subscribed {@link URL urls} {@link Map} of {@link MetadataService}, | ||
* whose key is the return value of {@link URL#getServiceKey()} method and value is the {@link List} of | ||
* the {@link URL URLs} | ||
*/ | ||
private final ConcurrentMap<String, List<URL>> subscribedServiceURLs = new ConcurrentHashMap<>(); | ||
|
||
// ==================================================================================== // | ||
|
||
@Override | ||
public List<String> getSubscribedURLs() { | ||
return getAllServiceURLs(subscribedServiceURLs); | ||
} | ||
|
||
@Override | ||
public List<String> getExportedURLs(String serviceInterface, String group, String version, String protocol) { | ||
if (ALL_SERVICE_INTERFACES.equals(serviceInterface)) { | ||
return getAllServiceURLs(exportedServiceURLs); | ||
} | ||
String serviceKey = buildKey(serviceInterface, group, version); | ||
return unmodifiableList(getServiceURLs(exportedServiceURLs, serviceKey, protocol)); | ||
} | ||
|
||
protected List<String> getServiceURLs(ConcurrentMap<String, List<URL>> exportedServiceURLs, String serviceKey, | ||
String protocol) { | ||
List<URL> serviceURLs = getServiceURLs(exportedServiceURLs, serviceKey); | ||
return serviceURLs.stream().filter( | ||
url -> protocol == null || protocol.equals(url.getParameter(PROTOCOL_KEY))) | ||
.map(URL::toFullString) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
|
||
private boolean isMetadataServiceURL(URL url) { | ||
String serviceInterface = url.getServiceInterface(); | ||
return METADATA_SERVICE_CLASS_NAME.equals(serviceInterface); | ||
} | ||
|
||
@Override | ||
public boolean exportURL(URL url) { | ||
if (isMetadataServiceURL(url)) { // ignore MetadataService in the export phase | ||
return true; | ||
} | ||
return addURL(exportedServiceURLs, url); | ||
} | ||
|
||
@Override | ||
public boolean unexportURL(URL url) { | ||
if (isMetadataServiceURL(url)) { // ignore MetadataService in the export phase | ||
return true; | ||
} | ||
return removeURL(exportedServiceURLs, url); | ||
} | ||
|
||
@Override | ||
public boolean subscribeURL(URL url) { | ||
return addURL(subscribedServiceURLs, url); | ||
} | ||
|
||
@Override | ||
public boolean unsubscribeURL(URL url) { | ||
return removeURL(subscribedServiceURLs, url); | ||
} | ||
|
||
protected boolean addURL(Map<String, List<URL>> serviceURLs, URL url) { | ||
String serviceKey = url.getServiceKey(); | ||
List<URL> urls = getServiceURLs(serviceURLs, serviceKey); | ||
if (!urls.contains(url)) { | ||
return urls.add(url); | ||
} | ||
return false; | ||
} | ||
|
||
protected boolean removeURL(Map<String, List<URL>> serviceURLs, URL url) { | ||
String serviceKey = url.getServiceKey(); | ||
List<URL> urls = getServiceURLs(serviceURLs, serviceKey); | ||
return urls.remove(url); | ||
} | ||
|
||
protected List<URL> getServiceURLs(Map<String, List<URL>> serviceURLs, String serviceKey) { | ||
return serviceURLs.computeIfAbsent(serviceKey, s -> new LinkedList()); | ||
} | ||
|
||
protected List<String> getAllServiceURLs(Map<String, List<URL>> serviceURLs) { | ||
return serviceURLs | ||
.values() | ||
.stream() | ||
.flatMap(Collection::stream) | ||
.map(URL::toFullString) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
dubbo-metadata/src/main/java/org/apache/dubbo/metadata/LocalMetadataService.java
This file contains 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,88 @@ | ||
/* | ||
* 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; | ||
|
||
import org.apache.dubbo.common.URL; | ||
import org.apache.dubbo.common.extension.ExtensionLoader; | ||
import org.apache.dubbo.common.extension.SPI; | ||
import org.apache.dubbo.rpc.model.ApplicationModel; | ||
|
||
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader; | ||
|
||
/** | ||
* Local {@link MetadataService} that extends {@link MetadataService} and provides the modification, which is used for | ||
* Dubbo's consumers and providers. | ||
* | ||
* @since 2.7.2 | ||
*/ | ||
@SPI("default") | ||
public interface LocalMetadataService extends MetadataService { | ||
|
||
/** | ||
* Gets the current Dubbo Service name | ||
* | ||
* @return non-null | ||
*/ | ||
@Override | ||
default String serviceName() { | ||
return ApplicationModel.getApplication(); | ||
} | ||
|
||
/** | ||
* Exports a {@link URL} | ||
* | ||
* @param url a {@link URL} | ||
* @return If success , return <code>true</code> | ||
*/ | ||
boolean exportURL(URL url); | ||
|
||
/** | ||
* Unexports a {@link URL} | ||
* | ||
* @param url a {@link URL} | ||
* @return If success , return <code>true</code> | ||
*/ | ||
boolean unexportURL(URL url); | ||
|
||
/** | ||
* Subscribes a {@link URL} | ||
* | ||
* @param url a {@link URL} | ||
* @return If success , return <code>true</code> | ||
*/ | ||
boolean subscribeURL(URL url); | ||
|
||
/** | ||
* Unsubscribes a {@link URL} | ||
* | ||
* @param url a {@link URL} | ||
* @return If success , return <code>true</code> | ||
*/ | ||
boolean unsubscribeURL(URL url); | ||
|
||
|
||
/** | ||
* Get {@link ExtensionLoader#getDefaultExtension() the defautl extension} of {@link LocalMetadataService} | ||
* | ||
* @return non-null | ||
* @see InMemoryLocalMetadataService | ||
*/ | ||
public static LocalMetadataService getDefaultExtension() { | ||
return getExtensionLoader(LocalMetadataService.class).getDefaultExtension(); | ||
} | ||
|
||
} |
Oops, something went wrong.