This is an automated email from the ASF dual-hosted git repository.
liujun pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.0 by this push:
new 53f2322 customize instance metadata.
53f2322 is described below
commit 53f2322a5eae57d29caff6bef7631f8b90f4169d
Author: ken.lj <[email protected]>
AuthorDate: Fri Jul 24 17:36:54 2020 +0800
customize instance metadata.
---
.../client/ServiceInstanceMetadataCustomizer.java | 73 ------------------
...MetadataServiceURLParamsMetadataCustomizer.java | 31 ++++----
.../ServiceInstanceMetadataCustomizer.java | 86 ++++++++++++++++++++++
...dubbo.registry.client.ServiceInstanceCustomizer | 1 +
4 files changed, 105 insertions(+), 86 deletions(-)
diff --git
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceInstanceMetadataCustomizer.java
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceInstanceMetadataCustomizer.java
deleted file mode 100644
index 457441f..0000000
---
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/ServiceInstanceMetadataCustomizer.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * 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.registry.client;
-
-import java.util.Map;
-
-import static org.apache.dubbo.common.utils.StringUtils.isBlank;
-
-/**
- * The abstract class to customize {@link ServiceInstance#getMetadata()} the
service instances' metadata}
- *
- * @see ServiceInstance#getMetadata()
- * @see ServiceInstanceCustomizer
- * @since 2.7.5
- */
-public abstract class ServiceInstanceMetadataCustomizer implements
ServiceInstanceCustomizer {
-
- @Override
- public final void customize(ServiceInstance serviceInstance) {
-
- Map<String, String> metadata = serviceInstance.getMetadata();
-
- String propertyName = resolveMetadataPropertyName(serviceInstance);
- String propertyValue = resolveMetadataPropertyValue(serviceInstance);
-
- if (!isBlank(propertyName) && !isBlank(propertyValue)) {
- String existedValue = metadata.get(propertyName);
- boolean put = existedValue == null || isOverride();
- if (put) {
- metadata.put(propertyName, propertyValue);
- }
- }
- }
-
- /**
- * Resolve the property name of metadata
- *
- * @param serviceInstance the instance of {@link ServiceInstance}
- * @return non-null key
- */
- protected abstract String resolveMetadataPropertyName(ServiceInstance
serviceInstance);
-
- /**
- * Resolve the property value of metadata
- *
- * @param serviceInstance the instance of {@link ServiceInstance}
- * @return non-null value
- */
- protected abstract String resolveMetadataPropertyValue(ServiceInstance
serviceInstance);
-
- /**
- * Is override {@link ServiceInstance#getMetadata()} the service
instances' metadata} or not
- *
- * @return default is <code>false</code>
- */
- protected boolean isOverride() {
- return false;
- }
-}
diff --git
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizer.java
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizer.java
index be78440..8b525c7 100644
---
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizer.java
+++
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/MetadataServiceURLParamsMetadataCustomizer.java
@@ -16,35 +16,40 @@
*/
package org.apache.dubbo.registry.client.metadata;
-import org.apache.dubbo.common.URL;
import org.apache.dubbo.metadata.MetadataService;
import org.apache.dubbo.metadata.WritableMetadataService;
import org.apache.dubbo.registry.client.ServiceInstance;
-import org.apache.dubbo.registry.client.ServiceInstanceMetadataCustomizer;
+import org.apache.dubbo.registry.client.ServiceInstanceCustomizer;
+import java.util.Map;
import java.util.SortedSet;
+import static org.apache.dubbo.common.utils.StringUtils.isBlank;
import static org.apache.dubbo.metadata.MetadataService.toURLs;
import static
org.apache.dubbo.metadata.WritableMetadataService.getDefaultExtension;
import static
org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME;
import static
org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils.getMetadataServiceParameter;
-/**
- * An {@link ServiceInstanceMetadataCustomizer} to customize the {@link URL
urls} of {@link MetadataService}
- * into {@link ServiceInstance#getMetadata() the service instances' metadata}
- *
- * @see ServiceInstanceMetadataCustomizer
- * @since 2.7.5
- */
-public class MetadataServiceURLParamsMetadataCustomizer extends
ServiceInstanceMetadataCustomizer {
+public class MetadataServiceURLParamsMetadataCustomizer implements
ServiceInstanceCustomizer {
@Override
- public String resolveMetadataPropertyName(ServiceInstance serviceInstance)
{
+ public void customize(ServiceInstance serviceInstance) {
+
+ Map<String, String> metadata = serviceInstance.getMetadata();
+
+ String propertyName = resolveMetadataPropertyName(serviceInstance);
+ String propertyValue = resolveMetadataPropertyValue(serviceInstance);
+
+ if (!isBlank(propertyName) && !isBlank(propertyValue)) {
+ metadata.put(propertyName, metadata.get(propertyName));
+ }
+ }
+
+ private String resolveMetadataPropertyName(ServiceInstance
serviceInstance) {
return METADATA_SERVICE_URL_PARAMS_PROPERTY_NAME;
}
- @Override
- public String resolveMetadataPropertyValue(ServiceInstance
serviceInstance) {
+ private String resolveMetadataPropertyValue(ServiceInstance
serviceInstance) {
WritableMetadataService writableMetadataService =
getDefaultExtension();
String serviceInterface = MetadataService.class.getName();
diff --git
a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataCustomizer.java
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataCustomizer.java
new file mode 100644
index 0000000..8d9824a
--- /dev/null
+++
b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/metadata/ServiceInstanceMetadataCustomizer.java
@@ -0,0 +1,86 @@
+/*
+ * 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.registry.client.metadata;
+
+import org.apache.dubbo.common.extension.ExtensionLoader;
+import org.apache.dubbo.common.infra.InfraAdapter;
+import org.apache.dubbo.common.utils.CollectionUtils;
+import org.apache.dubbo.metadata.MetadataInfo;
+import org.apache.dubbo.metadata.MetadataParamsFilter;
+import org.apache.dubbo.metadata.WritableMetadataService;
+import org.apache.dubbo.registry.client.ServiceInstance;
+import org.apache.dubbo.registry.client.ServiceInstanceCustomizer;
+import
org.apache.dubbo.registry.client.metadata.store.InMemoryWritableMetadataService;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import static
org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
+
+/**
+ *
+ */
+public class ServiceInstanceMetadataCustomizer implements
ServiceInstanceCustomizer {
+
+ @Override
+ public void customize(ServiceInstance serviceInstance) {
+ Map<String, String> params = new HashMap<>();
+
+ ExtensionLoader<MetadataParamsFilter> loader =
ExtensionLoader.getExtensionLoader(MetadataParamsFilter.class);
+ Set<MetadataParamsFilter> paramsFilters =
loader.getSupportedExtensionInstances();
+
+ InMemoryWritableMetadataService localMetadataService
+ = (InMemoryWritableMetadataService)
WritableMetadataService.getDefaultExtension();
+ // pick the first interface metadata available.
+ // FIXME, check the same key in different urls has the same value
+ MetadataInfo metadataInfo =
localMetadataService.getMetadataInfos().values().iterator().next();
+ MetadataInfo.ServiceInfo serviceInfo =
metadataInfo.getServices().values().iterator().next();
+ Map<String, String> allParams = new HashMap<>(serviceInfo.getParams());
+
+ // load instance params users want to load.
+ // TODO, duplicate logic with that in ApplicationConfig
+ Set<InfraAdapter> adapters =
ExtensionLoader.getExtensionLoader(InfraAdapter.class).getSupportedExtensionInstances();
+ if (CollectionUtils.isNotEmpty(adapters)) {
+ Map<String, String> inputParameters = new HashMap<>();
+ inputParameters.put(APPLICATION_KEY, ApplicationModel.getName());
+ for (InfraAdapter adapter : adapters) {
+ Map<String, String> extraParameters =
adapter.getExtraAttributes(inputParameters);
+ if (CollectionUtils.isNotEmptyMap(extraParameters)) {
+ extraParameters.forEach(allParams::putIfAbsent);
+ }
+ }
+ }
+
+ if (CollectionUtils.isEmpty(paramsFilters)) {
+ serviceInstance.getMetadata().putAll(allParams);
+ return;
+ }
+
+ paramsFilters.forEach(filter -> {
+ String[] included = filter.instanceParamsIncluded();
+ if (included == null) {
+ serviceInstance.getMetadata().putAll(allParams);
+ } else {
+ for (String p : included) {
+ serviceInstance.getMetadata().put(p, allParams.get(p));
+ }
+ }
+ });
+ }
+}
diff --git
a/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceInstanceCustomizer
b/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceInstanceCustomizer
index 9307410..1f517c8 100644
---
a/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceInstanceCustomizer
+++
b/dubbo-registry/dubbo-registry-api/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.registry.client.ServiceInstanceCustomizer
@@ -3,3 +3,4 @@
exported-revision=org.apache.dubbo.registry.client.metadata.ExportedServicesRevi
subscribed-revision=org.apache.dubbo.registry.client.metadata.SubscribedServicesRevisionMetadataCustomizer
protocol-ports=org.apache.dubbo.registry.client.metadata.ProtocolPortsMetadataCustomizer
instance-port=org.apache.dubbo.config.metadata.ServiceInstancePortCustomizer
+instance-metadata=org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataCustomizer