diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilder.java new file mode 100644 index 00000000000..8a406f529ee --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilder.java @@ -0,0 +1,142 @@ +/* + * 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.config.spring.beans.factory.annotation; + +import static org.apache.dubbo.common.Constants.CONSUMERS_CATEGORY; +import static org.apache.dubbo.common.Constants.DEFAULT_PROTOCOL; +import static org.apache.dubbo.common.Constants.PROVIDERS_CATEGORY; +import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolveInterfaceName; +import static org.springframework.util.StringUtils.arrayToCommaDelimitedString; +import static org.springframework.util.StringUtils.hasText; + +import org.apache.dubbo.common.Constants; +import org.apache.dubbo.config.annotation.Reference; +import org.apache.dubbo.config.annotation.Service; +import org.apache.dubbo.registry.Registry; + +import org.springframework.core.env.Environment; + +/** + * The Bean Name Builder for the annotations {@link Service} and {@link Reference} + *

+ * The naming rule is consistent with the the implementation {@link Registry} that is based on the service-name aware + * infrastructure, e.g Spring Cloud, Cloud Native and so on. + *

+ * The pattern of bean name : ${category}:${protocol}:${serviceInterface}:${version}:${group}. + *

+ * ${version} and ${group} are optional. + * + * @since 2.6.6 + */ +class AnnotationBeanNameBuilder { + + private static final String SEPARATOR = ":"; + + // Required properties + + private final String category; + + private final String protocol; + + private final String interfaceClassName; + + // Optional properties + + private String version; + + private String group; + + private Environment environment; + + private AnnotationBeanNameBuilder(String category, String protocol, String interfaceClassName) { + this.category = category; + this.protocol = protocol; + this.interfaceClassName = interfaceClassName; + } + + private AnnotationBeanNameBuilder(Service service, Class interfaceClass) { + this(PROVIDERS_CATEGORY, resolveProtocol(service.protocol()), resolveInterfaceName(service, interfaceClass)); + this.group(service.group()); + this.version(service.version()); + } + + private AnnotationBeanNameBuilder(Reference reference, Class interfaceClass) { + this(CONSUMERS_CATEGORY, resolveProtocol(reference.protocol()), resolveInterfaceName(reference, interfaceClass)); + this.group(reference.group()); + this.version(reference.version()); + } + + public static AnnotationBeanNameBuilder create(Service service, Class interfaceClass) { + return new AnnotationBeanNameBuilder(service, interfaceClass); + } + + public static AnnotationBeanNameBuilder create(Reference reference, Class interfaceClass) { + return new AnnotationBeanNameBuilder(reference, interfaceClass); + } + + private static void append(StringBuilder builder, String value) { + if (hasText(value)) { + builder.append(SEPARATOR).append(value); + } + } + + public AnnotationBeanNameBuilder group(String group) { + this.group = group; + return this; + } + + public AnnotationBeanNameBuilder version(String version) { + this.version = version; + return this; + } + + public AnnotationBeanNameBuilder environment(Environment environment) { + this.environment = environment; + return this; + } + + /** + * Resolve the protocol + * + * @param protocols one or more protocols + * @return if protocols == null, it will return + * {@link Constants#DEFAULT_PROTOCOL "dubbo"} as the default protocol + * @see Constants#DEFAULT_PROTOCOL + */ + private static String resolveProtocol(String... protocols) { + String protocol = arrayToCommaDelimitedString(protocols); + return hasText(protocol) ? protocol : DEFAULT_PROTOCOL; + } + + /** + * Build bean name while resolve the placeholders if possible. + * + * @return pattern : ${category}:${protocol}:${serviceInterface}:${version}:${group} + */ + public String build() { + // Append the required properties + StringBuilder beanNameBuilder = new StringBuilder(category); + append(beanNameBuilder, protocol); + append(beanNameBuilder, interfaceClassName); + // Append the optional properties + append(beanNameBuilder, version); + append(beanNameBuilder, group); + String beanName = beanNameBuilder.toString(); + // Resolve placeholders + return environment != null ? environment.resolvePlaceholders(beanName) : beanName; + } +} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessor.java index 96c4d3885b0..9bfa2606e2d 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessor.java @@ -20,6 +20,7 @@ import org.apache.dubbo.config.AbstractConfig; import org.apache.dubbo.config.spring.context.annotation.DubboConfigBindingRegistrar; import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfigBinding; +import org.apache.dubbo.config.spring.context.config.DubboConfigBeanCustomizer; import org.apache.dubbo.config.spring.context.properties.DefaultDubboConfigBinder; import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder; @@ -30,8 +31,16 @@ import org.springframework.beans.factory.config.BeanPostProcessor; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.env.Environment; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import static org.springframework.beans.factory.BeanFactoryUtils.beansOfTypeIncludingAncestors; + /** * Dubbo Config Binding {@link BeanPostProcessor} * @@ -62,6 +71,8 @@ public class DubboConfigBindingBeanPostProcessor implements BeanPostProcessor, A private boolean ignoreInvalidFields = true; + private List configBeanCustomizers = Collections.emptyList(); + /** * @param prefix the prefix of Configuration Properties * @param beanName the binding Bean Name @@ -80,18 +91,34 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) thro AbstractConfig dubboConfig = (AbstractConfig) bean; - dubboConfigBinder.bind(prefix, dubboConfig); + bind(prefix, dubboConfig); + + customize(beanName, dubboConfig); - if (log.isInfoEnabled()) { - log.info("The properties of bean [name : " + beanName + "] have been binding by prefix of " + - "configuration properties : " + prefix); - } } return bean; } + private void bind(String prefix, AbstractConfig dubboConfig) { + + dubboConfigBinder.bind(prefix, dubboConfig); + + if (log.isInfoEnabled()) { + log.info("The properties of bean [name : " + beanName + "] have been binding by prefix of " + + "configuration properties : " + prefix); + } + } + + private void customize(String beanName, AbstractConfig dubboConfig) { + + for (DubboConfigBeanCustomizer customizer : configBeanCustomizers) { + customizer.customize(beanName, dubboConfig); + } + + } + public boolean isIgnoreUnknownFields() { return ignoreUnknownFields; } @@ -129,6 +156,14 @@ public void setApplicationContext(ApplicationContext applicationContext) throws @Override public void afterPropertiesSet() throws Exception { + initDubboConfigBinder(); + + initConfigBeanCustomizers(); + + } + + private void initDubboConfigBinder() { + if (dubboConfigBinder == null) { try { dubboConfigBinder = applicationContext.getBean(DubboConfigBinder.class); @@ -146,6 +181,16 @@ public void afterPropertiesSet() throws Exception { } + private void initConfigBeanCustomizers() { + + Collection configBeanCustomizers = + beansOfTypeIncludingAncestors(applicationContext, DubboConfigBeanCustomizer.class).values(); + + this.configBeanCustomizers = new ArrayList(configBeanCustomizers); + + AnnotationAwareOrderComparator.sort(this.configBeanCustomizers); + } + /** * Create {@link DubboConfigBinder} instance. * diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java index 82fb9b25eb0..35f1e1d3c4a 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java @@ -16,6 +16,17 @@ */ package org.apache.dubbo.config.spring.beans.factory.annotation; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.spring.ReferenceBean; import org.apache.dubbo.config.spring.ServiceBean; @@ -30,16 +41,6 @@ import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - /** * {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation * that Consumer service {@link Reference} annotated fields @@ -155,7 +156,18 @@ private ReferenceBeanInvocationHandler(ReferenceBean referenceBean) { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - return method.invoke(bean, args); + Object result = null; + try { + if (bean == null) { // If the bean is not initialized, invoke init() + // issue: https://github.com/apache/incubator-dubbo/issues/3429 + init(); + } + result = method.invoke(bean, args); + } catch (InvocationTargetException e) { + // re-throws the actual Exception. + throw e.getTargetException(); + } + return result; } private void init() { @@ -176,7 +188,9 @@ protected String buildInjectedObjectCacheKey(Reference reference, Object bean, S private String buildReferencedBeanName(Reference reference, Class injectedType) { - ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(reference, injectedType, getEnvironment()); + AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(reference, injectedType); + + builder.environment(getEnvironment()); return getEnvironment().resolvePlaceholders(builder.build()); } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java index bc5339b55dc..6f7e8cf445e 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceAnnotationBeanPostProcessor.java @@ -16,6 +16,18 @@ */ package org.apache.dubbo.config.spring.beans.factory.annotation; +import static org.apache.dubbo.config.spring.util.ObjectUtils.of; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; +import static org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR; +import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; +import static org.springframework.util.ClassUtils.resolveClassName; + +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; + import org.apache.dubbo.common.logger.Logger; import org.apache.dubbo.common.logger.LoggerFactory; import org.apache.dubbo.config.annotation.Service; @@ -51,18 +63,6 @@ import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; - -import static org.apache.dubbo.config.spring.util.ObjectUtils.of; -import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; -import static org.springframework.context.annotation.AnnotationConfigUtils.CONFIGURATION_BEAN_NAME_GENERATOR; -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; -import static org.springframework.util.ClassUtils.resolveClassName; - /** * {@link Service} Annotation * {@link BeanDefinitionRegistryPostProcessor Bean Definition Registry Post Processor} @@ -289,8 +289,9 @@ private void registerServiceBean(BeanDefinitionHolder beanDefinitionHolder, Bean */ private String generateServiceBeanName(Service service, Class interfaceClass, String annotatedServiceBeanName) { - ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(service, interfaceClass, environment); + AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(service, interfaceClass); + builder.environment(environment); return builder.build(); @@ -315,8 +316,9 @@ private Class resolveServiceInterfaceClass(Class annotatedServiceBeanClass } if (interfaceClass == null) { - - Class[] allInterfaces = annotatedServiceBeanClass.getInterfaces(); + // Find all interfaces from the annotated class + // To resolve an issue : https://github.com/apache/incubator-dubbo/issues/3251 + Class[] allInterfaces = ClassUtils.getAllInterfacesForClass(annotatedServiceBeanClass); if (allInterfaces.length > 0) { interfaceClass = allInterfaces[0]; diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java deleted file mode 100644 index 6cd712408be..00000000000 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilder.java +++ /dev/null @@ -1,113 +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.config.spring.beans.factory.annotation; - -import org.apache.dubbo.config.annotation.Reference; -import org.apache.dubbo.config.annotation.Service; -import org.apache.dubbo.config.spring.ReferenceBean; -import org.apache.dubbo.config.spring.ServiceBean; - -import org.springframework.core.env.Environment; -import org.springframework.util.StringUtils; - -import static org.apache.dubbo.config.spring.util.AnnotationUtils.resolveInterfaceName; - - -/** - * Dubbo {@link Service @Service} Bean Builder - * - * @see Service - * @see Reference - * @see ServiceBean - * @see ReferenceBean - * @since 2.6.5 - */ -class ServiceBeanNameBuilder { - - private static final String SEPARATOR = ":"; - - private final String interfaceClassName; - - private final Environment environment; - - // Optional - private String version; - - private String group; - - private ServiceBeanNameBuilder(String interfaceClassName, Environment environment) { - this.interfaceClassName = interfaceClassName; - this.environment = environment; - } - - private ServiceBeanNameBuilder(Class interfaceClass, Environment environment) { - this(interfaceClass.getName(), environment); - } - - private ServiceBeanNameBuilder(Service service, Class interfaceClass, Environment environment) { - this(resolveInterfaceName(service, interfaceClass), environment); - this.group(service.group()); - this.version(service.version()); - } - - private ServiceBeanNameBuilder(Reference reference, Class interfaceClass, Environment environment) { - this(resolveInterfaceName(reference, interfaceClass), environment); - this.group(reference.group()); - this.version(reference.version()); - } - - public static ServiceBeanNameBuilder create(Class interfaceClass, Environment environment) { - return new ServiceBeanNameBuilder(interfaceClass, environment); - } - - public static ServiceBeanNameBuilder create(Service service, Class interfaceClass, Environment environment) { - return new ServiceBeanNameBuilder(service, interfaceClass, environment); - } - - public static ServiceBeanNameBuilder create(Reference reference, Class interfaceClass, Environment environment) { - return new ServiceBeanNameBuilder(reference, interfaceClass, environment); - } - - private static void append(StringBuilder builder, String value) { - if (StringUtils.hasText(value)) { - builder.append(SEPARATOR).append(value); - } - } - - public ServiceBeanNameBuilder group(String group) { - this.group = group; - return this; - } - - public ServiceBeanNameBuilder version(String version) { - this.version = version; - return this; - } - - public String build() { - StringBuilder beanNameBuilder = new StringBuilder("ServiceBean"); - // Required - append(beanNameBuilder, interfaceClassName); - // Optional - append(beanNameBuilder, version); - append(beanNameBuilder, group); - // Build - String rawBeanName = beanNameBuilder.toString(); - // Resolve placeholders - return environment.resolvePlaceholders(rawBeanName); - } -} \ No newline at end of file diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java index e797e5585ec..746db48de69 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigBindingRegistrar.java @@ -18,6 +18,7 @@ import org.apache.dubbo.config.AbstractConfig; import org.apache.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor; +import org.apache.dubbo.config.spring.context.config.NamePropertyDefaultValueDubboConfigBeanCustomizer; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -41,6 +42,8 @@ import java.util.Map; import java.util.Set; +import static org.apache.dubbo.config.spring.context.config.NamePropertyDefaultValueDubboConfigBeanCustomizer.BEAN_NAME; +import static org.apache.dubbo.config.spring.util.BeanRegistrar.registerInfrastructureBean; import static org.apache.dubbo.config.spring.util.PropertySourcesUtils.getSubProperties; import static org.apache.dubbo.config.spring.util.PropertySourcesUtils.normalizePrefix; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; @@ -107,6 +110,8 @@ private void registerDubboConfigBeans(String prefix, } + registerDubboConfigBeanCustomizers(registry); + } private void registerDubboConfigBean(String beanName, Class configClass, @@ -149,6 +154,10 @@ private void registerDubboConfigBindingBeanPostProcessor(String prefix, String b } + private void registerDubboConfigBeanCustomizers(BeanDefinitionRegistry registry) { + registerInfrastructureBean(registry, BEAN_NAME, NamePropertyDefaultValueDubboConfigBeanCustomizer.class); + } + @Override public void setEnvironment(Environment environment) { diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationRegistrar.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationRegistrar.java index cc5f4f8c722..d01dd398ee0 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationRegistrar.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboConfigConfigurationRegistrar.java @@ -44,10 +44,11 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B boolean multiple = attributes.getBoolean("multiple"); - if (multiple) { + // Single Config Bindings + registerBeans(registry, DubboConfigConfiguration.Single.class); + + if (multiple) { // Since 2.6.6 https://github.com/apache/incubator-dubbo/issues/3193 registerBeans(registry, DubboConfigConfiguration.Multiple.class); - } else { - registerBeans(registry, DubboConfigConfiguration.Single.class); } } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubbo.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubbo.java index 4535f188e4c..706e288597e 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubbo.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubbo.java @@ -76,6 +76,6 @@ * @see EnableDubboConfig#multiple() */ @AliasFor(annotation = EnableDubboConfig.class, attribute = "multiple") - boolean multipleConfig() default false; + boolean multipleConfig() default true; } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfig.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfig.java index 47bafa53b54..5a9fe0d595d 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfig.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfig.java @@ -59,7 +59,7 @@ * * @see EnableDubboConfigBinding * @see DubboConfigConfiguration - * @see DubboConfigConfigurationSelector + * @see DubboConfigConfigurationRegistrar * @since 2.5.8 */ @Target({ElementType.TYPE}) @@ -72,9 +72,9 @@ /** * It indicates whether binding to multiple Spring Beans. * - * @return the default value is false + * @return the default value is true since 2.6.6, the value is inverse earlier. * @revised 2.5.9 */ - boolean multiple() default false; + boolean multiple() default true; } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/config/DubboConfigBeanCustomizer.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/config/DubboConfigBeanCustomizer.java new file mode 100644 index 00000000000..75bf0dd2eb6 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/config/DubboConfigBeanCustomizer.java @@ -0,0 +1,47 @@ +/* + * 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.config.spring.context.config; + +import org.apache.dubbo.config.AbstractConfig; +import org.apache.dubbo.config.spring.beans.factory.annotation.DubboConfigBindingBeanPostProcessor; +import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder; + +import org.springframework.context.ApplicationContext; +import org.springframework.core.Ordered; + +/** + * The Bean customizer for {@link AbstractConfig Dubbo Config}. Generally, The subclass will be registered as a Spring + * Bean that is used to {@link #customize(String, AbstractConfig) customize} {@link AbstractConfig Dubbo Config} bean + * after {@link DubboConfigBinder#bind(String, AbstractConfig) its binding}. + *

+ * If There are multiple {@link DubboConfigBeanCustomizer} beans in the Spring {@link ApplicationContext context}, they + * are executed orderly, thus the subclass should be aware to implement the {@link #getOrder()} method. + * + * @see DubboConfigBinder#bind(String, AbstractConfig) + * @see DubboConfigBindingBeanPostProcessor + * @since 2.6.6 + */ +public interface DubboConfigBeanCustomizer extends Ordered { + + /** + * Customize {@link AbstractConfig Dubbo Config Bean} + * + * @param beanName the name of {@link AbstractConfig Dubbo Config Bean} + * @param dubboConfigBean the instance of {@link AbstractConfig Dubbo Config Bean} + */ + void customize(String beanName, AbstractConfig dubboConfigBean); +} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/config/NamePropertyDefaultValueDubboConfigBeanCustomizer.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/config/NamePropertyDefaultValueDubboConfigBeanCustomizer.java new file mode 100644 index 00000000000..fcd053d503c --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/config/NamePropertyDefaultValueDubboConfigBeanCustomizer.java @@ -0,0 +1,83 @@ +/* + * 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.config.spring.context.config; + +import org.apache.dubbo.config.AbstractConfig; + +import org.springframework.util.ReflectionUtils; + +import java.beans.PropertyDescriptor; +import java.lang.reflect.Method; +import java.util.Arrays; + +import static org.apache.dubbo.config.spring.util.ObjectUtils.of; +import static org.springframework.beans.BeanUtils.getPropertyDescriptor; + +/** + * {@link DubboConfigBeanCustomizer} for the default value for the "name" property that will be taken bean name + * if absent. + * + * @since 2.6.6 + */ +public class NamePropertyDefaultValueDubboConfigBeanCustomizer implements DubboConfigBeanCustomizer { + + /** + * The bean name of {@link NamePropertyDefaultValueDubboConfigBeanCustomizer} + * + * @since 2.7.1 + */ + public static final String BEAN_NAME = "namePropertyDefaultValueDubboConfigBeanCustomizer"; + + /** + * The name of property that is "name" maybe is absent in target class + */ + private static final String PROPERTY_NAME = "name"; + + @Override + public void customize(String beanName, AbstractConfig dubboConfigBean) { + + PropertyDescriptor propertyDescriptor = getPropertyDescriptor(dubboConfigBean.getClass(), PROPERTY_NAME); + + if (propertyDescriptor != null) { // "name" property is present + + Method getNameMethod = propertyDescriptor.getReadMethod(); + + if (getNameMethod == null) { // if "getName" method is absent + return; + } + + Object propertyValue = ReflectionUtils.invokeMethod(getNameMethod, dubboConfigBean); + + if (propertyValue != null) { // If The return value of "getName" method is not null + return; + } + + Method setNameMethod = propertyDescriptor.getWriteMethod(); + if (setNameMethod != null && getNameMethod != null) { // "setName" and "getName" methods are present + if (Arrays.equals(of(String.class), setNameMethod.getParameterTypes())) { // the param type is String + // set bean name to the value of the "name" property + ReflectionUtils.invokeMethod(setNameMethod, dubboConfigBean, beanName); + } + } + } + } + + @Override + public int getOrder() { + return HIGHEST_PRECEDENCE; + } +} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/SpringStatusChecker.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/SpringStatusChecker.java index 9a99ec138fd..63517c6e52a 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/SpringStatusChecker.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/status/SpringStatusChecker.java @@ -1,94 +1,101 @@ -/* - * 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.config.spring.status; - -import org.apache.dubbo.common.extension.Activate; -import org.apache.dubbo.common.logger.Logger; -import org.apache.dubbo.common.logger.LoggerFactory; -import org.apache.dubbo.common.status.Status; -import org.apache.dubbo.common.status.StatusChecker; -import org.apache.dubbo.config.spring.extension.SpringExtensionFactory; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.Lifecycle; - -import java.lang.reflect.Method; - -/** - * SpringStatusChecker - */ -@Activate -public class SpringStatusChecker implements StatusChecker { - - private static final Logger logger = LoggerFactory.getLogger(SpringStatusChecker.class); - - @Override - public Status check() { - ApplicationContext context = null; - for (ApplicationContext c : SpringExtensionFactory.getContexts()) { - if (c != null) { - context = c; - break; - } - } - - if (context == null) { - return new Status(Status.Level.UNKNOWN); - } - - Status.Level level = Status.Level.OK; - if (context instanceof Lifecycle) { - if (((Lifecycle) context).isRunning()) { - level = Status.Level.OK; - } else { - level = Status.Level.ERROR; - } - } else { - level = Status.Level.UNKNOWN; - } - StringBuilder buf = new StringBuilder(); - try { - Class cls = context.getClass(); - Method method = null; - while (cls != null && method == null) { - try { - method = cls.getDeclaredMethod("getConfigLocations", new Class[0]); - } catch (NoSuchMethodException t) { - cls = cls.getSuperclass(); - } - } - if (method != null) { - if (!method.isAccessible()) { - method.setAccessible(true); - } - String[] configs = (String[]) method.invoke(context, new Object[0]); - if (configs != null && configs.length > 0) { - for (String config : configs) { - if (buf.length() > 0) { - buf.append(","); - } - buf.append(config); - } - } - } - } catch (Throwable t) { - logger.warn(t.getMessage(), t); - } - return new Status(level, buf.toString()); - } - -} +/* + * 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.config.spring.status; + +import org.apache.dubbo.common.extension.Activate; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.common.status.Status; +import org.apache.dubbo.common.status.StatusChecker; +import org.apache.dubbo.config.spring.extension.SpringExtensionFactory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.Lifecycle; +import org.springframework.web.context.support.GenericWebApplicationContext; + +import java.lang.reflect.Method; + +/** + * SpringStatusChecker + */ +@Activate +public class SpringStatusChecker implements StatusChecker { + + private static final Logger logger = LoggerFactory.getLogger(SpringStatusChecker.class); + + @Override + public Status check() { + ApplicationContext context = null; + for (ApplicationContext c : SpringExtensionFactory.getContexts()) { + // [Issue] SpringStatusChecker execute errors on non-XML Spring configuration + // issue : https://github.com/apache/incubator-dubbo/issues/3615 + if(c instanceof GenericWebApplicationContext) { // ignore GenericXmlApplicationContext + continue; + } + + if (c != null) { + context = c; + break; + } + } + + if (context == null) { + return new Status(Status.Level.UNKNOWN); + } + + Status.Level level = Status.Level.OK; + if (context instanceof Lifecycle) { + if (((Lifecycle) context).isRunning()) { + level = Status.Level.OK; + } else { + level = Status.Level.ERROR; + } + } else { + level = Status.Level.UNKNOWN; + } + StringBuilder buf = new StringBuilder(); + try { + Class cls = context.getClass(); + Method method = null; + while (cls != null && method == null) { + try { + method = cls.getDeclaredMethod("getConfigLocations", new Class[0]); + } catch (NoSuchMethodException t) { + cls = cls.getSuperclass(); + } + } + if (method != null) { + if (!method.isAccessible()) { + method.setAccessible(true); + } + String[] configs = (String[]) method.invoke(context, new Object[0]); + if (configs != null && configs.length > 0) { + for (String config : configs) { + if (buf.length() > 0) { + buf.append(","); + } + buf.append(config); + } + } + } + } catch (Throwable t) { + logger.warn(t.getMessage(), t); + } + return new Status(level, buf.toString()); + } + +} diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilderTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilderTest.java similarity index 51% rename from dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilderTest.java rename to dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilderTest.java index b3616cd58a3..1dcd11391e1 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceBeanNameBuilderTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationBeanNameBuilderTest.java @@ -16,29 +16,31 @@ */ package org.apache.dubbo.config.spring.beans.factory.annotation; +import static org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationBeanNameBuilderTest.GROUP; +import static org.apache.dubbo.config.spring.beans.factory.annotation.AnnotationBeanNameBuilderTest.VERSION; import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.annotation.Service; import org.apache.dubbo.config.spring.api.DemoService; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.mock.env.MockEnvironment; import org.springframework.util.ReflectionUtils; - /** - * {@link ServiceBeanNameBuilder} Test + * {@link AnnotationBeanNameBuilder} Test * - * @see ServiceBeanNameBuilder - * @since 2.6.5 + * @see AnnotationBeanNameBuilder + * @since 2.6.6 */ -@Service(interfaceClass = DemoService.class, group = ServiceBeanNameBuilderTest.GROUP, version = ServiceBeanNameBuilderTest.VERSION, +@Service(interfaceClass = DemoService.class, group = GROUP, version = VERSION, application = "application", module = "module", registry = {"1", "2", "3"}) -public class ServiceBeanNameBuilderTest { +public class AnnotationBeanNameBuilderTest { - @Reference(interfaceClass = DemoService.class, group = "DUBBO", version = "1.0.0", + @Reference(interfaceClass = DemoService.class, group = "DUBBO", version = "${dubbo.version}", application = "application", module = "module", registry = {"1", "2", "3"}) static final Class INTERFACE_CLASS = DemoService.class; @@ -46,30 +48,37 @@ public class ServiceBeanNameBuilderTest { static final String VERSION = "1.0.0"; - static final String BEAN_NAME = "ServiceBean:org.apache.dubbo.config.spring.api.DemoService:1.0.0:DUBBO"; - - private MockEnvironment environment = new MockEnvironment(); + private MockEnvironment environment; - @Test - public void testRequiredAttributes() { - ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(INTERFACE_CLASS, environment); - Assertions.assertEquals("ServiceBean:org.apache.dubbo.config.spring.api.DemoService", builder.build()); + @Before + public void prepare() { + environment = new MockEnvironment(); + environment.setProperty("dubbo.version", "1.0.0"); } @Test public void testServiceAnnotation() { - Service service = AnnotationUtils.getAnnotation(ServiceBeanNameBuilderTest.class, Service.class); - ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(service, INTERFACE_CLASS, environment); - Assertions.assertEquals(BEAN_NAME, + Service service = AnnotationUtils.getAnnotation(AnnotationBeanNameBuilderTest.class, Service.class); + AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(service, INTERFACE_CLASS); + Assert.assertEquals("providers:dubbo:org.apache.dubbo.config.spring.api.DemoService:1.0.0:DUBBO", + builder.build()); + + builder.environment(environment); + Assert.assertEquals("providers:dubbo:org.apache.dubbo.config.spring.api.DemoService:1.0.0:DUBBO", builder.build()); } @Test public void testReferenceAnnotation() { - Reference reference = AnnotationUtils.getAnnotation(ReflectionUtils.findField(ServiceBeanNameBuilderTest.class, "INTERFACE_CLASS"), Reference.class); - ServiceBeanNameBuilder builder = ServiceBeanNameBuilder.create(reference, INTERFACE_CLASS, environment); - Assertions.assertEquals(BEAN_NAME, + Reference reference = AnnotationUtils.getAnnotation(ReflectionUtils.findField(AnnotationBeanNameBuilderTest.class, "INTERFACE_CLASS"), Reference.class); + AnnotationBeanNameBuilder builder = AnnotationBeanNameBuilder.create(reference, INTERFACE_CLASS); + Assert.assertEquals("consumers:dubbo:org.apache.dubbo.config.spring.api.DemoService:${dubbo.version}:DUBBO", + builder.build()); + + builder.environment(environment); + Assert.assertEquals("consumers:dubbo:org.apache.dubbo.config.spring.api.DemoService:1.0.0:DUBBO", builder.build()); } } + diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapterTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapterTest.java index 0baa7bbe450..15d9fb9e550 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapterTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/AnnotationPropertyValuesAdapterTest.java @@ -17,6 +17,12 @@ package org.apache.dubbo.config.spring.beans.factory.annotation; +import static org.springframework.util.StringUtils.arrayToCommaDelimitedString; + +import java.lang.reflect.Field; +import java.util.LinkedHashMap; +import java.util.Map; + import org.apache.dubbo.common.utils.CollectionUtils; import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.spring.ReferenceBean; @@ -31,12 +37,6 @@ import org.springframework.util.ReflectionUtils; import org.springframework.validation.DataBinder; -import java.lang.reflect.Field; -import java.util.LinkedHashMap; -import java.util.Map; - -import static org.springframework.util.StringUtils.arrayToCommaDelimitedString; - /** * {@link AnnotationPropertyValuesAdapter} Test * @@ -96,7 +96,7 @@ public Map convert(String[] source) { Assert.assertEquals("dubbo://localhost:12345", referenceBean.getUrl()); Assert.assertEquals("client", referenceBean.getClient()); Assert.assertEquals(true, referenceBean.isGeneric()); - Assert.assertEquals(true, referenceBean.isInjvm()); + Assert.assertNull(referenceBean.isInjvm()); Assert.assertEquals(false, referenceBean.isCheck()); Assert.assertEquals(true, referenceBean.isInit()); Assert.assertEquals(true, referenceBean.getLazy()); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessorTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessorTest.java index 7811c7371f1..e679f4f976c 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessorTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/DubboConfigBindingBeanPostProcessorTest.java @@ -17,50 +17,56 @@ package org.apache.dubbo.config.spring.beans.factory.annotation; import org.apache.dubbo.config.ApplicationConfig; +import org.apache.dubbo.config.spring.context.config.NamePropertyDefaultValueDubboConfigBeanCustomizer; import org.apache.dubbo.config.spring.context.properties.DefaultDubboConfigBinder; -import org.apache.dubbo.config.spring.context.properties.DubboConfigBinder; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; /** * {@link DubboConfigBindingBeanPostProcessor} */ -@PropertySource({"classpath:/META-INF/config.properties"}) -@Configuration +@RunWith(SpringRunner.class) +@ContextConfiguration(classes = { + DefaultDubboConfigBinder.class, + NamePropertyDefaultValueDubboConfigBeanCustomizer.class, + DubboConfigBindingBeanPostProcessorTest.class +}) +@TestPropertySource(properties = { + "dubbo.application.id = dubbo-demo-application", + "dubbo.application.owner = mercyblitz", + "dubbo.application.organization = Apache", + +}) public class DubboConfigBindingBeanPostProcessorTest { - @Bean("applicationBean") + @Bean("dubbo-demo-application") public ApplicationConfig applicationConfig() { return new ApplicationConfig(); } @Bean - public DubboConfigBinder dubboConfigBinder() { - return new DefaultDubboConfigBinder(); + public DubboConfigBindingBeanPostProcessor bindingBeanPostProcessor() { + return new DubboConfigBindingBeanPostProcessor("dubbo.application", "dubbo-demo-application"); } + @Autowired + private ApplicationContext applicationContext; + @Test public void test() { - final AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); - - applicationContext.register(getClass()); - - Class processorClass = DubboConfigBindingBeanPostProcessor.class; - - applicationContext.registerBeanDefinition("DubboConfigBindingBeanPostProcessor", rootBeanDefinition(processorClass).addConstructorArgValue("dubbo.application").addConstructorArgValue("applicationBean").getBeanDefinition()); - - applicationContext.refresh(); - ApplicationConfig applicationConfig = applicationContext.getBean(ApplicationConfig.class); - Assertions.assertEquals("dubbo-demo-application", applicationConfig.getName()); - + Assert.assertEquals("dubbo-demo-application", applicationConfig.getName()); + Assert.assertEquals("mercyblitz", applicationConfig.getOwner()); + Assert.assertEquals("Apache", applicationConfig.getOrganization()); } } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilderTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilderTest.java index 27f6c9663ed..9018bb0e4b5 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilderTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceBeanBuilderTest.java @@ -17,6 +17,13 @@ package org.apache.dubbo.config.spring.beans.factory.annotation; +import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; +import static org.springframework.util.ReflectionUtils.findField; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + import org.apache.dubbo.config.annotation.Reference; import org.apache.dubbo.config.spring.ReferenceBean; @@ -28,13 +35,6 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import static org.springframework.core.annotation.AnnotationUtils.findAnnotation; -import static org.springframework.util.ReflectionUtils.findField; - /** * {@link ReferenceBeanBuilder} Test * @@ -81,7 +81,7 @@ public void testBuild() throws Exception { Assert.assertEquals("dubbo://localhost:12345", referenceBean.getUrl()); Assert.assertEquals("client", referenceBean.getClient()); Assert.assertEquals(true, referenceBean.isGeneric()); - Assert.assertEquals(true, referenceBean.isInjvm()); + Assert.assertNull(referenceBean.isInjvm()); Assert.assertEquals(false, referenceBean.isCheck()); Assert.assertEquals(null, referenceBean.isInit()); Assert.assertEquals(true, referenceBean.getLazy()); diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java index 5f52a3e0ec0..4945be8a55b 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboConfigTest.java @@ -26,7 +26,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; - import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.PropertySource; @@ -101,7 +100,7 @@ private static class TestMultipleConfig { } - @EnableDubboConfig + @EnableDubboConfig(multiple = false) @PropertySource("META-INF/config.properties") private static class TestConfig { diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DemoServiceImpl.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DemoServiceImpl.java index eb12225588f..3aa1e86f4e6 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DemoServiceImpl.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DemoServiceImpl.java @@ -45,11 +45,6 @@ public String sayName(String name) { @Override public Box getBox() { - return new Box() { - @Override - public String getName() { - return "MyBox"; - } - }; + throw new UnsupportedOperationException("For Purposes!"); } } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboConfigTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboConfigTest.java index fba874332b3..691f251dac6 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboConfigTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/context/annotation/EnableDubboConfigTest.java @@ -25,11 +25,14 @@ import org.apache.dubbo.config.RegistryConfig; import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig; +import org.junit.Assert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.PropertySource; +import java.util.Map; + /** * {@link EnableDubboConfig} Test * @@ -93,15 +96,23 @@ public void testMultiple() { ApplicationConfig applicationBean3 = context.getBean("applicationBean3", ApplicationConfig.class); Assertions.assertEquals("dubbo-demo-application3", applicationBean3.getName()); + Map protocolConfigs = context.getBeansOfType(ProtocolConfig.class); + + for (Map.Entry entry : protocolConfigs.entrySet()) { + String beanName = entry.getKey(); + ProtocolConfig protocol = entry.getValue(); + Assert.assertEquals(beanName, protocol.getName()); + } + } - @EnableDubboConfig(multiple = true) + @EnableDubboConfig @PropertySource("META-INF/config.properties") private static class TestMultipleConfig { } - @EnableDubboConfig + @EnableDubboConfig(multiple = false) @PropertySource("META-INF/config.properties") private static class TestConfig { diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/SpringStatusCheckerTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/SpringStatusCheckerTest.java index f3be445103c..e3386e2588d 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/SpringStatusCheckerTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/status/SpringStatusCheckerTest.java @@ -20,14 +20,15 @@ import org.apache.dubbo.config.spring.ServiceBean; import org.apache.dubbo.config.spring.extension.SpringExtensionFactory; +import org.junit.Assert; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import org.mockito.Mock; import org.mockito.Mockito; import org.springframework.context.ApplicationContext; import org.springframework.context.Lifecycle; +import org.springframework.web.context.support.GenericWebApplicationContext; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -90,4 +91,14 @@ public void testWithoutLifeCycleRunning() { interface ApplicationLifeCycle extends Lifecycle, ApplicationContext { String[] getConfigLocations(); } + + @Test + public void testGenericWebApplicationContext() { + SpringExtensionFactory.clearContexts(); + GenericWebApplicationContext context = new GenericWebApplicationContext(); + SpringExtensionFactory.addApplicationContext(context); + SpringStatusChecker checker = new SpringStatusChecker(); + Status status = checker.check(); + Assert.assertEquals(Status.Level.UNKNOWN, status.getLevel()); + } } diff --git a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/config.properties b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/config.properties index 6e728ccb208..aa79925d4e7 100644 --- a/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/config.properties +++ b/dubbo-config/dubbo-config-spring/src/test/resources/META-INF/config.properties @@ -14,9 +14,13 @@ dubbo.module.name = dubbo-demo-module dubbo.registry.address = zookeeper://192.168.99.100:32770 ## protocol +dubbo.protocol.id = dubbo dubbo.protocol.name = dubbo dubbo.protocol.port = 20880 +dubbo.protocols.rest.port=8080 +dubbo.protocols.thrift.port=9090 + ## monitor dubbo.monitor.address = zookeeper://127.0.0.1:32770 diff --git a/dubbo-metrics/dubbo-metrics-api/pom.xml b/dubbo-metrics/dubbo-metrics-api/pom.xml deleted file mode 100644 index b3f55e53171..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/pom.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - dubbo-metrics - org.apache.dubbo - 2.7.1-SNAPSHOT - - 4.0.0 - - dubbo-metrics-api - - - \ No newline at end of file diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/BucketCounter.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/BucketCounter.java deleted file mode 100644 index 8c449fb4df0..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/BucketCounter.java +++ /dev/null @@ -1,57 +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.metrics; - -import java.util.Map; - -/** - * Store the count in multiple buckets, - * every event will go into one specific bucket according to the happening timestamp. - * The BucketCounter will reserve data for the last N time interval, - * older values will be automatically discarded. - */ -public interface BucketCounter extends Metric { - - /** - * update the counter to the given bucket - */ - void update(); - - /** - * update the counter to the given bucket - */ - void update(long n); - - /** - * Return the bucket count, keyed by timestamp - * @return the bucket count, keyed by timestamp - */ - Map getBucketCounts(); - - /** - * Return the bucket count, keyed by timestamp, since (including) the startTime. - * @param startTime the start time - * @return the bucket count, keyed by timestamp - */ - Map getBucketCounts(long startTime); - - /** - * Get the interval of the bucket - * @return the interval of the bucket - */ - int getBucketInterval(); -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Compass.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Compass.java deleted file mode 100644 index eff395d111d..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Compass.java +++ /dev/null @@ -1,75 +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.metrics; - -import java.util.Map; - -/** - * A metric that provides an easy way to collect method invocation, - * response time, success count, and error code count. - */ -public interface Compass extends Metric { - - /** - * record a method invocation with execution time and sub-categories - * @param duration must be milliseconds - * @param subCategory all the sub-categories should be orthogonal, - * which will be added up to the total number of method invocations - */ - void record(long duration, String subCategory); - - /** - * return method count per bucket per category - * @return - */ - Map> getMethodCountPerCategory(); - - /** - * return method count per bucket per category - * @return - */ - Map> getMethodCountPerCategory(long startTime); - - /** - * return method execution time per bucket per category - * @return - */ - Map> getMethodRtPerCategory(); - - /** - * return method execution time per bucket per category - * @return - */ - Map> getMethodRtPerCategory(long startTime); - - /** - * return method execution time and count per bucket per category - * @return - */ - Map> getCountAndRtPerCategory(); - - /** - * return method execution time and count per bucket per category - * @return - */ - Map> getCountAndRtPerCategory(long startTime); - - /** - * @return the bucket interval - */ - int getBucketInterval(); -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counter.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counter.java deleted file mode 100644 index 22be6620380..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counter.java +++ /dev/null @@ -1,50 +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.metrics; - -/** - *

- * An incrementing and decrementing counter metric.
- * 
- */ -public interface Counter extends Metric, Counting { - - /** - * Increment the counter by one. - */ - void inc(); - - /** - * Increment the counter by {@code n}. - * - * @param n the amount by which the counter will be increased - */ - void inc(long n); - - /** - * Decrement the counter by one. - */ - void dec(); - - /** - * Decrement the counter by {@code n}. - * - * @param n the amount by which the counter will be decreased - */ - void dec(long n); - -} \ No newline at end of file diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counting.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counting.java deleted file mode 100644 index 6795902e06b..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Counting.java +++ /dev/null @@ -1,29 +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.metrics; - -/** - * An interface for metric types which have counts. - */ -public interface Counting { - /** - * Returns the current count. - * - * @return the current count - */ - long getCount(); -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Gauge.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Gauge.java deleted file mode 100644 index a094225f671..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Gauge.java +++ /dev/null @@ -1,42 +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.metrics; - - -/** - *
- * A gauge metric is an instantaneous reading of a particular value. To instrument a queue's depth,
- * for example:
- *
- * final Queue<String> queue = new ConcurrentLinkedQueue<String>();
- * final Gauge<Integer> queueDepth = new Gauge<Integer>() {
- *     public Integer getValue() {
- *         return queue.size();
- *     }
- * };
- * 
- * - * @param the type of the metric's value - */ -public interface Gauge extends Metric { - /** - * Returns the metric's current value. - * - * @return the metric's current value - */ - T getValue(); -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/IMetricManager.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/IMetricManager.java deleted file mode 100644 index eef6720e2bc..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/IMetricManager.java +++ /dev/null @@ -1,104 +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.metrics; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -public interface IMetricManager { - - /** - * Create a {@link Counter} metric in given group, and name. - * if not exist, an instance will be created. - * - * @param group the group of MetricRegistry - * @param name the name of the metric - * @return an instance of counter - */ - Counter getCounter(String group, MetricName name); - - /** - * Create a {@link BucketCounter} metric in given group, and name. - * if not exist, an instance will be created. - * - * @param group the group of MetricRegistry - * @param name the name of the metric - * @return an instance of {@link BucketCounter} - */ - BucketCounter getBucketCounter(String group, MetricName name); - - /** - * Create a {@link Compass} metric in give group, name, and type - * if not exist, an instance will be created. - * @param group the group of MetricRegistry - * @param name the name of the metric - * @return an instance of {@link Compass} - */ - Compass getCompass(String group, MetricName name); - - /** - * Register a customized metric to specified group. - * @param group: the group name of MetricRegistry - * @param metric the metric to register - */ - void register(String group, MetricName name, Metric metric); - - /** - * Get a list of group in current MetricManager - * @return a list of group name - */ - List listMetricGroups(); - - /** - * list all metric names by group - * @return a map of metric name set, keyed by group name - */ - Map> listMetricNamesByGroup(); - - /** - * Get metric registry by group name, - * if not found, null will be returned - * @param group the group name to query - * @return the MetricRegistry that is correspondent to the group - */ - MetricRegistry getMetricRegistryByGroup(String group); - - /** - * Get all the counters by the specific group and filter - * @param group the given group - * @param filter the given filter - * @return the MetricName to Counter map - */ - Map getCounters(String group, MetricFilter filter); - - /** - * Get all the compasses by the specific group and filter - * @param group the given group - * @param filter the given filter - * @return the MetricName to Compass map - */ - Map getCompasses(String group, MetricFilter filter); - - /** - * A map of metric names to metrics. - * - * @return all the metrics - */ - Map getMetrics(String group); - -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Metric.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Metric.java deleted file mode 100644 index 70823f0998a..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/Metric.java +++ /dev/null @@ -1,29 +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.metrics; - -/** - * A tag interface to indicate that a class is a metric. - */ -public interface Metric { - - /** - * Return the last update time in milliseconds - * @return the last updated time in milliseconds - */ - long lastUpdateTime(); -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricFilter.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricFilter.java deleted file mode 100644 index 243c3b7813e..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricFilter.java +++ /dev/null @@ -1,42 +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.metrics; - -/** - * A filter used to determine whether or not a metric should be reported, among other things. - */ -public interface MetricFilter { - - /** - * Matches all metrics, regardless of type or name. - */ - MetricFilter ALL = new MetricFilter() { - @Override - public boolean matches(MetricName name, Metric metric) { - return true; - } - }; - - /** - * Returns {@code true} if the metric matches the filter; {@code false} otherwise. - * - * @param name the metric's name - * @param metric the metric - * @return {@code true} if the metric matches the filter - */ - boolean matches(MetricName name, Metric metric); -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricLevel.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricLevel.java deleted file mode 100644 index d04509a0673..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricLevel.java +++ /dev/null @@ -1,45 +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.metrics; - -/** - * An enumeration class to represent the metric level - */ -public enum MetricLevel { - - TRIVIAL, // trivial metrics - - MINOR, // minor metrics - - NORMAL, // normal metrics - - MAJOR, // major metrics - - CRITICAL; // critical metrics - - public static int getMaxValue() { - MetricLevel[] levels = MetricLevel.values(); - int max = levels[0].ordinal(); - for (MetricLevel level : levels) { - int value = level.ordinal(); - if (value > max) { - max = value; - } - } - return max; - } -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricManager.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricManager.java deleted file mode 100644 index 1c032ace08f..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricManager.java +++ /dev/null @@ -1,108 +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.metrics; - -import java.lang.reflect.Method; - -/** - * The design concept is heavily borrowed from SLF4j (http://www.slf4j.org/), the logging framework. - * The application only depends on the metrics api. - * The implementation will be dynamically bound. - * If the implementation if not found in classpath, by default the {@link NOPMetricManager} will be bound. - */ -public class MetricManager { - - private static final String BINDER_CLASS = "org.apache.dubbo.metrics.MetricManagerBinder"; - - private static final IMetricManager NOP_METRIC_MANAGER = new NOPMetricManager(); - - private static volatile IMetricManager iMetricManager; - - /** - * Create a {@link Counter} metric in given group, and name. - * if not exist, an instance will be created. - * - * @param group the group of MetricRegistry - * @param name the name of the metric - * @return an instance of counter - */ - public static Counter getCounter(String group, MetricName name) { - IMetricManager manager = getIMetricManager(); - return manager.getCounter(group, name); - } - - /** - * Create a {@link BucketCounter} metric in given group, and name. - * if not exist, an instance will be created. - * - * @param group the group of MetricRegistry - * @param name the name of the metric - * @return an instance of {@link BucketCounter} - */ - public static BucketCounter getBucketCounters(String group, MetricName name) { - IMetricManager manager = getIMetricManager(); - return manager.getBucketCounter(group, name); - } - - /** - * Create a {@link Compass} metric in given group, and name - * if not exist, an instance will be created. - * - * @param group the group of MetricRegistry - * @param name the name of the metric - * @return an instance of {@link Compass} - */ - public static Compass getCompass(String group, MetricName name) { - IMetricManager manager = getIMetricManager(); - return manager.getCompass(group, name); - } - - /** - * Register a customized metric to specified group. - * @param group the group name of MetricRegistry - * @param metric the metric to register - */ - public static void register(String group, MetricName name, Metric metric) { - IMetricManager manager = getIMetricManager(); - manager.register(group, name, metric); - } - - /** - * get dynamically bound {@link IMetricManager} instance - * @return the {@link IMetricManager} instance bound - */ - @SuppressWarnings("unchecked") - public static IMetricManager getIMetricManager() { - if (iMetricManager == null) { - synchronized (MetricManager.class) { - if (iMetricManager == null) { - try { - Class binderClazz = MetricManager.class.getClassLoader().loadClass(BINDER_CLASS); - Method getSingleton = binderClazz.getMethod("getSingleton"); - Object binderObject = getSingleton.invoke(null); - Method getMetricManager = binderClazz.getMethod("getMetricManager"); - iMetricManager = (IMetricManager) getMetricManager.invoke(binderObject); - } catch (Exception e) { - iMetricManager = NOP_METRIC_MANAGER; - } - } - } - } - return iMetricManager; - } - -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricName.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricName.java deleted file mode 100644 index ca7edb9292a..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricName.java +++ /dev/null @@ -1,389 +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.metrics; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; - -/** - * This class is based on Dropwizard metrics, see io/dropwizard/metrics/MetricName.java - * - * The following changes are made: - * * Add metric level - * * Cache the hash code - * - */ -public class MetricName implements Comparable { - - public static final String SEPARATOR = "."; - public static final Map EMPTY_TAGS = Collections.emptyMap(); - public static final MetricName EMPTY = new MetricName(); - - private final String key; - private final Map tags; - // the level to indicate the importance of a metric - private MetricLevel level; - - private int hashCode = 0; - - private boolean hashCodeCached = false; - - public MetricName() { - this(null, null, null); - } - - public MetricName(String key) { - this(key, null, null); - } - - public MetricName(String key, Map tags) { - this(key, tags, null); - } - - public MetricName(String key, MetricLevel level) { - this(key, null, level); - } - - public MetricName(String key, Map tags, MetricLevel level) { - this.key = key; - this.tags = checkTags(tags); - this.level = level == null ? MetricLevel.NORMAL : level; - } - - private Map checkTags(Map tags) { - if (tags == null || tags.isEmpty()) { - return EMPTY_TAGS; - } - - return Collections.unmodifiableMap(tags); - } - - public String getKey() { - return key; - } - - public Map getTags() { - return tags; - } - - /** - * Return the level of this metric - * The level indicates the importance of the metric - * - * @return when level tag do not exist or illegal tag, will return null. - */ - public MetricLevel getMetricLevel() { - return level; - } - - - /** - * Metric level can be changed during runtime - * @param level the level to set - */ - public MetricName level(MetricLevel level) { - this.level = level; - return this; - } - - - /** - * @see {@link #resolve(String, boolean)} - */ - public MetricName resolve(String p) { - return resolve(p, true); - } - - /** - * Build the MetricName that is this with another path appended to it. - * - * The new MetricName inherits the tags of this one. - * - * @param p The extra path element to add to the new metric. - * @param inheritTags if true, tags will be inherited - * @return A new metric name relative to the original by the path specified - * in p. - */ - public MetricName resolve(String p, boolean inheritTags) { - final String next; - - if (p != null && !p.isEmpty()) { - if (key != null && !key.isEmpty()) { - next = key + SEPARATOR + p; - } else { - next = p; - } - } else { - next = this.key; - } - - return inheritTags ? new MetricName(next, tags, level) : new MetricName(next, level); - } - - /** - * Add tags to a metric name and return the newly created MetricName. - * - * @param add Tags to add. - * @return A newly created metric name with the specified tags associated with it. - */ - public MetricName tag(Map add) { - final Map tags = new HashMap(add); - tags.putAll(this.tags); - return new MetricName(key, tags, level); - } - - /** - * Same as {@link #tag(Map)}, but takes a variadic list - * of arguments. - * - * @see #tag(Map) - * @param pairs An even list of strings acting as key-value pairs. - * @return A newly created metric name with the specified tags associated - * with it. - */ - public MetricName tag(String... pairs) { - if (pairs == null) { - return this; - } - - if (pairs.length % 2 != 0) { - throw new IllegalArgumentException("Argument count must be even"); - } - - final Map add = new HashMap(); - - for (int i = 0; i < pairs.length; i += 2) { - add.put(pairs[i], pairs[i+1]); - } - - return tag(add); - } - - /** - * Join the specified set of metric names. - * - * @param parts Multiple metric names to join using the separator. - * @return A newly created metric name which has the name of the specified - * parts and includes all tags of all child metric names. - **/ - public static MetricName join(MetricName... parts) { - final StringBuilder nameBuilder = new StringBuilder(); - final Map tags = new HashMap(); - - boolean first = true; - MetricName firstName = null; - - for (MetricName part : parts) { - final String name = part.getKey(); - - if (name != null && !name.isEmpty()) { - if (first) { - first = false; - firstName = part; - } else { - nameBuilder.append(SEPARATOR); - } - - nameBuilder.append(name); - } - - if (!part.getTags().isEmpty()) { - tags.putAll(part.getTags()); - } - } - - MetricLevel level = firstName == null ? null : firstName.getMetricLevel(); - return new MetricName(nameBuilder.toString(), tags, level); - } - - /** - * Build a new metric name using the specific path components. - * - * @param parts Path of the new metric name. - * @return A newly created metric name with the specified path. - **/ - public static MetricName build(String... parts) { - if (parts == null || parts.length == 0) { - return MetricName.EMPTY; - } - - if (parts.length == 1) { - return new MetricName(parts[0], EMPTY_TAGS); - } - - return new MetricName(buildName(parts), EMPTY_TAGS); - } - - private static String buildName(String... names) { - final StringBuilder builder = new StringBuilder(); - boolean first = true; - - for (String name : names) { - if (name == null || name.isEmpty()) { - continue; - } - - if (first) { - first = false; - } else { - builder.append(SEPARATOR); - } - - builder.append(name); - } - - return builder.toString(); - } - - @Override - public String toString() { - if (tags.isEmpty()) { - return key; - } - - return key + tags; - } - - @Override - public int hashCode() { - - if (!hashCodeCached){ - - final int prime = 31; - int result = 1; - result = prime * result + ((key == null) ? 0 : key.hashCode()); - result = prime * result + ((tags == null) ? 0 : tags.hashCode()); - - hashCode = result; - hashCodeCached = true; - } - - return hashCode; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - - if (obj == null) { - return false; - } - - if (getClass() != obj.getClass()) { - return false; - } - - MetricName other = (MetricName) obj; - - if (key == null) { - if (other.key != null) { - return false; - } - } else if (!key.equals(other.key)) { - return false; - } - - if (!tags.equals(other.tags)) { - return false; - } - - return true; - } - - @Override - public int compareTo(MetricName o) { - if (o == null) { - return -1; - } - - int c = compareName(key, o.getKey()); - - if (c != 0) { - return c; - } - - return compareTags(tags, o.getTags()); - } - - private int compareName(String left, String right) { - if (left == null && right == null) { - return 0; - } - - if (left == null) { - return 1; - } - - if (right == null) { - return -1; - } - - return left.compareTo(right); - } - - private int compareTags(Map left, Map right) { - if (left == null && right == null) { - return 0; - } - - if (left == null) { - return 1; - } - - if (right == null) { - return -1; - } - - final Iterable keys = uniqueSortedKeys(left, right); - - for (final String key : keys) { - final String a = left.get(key); - final String b = right.get(key); - - if (a == null && b == null) { - continue; - } - - if (a == null) { - return -1; - } - - if (b == null) { - return 1; - } - - int c = a.compareTo(b); - - if (c != 0) { - return c; - } - } - - return 0; - } - - private Iterable uniqueSortedKeys(Map left, Map right) { - final Set set = new TreeSet(left.keySet()); - set.addAll(right.keySet()); - return set; - } -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricRegistry.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricRegistry.java deleted file mode 100644 index 567ee4aa733..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricRegistry.java +++ /dev/null @@ -1,157 +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.metrics; - -import java.util.Map; -import java.util.Set; - -/** - * A registry of metric instances. - */ -public abstract class MetricRegistry implements MetricSet { - - /** - * Given a {@link Metric}, registers it under the given name. - * - * @param name the name of the metric - * @param metric the metric - * @param the type of the metric - * @return {@code metric} - * @throws IllegalArgumentException if the name is already registered - */ - public abstract T register(String name, T metric) throws IllegalArgumentException; - - /** - * Given a {@link Metric}, registers it under the given name. - * - * @param name the name of the metric - * @param metric the metric - * @param the type of the metric - * @return {@code metric} - * @throws IllegalArgumentException if the name is already registered - */ - public abstract T register(MetricName name, T metric) throws IllegalArgumentException; - - /** - * Given a metric set, registers them. - * - * @param metrics a set of metrics - * @throws IllegalArgumentException if any of the names are already registered - */ - public abstract void registerAll(MetricSet metrics) throws IllegalArgumentException; - - /** - * Creates a new {@link Counter} and registers it under the given name. - * - * @param name the name of the metric - * @return a new {@link Counter} - */ - public abstract Counter counter(String name); - - /** - * Return the {@link Counter} registered under this name; or create and register - * a new {@link Counter} if none is registered. - * - * @param name the name of the metric - * @return a new or pre-existing {@link Counter} - */ - public abstract Counter counter(MetricName name); - - /** - * Create a FastCompass with given name - * @param name the name of the metric - * @return a FastCompass instance - */ - public abstract Compass compass(MetricName name); - - /** - * Removes the metric with the given name. - * - * @param name the name of the metric - * @return whether or not the metric was removed - */ - public abstract boolean remove(MetricName name); - - /** - * Removes all metrics which match the given filter. - * - * @param filter a filter - */ - public abstract void removeMatching(MetricFilter filter); - - /** - * Returns a set of the names of all the metrics in the registry. - * - * @return the names of all the metrics - */ - public abstract Set getNames(); - - /** - * Returns a map of all the gauges in the registry and their names. - * - * @return all the gauges in the registry - */ - public abstract Map getGauges(); - - /** - * Returns a map of all the gauges in the registry and their names which match the given filter. - * - * @param filter the metric filter to match - * @return all the gauges in the registry - */ - public abstract Map getGauges(MetricFilter filter); - - /** - * Returns a map of all the counters in the registry and their names. - * - * @return all the counters in the registry - */ - public abstract Map getCounters(); - - /** - * Returns a map of all the counters in the registry and their names which match the given - * filter. - * - * @param filter the metric filter to match - * @return all the counters in the registry - */ - public abstract Map getCounters(MetricFilter filter); - - /** - * Returns a map of all the compasses in the registry and their names. - * - * @return all the compasses in the registry - */ - public abstract Map getCompasses(); - - /** - * Returns a map of all the compasses in the registry and their names which match the given filter. - * - * @param filter the metric filter to match - * @return all the compasses in the registry - */ - public abstract Map getCompasses(MetricFilter filter); - - /** - * Returns a map of all the metrics in the registry and their names which match the given filter - * @param filter the metric filter to match - * @return all the metrics in the registry - */ - public abstract Map getMetrics(MetricFilter filter); - - -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricSet.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricSet.java deleted file mode 100644 index 68c95a124e1..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricSet.java +++ /dev/null @@ -1,33 +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.metrics; - -import java.util.Map; - -/** - * A set of named metrics. - * - * @see MetricRegistry#registerAll(MetricSet) - */ -public interface MetricSet extends Metric { - /** - * A map of metric names to metrics. - * - * @return the metrics - */ - Map getMetrics(); -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/NOPMetricManager.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/NOPMetricManager.java deleted file mode 100644 index 2228fd774a7..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/NOPMetricManager.java +++ /dev/null @@ -1,292 +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.metrics; - -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; - -/** - * The empty implementation for IMetricManager - */ -public class NOPMetricManager implements IMetricManager { - - private static final Map emptyMap = new HashMap(); - private static final Set emptySet = new HashSet(); - - @Override - public Counter getCounter(String group, MetricName name) { - return NOP_COUNTER; - } - - @Override - public BucketCounter getBucketCounter(String group, MetricName name) { - return NOP_BUCKET_COUNTER; - } - - @Override - public Compass getCompass(String group, MetricName name) { - return NOP_COMPASS; - } - - @Override - public List listMetricGroups() { - return Collections.emptyList(); - } - - @Override - public Map> listMetricNamesByGroup() { - return Collections.emptyMap(); - } - - @Override - public MetricRegistry getMetricRegistryByGroup(String group) { - return NOP_REGISTRY; - } - - @Override - @SuppressWarnings("unchecked") - public Map getCounters(String group, MetricFilter filter) { - return emptyMap; - } - - @Override - @SuppressWarnings("unchecked") - public Map getCompasses(String group, MetricFilter filter) { - return emptyMap; - } - - @Override - public void register(String group, MetricName name, Metric metric) { - - } - - static final BucketCounter NOP_BUCKET_COUNTER = new BucketCounter() { - @Override - public void update() { - - } - - @Override - public void update(long n) { - - } - - @Override - public Map getBucketCounts() { - return emptyMap; - } - - @Override - public Map getBucketCounts(long startTime) { - return emptyMap; - } - - @Override - public int getBucketInterval() { - return 0; - } - - @Override - public long lastUpdateTime() { - return 0; - } - }; - - static final Counter NOP_COUNTER = new Counter() { - @Override - public void inc() { - } - - @Override - public void inc(long n) { - } - - @Override - public void dec() { - } - - @Override - public void dec(long n) { - } - - @Override - public long getCount() { - return 0; - } - - @Override - public long lastUpdateTime() { - return 0; - } - }; - - static final Compass NOP_COMPASS = new Compass() { - @Override - public void record(long duration, String subCategory) { - - } - - @Override - public Map> getMethodCountPerCategory() { - return emptyMap; - } - - @Override - public Map> getMethodRtPerCategory() { - return emptyMap; - } - - @Override - public Map> getMethodCountPerCategory(long startTime) { - return emptyMap; - } - - @Override - public Map> getMethodRtPerCategory(long startTime) { - return emptyMap; - } - - @Override - public int getBucketInterval() { - return 0; - } - - @Override - public Map> getCountAndRtPerCategory() { - return emptyMap; - } - - @Override - public Map> getCountAndRtPerCategory(long startTime) { - return emptyMap; - } - - @Override - public long lastUpdateTime() { - return 0; - } - }; - - private static final MetricRegistry NOP_REGISTRY = new MetricRegistry() { - @Override - public T register(String name, T metric) throws IllegalArgumentException { - return metric; - } - - @Override - public T register(MetricName name, T metric) throws IllegalArgumentException { - return metric; - } - - @Override - public void registerAll(MetricSet metrics) throws IllegalArgumentException { - - } - - @Override - public Counter counter(String name) { - return NOP_COUNTER; - } - - @Override - public Counter counter(MetricName name) { - return NOP_COUNTER; - } - - @Override - public Compass compass(MetricName name) { - return NOP_COMPASS; - } - - @Override - public boolean remove(MetricName name) { - return false; - } - - @Override - public void removeMatching(MetricFilter filter) { - - } - - @Override - @SuppressWarnings("unchecked") - public Set getNames() { - return emptySet; - } - - @Override - @SuppressWarnings("unchecked") - public Map getGauges() { - return emptyMap; - } - - @Override - @SuppressWarnings("unchecked") - public Map getGauges(MetricFilter filter) { - return emptyMap; - } - - @Override - @SuppressWarnings("unchecked") - public Map getCounters() { - return emptyMap; - } - - @Override - @SuppressWarnings("unchecked") - public Map getCounters(MetricFilter filter) { - return emptyMap; - } - - @Override - public Map getCompasses() { - return emptyMap; - } - - @Override - public Map getCompasses(MetricFilter filter) { - return emptyMap; - } - - @Override - @SuppressWarnings("unchecked") - public Map getMetrics(MetricFilter filter) { - return emptyMap; - } - - @Override - @SuppressWarnings("unchecked") - public Map getMetrics() { - return emptyMap; - } - - @Override - public long lastUpdateTime() { - return 0; - } - }; - - @Override - public Map getMetrics(String group) { - return emptyMap; - } - -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricManagerTest.java b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricManagerTest.java deleted file mode 100644 index 17b4a8b3097..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricManagerTest.java +++ /dev/null @@ -1,53 +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.metrics; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -public class MetricManagerTest { - - @Test - public void testNOPMetricManager() { - Assertions.assertTrue(MetricManager.getIMetricManager() instanceof NOPMetricManager); - } - - @Test - public void testNOPCompass() { - Compass compass = MetricManager.getCompass("test", MetricName.build("test")); - compass.record(10, "success"); - - Assertions.assertEquals(0, compass.getCountAndRtPerCategory().size()); - Assertions.assertEquals(0, compass.getMethodCountPerCategory().size()); - Assertions.assertEquals(0, compass.getMethodRtPerCategory().size()); - } - - @Test - public void testNopCounter() { - Counter counter = MetricManager.getCounter("test", MetricName.build("test2")); - counter.inc(); - Assertions.assertEquals(0, counter.getCount()); - } - - @Test - public void testBucketCounter() { - BucketCounter bc = MetricManager.getBucketCounters("test", MetricName.build("test3")); - bc.update(); - Assertions.assertEquals(0, bc.getBucketInterval()); - Assertions.assertEquals(0, bc.getBucketCounts().size()); - } -} diff --git a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricNameTest.java b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricNameTest.java deleted file mode 100644 index 102a68e4cb4..00000000000 --- a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/MetricNameTest.java +++ /dev/null @@ -1,138 +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.metrics; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.util.HashMap; -import java.util.Map; - - -public class MetricNameTest { - - @Test - public void testEmpty() { - Assertions.assertEquals(MetricName.EMPTY.getTags(), MetricName.EMPTY_TAGS); - Assertions.assertNull(MetricName.EMPTY.getKey()); - Assertions.assertEquals(new MetricName().getTags(), MetricName.EMPTY_TAGS); - - Assertions.assertEquals(MetricName.EMPTY, new MetricName()); - Assertions.assertEquals(MetricName.build(), MetricName.EMPTY); - Assertions.assertEquals(MetricName.EMPTY.resolve(null), MetricName.EMPTY); - } - - @Test - public void testEmptyResolve() { - final MetricName name = new MetricName(); - Assertions.assertEquals(name.resolve("foo"), new MetricName("foo")); - } - - @Test - public void testResolveToEmpty() { - final MetricName name = new MetricName("foo"); - Assertions.assertEquals(name.resolve(null), new MetricName("foo")); - } - - @Test - public void testResolve() { - final MetricName name = new MetricName("foo"); - Assertions.assertEquals(name.resolve("bar"), new MetricName("foo.bar")); - } - - @Test - public void testResolveWithTags() { - final MetricName name = new MetricName("foo").tag("key", "value"); - Assertions.assertEquals(name.resolve("bar"), new MetricName("foo.bar").tag("key", "value")); - } - - @Test - public void testResolveWithoutTags() { - final MetricName name = new MetricName("foo").tag("key", "value"); - Assertions.assertEquals(name.resolve("bar", false), new MetricName("foo.bar")); - } - - @Test - public void testResolveBothEmpty() { - final MetricName name = new MetricName(null); - Assertions.assertEquals(name.resolve(null), new MetricName()); - } - - @Test - public void testAddTagsVarious() { - final Map refTags = new HashMap(); - refTags.put("foo", "bar"); - final MetricName test = MetricName.EMPTY.tag("foo", "bar"); - final MetricName test2 = MetricName.EMPTY.tag(refTags); - - Assertions.assertEquals(test, new MetricName(null, refTags)); - Assertions.assertEquals(test.getTags(), refTags); - - Assertions.assertEquals(test2, new MetricName(null, refTags)); - Assertions.assertEquals(test2.getTags(), refTags); - } - - @Test - public void testTaggedMoreArguments() { - final Map refTags = new HashMap(); - refTags.put("foo", "bar"); - refTags.put("baz", "biz"); - Assertions.assertEquals(MetricName.EMPTY.tag("foo", "bar", "baz", "biz").getTags(), refTags); - } - - @Test - public void testTaggedNotPairs() { - Assertions.assertThrows(IllegalArgumentException.class, () -> MetricName.EMPTY.tag("foo")); - } - - @Test - public void testTaggedNotPairs2() { - Assertions.assertThrows(IllegalArgumentException.class, () -> MetricName.EMPTY.tag("foo", "bar", "baz")); - } - - @Test - public void testCompareTo() { - final MetricName a = MetricName.EMPTY.tag("foo", "bar"); - final MetricName b = MetricName.EMPTY.tag("foo", "baz"); - - Assertions.assertTrue(a.compareTo(b) < 0); - Assertions.assertTrue(b.compareTo(a) > 0); - Assertions.assertTrue(b.compareTo(b) == 0); - Assertions.assertTrue(b.resolve("key").compareTo(b) < 0); - Assertions.assertTrue(b.compareTo(b.resolve("key")) > 0); - } - - @Test - public void testTaggedWithLevel() { - MetricName name = MetricName.build("test").level(MetricLevel.CRITICAL); - MetricName tagged = name.tag("foo", "bar"); - Assertions.assertEquals(tagged.getMetricLevel(), MetricLevel.CRITICAL); - } - - @Test - public void testJoinWithLevel() { - MetricName name = MetricName.build("test").level(MetricLevel.CRITICAL); - MetricName tagged = MetricName.join(name, MetricName.build("abc")); - Assertions.assertEquals(tagged.getMetricLevel(), MetricLevel.CRITICAL); - } - - @Test - public void testResolveWithLevel() { - final MetricName name = new MetricName("foo").level(MetricLevel.CRITICAL).tag("key", "value"); - Assertions.assertEquals(name.resolve("bar"), new MetricName("foo.bar").tag("key", "value").level(MetricLevel.CRITICAL)); - } -} diff --git a/dubbo-metrics/pom.xml b/dubbo-metrics/pom.xml deleted file mode 100644 index b1ecde927ba..00000000000 --- a/dubbo-metrics/pom.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - org.apache.dubbo - dubbo-parent - 2.7.1-SNAPSHOT - - 4.0.0 - - dubbo-metrics - pom - ${project.artifactId} - The metrics module of dubbo project - - - false - - - - dubbo-metrics-api - - - - \ No newline at end of file