Skip to content

Commit

Permalink
Polish apache#4050 : Dubbo Cloud Native : Add a mechanism to upgrade …
Browse files Browse the repository at this point in the history
…Dubbo services smoothly
  • Loading branch information
mercyblitz committed Jun 4, 2019
1 parent 9208a34 commit 9702156
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public interface DemoService {

int echo(int i);

default String name() {
return getClass().getSimpleName();
}
// default String name() {
// return getClass().getSimpleName();
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

/**
* The customizer to a add the metadata that the reversion of Dubbo exported services calculates.
* <p>
* The reversion is calculated on the methods that all Dubbo exported interfaces declare
*
* @since 2.7.3
*/
Expand All @@ -48,17 +50,20 @@ protected String buildMetadataValue(ServiceInstance serviceInstance) {
LocalMetadataService localMetadataService = LocalMetadataService.getDefaultExtension();
List<String> exportedURLs = localMetadataService.getExportedURLs();
Object[] data = exportedURLs.stream()
.map(URL::valueOf)
.map(URL::getServiceInterface)
.filter(name -> !MetadataService.class.getName().equals(name))
.map(ClassUtils::forName)
.map(Class::getMethods)
.map(Arrays::asList)
.flatMap(Collection::stream)
.map(Object::toString)
.sorted()
.toArray();
.map(URL::valueOf) // String to URL
.map(URL::getServiceInterface) // get the service interface
.filter(this::isNotMetadataService) // filter not MetadataService interface
.map(ClassUtils::forName) // load business interface class
.map(Class::getMethods) // get all public methods from business interface
.map(Arrays::asList) // Array to List
.flatMap(Collection::stream) // flat Stream<Stream> to be Stream
.map(Object::toString) // Method to String
.sorted() // sort methods marking sure the calculation of reversion is stable
.toArray(); // Stream to Array
return valueOf(hash(data)); // calculate the hash code as reversion
}

return valueOf(hash(data));
private boolean isNotMetadataService(String serviceInterface) {
return !MetadataService.class.getName().equals(serviceInterface);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;
Expand Down Expand Up @@ -142,11 +143,11 @@ public void doRegister(URL url) {
}
if (localMetadataService.exportURL(url)) {
if (logger.isInfoEnabled()) {
logger.info(String.format("The URL[%s] registered successfully.", url.toString()));
logger.info(format("The URL[%s] registered successfully.", url.toString()));
}
} else {
if (logger.isWarnEnabled()) {
logger.info(String.format("The URL[%s] has been registered.", url.toString()));
logger.info(format("The URL[%s] has been registered.", url.toString()));
}
}
}
Expand All @@ -158,11 +159,11 @@ public void doUnregister(URL url) {
}
if (localMetadataService.unexportURL(url)) {
if (logger.isInfoEnabled()) {
logger.info(String.format("The URL[%s] deregistered successfully.", url.toString()));
logger.info(format("The URL[%s] deregistered successfully.", url.toString()));
}
} else {
if (logger.isWarnEnabled()) {
logger.info(String.format("The URL[%s] has been deregistered.", url.toString()));
logger.info(format("The URL[%s] has been deregistered.", url.toString()));
}
}
}
Expand Down Expand Up @@ -194,11 +195,12 @@ public void destroy() {

protected void subscribeURLs(URL url, NotifyListener listener) {

localMetadataService.subscribeURL(url);

Set<String> serviceNames = getServices(url);

serviceNames.forEach(serviceName -> subscribeURLs(url, listener, serviceName));

localMetadataService.subscribeURL(url);
}

protected void subscribeURLs(URL url, NotifyListener listener, String serviceName) {
Expand All @@ -217,7 +219,7 @@ protected void subscribeURLs(URL subscribedURL, NotifyListener listener, String
Collection<ServiceInstance> serviceInstances) {

if (isEmpty(serviceInstances)) {
logger.warn(String.format("There is no instance in service[name : %s]", serviceName));
logger.warn(format("There is no instance in service[name : %s]", serviceName));
return;
}

Expand Down Expand Up @@ -350,7 +352,30 @@ protected List<URL> getTemplateURLs(URL subscribedURL, ServiceInstance selectedI
Map<String, List<URL>> revisionURLsCache) {
// get the revision from the specified {@link ServiceInstance}
String revision = getExportedServicesRevision(selectedInstance);
return revisionURLsCache.computeIfAbsent(revision, key -> getProviderExportedURLs(subscribedURL, selectedInstance));
// try to get templateURLs from cache
List<URL> templateURLs = revisionURLsCache.get(revision);

if (isEmpty(templateURLs)) { // not exists or getting failed last time

if (!revisionURLsCache.isEmpty()) { // it's not first time
if (logger.isWarnEnabled()) {
logger.warn(format("The ServiceInstance[id: %s, host : %s , port : %s] has different revision : %s" +
", please make sure the service [name : %s] is changing or not.",
selectedInstance.getId(),
selectedInstance.getHost(),
selectedInstance.getPort(),
revision,
selectedInstance.getServiceName()
));
}
}
// get or get again
templateURLs = getProviderExportedURLs(subscribedURL, selectedInstance);
// put into cache
revisionURLsCache.put(revision, templateURLs);
}

return templateURLs;
}

/**
Expand Down

0 comments on commit 9702156

Please sign in to comment.