diff --git a/dubbo-plugin/dubbo-qos-api/src/main/java/org/apache/dubbo/qos/api/QosConfiguration.java b/dubbo-plugin/dubbo-qos-api/src/main/java/org/apache/dubbo/qos/api/QosConfiguration.java
index 476079aa14..69f9b10e19 100644
--- a/dubbo-plugin/dubbo-qos-api/src/main/java/org/apache/dubbo/qos/api/QosConfiguration.java
+++ b/dubbo-plugin/dubbo-qos-api/src/main/java/org/apache/dubbo/qos/api/QosConfiguration.java
@@ -38,6 +38,9 @@ public class QosConfiguration {
// the default value is Cmd.PermissionLevel.PUBLIC, can only access PUBLIC level cmd
private PermissionLevel anonymousAccessPermissionLevel = PermissionLevel.PUBLIC;
+ // the allow commands for anonymous access, the delimiter is colon(,)
+ private String anonymousAllowCommands;
+
private QosConfiguration() {
}
@@ -46,6 +49,7 @@ public class QosConfiguration {
this.acceptForeignIp = builder.isAcceptForeignIp();
this.acceptForeignIpWhitelist = builder.getAcceptForeignIpWhitelist();
this.anonymousAccessPermissionLevel = builder.getAnonymousAccessPermissionLevel();
+ this.anonymousAllowCommands = builder.getAnonymousAllowCommands();
buildPredicate();
}
@@ -93,6 +97,10 @@ public class QosConfiguration {
return acceptForeignIp;
}
+ public String getAnonymousAllowCommands() {
+ return anonymousAllowCommands;
+ }
+
public static Builder builder() {
return new Builder();
}
@@ -103,6 +111,7 @@ public class QosConfiguration {
private boolean acceptForeignIp;
private String acceptForeignIpWhitelist;
private PermissionLevel anonymousAccessPermissionLevel = PermissionLevel.PUBLIC;
+ private String anonymousAllowCommands;
private Builder() {
}
@@ -127,6 +136,11 @@ public class QosConfiguration {
return this;
}
+ public Builder anonymousAllowCommands(String anonymousAllowCommands) {
+ this.anonymousAllowCommands = anonymousAllowCommands;
+ return this;
+ }
+
public QosConfiguration build() {
return new QosConfiguration(this);
}
@@ -146,5 +160,9 @@ public class QosConfiguration {
public PermissionLevel getAnonymousAccessPermissionLevel() {
return anonymousAccessPermissionLevel;
}
+
+ public String getAnonymousAllowCommands() {
+ return anonymousAllowCommands;
+ }
}
}
diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/permission/DefaultAnonymousAccessPermissionChecker.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/permission/DefaultAnonymousAccessPermissionChecker.java
index 8efd756e00..61736f2247 100644
--- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/permission/DefaultAnonymousAccessPermissionChecker.java
+++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/permission/DefaultAnonymousAccessPermissionChecker.java
@@ -16,13 +16,16 @@
*/
package org.apache.dubbo.qos.permission;
-import io.netty.channel.Channel;
+import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.qos.api.CommandContext;
import org.apache.dubbo.qos.api.PermissionLevel;
import org.apache.dubbo.qos.api.QosConfiguration;
+import io.netty.channel.Channel;
+
import java.net.InetAddress;
import java.net.InetSocketAddress;
+import java.util.Arrays;
import java.util.Optional;
public class DefaultAnonymousAccessPermissionChecker implements PermissionChecker {
@@ -37,6 +40,15 @@ public class DefaultAnonymousAccessPermissionChecker implements PermissionChecke
.orElse(null);
QosConfiguration qosConfiguration = commandContext.getQosConfiguration();
+ String anonymousAllowCommands = qosConfiguration.getAnonymousAllowCommands();
+ if (StringUtils.isNotEmpty(anonymousAllowCommands) &&
+ Arrays.stream(anonymousAllowCommands.split(","))
+ .filter(StringUtils::isNotEmpty)
+ .map(String::trim)
+ .anyMatch(cmd -> cmd.equals(commandContext.getCommandName()))) {
+ return true;
+ }
+
PermissionLevel currentLevel = qosConfiguration.getAnonymousAccessPermissionLevel();
// Local has private permission
diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/protocol/QosProtocolWrapper.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/protocol/QosProtocolWrapper.java
index ef2f35c3dd..127fa8bb42 100644
--- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/protocol/QosProtocolWrapper.java
+++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/protocol/QosProtocolWrapper.java
@@ -40,6 +40,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.QOS_FAILED_START_SERVER;
import static org.apache.dubbo.common.constants.QosConstants.ACCEPT_FOREIGN_IP;
import static org.apache.dubbo.common.constants.QosConstants.ACCEPT_FOREIGN_IP_WHITELIST;
+import static org.apache.dubbo.common.constants.QosConstants.ANONYMOUS_ACCESS_ALLOW_COMMANDS;
import static org.apache.dubbo.common.constants.QosConstants.ANONYMOUS_ACCESS_PERMISSION_LEVEL;
import static org.apache.dubbo.common.constants.QosConstants.QOS_ENABLE;
import static org.apache.dubbo.common.constants.QosConstants.QOS_HOST;
@@ -119,6 +120,7 @@ public class QosProtocolWrapper implements Protocol, ScopeModelAware {
boolean acceptForeignIp = Boolean.parseBoolean(url.getParameter(ACCEPT_FOREIGN_IP, "false"));
String acceptForeignIpWhitelist = url.getParameter(ACCEPT_FOREIGN_IP_WHITELIST, StringUtils.EMPTY_STRING);
String anonymousAccessPermissionLevel = url.getParameter(ANONYMOUS_ACCESS_PERMISSION_LEVEL, PermissionLevel.PUBLIC.name());
+ String anonymousAllowCommands = url.getParameter(ANONYMOUS_ACCESS_ALLOW_COMMANDS, StringUtils.EMPTY_STRING);
Server server = frameworkModel.getBeanFactory().getBean(Server.class);
if (server.isStarted()) {
@@ -130,6 +132,7 @@ public class QosProtocolWrapper implements Protocol, ScopeModelAware {
server.setAcceptForeignIp(acceptForeignIp);
server.setAcceptForeignIpWhitelist(acceptForeignIpWhitelist);
server.setAnonymousAccessPermissionLevel(anonymousAccessPermissionLevel);
+ server.setAnonymousAllowCommands(anonymousAllowCommands);
server.start();
} catch (Throwable throwable) {
diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/Server.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/Server.java
index 4dc03d1cd7..65e7e3b9fa 100644
--- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/Server.java
+++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/server/Server.java
@@ -58,6 +58,8 @@ public class Server {
private String anonymousAccessPermissionLevel = PermissionLevel.NONE.name();
+ private String anonymousAllowCommands = StringUtils.EMPTY_STRING;
+
private EventLoopGroup boss;
private EventLoopGroup worker;
@@ -108,6 +110,7 @@ public class Server {
.acceptForeignIp(acceptForeignIp)
.acceptForeignIpWhitelist(acceptForeignIpWhitelist)
.anonymousAccessPermissionLevel(anonymousAccessPermissionLevel)
+ .anonymousAllowCommands(anonymousAllowCommands)
.build()
));
}
@@ -167,6 +170,10 @@ public class Server {
this.anonymousAccessPermissionLevel = anonymousAccessPermissionLevel;
}
+ public void setAnonymousAllowCommands(String anonymousAllowCommands) {
+ this.anonymousAllowCommands = anonymousAllowCommands;
+ }
+
public String getWelcome() {
return welcome;
}
diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/permission/DefaultAnonymousAccessPermissionCheckerTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/permission/DefaultAnonymousAccessPermissionCheckerTest.java
index 11f466ca39..eef2013fde 100644
--- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/permission/DefaultAnonymousAccessPermissionCheckerTest.java
+++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/permission/DefaultAnonymousAccessPermissionCheckerTest.java
@@ -16,10 +16,11 @@
*/
package org.apache.dubbo.qos.permission;
-import io.netty.channel.Channel;
import org.apache.dubbo.qos.api.CommandContext;
import org.apache.dubbo.qos.api.PermissionLevel;
import org.apache.dubbo.qos.api.QosConfiguration;
+
+import io.netty.channel.Channel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
@@ -79,5 +80,26 @@ class DefaultAnonymousAccessPermissionCheckerTest {
Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PUBLIC));
Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PROTECTED));
Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PRIVATE));
+
+ Mockito.when(qosConfiguration.getAcceptForeignIpWhitelistPredicate()).thenReturn(ip -> false);
+ Mockito.when(qosConfiguration.getAnonymousAllowCommands()).thenReturn("test1,test2");
+
+ Mockito.when(commandContext.getCommandName()).thenReturn("test1");
+ Assertions.assertTrue(checker.access(commandContext, PermissionLevel.NONE));
+ Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PUBLIC));
+ Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PROTECTED));
+ Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PRIVATE));
+
+ Mockito.when(commandContext.getCommandName()).thenReturn("test2");
+ Assertions.assertTrue(checker.access(commandContext, PermissionLevel.NONE));
+ Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PUBLIC));
+ Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PROTECTED));
+ Assertions.assertTrue(checker.access(commandContext, PermissionLevel.PRIVATE));
+
+ Mockito.when(commandContext.getCommandName()).thenReturn("test");
+ Assertions.assertTrue(checker.access(commandContext, PermissionLevel.NONE));
+ Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PUBLIC));
+ Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PROTECTED));
+ Assertions.assertFalse(checker.access(commandContext, PermissionLevel.PRIVATE));
}
}
From 742e035a8a1c3d7c13f8476b60ad12da7456851e Mon Sep 17 00:00:00 2001
From: namelessssssssssss
<100946116+namelessssssssssss@users.noreply.github.com>
Date: Wed, 19 Apr 2023 12:41:53 +0800
Subject: [PATCH 13/59] Add metrics provider/consumer uts (#12120)
* Provide uts in metrics-api
* Provide uts in metrics-default
* Provide uts in metrics-default
* Update pom.xml
* Update pom.xml
* Remove 'import *'
* Remove 'import *'
* Merge remote branch
* Add license
* Update DefaultDubboClientObservationConventionTest.java
* Merge remote branch
* Add ut for METRIC_QPS
* Add ut for Provider/Consumer Metrics
* Add ut for Provider/Consumer Metrics
* Add ut for Provider/Consumer Metrics
* Update pom.xml for test
* Add test for p95 & p99
* Add test for p95 & p99
* Update pom.xml
* Update import
* Remove unused todo
* Update test
* Remove unused import
* Remove unnecessary test unit
* Fix codestyle problems
* Fix codestyle problems
---
.../dubbo/common/utils/ReflectionUtils.java | 101 ++++++++++
...tDubboClientObservationConventionTest.java | 71 +++++++
...tDubboServerObservationConventionTest.java | 69 +++++++
.../utils/ObservationConventionUtils.java | 48 +++++
dubbo-metrics/dubbo-metrics-default/pom.xml | 1 -
.../metrics/DefaultMetricsServiceTest.java | 84 ++++++++
.../AggregateMetricsCollectorTest.java | 151 ++++++++++++++-
.../ConfigCenterMetricsCollectorTest.java | 7 -
.../sample/ThreadPoolMetricsSamplerTest.java | 180 ++++++++++++++++++
.../metrics/sampler/MethodMetricsTest.java | 132 +++++++++++++
.../collector/RegistryStatCompositeTest.java | 46 +++++
11 files changed, 875 insertions(+), 15 deletions(-)
create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectionUtils.java
create mode 100644 dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/DefaultDubboClientObservationConventionTest.java
create mode 100644 dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/DefaultDubboServerObservationConventionTest.java
create mode 100644 dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/utils/ObservationConventionUtils.java
create mode 100644 dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/DefaultMetricsServiceTest.java
create mode 100644 dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSamplerTest.java
create mode 100644 dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/sampler/MethodMetricsTest.java
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectionUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectionUtils.java
new file mode 100644
index 0000000000..b659dcb97b
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectionUtils.java
@@ -0,0 +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.common.utils;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.util.Arrays;
+
+/**
+ * A utility class that provides methods for accessing and manipulating private fields and methods of an object.
+ * This is useful for white-box testing, where the internal workings of a class need to be tested directly.
+ *
+ * Note: Usage of this class should be limited to testing purposes only, as it violates the encapsulation principle.
+ */
+public class ReflectionUtils {
+
+ private ReflectionUtils(){}
+
+ /**
+ * Retrieves the value of the specified field from the given object.
+ *
+ * @param source The object from which to retrieve the field value.
+ * @param fieldName The name of the field to retrieve.
+ * @return The value of the specified field in the given object.
+ * @throws RuntimeException If the specified field does not exist.
+ */
+ public static Object getField(Object source, String fieldName) {
+ try {
+ Field f = source.getClass().getDeclaredField(fieldName);
+ f.setAccessible(true);
+ return f.get(source);
+ } catch (Exception e) {
+ throw new ReflectionException(e);
+ }
+ }
+
+ /**
+ * Invokes the specified method on the given object with the provided parameters.
+ *
+ * @param source The object on which to invoke the method.
+ * @param methodName The name of the method to invoke.
+ * @param params The parameters to pass to the method.
+ * @return The result of invoking the specified method on the given object.
+ */
+ public static Object invoke(Object source, String methodName, Object... params) {
+ try {
+ Class>[] classes = Arrays.stream(params)
+ .map(param -> param != null ? param.getClass() : null)
+ .toArray(Class>[]::new);
+
+ for (Method method : source.getClass().getDeclaredMethods()) {
+ if (method.getName().equals(methodName) && matchParameters(method.getParameterTypes(), classes)) {
+ method.setAccessible(true);
+ return method.invoke(source, params);
+ }
+ }
+ throw new NoSuchMethodException("No method found with the specified name and parameter types");
+ } catch (Exception e) {
+ throw new ReflectionException(e);
+ }
+ }
+
+ private static boolean matchParameters(Class>[] methodParamTypes, Class>[] givenParamTypes) {
+ if (methodParamTypes.length != givenParamTypes.length) {
+ return false;
+ }
+
+ for (int i = 0; i < methodParamTypes.length; i++) {
+ if (givenParamTypes[i] == null) {
+ if (methodParamTypes[i].isPrimitive()) {
+ return false;
+ }
+ } else if (!methodParamTypes[i].isAssignableFrom(givenParamTypes[i])) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ public static class ReflectionException extends RuntimeException{
+ public ReflectionException(Throwable cause) {
+ super(cause);
+ }
+ }
+
+}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/DefaultDubboClientObservationConventionTest.java b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/DefaultDubboClientObservationConventionTest.java
new file mode 100644
index 0000000000..8b91e2531d
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/DefaultDubboClientObservationConventionTest.java
@@ -0,0 +1,71 @@
+/*
+ * 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.observation;
+
+import io.micrometer.common.KeyValues;
+import org.apache.dubbo.metrics.observation.utils.ObservationConventionUtils;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.RpcInvocation;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+
+public class DefaultDubboClientObservationConventionTest {
+
+ static DubboClientObservationConvention dubboClientObservationConvention = DefaultDubboClientObservationConvention.getInstance();
+
+ @Test
+ void testGetName() {
+ Assertions.assertEquals("rpc.client.duration", dubboClientObservationConvention.getName());
+ }
+
+ @Test
+ void testGetLowCardinalityKeyValues() throws NoSuchFieldException, IllegalAccessException {
+ RpcInvocation invocation = new RpcInvocation();
+ invocation.setMethodName("testMethod");
+ invocation.setAttachment("interface", "com.example.TestService");
+ invocation.setTargetServiceUniqueName("targetServiceName1");
+
+ Invoker> invoker = ObservationConventionUtils.getMockInvokerWithUrl();
+ invocation.setInvoker(invoker);
+
+ DubboClientContext context = new DubboClientContext(invoker, invocation);
+
+ KeyValues keyValues = dubboClientObservationConvention.getLowCardinalityKeyValues(context);
+
+ Assertions.assertEquals("testMethod", ObservationConventionUtils.getValueForKey(keyValues, "rpc.method"));
+ Assertions.assertEquals("targetServiceName1", ObservationConventionUtils.getValueForKey(keyValues, "rpc.service"));
+ Assertions.assertEquals("apache_dubbo", ObservationConventionUtils.getValueForKey(keyValues, "rpc.system"));
+ }
+
+ @Test
+ void testGetContextualName() {
+ RpcInvocation invocation = new RpcInvocation();
+ Invoker> invoker = ObservationConventionUtils.getMockInvokerWithUrl();
+ invocation.setMethodName("testMethod");
+ invocation.setServiceName("com.example.TestService");
+
+ DubboClientContext context = new DubboClientContext(invoker, invocation);
+
+ DefaultDubboClientObservationConvention convention = new DefaultDubboClientObservationConvention();
+
+ String contextualName = convention.getContextualName(context);
+ Assertions.assertEquals("com.example.TestService/testMethod", contextualName);
+ }
+
+
+}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/DefaultDubboServerObservationConventionTest.java b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/DefaultDubboServerObservationConventionTest.java
new file mode 100644
index 0000000000..faa49457e7
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/DefaultDubboServerObservationConventionTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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.observation;
+
+import io.micrometer.common.KeyValues;
+import org.apache.dubbo.metrics.observation.utils.ObservationConventionUtils;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.RpcInvocation;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+@SuppressWarnings("deprecation")
+public class DefaultDubboServerObservationConventionTest {
+
+ static DubboServerObservationConvention dubboServerObservationConvention = DefaultDubboServerObservationConvention.getInstance();
+
+ @Test
+ void testGetName() {
+ Assertions.assertEquals("rpc.server.duration", dubboServerObservationConvention.getName());
+ }
+
+ @Test
+ void testGetLowCardinalityKeyValues() throws NoSuchFieldException, IllegalAccessException {
+ RpcInvocation invocation = new RpcInvocation();
+ invocation.setMethodName("testMethod");
+ invocation.setAttachment("interface", "com.example.TestService");
+ invocation.setTargetServiceUniqueName("targetServiceName1");
+
+ Invoker> invoker = ObservationConventionUtils.getMockInvokerWithUrl();
+ invocation.setInvoker(invoker);
+
+ DubboServerContext context = new DubboServerContext(invoker, invocation);
+
+ KeyValues keyValues = dubboServerObservationConvention.getLowCardinalityKeyValues(context);
+
+ Assertions.assertEquals("testMethod", ObservationConventionUtils.getValueForKey(keyValues, "rpc.method"));
+ Assertions.assertEquals("targetServiceName1", ObservationConventionUtils.getValueForKey(keyValues, "rpc.service"));
+ Assertions.assertEquals("apache_dubbo", ObservationConventionUtils.getValueForKey(keyValues, "rpc.system"));
+ }
+
+ @Test
+ void testGetContextualName() {
+ RpcInvocation invocation = new RpcInvocation();
+ Invoker> invoker = ObservationConventionUtils.getMockInvokerWithUrl();
+ invocation.setMethodName("testMethod");
+ invocation.setServiceName("com.example.TestService");
+
+ DubboClientContext context = new DubboClientContext(invoker, invocation);
+
+ DefaultDubboClientObservationConvention convention = new DefaultDubboClientObservationConvention();
+
+ String contextualName = convention.getContextualName(context);
+ Assertions.assertEquals("com.example.TestService/testMethod", contextualName);
+ }
+}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/utils/ObservationConventionUtils.java b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/utils/ObservationConventionUtils.java
new file mode 100644
index 0000000000..4bf2abfa1e
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/observation/utils/ObservationConventionUtils.java
@@ -0,0 +1,48 @@
+/*
+ * 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.observation.utils;
+
+import io.micrometer.common.KeyValue;
+import io.micrometer.common.KeyValues;
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.rpc.Invoker;
+import org.mockito.Mockito;
+
+import java.lang.reflect.Field;
+
+public class ObservationConventionUtils {
+
+
+ public static Invoker> getMockInvokerWithUrl(){
+ URL url = URL.valueOf("dubbo://127.0.0.1:12345/com.example.TestService?anyhost=true&application=test&category=providers&dubbo=2.0.2&generic=false&interface=com.example.TestService&methods=testMethod&pid=26716&side=provider×tamp=1633863896653");
+ Invoker> invoker = Mockito.mock(Invoker.class);
+ Mockito.when(invoker.getUrl()).thenReturn(url);
+ return invoker;
+ }
+
+ public static String getValueForKey(KeyValues keyValues, Object key) throws NoSuchFieldException, IllegalAccessException {
+ Field f = KeyValues.class.getDeclaredField("keyValues");
+ f.setAccessible(true);
+ KeyValue[] kv = (KeyValue[]) f.get(keyValues);
+ for (KeyValue keyValue : kv) {
+ if (keyValue.getKey().equals(key)) {
+ return keyValue.getValue();
+ }
+ }
+ return null;
+ }
+}
diff --git a/dubbo-metrics/dubbo-metrics-default/pom.xml b/dubbo-metrics/dubbo-metrics-default/pom.xml
index 87ec6a3b9b..71be413c6a 100644
--- a/dubbo-metrics/dubbo-metrics-default/pom.xml
+++ b/dubbo-metrics/dubbo-metrics-default/pom.xml
@@ -46,6 +46,5 @@
micrometer-tracing-integration-test
test
-
diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/DefaultMetricsServiceTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/DefaultMetricsServiceTest.java
new file mode 100644
index 0000000000..5bb8c014b7
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/DefaultMetricsServiceTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.apache.dubbo.common.beans.factory.ScopeBeanFactory;
+import org.apache.dubbo.metrics.collector.MetricsCollector;
+import org.apache.dubbo.metrics.model.MetricsCategory;
+import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
+import org.apache.dubbo.metrics.service.DefaultMetricsService;
+import org.apache.dubbo.metrics.service.MetricsEntity;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static org.mockito.Mockito.when;
+
+@SuppressWarnings("rawtypes")
+public class DefaultMetricsServiceTest {
+
+ private MetricsCollector metricsCollector;
+
+ private DefaultMetricsService defaultMetricsService;
+
+
+ @BeforeEach
+ public void setUp() {
+ ApplicationModel applicationModel = Mockito.mock(ApplicationModel.class);
+ ScopeBeanFactory beanFactory = Mockito.mock(ScopeBeanFactory.class);
+ metricsCollector = Mockito.mock(MetricsCollector.class);
+
+ when(applicationModel.getBeanFactory()).thenReturn(beanFactory);
+ when(beanFactory.getBeansOfType(MetricsCollector.class)).thenReturn(Collections.singletonList(metricsCollector));
+
+ defaultMetricsService = new DefaultMetricsService(applicationModel);
+ }
+
+ @Test
+ public void testGetMetricsByCategories() {
+ MetricSample sample = new GaugeMetricSample<>(
+ "testMetric",
+ "testDescription",
+ null,
+ MetricsCategory.REQUESTS,
+ 42,
+ value -> 42.0
+ );
+ when(metricsCollector.collect()).thenReturn(Collections.singletonList(sample));
+ List categories = Collections.singletonList(MetricsCategory.REQUESTS);
+
+ Map> result = defaultMetricsService.getMetricsByCategories(categories);
+
+ Assertions.assertNotNull(result);
+ Assertions.assertEquals(1, result.size());
+ List entities = result.get(MetricsCategory.REQUESTS);
+ Assertions.assertNotNull(entities);
+ Assertions.assertEquals(1, entities.size());
+
+ MetricsEntity entity = entities.get(0);
+ Assertions.assertEquals("testMetric", entity.getName());
+ Assertions.assertEquals(42.0, entity.getValue());
+ Assertions.assertEquals(MetricsCategory.REQUESTS, entity.getCategory());
+ }
+}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
index f2275125b1..7bc56579b6 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
@@ -18,40 +18,53 @@
package org.apache.dubbo.metrics.collector;
import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.utils.ReflectionUtils;
import org.apache.dubbo.config.ApplicationConfig;
+
import org.apache.dubbo.config.MetricsConfig;
+
+import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.config.nested.AggregationConfig;
import org.apache.dubbo.metrics.TestMetricsInvoker;
+import org.apache.dubbo.metrics.aggregate.TimeWindowCounter;
import org.apache.dubbo.metrics.collector.sample.MethodMetricsSampler;
import org.apache.dubbo.metrics.event.MetricsEvent;
+import org.apache.dubbo.metrics.event.RTEvent;
+import org.apache.dubbo.metrics.model.MethodMetric;
import org.apache.dubbo.metrics.model.key.MetricsKey;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
-
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
+import java.util.Collections;
+import java.util.concurrent.ConcurrentHashMap;
+
import java.util.stream.Collectors;
-import static org.apache.dubbo.common.constants.CommonConstants.*;
-import static org.apache.dubbo.common.constants.MetricsConstants.TAG_GROUP_KEY;
-import static org.apache.dubbo.common.constants.MetricsConstants.TAG_INTERFACE_KEY;
-import static org.apache.dubbo.common.constants.MetricsConstants.TAG_METHOD_KEY;
-import static org.apache.dubbo.common.constants.MetricsConstants.TAG_VERSION_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
+import static org.apache.dubbo.common.constants.MetricsConstants.*;
+import static org.apache.dubbo.metrics.model.MetricsCategory.QPS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.spy;
class AggregateMetricsCollectorTest {
private ApplicationModel applicationModel;
private DefaultMetricsCollector defaultCollector;
-
private String interfaceName;
private String methodName;
private String group;
@@ -59,6 +72,37 @@ class AggregateMetricsCollectorTest {
private RpcInvocation invocation;
private String side;
+ public static MethodMetric getTestMethodMetric() {
+
+ MethodMetric methodMetric = new MethodMetric();
+ methodMetric.setApplicationName("TestApp");
+ methodMetric.setInterfaceName("TestInterface");
+ methodMetric.setMethodName("TestMethod");
+ methodMetric.setGroup("TestGroup");
+ methodMetric.setVersion("1.0.0");
+ methodMetric.setSide("PROVIDER");
+
+ return methodMetric;
+ }
+
+ public static AggregateMetricsCollector getTestCollector() {
+
+ ApplicationModel applicationModel = mock(ApplicationModel.class);
+ ConfigManager configManager = new ConfigManager(applicationModel);
+ MetricsConfig metricsConfig = spy(new MetricsConfig());
+
+ configManager.setMetrics(metricsConfig);
+
+ when(metricsConfig.getAggregation()).thenReturn(new AggregationConfig());
+ when(applicationModel.getApplicationConfigManager()).thenReturn(configManager);
+
+ ScopeBeanFactory beanFactory = mock(ScopeBeanFactory.class);
+ when(beanFactory.getBean(DefaultMetricsCollector.class)).thenReturn(new DefaultMetricsCollector());
+ when(applicationModel.getBeanFactory()).thenReturn(beanFactory);
+
+ return new AggregateMetricsCollector(applicationModel);
+ }
+
@BeforeEach
public void setup() {
ApplicationConfig config = new ApplicationConfig();
@@ -167,4 +211,97 @@ class AggregateMetricsCollectorTest {
Assertions.assertTrue(sampleMap.containsKey(MetricsKey.METRIC_RT_P99.getNameByType(side)));
Assertions.assertTrue(sampleMap.containsKey(MetricsKey.METRIC_RT_P95.getNameByType(side)));
}
+
+ @Test
+ public void testQPS() {
+ ApplicationModel applicationModel = mock(ApplicationModel.class);
+ ConfigManager configManager = mock(ConfigManager.class);
+ MetricsConfig metricsConfig = mock(MetricsConfig.class);
+ ScopeBeanFactory beanFactory = mock(ScopeBeanFactory.class);
+ AggregationConfig aggregationConfig = mock(AggregationConfig.class);
+
+ when(applicationModel.getApplicationConfigManager()).thenReturn(configManager);
+ when(applicationModel.getBeanFactory()).thenReturn(beanFactory);
+ when(beanFactory.getBean(DefaultMetricsCollector.class)).thenReturn(new DefaultMetricsCollector());
+ when(configManager.getMetrics()).thenReturn(Optional.of(metricsConfig));
+ when(metricsConfig.getAggregation()).thenReturn(aggregationConfig);
+ when(aggregationConfig.getEnabled()).thenReturn(Boolean.TRUE);
+
+ AggregateMetricsCollector collector = new AggregateMetricsCollector(applicationModel);
+
+ MethodMetric methodMetric = getTestMethodMetric();
+
+ TimeWindowCounter qpsCounter = new TimeWindowCounter(10, 120);
+
+ for (int i = 0; i < 10000; i++) {
+ qpsCounter.increment();
+ }
+
+ @SuppressWarnings("unchecked")
+ ConcurrentHashMap qps = (ConcurrentHashMap) ReflectionUtils.getField(collector, "qps");
+ qps.put(methodMetric, qpsCounter);
+
+ List collectedQPS = new ArrayList<>();
+ ReflectionUtils.invoke(collector, "collectQPS", collectedQPS);
+
+ Assertions.assertFalse(collectedQPS.isEmpty());
+ Assertions.assertEquals(1, collectedQPS.size());
+
+ MetricSample sample = collectedQPS.get(0);
+ Assertions.assertEquals(MetricsKey.METRIC_QPS.getNameByType("PROVIDER"), sample.getName());
+ Assertions.assertEquals(MetricsKey.METRIC_QPS.getDescription(), sample.getDescription());
+
+ Assertions.assertEquals(QPS, sample.getCategory());
+ Assertions.assertEquals(10000, ((TimeWindowCounter) ((GaugeMetricSample>) sample).getValue()).get());
+ }
+
+ @Test
+ void testP95AndP99() throws InterruptedException {
+ AggregateMetricsCollector collector = getTestCollector();
+ MethodMetric methodMetric = getTestMethodMetric();
+
+ List requestTimes = new ArrayList<>(10000);
+
+ for (int i = 0; i < 300; i++) {
+ requestTimes.add(1000 * Math.random());
+ }
+
+ Collections.sort(requestTimes);
+ double p95Index = 0.95 * (requestTimes.size() - 1);
+ double p99Index = 0.99 * (requestTimes.size() - 1);
+
+ double manualP95 = requestTimes.get((int) Math.round(p95Index));
+ double manualP99 = requestTimes.get((int) Math.round(p99Index));
+
+ for (Double requestTime : requestTimes) {
+ collector.onEvent(new RTEvent(applicationModel, methodMetric, requestTime.longValue()));
+ }
+ Thread.sleep(4000L);
+
+ List samples = collector.collect();
+
+ GaugeMetricSample> p95Sample = samples.stream()
+ .filter(sample -> sample.getName().endsWith("p95"))
+ .map(sample -> (GaugeMetricSample>) sample)
+ .findFirst()
+ .orElse(null);
+
+ GaugeMetricSample> p99Sample = samples.stream()
+ .filter(sample -> sample.getName().endsWith("p99"))
+ .map(sample -> (GaugeMetricSample>) sample)
+ .findFirst()
+ .orElse(null);
+
+ Assertions.assertNotNull(p95Sample);
+ Assertions.assertNotNull(p99Sample);
+
+ double p95 = p95Sample.applyAsDouble();
+ double p99 = p99Sample.applyAsDouble();
+
+ //An error of less than 5% is allowed
+ Assertions.assertTrue(Math.abs(1 - p95 / manualP95) < 0.05);
+ Assertions.assertTrue(Math.abs(1 - p99 / manualP99) < 0.05);
+ }
+
}
+
diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollectorTest.java
index e234279339..544dbe7f0c 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollectorTest.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollectorTest.java
@@ -20,7 +20,6 @@ package org.apache.dubbo.metrics.collector;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.config.ApplicationConfig;
-import org.apache.dubbo.metrics.model.ConfigCenterMetric;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.model.ApplicationModel;
@@ -32,14 +31,8 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
-import java.util.function.Supplier;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME;
-import static org.apache.dubbo.common.constants.MetricsConstants.TAG_GROUP_KEY;
-import static org.apache.dubbo.common.constants.MetricsConstants.TAG_INTERFACE_KEY;
-import static org.apache.dubbo.common.constants.MetricsConstants.TAG_METHOD_KEY;
-import static org.apache.dubbo.common.constants.MetricsConstants.TAG_VERSION_KEY;
-import static org.junit.jupiter.api.Assertions.*;
class ConfigCenterMetricsCollectorTest {
diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSamplerTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSamplerTest.java
new file mode 100644
index 0000000000..4b336d91f6
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/sample/ThreadPoolMetricsSamplerTest.java
@@ -0,0 +1,180 @@
+/*
+ * 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.collector.sample;
+
+import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
+import org.apache.dubbo.common.extension.ExtensionLoader;
+import org.apache.dubbo.common.store.DataStore;
+import org.apache.dubbo.common.threadpool.manager.FrameworkExecutorRepository;
+import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
+import org.apache.dubbo.metrics.model.ThreadPoolMetric;
+import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadPoolExecutor;
+
+import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SHARED_EXECUTOR_SERVICE_COMPONENT_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.EXECUTOR_SERVICE_COMPONENT_KEY;
+import static org.mockito.Mockito.when;
+
+@SuppressWarnings("all")
+public class ThreadPoolMetricsSamplerTest {
+
+ ThreadPoolMetricsSampler sampler;
+
+ @BeforeEach
+ void setUp() {
+ DefaultMetricsCollector collector = new DefaultMetricsCollector();
+ sampler = new ThreadPoolMetricsSampler(collector);
+ }
+
+ @Test
+ void testSample() {
+
+ ExecutorService executorService = java.util.concurrent.Executors.newFixedThreadPool(5);
+ ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executorService;
+ sampler.addExecutors("testPool", executorService);
+
+ List metricSamples = sampler.sample();
+
+ Assertions.assertEquals(6, metricSamples.size());
+
+ boolean coreSizeFound = false;
+ boolean maxSizeFound = false;
+ boolean activeSizeFound = false;
+ boolean threadCountFound = false;
+ boolean queueSizeFound = false;
+ boolean largestSizeFound = false;
+
+ for (MetricSample sample : metricSamples) {
+ ThreadPoolMetric threadPoolMetric = ((ThreadPoolMetric) ((GaugeMetricSample) sample).getValue());
+ switch (sample.getName()) {
+ case "dubbo.thread.pool.core.size":
+ coreSizeFound = true;
+ Assertions.assertEquals(5, threadPoolMetric.getCorePoolSize());
+ break;
+ case "dubbo.thread.pool.largest.size":
+ largestSizeFound = true;
+ Assertions.assertEquals(0, threadPoolMetric.getLargestPoolSize());
+ break;
+ case "dubbo.thread.pool.max.size":
+ maxSizeFound = true;
+ Assertions.assertEquals(5, threadPoolMetric.getMaximumPoolSize());
+ break;
+ case "dubbo.thread.pool.active.size":
+ activeSizeFound = true;
+ Assertions.assertEquals(0, threadPoolMetric.getActiveCount());
+ break;
+ case "dubbo.thread.pool.thread.count":
+ threadCountFound = true;
+ Assertions.assertEquals(0, threadPoolMetric.getPoolSize());
+ break;
+ case "dubbo.thread.pool.queue.size":
+ queueSizeFound = true;
+ Assertions.assertEquals(0, threadPoolMetric.getQueueSize());
+ break;
+ }
+ }
+
+ Assertions.assertTrue(coreSizeFound);
+ Assertions.assertTrue(maxSizeFound);
+ Assertions.assertTrue(activeSizeFound);
+ Assertions.assertTrue(threadCountFound);
+ Assertions.assertTrue(queueSizeFound);
+ Assertions.assertTrue(largestSizeFound);
+
+ executorService.shutdown();
+ }
+
+ private DefaultMetricsCollector collector;
+
+ private ThreadPoolMetricsSampler sampler2;
+
+ @Mock
+ private ApplicationModel applicationModel;
+
+ @Mock
+ ScopeBeanFactory scopeBeanFactory;
+
+ @Mock
+ private DataStore dataStore;
+
+ @Mock
+ private FrameworkExecutorRepository frameworkExecutorRepository;
+
+ @Mock
+ private ExtensionLoader extensionLoader;
+
+ @BeforeEach
+ public void setUp2() {
+ MockitoAnnotations.openMocks(this);
+
+ collector = new DefaultMetricsCollector();
+ sampler2 = new ThreadPoolMetricsSampler(collector);
+
+ when(scopeBeanFactory.getBean(FrameworkExecutorRepository.class)).thenReturn(new FrameworkExecutorRepository());
+
+ collector.collectApplication(applicationModel);
+ when(applicationModel.getBeanFactory()).thenReturn(scopeBeanFactory);
+ when(applicationModel.getExtensionLoader(DataStore.class)).thenReturn(extensionLoader);
+ when(extensionLoader.getDefaultExtension()).thenReturn(dataStore);
+ }
+
+ @Test
+ public void testRegistryDefaultSampleThreadPoolExecutor() throws NoSuchFieldException, IllegalAccessException {
+
+ Map serverExecutors = new HashMap<>();
+ Map clientExecutors = new HashMap<>();
+
+ ExecutorService serverExecutor = Executors.newFixedThreadPool(5);
+ ExecutorService clientExecutor = Executors.newFixedThreadPool(5);
+
+ serverExecutors.put("server1", serverExecutor);
+ clientExecutors.put("client1", clientExecutor);
+
+ when(dataStore.get(EXECUTOR_SERVICE_COMPONENT_KEY)).thenReturn(serverExecutors);
+ when(dataStore.get(CONSUMER_SHARED_EXECUTOR_SERVICE_COMPONENT_KEY)).thenReturn(clientExecutors);
+
+ when(frameworkExecutorRepository.getSharedExecutor()).thenReturn(Executors.newFixedThreadPool(5));
+
+ sampler2.registryDefaultSampleThreadPoolExecutor();
+
+ Field f = ThreadPoolMetricsSampler.class.getDeclaredField("sampleThreadPoolExecutor");
+ f.setAccessible(true);
+ Map executors = (Map) f.get(sampler2);
+
+ Assertions.assertEquals(3, executors.size());
+ Assertions.assertTrue(executors.containsKey("DubboServerHandler-server1"));
+ Assertions.assertTrue(executors.containsKey("DubboClientHandler-client1"));
+ Assertions.assertTrue(executors.containsKey("sharedExecutor"));
+
+ serverExecutor.shutdown();
+ clientExecutor.shutdown();
+ }
+}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/sampler/MethodMetricsTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/sampler/MethodMetricsTest.java
new file mode 100644
index 0000000000..9e4eff6941
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/sampler/MethodMetricsTest.java
@@ -0,0 +1,132 @@
+/*
+ * 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.sampler;
+
+import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
+import org.apache.dubbo.metrics.collector.sample.MethodMetricsSampler;
+import org.apache.dubbo.metrics.event.MetricsEvent;
+import org.apache.dubbo.metrics.model.key.MetricsKey;
+import org.apache.dubbo.metrics.model.sample.CounterMetricSample;
+import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
+import org.apache.dubbo.metrics.observation.MockInvocation;
+import org.apache.dubbo.rpc.Invocation;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.concurrent.atomic.AtomicLong;
+
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+public class MethodMetricsTest {
+
+ private DefaultMetricsCollector collector;
+ private MethodMetricsSampler sampler;
+
+ private Invocation invocation;
+
+ @BeforeEach
+ public void setUp() {
+ collector = new DefaultMetricsCollector();
+ sampler = new MethodMetricsSampler(collector);
+ invocation = spy(new MockInvocation());
+ when(invocation.getTargetServiceUniqueName()).thenReturn("TestService-1");
+ }
+
+ @Test
+ void testRequestsCount() {
+ final long requestTimes = 1000L;
+
+ //METRIC_REQUESTS
+ for (long i = 0; i < requestTimes; i++) {
+ sampler.inc(invocation, MetricsEvent.Type.TOTAL.getNameByType("provider"));
+ }
+
+ List samples = sampler.sample();
+
+ MetricSample requestsSample = samples.stream()
+ .filter(sample -> MetricsKey.METRIC_REQUESTS.getNameByType("provider").equals(sample.getName()))
+ .findFirst()
+ .orElse(null);
+
+ Assertions.assertNotNull(requestsSample, "METRIC_REQUESTS sample should not be null");
+ Assertions.assertEquals(MetricSample.Type.COUNTER, requestsSample.getType(), "METRIC_REQUESTS sample should have a COUNTER type");
+ Assertions.assertTrue(requestsSample instanceof CounterMetricSample);
+ Assertions.assertEquals(requestTimes, ((CounterMetricSample) requestsSample).getValue().longValue());
+ }
+
+ @Test
+ void testRequestsProcessing() {
+ final long requestTimes = 1000L;
+
+ //METRIC_REQUESTS
+ for (long i = 0; i < requestTimes; i++) {
+ sampler.inc(invocation, MetricsEvent.Type.PROCESSING.getNameByType("provider"));
+ }
+
+ List samples = sampler.sample();
+
+ MetricSample requestsSample = samples.stream()
+ .filter(sample -> MetricsKey.METRIC_REQUESTS_PROCESSING.getNameByType("provider").equals(sample.getName()))
+ .findFirst()
+ .orElse(null);
+
+ Assertions.assertNotNull(requestsSample, "METRIC_REQUESTS_PROCESSING sample should not be null");
+ Assertions.assertEquals(MetricSample.Type.GAUGE, requestsSample.getType(), "METRIC_REQUESTS_PROCESSING sample should have a GAUGE type");
+ Assertions.assertTrue(requestsSample instanceof GaugeMetricSample);
+ Assertions.assertEquals(requestTimes, ((AtomicLong) ((GaugeMetricSample) requestsSample).getValue()).get());
+
+ for (long i = 0; i < requestTimes; i++) {
+ sampler.dec(invocation, MetricsEvent.Type.PROCESSING.getNameByType("provider"));
+ }
+
+ samples = sampler.sample();
+
+ requestsSample = samples.stream()
+ .filter(sample -> MetricsKey.METRIC_REQUESTS_PROCESSING.getNameByType("provider").equals(sample.getName()))
+ .findFirst()
+ .orElse(null);
+
+ Assertions.assertEquals(0, ((AtomicLong) ((GaugeMetricSample) requestsSample).getValue()).get());
+ }
+
+ @Test
+ void testRequestSucceed() {
+ final long requestTimes = 1000L;
+
+ //METRIC_REQUESTS_SUCCEED
+ for (long i = 0; i < requestTimes; i++) {
+ sampler.inc(invocation, MetricsEvent.Type.SUCCEED.getNameByType("provider"));
+ }
+
+ List samples = sampler.sample();
+
+ MetricSample requestsSample = samples.stream()
+ .filter(sample -> MetricsKey.METRIC_REQUESTS_SUCCEED.getNameByType("provider").equals(sample.getName()))
+ .findFirst()
+ .orElse(null);
+
+ Assertions.assertNotNull(requestsSample, "METRIC_REQUESTS_SUCCEED sample should not be null");
+ Assertions.assertEquals(MetricSample.Type.COUNTER, requestsSample.getType(), "METRIC_REQUESTS_SUCCEED sample should have a COUNTER type");
+ Assertions.assertTrue(requestsSample instanceof CounterMetricSample);
+ Assertions.assertEquals(requestTimes, ((AtomicLong) ((CounterMetricSample) requestsSample).getValue()).get());
+ }
+
+}
diff --git a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java
index ba44ab6c15..b1a34e7e32 100644
--- a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java
+++ b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java
@@ -21,15 +21,22 @@ import org.apache.dubbo.metrics.data.ApplicationStatComposite;
import org.apache.dubbo.metrics.data.BaseStatComposite;
import org.apache.dubbo.metrics.data.RtStatComposite;
import org.apache.dubbo.metrics.data.ServiceStatComposite;
+import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.container.LongContainer;
import org.apache.dubbo.metrics.registry.RegistryMetricsConstants;
+import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
+import static org.apache.dubbo.metrics.model.key.MetricsKey.METRIC_RT_AVG;
+import static org.apache.dubbo.metrics.model.key.MetricsKey.METRIC_RT_MAX;
+import static org.apache.dubbo.metrics.model.key.MetricsKey.METRIC_RT_MIN;
import static org.apache.dubbo.metrics.model.key.MetricsKey.REGISTER_METRIC_REQUESTS;
import static org.apache.dubbo.metrics.registry.RegistryMetricsConstants.OP_TYPE_NOTIFY;
import static org.apache.dubbo.metrics.registry.RegistryMetricsConstants.OP_TYPE_REGISTER;
@@ -37,6 +44,7 @@ import static org.apache.dubbo.metrics.registry.RegistryMetricsConstants.OP_TYPE
import static org.apache.dubbo.metrics.registry.RegistryMetricsConstants.OP_TYPE_SUBSCRIBE;
import static org.apache.dubbo.metrics.registry.RegistryMetricsConstants.OP_TYPE_SUBSCRIBE_SERVICE;
+
public class RegistryStatCompositeTest {
private final String applicationName = "app1";
@@ -77,4 +85,42 @@ public class RegistryStatCompositeTest {
Optional> subContainer = statComposite.getRtStatComposite().getRtStats().stream().filter(longContainer -> longContainer.specifyType(OP_TYPE_NOTIFY.getType())).findFirst();
subContainer.ifPresent(v -> Assertions.assertEquals(10L, v.get(applicationName).longValue()));
}
+
+ @Test
+ @SuppressWarnings("rawtypes")
+ void testCalcServiceKeyRt() {
+ String applicationName = "TestApp";
+ String serviceKey = "TestService";
+ String registryOpType = OP_TYPE_REGISTER_SERVICE.getType();
+ Long responseTime1 = 100L;
+ Long responseTime2 = 200L;
+
+ statComposite.calcServiceKeyRt(applicationName, serviceKey, registryOpType, responseTime1);
+ statComposite.calcServiceKeyRt(applicationName, serviceKey, registryOpType, responseTime2);
+
+ List exportedRtMetrics = statComposite.export(MetricsCategory.RT);
+
+ GaugeMetricSample minSample = exportedRtMetrics.stream()
+ .filter(sample -> sample.getTags().containsValue(applicationName))
+ .filter(sample -> sample.getName().equals(METRIC_RT_MIN.getNameByType("register.service")))
+ .findFirst().orElse(null);
+ GaugeMetricSample maxSample = exportedRtMetrics.stream()
+ .filter(sample -> sample.getTags().containsValue(applicationName))
+ .filter(sample -> sample.getName().equals(METRIC_RT_MAX.getNameByType("register.service")))
+ .findFirst().orElse(null);
+ GaugeMetricSample avgSample = exportedRtMetrics.stream()
+ .filter(sample -> sample.getTags().containsValue(applicationName))
+ .filter(sample -> sample.getName().equals(METRIC_RT_AVG.getNameByType("register.service")))
+ .findFirst().orElse(null);
+
+ Assertions.assertNotNull(minSample);
+ Assertions.assertNotNull(maxSample);
+ Assertions.assertNotNull(avgSample);
+
+ Assertions.assertEquals(responseTime1, minSample.applyAsLong());
+ Assertions.assertEquals(responseTime2, maxSample.applyAsLong());
+ Assertions.assertEquals((responseTime1 + responseTime2) / 2, avgSample.applyAsLong());
+ }
+
+
}
From d6f9d8d38fb6ac804cb8375f4770079337279538 Mon Sep 17 00:00:00 2001
From: wxbty <38374721+wxbty@users.noreply.github.com>
Date: Wed, 19 Apr 2023 18:33:13 +0800
Subject: [PATCH 14/59] Configuration center metrics separate module (#12096)
* init config module
* remove old
* add pom
* add pom
* add in artifact
* use consts
* add comment
---------
Co-authored-by: x-shadow-man <1494445739@qq.com>
Co-authored-by: songxiaosheng
Co-authored-by: Albumen Kevin
---
.artifacts | 1 +
dubbo-config/dubbo-config-api/pom.xml | 7 ++
.../deploy/DefaultApplicationDeployer.java | 14 ++--
.../dubbo-configcenter-apollo/pom.xml | 5 ++
.../apollo/ApolloDynamicConfiguration.java | 26 +++----
.../dubbo-configcenter-nacos/pom.xml | 5 ++
.../nacos/NacosDynamicConfiguration.java | 36 +++++-----
.../dubbo-configcenter-zookeeper/pom.xml | 5 ++
.../zookeeper/ZookeeperDataListener.java | 10 +--
dubbo-distribution/dubbo-all/pom.xml | 8 +++
dubbo-distribution/dubbo-bom/pom.xml | 5 ++
.../dubbo/metrics/model/key/MetricsLevel.java | 2 +-
.../dubbo/metrics/model/key/TypeWrapper.java | 8 +--
.../dubbo-metrics-config-center/pom.xml | 34 +++++++++
.../config/ConfigCenterMetricsConstants.java | 26 +++++++
.../ConfigCenterMetricsCollector.java | 61 ++++++++--------
.../config/event/ConfigCenterEvent.java | 70 +++++++++++++++++++
.../event/ConfigCenterMetricsDispatcher.java | 58 +++++++++++++++
...e.dubbo.metrics.collector.MetricsCollector | 1 +
.../ConfigCenterMetricsCollectorTest.java | 15 ++--
dubbo-metrics/pom.xml | 1 +
dubbo-test/dubbo-dependencies-all/pom.xml | 5 ++
22 files changed, 322 insertions(+), 81 deletions(-)
create mode 100644 dubbo-metrics/dubbo-metrics-config-center/pom.xml
create mode 100644 dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/ConfigCenterMetricsConstants.java
rename dubbo-metrics/{dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics => dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config}/collector/ConfigCenterMetricsCollector.java (60%)
create mode 100644 dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/event/ConfigCenterEvent.java
create mode 100644 dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/event/ConfigCenterMetricsDispatcher.java
create mode 100644 dubbo-metrics/dubbo-metrics-config-center/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metrics.collector.MetricsCollector
rename dubbo-metrics/{dubbo-metrics-default => dubbo-metrics-config-center}/src/test/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollectorTest.java (86%)
diff --git a/.artifacts b/.artifacts
index d6d687629d..1e4e886012 100644
--- a/.artifacts
+++ b/.artifacts
@@ -60,6 +60,7 @@ dubbo-metrics-default
dubbo-metrics-metadata
dubbo-metrics-prometheus
dubbo-metrics-registry
+dubbo-metrics-config-center
dubbo-monitor
dubbo-monitor-api
dubbo-monitor-default
diff --git a/dubbo-config/dubbo-config-api/pom.xml b/dubbo-config/dubbo-config-api/pom.xml
index 0d0b432e19..3b58b9ed2b 100644
--- a/dubbo-config/dubbo-config-api/pom.xml
+++ b/dubbo-config/dubbo-config-api/pom.xml
@@ -66,6 +66,12 @@
${project.parent.version}
+
+ org.apache.dubbo
+ dubbo-metrics-config-center
+ ${project.parent.version}
+
+
org.apache.dubbo
dubbo-monitor-api
@@ -227,5 +233,6 @@
test
+
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
index bd30eb9b56..b7fd2e347e 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
@@ -20,6 +20,7 @@ import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.config.Environment;
import org.apache.dubbo.common.config.ReferenceCache;
+import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
import org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory;
import org.apache.dubbo.common.config.configcenter.wrapper.CompositeDynamicConfiguration;
@@ -50,8 +51,8 @@ import org.apache.dubbo.config.utils.CompositeReferenceCache;
import org.apache.dubbo.config.utils.ConfigValidationUtils;
import org.apache.dubbo.metadata.report.MetadataReportFactory;
import org.apache.dubbo.metadata.report.MetadataReportInstance;
-import org.apache.dubbo.metrics.collector.ConfigCenterMetricsCollector;
import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
+import org.apache.dubbo.metrics.config.event.ConfigCenterEvent;
import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.metrics.registry.event.RegistryEvent;
import org.apache.dubbo.metrics.report.MetricsReporter;
@@ -799,8 +800,6 @@ public class DefaultApplicationDeployer extends AbstractDeployer${apollo_mock_server_version}
test
+
+ org.apache.dubbo
+ dubbo-metrics-config-center
+ ${project.parent.version}
+
diff --git a/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java b/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java
index 1b33e85781..75b6973f1f 100644
--- a/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java
+++ b/dubbo-configcenter/dubbo-configcenter-apollo/src/main/java/org/apache/dubbo/configcenter/support/apollo/ApolloDynamicConfiguration.java
@@ -16,15 +16,6 @@
*/
package org.apache.dubbo.configcenter.support.apollo;
-import org.apache.dubbo.common.URL;
-import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
-import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
-import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
-import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
-import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
-import org.apache.dubbo.common.logger.LoggerFactory;
-import org.apache.dubbo.common.utils.StringUtils;
-
import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigChangeListener;
import com.ctrip.framework.apollo.ConfigFile;
@@ -33,7 +24,16 @@ import com.ctrip.framework.apollo.core.enums.ConfigFileFormat;
import com.ctrip.framework.apollo.enums.ConfigSourceType;
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange;
-import org.apache.dubbo.metrics.collector.ConfigCenterMetricsCollector;
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
+import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
+import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
+import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.metrics.config.event.ConfigCenterEvent;
+import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.Arrays;
@@ -52,6 +52,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.CONFIG_NAMESPACE
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_FAILED_CLOSE_CONNECT_APOLLO;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_FAILED_CONNECT_REGISTRY;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_NOT_EFFECT_EMPTY_RULE_APOLLO;
+import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;
/**
* Apollo implementation, https://github.com/ctripcorp/apollo
@@ -250,9 +251,8 @@ public class ApolloDynamicConfiguration implements DynamicConfiguration {
ConfigChangedEvent event = new ConfigChangedEvent(key, change.getNamespace(), change.getNewValue(), getChangeType(change));
listeners.forEach(listener -> listener.process(event));
- ConfigCenterMetricsCollector collector =
- applicationModel.getBeanFactory().getBean(ConfigCenterMetricsCollector.class);
- collector.increaseUpdated("apollo", applicationModel.getApplicationName(), event);
+ MetricsEventBus.publish(ConfigCenterEvent.toChangeEvent(applicationModel, event.getKey(), event.getGroup(),
+ ConfigCenterEvent.APOLLO_PROTOCOL, ConfigChangeType.ADDED.name(), SELF_INCREMENT_SIZE));
}
}
diff --git a/dubbo-configcenter/dubbo-configcenter-nacos/pom.xml b/dubbo-configcenter/dubbo-configcenter-nacos/pom.xml
index a6bc25f483..730c695b31 100644
--- a/dubbo-configcenter/dubbo-configcenter-nacos/pom.xml
+++ b/dubbo-configcenter/dubbo-configcenter-nacos/pom.xml
@@ -56,5 +56,10 @@
dubbo-metrics-prometheus
${project.parent.version}
+
+ org.apache.dubbo
+ dubbo-metrics-config-center
+ ${project.parent.version}
+
diff --git a/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java b/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java
index 9a8e42b2bb..7a851e45f0 100644
--- a/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java
+++ b/dubbo-configcenter/dubbo-configcenter-nacos/src/main/java/org/apache/dubbo/configcenter/support/nacos/NacosDynamicConfiguration.java
@@ -17,14 +17,11 @@
package org.apache.dubbo.configcenter.support.nacos;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.CopyOnWriteArraySet;
-import java.util.concurrent.Executor;
-
+import com.alibaba.nacos.api.NacosFactory;
+import com.alibaba.nacos.api.PropertyKeyConst;
+import com.alibaba.nacos.api.config.ConfigService;
+import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
+import com.alibaba.nacos.api.exception.NacosException;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
@@ -37,15 +34,18 @@ import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
import org.apache.dubbo.common.utils.MD5Utils;
import org.apache.dubbo.common.utils.StringUtils;
-
-import com.alibaba.nacos.api.NacosFactory;
-import com.alibaba.nacos.api.PropertyKeyConst;
-import com.alibaba.nacos.api.config.ConfigService;
-import com.alibaba.nacos.api.config.listener.AbstractSharedListener;
-import com.alibaba.nacos.api.exception.NacosException;
-import org.apache.dubbo.metrics.collector.ConfigCenterMetricsCollector;
+import org.apache.dubbo.metrics.config.event.ConfigCenterEvent;
+import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.rpc.model.ApplicationModel;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.CopyOnWriteArraySet;
+import java.util.concurrent.Executor;
+
import static com.alibaba.nacos.api.PropertyKeyConst.PASSWORD;
import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
import static com.alibaba.nacos.api.PropertyKeyConst.USERNAME;
@@ -55,6 +55,7 @@ import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_INT
import static org.apache.dubbo.common.constants.RemotingConstants.BACKUP_KEY;
import static org.apache.dubbo.common.utils.StringConstantFieldValuePredicate.of;
import static org.apache.dubbo.common.utils.StringUtils.HYPHEN_CHAR;
+import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;
/**
* The nacos implementation of {@link DynamicConfiguration}
@@ -345,9 +346,8 @@ public class NacosDynamicConfiguration implements DynamicConfiguration {
}
listeners.forEach(listener -> listener.process(event));
- ConfigCenterMetricsCollector collector =
- applicationModel.getBeanFactory().getOrRegisterBean(ConfigCenterMetricsCollector.class);
- collector.increaseUpdated("nacos", applicationModel.getApplicationName(), event);
+ MetricsEventBus.publish(ConfigCenterEvent.toChangeEvent(applicationModel, event.getKey(), event.getGroup(),
+ ConfigCenterEvent.NACOS_PROTOCOL, ConfigChangeType.ADDED.name(), SELF_INCREMENT_SIZE));
}
void addListener(ConfigurationListener configurationListener) {
diff --git a/dubbo-configcenter/dubbo-configcenter-zookeeper/pom.xml b/dubbo-configcenter/dubbo-configcenter-zookeeper/pom.xml
index 825fbbb289..c4fbdb3ac6 100644
--- a/dubbo-configcenter/dubbo-configcenter-zookeeper/pom.xml
+++ b/dubbo-configcenter/dubbo-configcenter-zookeeper/pom.xml
@@ -75,6 +75,11 @@
dubbo-metrics-prometheus
${project.parent.version}
+
+ org.apache.dubbo
+ dubbo-metrics-config-center
+ ${project.parent.version}
+
diff --git a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDataListener.java b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDataListener.java
index a21babe2a2..df22f5de0f 100644
--- a/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDataListener.java
+++ b/dubbo-configcenter/dubbo-configcenter-zookeeper/src/main/java/org/apache/dubbo/configcenter/support/zookeeper/ZookeeperDataListener.java
@@ -20,7 +20,8 @@ import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.common.config.configcenter.ConfigurationListener;
import org.apache.dubbo.common.utils.CollectionUtils;
-import org.apache.dubbo.metrics.collector.ConfigCenterMetricsCollector;
+import org.apache.dubbo.metrics.config.event.ConfigCenterEvent;
+import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.remoting.zookeeper.DataListener;
import org.apache.dubbo.remoting.zookeeper.EventType;
import org.apache.dubbo.rpc.model.ApplicationModel;
@@ -28,6 +29,8 @@ import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
+import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;
+
/**
* one path has multi configurationListeners
*/
@@ -77,9 +80,8 @@ public class ZookeeperDataListener implements DataListener {
listeners.forEach(listener -> listener.process(configChangeEvent));
}
- ConfigCenterMetricsCollector collector =
- applicationModel.getBeanFactory().getBean(ConfigCenterMetricsCollector.class);
- collector.increaseUpdated("zookeeper", applicationModel.getApplicationName(), configChangeEvent);
+ MetricsEventBus.publish(ConfigCenterEvent.toChangeEvent(applicationModel, configChangeEvent.getKey(), configChangeEvent.getGroup(),
+ ConfigCenterEvent.ZK_PROTOCOL, ConfigChangeType.ADDED.name(), SELF_INCREMENT_SIZE));
}
}
diff --git a/dubbo-distribution/dubbo-all/pom.xml b/dubbo-distribution/dubbo-all/pom.xml
index ade64fe84d..66c0103bf6 100644
--- a/dubbo-distribution/dubbo-all/pom.xml
+++ b/dubbo-distribution/dubbo-all/pom.xml
@@ -211,6 +211,13 @@
compile
true
+
+ org.apache.dubbo
+ dubbo-metrics-config-center
+ ${project.version}
+ compile
+ true
+
@@ -521,6 +528,7 @@
org.apache.dubbo:dubbo-metrics-default
org.apache.dubbo:dubbo-metrics-registry
org.apache.dubbo:dubbo-metrics-metadata
+ org.apache.dubbo:dubbo-metrics-config-center
org.apache.dubbo:dubbo-metrics-prometheus
org.apache.dubbo:dubbo-monitor-api
org.apache.dubbo:dubbo-monitor-default
diff --git a/dubbo-distribution/dubbo-bom/pom.xml b/dubbo-distribution/dubbo-bom/pom.xml
index 26aeeb9b0b..3678df9c02 100644
--- a/dubbo-distribution/dubbo-bom/pom.xml
+++ b/dubbo-distribution/dubbo-bom/pom.xml
@@ -250,6 +250,11 @@
dubbo-metrics-metadata
${project.version}
+
+ org.apache.dubbo
+ dubbo-metrics-config-center
+ ${project.version}
+
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsLevel.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsLevel.java
index aa86ad4946..9711f5ae7b 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsLevel.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsLevel.java
@@ -18,5 +18,5 @@
package org.apache.dubbo.metrics.model.key;
public enum MetricsLevel {
- APP,SERVICE
+ APP,SERVICE,CONFIG
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/TypeWrapper.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/TypeWrapper.java
index 812db8f8e6..a46faa6471 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/TypeWrapper.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/TypeWrapper.java
@@ -25,6 +25,10 @@ public class TypeWrapper {
private final MetricsKey finishType;
private final MetricsKey errorType;
+ public TypeWrapper(MetricsLevel level, MetricsKey postType) {
+ this(level, postType, null, null);
+ }
+
public TypeWrapper(MetricsLevel level, MetricsKey postType, MetricsKey finishType, MetricsKey errorType) {
this.level = level;
this.postType = postType;
@@ -36,10 +40,6 @@ public class TypeWrapper {
return level;
}
- public MetricsKey getErrorType() {
- return errorType;
- }
-
public boolean isAssignableFrom(Object type) {
Assert.notNull(type, "Type can not be null");
return type.equals(postType) || type.equals(finishType) || type.equals(errorType);
diff --git a/dubbo-metrics/dubbo-metrics-config-center/pom.xml b/dubbo-metrics/dubbo-metrics-config-center/pom.xml
new file mode 100644
index 0000000000..aa65f3aef0
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-config-center/pom.xml
@@ -0,0 +1,34 @@
+
+
+ 4.0.0
+
+ org.apache.dubbo
+ dubbo-metrics
+ ${revision}
+ ../pom.xml
+
+ dubbo-metrics-config-center
+ ${project.artifactId}
+
+
+
+ org.apache.dubbo
+ dubbo-metrics-api
+ ${project.parent.version}
+
+
+
diff --git a/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/ConfigCenterMetricsConstants.java b/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/ConfigCenterMetricsConstants.java
new file mode 100644
index 0000000000..8a7d22c762
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/ConfigCenterMetricsConstants.java
@@ -0,0 +1,26 @@
+/*
+ * 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.config;
+
+public interface ConfigCenterMetricsConstants {
+
+ String ATTACHMENT_KEY_CONFIG_FILE = "configFileKey";
+ String ATTACHMENT_KEY_CONFIG_GROUP = "configGroup";
+ String ATTACHMENT_KEY_CONFIG_PROTOCOL = "configProtocol";
+ String ATTACHMENT_KEY_CHANGE_TYPE = "configChangeType";
+
+}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollector.java b/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/collector/ConfigCenterMetricsCollector.java
similarity index 60%
rename from dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollector.java
rename to dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/collector/ConfigCenterMetricsCollector.java
index 5fa34e4d42..17b3098710 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollector.java
+++ b/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/collector/ConfigCenterMetricsCollector.java
@@ -15,10 +15,16 @@
* limitations under the License.
*/
-package org.apache.dubbo.metrics.collector;
+package org.apache.dubbo.metrics.config.collector;
-import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
-import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
+import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.config.context.ConfigManager;
+import org.apache.dubbo.metrics.collector.CombMetricsCollector;
+import org.apache.dubbo.metrics.collector.MetricsCollector;
+import org.apache.dubbo.metrics.config.event.ConfigCenterEvent;
+import org.apache.dubbo.metrics.config.event.ConfigCenterMetricsDispatcher;
+import org.apache.dubbo.metrics.event.MetricsEvent;
+import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.model.ConfigCenterMetric;
import org.apache.dubbo.metrics.model.key.MetricsKey;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
@@ -28,25 +34,28 @@ import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
-import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_METRICS_CONFIGCENTER_ENABLE;
import static org.apache.dubbo.metrics.model.MetricsCategory.CONFIGCENTER;
-public class ConfigCenterMetricsCollector implements MetricsCollector {
- private boolean collectEnabled = true;
+/**
+ * Config center implementation of {@link MetricsCollector}
+ */
+@Activate
+public class ConfigCenterMetricsCollector extends CombMetricsCollector {
+
+ private Boolean collectEnabled = null;
private final ApplicationModel applicationModel;
private final Map updatedMetrics = new ConcurrentHashMap<>();
public ConfigCenterMetricsCollector(ApplicationModel applicationModel) {
+ super(null);
this.applicationModel = applicationModel;
- // default is true, disable when config false
- if ("false".equals(System.getProperty(DUBBO_METRICS_CONFIGCENTER_ENABLE))) {
- collectEnabled = false;
- }
+ super.setEventMulticaster(new ConfigCenterMetricsDispatcher(this));
}
public void setCollectEnabled(Boolean collectEnabled) {
@@ -57,29 +66,21 @@ public class ConfigCenterMetricsCollector implements MetricsCollector {
@Override
public boolean isCollectEnabled() {
- return collectEnabled;
+ if (collectEnabled == null) {
+ ConfigManager configManager = applicationModel.getApplicationConfigManager();
+ configManager.getMetrics().ifPresent(metricsConfig -> setCollectEnabled(metricsConfig.getEnableMetadata()));
+ }
+ return Optional.ofNullable(collectEnabled).orElse(true);
}
- public void increase4Initialized(String key, String group, String protocol, String applicationName, int count) {
+ public void increase(String key, String group, String protocol, String changeTypeName, int size) {
if (!isCollectEnabled()) {
return;
}
- if (count <= 0) {
- return;
- }
- ConfigCenterMetric metric = new ConfigCenterMetric(applicationName, key, group, protocol, ConfigChangeType.ADDED.name());
- AtomicLong aLong = updatedMetrics.computeIfAbsent(metric, k -> new AtomicLong(0L));
- aLong.addAndGet(count);
+ ConfigCenterMetric metric = new ConfigCenterMetric(applicationModel.getApplicationName(), key, group, protocol, changeTypeName);
+ updatedMetrics.computeIfAbsent(metric, k -> new AtomicLong(0L)).addAndGet(size);
}
- public void increaseUpdated(String protocol, String applicationName, ConfigChangedEvent event) {
- if (!isCollectEnabled()) {
- return;
- }
- ConfigCenterMetric metric = new ConfigCenterMetric(applicationName, event.getKey(), event.getGroup(), protocol, event.getChangeType().name());
- AtomicLong count = updatedMetrics.computeIfAbsent(metric, k -> new AtomicLong(0L));
- count.incrementAndGet();
- }
@Override
public List collect() {
@@ -88,12 +89,14 @@ public class ConfigCenterMetricsCollector implements MetricsCollector {
if (!isCollectEnabled()) {
return list;
}
- collect(list);
+ updatedMetrics.forEach((k, v) -> list.add(new GaugeMetricSample<>(MetricsKey.CONFIGCENTER_METRIC_TOTAL, k.getTags(), CONFIGCENTER, v, AtomicLong::get)));
return list;
}
- private void collect(List list) {
- updatedMetrics.forEach((k, v) -> list.add(new GaugeMetricSample<>(MetricsKey.CONFIGCENTER_METRIC_TOTAL, k.getTags(), CONFIGCENTER, v, AtomicLong::get)));
+
+ @Override
+ public boolean isSupport(MetricsEvent event) {
+ return event instanceof ConfigCenterEvent;
}
}
diff --git a/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/event/ConfigCenterEvent.java b/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/event/ConfigCenterEvent.java
new file mode 100644
index 0000000000..c076a080a4
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/event/ConfigCenterEvent.java
@@ -0,0 +1,70 @@
+/*
+ * 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.config.event;
+
+import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
+import org.apache.dubbo.metrics.config.collector.ConfigCenterMetricsCollector;
+import org.apache.dubbo.metrics.event.TimeCounterEvent;
+import org.apache.dubbo.metrics.model.key.MetricsLevel;
+import org.apache.dubbo.metrics.model.key.TypeWrapper;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+
+import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SIZE;
+import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CHANGE_TYPE;
+import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_FILE;
+import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_GROUP;
+import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_PROTOCOL;
+import static org.apache.dubbo.metrics.model.key.MetricsKey.CONFIGCENTER_METRIC_TOTAL;
+
+/**
+ * Registry related events
+ * Triggered in three types of configuration centers (apollo, zk, nacos)
+ */
+public class ConfigCenterEvent extends TimeCounterEvent {
+
+
+ public static final String NACOS_PROTOCOL = "nacos";
+ public static final String APOLLO_PROTOCOL = "apollo";
+ public static final String ZK_PROTOCOL = "zookeeper";
+
+
+ public ConfigCenterEvent(ApplicationModel applicationModel, TypeWrapper typeWrapper) {
+ super(applicationModel);
+ super.typeWrapper = typeWrapper;
+ ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
+ ConfigCenterMetricsCollector collector;
+ if (!beanFactory.isDestroyed()) {
+ collector = beanFactory.getBean(ConfigCenterMetricsCollector.class);
+ super.setAvailable(collector != null && collector.isCollectEnabled());
+ }
+ }
+
+
+ public static ConfigCenterEvent toChangeEvent(ApplicationModel applicationModel, String key, String group, String protocol, String changeType, int count) {
+ ConfigCenterEvent configCenterEvent = new ConfigCenterEvent(applicationModel, new TypeWrapper(MetricsLevel.CONFIG, CONFIGCENTER_METRIC_TOTAL));
+ configCenterEvent.putAttachment(ATTACHMENT_KEY_CONFIG_FILE, key);
+ configCenterEvent.putAttachment(ATTACHMENT_KEY_CONFIG_GROUP, group);
+ configCenterEvent.putAttachment(ATTACHMENT_KEY_CONFIG_PROTOCOL, protocol);
+ configCenterEvent.putAttachment(ATTACHMENT_KEY_CHANGE_TYPE, changeType);
+ configCenterEvent.putAttachment(ATTACHMENT_KEY_SIZE, count);
+ return configCenterEvent;
+
+ }
+
+
+}
diff --git a/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/event/ConfigCenterMetricsDispatcher.java b/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/event/ConfigCenterMetricsDispatcher.java
new file mode 100644
index 0000000000..36d1523045
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/event/ConfigCenterMetricsDispatcher.java
@@ -0,0 +1,58 @@
+/*
+ * 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.config.event;
+
+import org.apache.dubbo.metrics.config.collector.ConfigCenterMetricsCollector;
+import org.apache.dubbo.metrics.event.MetricsEvent;
+import org.apache.dubbo.metrics.event.SimpleMetricsEventMulticaster;
+import org.apache.dubbo.metrics.event.TimeCounterEvent;
+import org.apache.dubbo.metrics.listener.AbstractMetricsListener;
+import org.apache.dubbo.metrics.model.key.MetricsKey;
+
+import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SIZE;
+import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CHANGE_TYPE;
+import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_FILE;
+import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_GROUP;
+import static org.apache.dubbo.metrics.config.ConfigCenterMetricsConstants.ATTACHMENT_KEY_CONFIG_PROTOCOL;
+
+
+public final class ConfigCenterMetricsDispatcher extends SimpleMetricsEventMulticaster {
+
+ public ConfigCenterMetricsDispatcher(ConfigCenterMetricsCollector collector) {
+
+ super.addListener(new AbstractMetricsListener(MetricsKey.CONFIGCENTER_METRIC_TOTAL) {
+ @Override
+ public boolean isSupport(MetricsEvent event) {
+ return event instanceof ConfigCenterEvent;
+ }
+
+ @Override
+ public void onEvent(TimeCounterEvent event) {
+ collector.increase(
+ event.getAttachmentValue(ATTACHMENT_KEY_CONFIG_FILE),
+ event.getAttachmentValue(ATTACHMENT_KEY_CONFIG_GROUP),
+ event.getAttachmentValue(ATTACHMENT_KEY_CONFIG_PROTOCOL),
+ event.getAttachmentValue(ATTACHMENT_KEY_CHANGE_TYPE),
+ event.getAttachmentValue(ATTACHMENT_KEY_SIZE)
+ );
+ }
+ });
+
+ }
+
+
+}
diff --git a/dubbo-metrics/dubbo-metrics-config-center/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metrics.collector.MetricsCollector b/dubbo-metrics/dubbo-metrics-config-center/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metrics.collector.MetricsCollector
new file mode 100644
index 0000000000..fcf8df6889
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-config-center/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metrics.collector.MetricsCollector
@@ -0,0 +1 @@
+config-collector=org.apache.dubbo.metrics.config.collector.ConfigCenterMetricsCollector
diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-config-center/src/test/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollectorTest.java
similarity index 86%
rename from dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollectorTest.java
rename to dubbo-metrics/dubbo-metrics-config-center/src/test/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollectorTest.java
index 544dbe7f0c..f20c047871 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollectorTest.java
+++ b/dubbo-metrics/dubbo-metrics-config-center/src/test/java/org/apache/dubbo/metrics/collector/ConfigCenterMetricsCollectorTest.java
@@ -20,6 +20,7 @@ package org.apache.dubbo.metrics.collector;
import org.apache.dubbo.common.config.configcenter.ConfigChangeType;
import org.apache.dubbo.common.config.configcenter.ConfigChangedEvent;
import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.metrics.config.collector.ConfigCenterMetricsCollector;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.model.ApplicationModel;
@@ -33,6 +34,7 @@ import java.util.List;
import java.util.Map;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME;
+import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;
class ConfigCenterMetricsCollectorTest {
@@ -59,8 +61,8 @@ class ConfigCenterMetricsCollectorTest {
ConfigCenterMetricsCollector collector = new ConfigCenterMetricsCollector(applicationModel);
collector.setCollectEnabled(true);
String applicationName = applicationModel.getApplicationName();
- collector.increase4Initialized("key", "group", "nacos", applicationName, 1);
- collector.increase4Initialized("key", "group", "nacos", applicationName, 1);
+ collector.increase("key", "group", "nacos", ConfigChangeType.ADDED.name(), 1);
+ collector.increase("key", "group", "nacos", ConfigChangeType.ADDED.name(), 1);
List samples = collector.collect();
for (MetricSample sample : samples) {
@@ -80,9 +82,12 @@ class ConfigCenterMetricsCollectorTest {
String applicationName = applicationModel.getApplicationName();
ConfigChangedEvent event = new ConfigChangedEvent("key", "group", null, ConfigChangeType.ADDED);
-
- collector.increaseUpdated("nacos", applicationName, event);
- collector.increaseUpdated("nacos", applicationName, event);
+
+ collector.increase(event.getKey(), event.getGroup(),
+ "apollo", ConfigChangeType.ADDED.name(), SELF_INCREMENT_SIZE);
+
+ collector.increase(event.getKey(), event.getGroup(),
+ "apollo", ConfigChangeType.ADDED.name(), SELF_INCREMENT_SIZE);
List samples = collector.collect();
for (MetricSample sample : samples) {
diff --git a/dubbo-metrics/pom.xml b/dubbo-metrics/pom.xml
index d5527ecf7c..04abd6e077 100644
--- a/dubbo-metrics/pom.xml
+++ b/dubbo-metrics/pom.xml
@@ -23,6 +23,7 @@
dubbo-metrics-registry
dubbo-metrics-metadata
dubbo-metrics-prometheus
+ dubbo-metrics-config-center
org.apache.dubbo
diff --git a/dubbo-test/dubbo-dependencies-all/pom.xml b/dubbo-test/dubbo-dependencies-all/pom.xml
index 3e43c7cf36..e0b320e9d3 100644
--- a/dubbo-test/dubbo-dependencies-all/pom.xml
+++ b/dubbo-test/dubbo-dependencies-all/pom.xml
@@ -157,6 +157,11 @@
org.apache.dubbo
dubbo-metrics-metadata
+
+
+ org.apache.dubbo
+ dubbo-metrics-config-center
+
org.apache.dubbo
dubbo-metrics-prometheus
From 817659bd2320810e1096785433a08bd7ef73a72c Mon Sep 17 00:00:00 2001
From: suncairong163 <105478245+suncairong163@users.noreply.github.com>
Date: Thu, 20 Apr 2023 15:12:51 +0800
Subject: [PATCH 15/59] Feature/fix no javax validation dependency&spring
controller support (#12085)
* Simplify rest client
* Fix protocol
* fix codec
* tmp disable test
* rest metadata resolver add interface judge
* add rest metadata resolve unit test & fix AbstractServiceRestMetadataResolver
* org.apache.dubbo.metadata.rest.ParamType null exclude & add default accept header
* RestProtocolTest
refer add context path
unit test
* some fix
* ADD TODO
* RESOLVE HTTP client java.net.SocketException: socket closed
* RESOLVE HTTP client java.net.SocketException: socket closed
* add spring mvc rest protocol unit test
* rest protocol http response code deal
* rest protocol http response message
* fix some review advice
* fix some review advice
* add rest metadata resolve unit test & fix AbstractServiceRestMetadataResolver
* org.apache.dubbo.metadata.rest.ParamType null exclude & add default accept header
* RestProtocolTest
refer add context path
unit test
* some fix
* fix some review advice
* add spring mvc rest protocol unit test
* rest protocol http response code deal
* rest protocol http response message
* URLConnectionRestClient getMessage
* remove unused import
* import fix
* code merge
* remove unused import
* code merge
* code merge
* some fix
* code merge
* code merge
* code merge
* code merge
* Rest http server
* rest protocol do export
* code merge
* rest protocol provider
* rest protocol provider
* rest protocol response
* rest protocol netty request
* Fix conflicts
* change restResult InputStream to bytes protect from fd leak
* merge code
* Fix import
* Fix import
* Fix uts
* Remove unused code
* Fix logger
* Update okhttp version
* Update okhttp version
* rest protocol add AnotherUserRestService service ut
* rest protocol add String & byteArray codec
* Fix version
* Fix uts
* rest protocol add XMLCodec
* stream release
* ServiceRestMetadata port change int to Integer
* service RestMetadata service map init
* xml codec change for xxe
* Fix import
* code style
* code style
* Fix shade
* change for rest client recreate & destroy bugs
* remove recreate double check for code merge
* rest client destroy check
* add todo
* consumer merge code
* rest response
* BodyProviderParamParser bytes
* merge upstream 3.2 to 3.2_consumer_proxy_invocation_handler
* merge upstream 3.2 to feature/3.2-rest_protocol_provider
* fix rest provider ut
* path compare
* path compare
* fix provider rest ut
* fix provider rest ut
* fix provider rest ut
* testJaxrsPathPattern ut
* JAXRSServiceRestMetadataResolverTest ut
* JAXRSServiceRestMetadataResolverTest ut
* add netty http server
* add netty http server response encode
* rest netty http server codec
* rest netty http server codec
* some fix netty http server
* add spring mvc ut for netty http server
* okhttp_version to 3.14.2 to resolve dependency conflict
* Fix license
* add rest protocol exception mapper
* add TODO ExceptionMapper should be static or instance
* add TODO OKHttpRestClient implements of version >=4.0
* fix licence format
* fix ut
* fix testPathMatcher
* url context path
* format context path from url
* fix illegal logger method invocations:
* remove org.jetbrains.annotations.NotNull
* add Newly created SPI interface to dubbo-all
* some format
* merge 3.2
* merge 3.2
* merge 3.2
* Revert "merge 3.2"
This reverts commit ad19804437f22d422578572b8f8fbc8b63b68b09.
* merge upstream 3.2
* change HttpHandler throws IOException
* deal with RPCInvocation build error
* request is present
* remove request facade unused code
* remove thirdpart web server code
* remove thirdpart web server code
* remove unused imports
* remove unused code
* rest protocol directly implement AbstractProtocol
* code style
* change method modifier
* form body java bean convert
* change header name to RestHeaderEnum
* attachment header
* http handler error info
* rest header distinguish from attachment
* some log & double path check
* remove RestMethodMetadata ServiceRestMetadata refer
* reconstruct http handler
* content-type judge
* add RestInvoker
* setTargetServiceUniqueName
* path mapper & http handler
* remove unused code
* netty thread pool
* RestHttpRequestDecoder @ChannelHandler.Sharable
* add rest client ut
* fix metadata resolver test
* fix metadata resolver test
* add some description to code
* add error test
* error message append
* add request address
* request address
* http netty server reconstruct
* http netty server TODO add SslServerTlsHandler
* replace all pair use
* pair fix
* add NumberUtils ut
* add hex number ut
* add no annotation primitive ut
* remove unsed code
* throw exception
* add ut
* rename constants & remove unused code
* add ut
* add ut
* add ut
* add ut
* remove ut
* add restClient ut
* add restClient ut
* add DataParseUtils ut
* add path matcher ut
* add request template ut
* add request template ut
* fix xml codec
* some fix
* Class to Class>
* media type judge fix and add codec ut
* remove print
* add header map param ut
* add header map param null ut
* MediaTypeUtil ut
* add param & header & multimap ut
* remove unused import
* fix error log format
* test header param
* test header number param
* add MediaType ut
* add MediaType & metadata resolve ut
* imports
* add netty request facade ut
* some format
* add ExceptionMapperTest
* add hasExceptionMapper pre judge
* fix exception mapper throws
* change static ExceptionMapper to instance
* resolve Java_Zulu_jdk/17.0.6-10/x64 param is not throwable , exception
mapper ut
* fix reflects util some detail
* remove unused code
* add null arg ut
* rest invoker response future complete Exceptionally
* fix http netty server executor
* set rpc context request & response
* add rest token attachment
* add http method ut
* remove unused code
* some fix
* remove keep-alive header
* remove unused code
* close channel
* add response connection:close head
* fix no content-type
* fix no content-type form
* spring @RequestMapping or @RestController
* add exception mapper type judge
* remove content-length of OKHttpRestClient
GET request
* remove unused import
* fix ConstraintViolationException not exist error
* fix handleConstraintViolationException
* AbstractServiceRestMetadataResolver support spring controller service
* service type null judge
* spring controller support
* add todo
* ConstraintViolationException dependency pre judge
---------
Co-authored-by: Albumen Kevin
Co-authored-by: suncr
---
.../AbstractServiceRestMetadataResolver.java | 22 ++++--
.../rest/api/SpringControllerService.java | 71 +++++++++++++++++++
...ingMvcServiceRestMetadataResolverTest.java | 2 +
.../rpc/protocol/rest/RpcExceptionMapper.java | 28 ++++----
.../rest/annotation/ParamParserManager.java | 4 ++
.../ConstraintViolationExceptionConvert.java | 53 ++++++++++++++
6 files changed, 159 insertions(+), 21 deletions(-)
create mode 100644 dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/api/SpringControllerService.java
create mode 100644 dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/util/ConstraintViolationExceptionConvert.java
diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java
index 206fd1fad7..615911e93c 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/AbstractServiceRestMetadataResolver.java
@@ -72,15 +72,17 @@ public abstract class AbstractServiceRestMetadataResolver implements ServiceRest
@Override
public final boolean supports(Class> serviceType, boolean consumer) {
- // for consumer
- if (consumer) {
- // it is possible serviceType is impl
- return supports0(serviceType);
+
+ if (serviceType == null) {
+ return false;
}
+ // for consumer
+ // it is possible serviceType is impl
// for provider
// for xml config bean && isServiceAnnotationPresent(serviceType)
- return isImplementedInterface(serviceType) && supports0(serviceType);
+ // isImplementedInterface(serviceType) SpringController
+ return supports0(serviceType);
}
protected final boolean isImplementedInterface(Class> serviceType) {
@@ -172,9 +174,15 @@ public abstract class AbstractServiceRestMetadataResolver implements ServiceRest
// exclude the public methods declared in java.lang.Object.class
List declaredServiceMethods = new ArrayList<>(getAllMethods(serviceInterfaceClass, excludedDeclaredClass(Object.class)));
+ // controller class
+ if (serviceType.equals(serviceInterfaceClass)) {
+ putServiceMethodToMap(serviceMethodsMap, declaredServiceMethods);
+ return unmodifiableMap(serviceMethodsMap);
+ }
+
// for interface , such as consumer interface
if (serviceType.isInterface()) {
- putInterfaceMethodToMap(serviceMethodsMap, declaredServiceMethods);
+ putServiceMethodToMap(serviceMethodsMap, declaredServiceMethods);
return unmodifiableMap(serviceMethodsMap);
}
@@ -198,7 +206,7 @@ public abstract class AbstractServiceRestMetadataResolver implements ServiceRest
return unmodifiableMap(serviceMethodsMap);
}
- private void putInterfaceMethodToMap(Map serviceMethodsMap, List declaredServiceMethods) {
+ private void putServiceMethodToMap(Map serviceMethodsMap, List declaredServiceMethods) {
declaredServiceMethods.stream().forEach(method -> {
// filter static private default
diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/api/SpringControllerService.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/api/SpringControllerService.java
new file mode 100644
index 0000000000..dfea020905
--- /dev/null
+++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/api/SpringControllerService.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.metadata.rest.api;
+
+import org.apache.dubbo.metadata.rest.User;
+import org.springframework.http.MediaType;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class SpringControllerService {
+ @RequestMapping(value = "/param", method = RequestMethod.GET, consumes = MediaType.TEXT_PLAIN_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
+ public String param(@RequestParam String param) {
+ return param;
+ }
+
+ @RequestMapping(value = "/header", method = RequestMethod.GET, consumes = MediaType.TEXT_PLAIN_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
+ public String header(@RequestHeader String header) {
+ return header;
+ }
+
+ @RequestMapping(value = "/body", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
+ public User body(@RequestBody User user) {
+ return user;
+ }
+
+ @RequestMapping(value = "/multiValue", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
+ public MultiValueMap multiValue(@RequestBody MultiValueMap map) {
+ return map;
+ }
+
+ @RequestMapping(value = "/pathVariable/{a}", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
+ public String pathVariable(@PathVariable String a) {
+ return a;
+ }
+
+ @RequestMapping(value = "/noAnnoParam", method = RequestMethod.POST, consumes = MediaType.TEXT_PLAIN_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
+ public String noAnnoParam(String a) {
+ return a;
+ }
+
+ @RequestMapping(value = "/noAnnoNumber", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.ALL_VALUE)
+ public int noAnnoNumber(Integer b) {
+ return b;
+ }
+
+ @RequestMapping(value = "/noAnnoPrimitive", method = RequestMethod.POST, consumes = MediaType.ALL_VALUE, produces = MediaType.ALL_VALUE)
+ public int noAnnoPrimitive(int c) {
+ return c;
+ }
+}
diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolverTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolverTest.java
index 40f123a952..a79c6e5061 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolverTest.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/springmvc/SpringMvcServiceRestMetadataResolverTest.java
@@ -25,6 +25,7 @@ import org.apache.dubbo.metadata.rest.RestMethodMetadata;
import org.apache.dubbo.metadata.rest.RestService;
import org.apache.dubbo.metadata.rest.ServiceRestMetadata;
import org.apache.dubbo.metadata.rest.StandardRestService;
+import org.apache.dubbo.metadata.rest.api.SpringControllerService;
import org.apache.dubbo.metadata.rest.api.SpringRestService;
import org.apache.dubbo.metadata.rest.api.SpringRestServiceImpl;
import org.apache.dubbo.rpc.model.ApplicationModel;
@@ -86,6 +87,7 @@ class SpringMvcServiceRestMetadataResolverTest {
void testResolves() {
testResolve(SpringRestService.class);
testResolve(SpringRestServiceImpl.class);
+ testResolve(SpringControllerService.class);
}
void testResolve(Class service) {
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java
index a50a2dda5a..fe07516ba4 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RpcExceptionMapper.java
@@ -16,30 +16,30 @@
*/
package org.apache.dubbo.rpc.protocol.rest;
+import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.protocol.rest.exception.mapper.ExceptionHandler;
+import org.apache.dubbo.rpc.protocol.rest.util.ConstraintViolationExceptionConvert;
-import javax.validation.ConstraintViolation;
-import javax.validation.ConstraintViolationException;
public class RpcExceptionMapper implements ExceptionHandler {
- protected Object handleConstraintViolationException(ConstraintViolationException cve) {
- ViolationReport report = new ViolationReport();
- for (ConstraintViolation> cv : cve.getConstraintViolations()) {
- report.addConstraintViolation(new RestConstraintViolation(
- cv.getPropertyPath().toString(),
- cv.getMessage(),
- cv.getInvalidValue() == null ? "null" : cv.getInvalidValue().toString()));
- }
- return report;
- }
@Override
public Object result(RpcException e) {
- if (e.getCause() instanceof ConstraintViolationException) {
- return handleConstraintViolationException((ConstraintViolationException) e.getCause());
+
+ // javax dependency judge
+ if (violationDependency()) {
+ // ConstraintViolationException judge
+ if (ConstraintViolationExceptionConvert.needConvert(e)) {
+ return ConstraintViolationExceptionConvert.handleConstraintViolationException(e);
+ }
}
+
return "Internal server error: " + e.getMessage();
}
+
+ private boolean violationDependency() {
+ return ClassUtils.isPresent("javax.validation.ConstraintViolationException", RpcExceptionMapper.class.getClassLoader());
+ }
}
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/ParamParserManager.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/ParamParserManager.java
index fb865bf417..5c0dbaa236 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/ParamParserManager.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/annotation/ParamParserManager.java
@@ -56,6 +56,7 @@ public class ParamParserManager {
paramParser.parse(parseContext, args.get(i));
}
}
+ // TODO add param require or default & body arg size pre judge
return parseContext.getArgs().toArray(new Object[0]);
}
@@ -86,5 +87,8 @@ public class ParamParserManager {
}
}
+ // TODO add param require or default
+
+
}
}
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/util/ConstraintViolationExceptionConvert.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/util/ConstraintViolationExceptionConvert.java
new file mode 100644
index 0000000000..df0e34f057
--- /dev/null
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/util/ConstraintViolationExceptionConvert.java
@@ -0,0 +1,53 @@
+/*
+ * 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.rpc.protocol.rest.util;
+
+import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.protocol.rest.RestConstraintViolation;
+import org.apache.dubbo.rpc.protocol.rest.ViolationReport;
+
+import javax.validation.ConstraintViolation;
+import javax.validation.ConstraintViolationException;
+
+public class ConstraintViolationExceptionConvert {
+
+
+
+ public static Object handleConstraintViolationException(RpcException rpcException) {
+ ConstraintViolationException cve = (ConstraintViolationException) rpcException.getCause();
+ ViolationReport report = new ViolationReport();
+ for (ConstraintViolation> cv : cve.getConstraintViolations()) {
+ report.addConstraintViolation(new RestConstraintViolation(
+ cv.getPropertyPath().toString(),
+ cv.getMessage(),
+ cv.getInvalidValue() == null ? "null" : cv.getInvalidValue().toString()));
+ }
+ return report;
+ }
+
+ public static boolean needConvert(RpcException e) {
+ return isConstraintViolationException(e);
+ }
+
+ private static boolean isConstraintViolationException(RpcException e) {
+ try {
+ return e.getCause() instanceof ConstraintViolationException;
+ } catch (Throwable throwable) {
+ return false;
+ }
+ }
+}
From 2d46677d9b4138d9746ffeffccde06e9bbae1c67 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Thu, 20 Apr 2023 16:08:53 +0800
Subject: [PATCH 16/59] Bump spring-security-bom from 5.8.2 to 5.8.3 (#12116)
Bumps [spring-security-bom](https://github.com/spring-projects/spring-security) from 5.8.2 to 5.8.3.
- [Release notes](https://github.com/spring-projects/spring-security/releases)
- [Changelog](https://github.com/spring-projects/spring-security/blob/main/RELEASE.adoc)
- [Commits](https://github.com/spring-projects/spring-security/compare/5.8.2...5.8.3)
---
updated-dependencies:
- dependency-name: org.springframework.security:spring-security-bom
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
dubbo-dependencies-bom/pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml
index f065d8b057..ad66054085 100644
--- a/dubbo-dependencies-bom/pom.xml
+++ b/dubbo-dependencies-bom/pom.xml
@@ -92,7 +92,7 @@
5.3.25
- 5.8.2
+ 5.8.3
3.29.2-GA
1.14.4
3.2.10.Final
From 1729d5b69e8ce0fce48a39f1566c8618e6a0001d Mon Sep 17 00:00:00 2001
From: earthchen
Date: Thu, 20 Apr 2023 16:24:54 +0800
Subject: [PATCH 17/59] fix npe (#12146)
---
.../apache/dubbo/rpc/protocol/tri/stream/StreamUtils.java | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/StreamUtils.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/StreamUtils.java
index 68807a45ec..07def88303 100644
--- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/StreamUtils.java
+++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/stream/StreamUtils.java
@@ -100,10 +100,13 @@ public class StreamUtils {
if (TripleHeaderEnum.containsExcludeAttachments(key)) {
continue;
}
+ final Object v = entry.getValue();
+ if (v == null) {
+ continue;
+ }
if (needConvertHeaderKey && !key.equals(entry.getKey())) {
needConvertKey.put(key, entry.getKey());
}
- final Object v = entry.getValue();
convertSingleAttachment(headers, key, v);
}
if (!needConvertKey.isEmpty()) {
From 0678a09e67126f9cb21161dceec4ff99b98ae32c Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Thu, 20 Apr 2023 16:30:20 +0800
Subject: [PATCH 18/59] Bump micrometer-tracing-bom from 1.0.3 to 1.0.4
(#12061)
Bumps [micrometer-tracing-bom](https://github.com/micrometer-metrics/tracing) from 1.0.3 to 1.0.4.
- [Release notes](https://github.com/micrometer-metrics/tracing/releases)
- [Commits](https://github.com/micrometer-metrics/tracing/compare/v1.0.3...v1.0.4)
---
updated-dependencies:
- dependency-name: io.micrometer:micrometer-tracing-bom
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
dubbo-dependencies-bom/pom.xml | 2 +-
.../dubbo-spring-boot-observability-starter/pom.xml | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml
index ad66054085..2223e2cce8 100644
--- a/dubbo-dependencies-bom/pom.xml
+++ b/dubbo-dependencies-bom/pom.xml
@@ -135,7 +135,7 @@
0.1.35
1.10.6
- 1.0.3
+ 1.0.4
3.3
0.16.0
1.0.4
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/pom.xml b/dubbo-spring-boot/dubbo-spring-boot-observability-starter/pom.xml
index 4363cecebb..b4f34c65eb 100644
--- a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/pom.xml
+++ b/dubbo-spring-boot/dubbo-spring-boot-observability-starter/pom.xml
@@ -37,7 +37,7 @@
1.10.6
- 1.0.3
+ 1.0.4
1.25.0
2.16.3
0.16.0
From b7471424d9b72ffcb80583322a8992845a0a3b9b Mon Sep 17 00:00:00 2001
From: MartinDai
Date: Thu, 20 Apr 2023 16:50:57 +0800
Subject: [PATCH 19/59] fix comments (#12143)
Co-authored-by: daming
---
.../apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
index ce3a187e70..1d70dfb839 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
@@ -75,7 +75,7 @@ public class PathAndInvokerMapper {
return pathToServiceMapNoPathVariable.get(pathMather);
}
- // second search from pathToServiceMapNoPathVariable
+ // second search from pathToServiceMapContainPathVariable
if (pathToServiceMapContainPathVariable.containsKey(pathMather)) {
return pathToServiceMapContainPathVariable.get(pathMather);
}
From 3adfbecf2562721981cbcc922611453e23018a73 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Thu, 20 Apr 2023 17:49:19 +0800
Subject: [PATCH 20/59] Bump nacos-client from 2.1.2 to 2.2.2 (#12069)
* Bump nacos-client from 2.1.2 to 2.2.2
Bumps [nacos-client](https://github.com/alibaba/nacos) from 2.1.2 to 2.2.2.
- [Release notes](https://github.com/alibaba/nacos/releases)
- [Changelog](https://github.com/alibaba/nacos/blob/develop/CHANGELOG.md)
- [Commits](https://github.com/alibaba/nacos/compare/2.1.2...2.2.2)
---
updated-dependencies:
- dependency-name: com.alibaba.nacos:nacos-client
dependency-type: direct:development
update-type: version-update:semver-minor
...
Signed-off-by: dependabot[bot]
* Fix compile
---------
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Albumen Kevin
---
dubbo-dependencies-bom/pom.xml | 2 +-
.../org/apache/dubbo/registry/nacos/MockNamingService.java | 5 +++++
2 files changed, 6 insertions(+), 1 deletion(-)
diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml
index 2223e2cce8..aa0c8841a9 100644
--- a/dubbo-dependencies-bom/pom.xml
+++ b/dubbo-dependencies-bom/pom.xml
@@ -148,7 +148,7 @@
1.9.13
8.5.87
0.7.5
- 2.1.2
+ 2.2.2
1.54.1
0.8.1
1.2.2
diff --git a/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/MockNamingService.java b/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/MockNamingService.java
index b7c62f9c11..e611bae357 100644
--- a/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/MockNamingService.java
+++ b/dubbo-registry/dubbo-registry-nacos/src/test/java/org/apache/dubbo/registry/nacos/MockNamingService.java
@@ -92,6 +92,11 @@ public class MockNamingService implements NamingService {
}
+ @Override
+ public void batchDeregisterInstance(String s, String s1, List list) throws NacosException {
+
+ }
+
@Override
public List getAllInstances(String serviceName) {
return null;
From f7c90572ecb58fd1e28569ef44e06cab26c807ca Mon Sep 17 00:00:00 2001
From: suncairong163 <105478245+suncairong163@users.noreply.github.com>
Date: Fri, 21 Apr 2023 10:39:00 +0800
Subject: [PATCH 21/59] fix 405 && differnt httpmethod path doubleCheck
(#12152)
---
.../dubbo/metadata/rest/PathMatcher.java | 27 ++++++++++-
.../dubbo/metadata/rest/RequestMetadata.java | 4 ++
.../metadata/rest/ServiceRestMetadata.java | 2 +-
.../metadata/rest/api/JaxrsUsingService.java | 48 +++++++++++++++++++
.../rest/jaxrs/JaxrsRestDoubleCheckTest.java | 28 +++++++++++
.../protocol/rest/PathAndInvokerMapper.java | 4 +-
.../protocol/rest/RestRPCInvocationUtil.java | 3 +-
.../rest/handler/NettyHttpHandler.java | 16 +++++--
.../protocol/rest/JaxrsRestProtocolTest.java | 38 ++++++++++++++-
.../rest/rest/RestDemoForTestException.java | 5 ++
.../protocol/rest/rest/RestDemoService.java | 4 ++
.../rest/rest/RestDemoServiceImpl.java | 5 ++
12 files changed, 173 insertions(+), 11 deletions(-)
create mode 100644 dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/api/JaxrsUsingService.java
diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/PathMatcher.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/PathMatcher.java
index 12d0644ac8..f4d57b1c30 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/PathMatcher.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/PathMatcher.java
@@ -31,6 +31,9 @@ public class PathMatcher {
private String[] pathSplits;
private boolean hasPathVariable;
private String contextPath;
+ private String httpMethod;
+ // for provider http method compare
+ private boolean needCompareMethod = true;
public PathMatcher(String path) {
@@ -45,6 +48,10 @@ public class PathMatcher {
this.port = (port == null || port == -1 || port == 0) ? null : port;
}
+ public PathMatcher(String path, String version, String group, Integer port, String httpMethod) {
+ this(path, version, group, port);
+ setHttpMethod(httpMethod);
+ }
private void dealPathVariable(String path) {
this.pathSplits = path.split(SEPARATOR);
@@ -88,8 +95,8 @@ public class PathMatcher {
}
- public static PathMatcher getInvokeCreatePathMatcher(String path, String version, String group, Integer port) {
- return new PathMatcher(path, version, group, port);
+ public static PathMatcher getInvokeCreatePathMatcher(String path, String version, String group, Integer port, String method) {
+ return new PathMatcher(path, version, group, port, method).noNeedHttpMethodCompare();
}
public boolean hasPathVariable() {
@@ -100,6 +107,20 @@ public class PathMatcher {
return port;
}
+ public String getHttpMethod() {
+ return httpMethod;
+ }
+
+ public PathMatcher setHttpMethod(String httpMethod) {
+ this.httpMethod = httpMethod;
+ return this;
+ }
+
+ private PathMatcher noNeedHttpMethodCompare() {
+ this.needCompareMethod = false;
+ return this;
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) return true;
@@ -107,6 +128,7 @@ public class PathMatcher {
PathMatcher that = (PathMatcher) o;
return pathEqual(that)
&& Objects.equals(version, that.version)
+ && (this.needCompareMethod ? Objects.equals(httpMethod, that.httpMethod) : true)
&& Objects.equals(group, that.group) && Objects.equals(port, that.port);
}
@@ -200,6 +222,7 @@ public class PathMatcher {
", port=" + port +
", hasPathVariable=" + hasPathVariable +
", contextPath='" + contextPath + '\'' +
+ ", httpMethod='" + httpMethod + '\'' +
'}';
}
}
diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/RequestMetadata.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/RequestMetadata.java
index 66ff17f5ad..e7f825d26a 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/RequestMetadata.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/RequestMetadata.java
@@ -206,6 +206,10 @@ public class RequestMetadata implements Serializable {
setPath(contextPathFromUrl + path);
}
+ public boolean methodAllowed(String method) {
+ return method != null && method.equals(this.method);
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
diff --git a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/ServiceRestMetadata.java b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/ServiceRestMetadata.java
index 55970c7fe7..14f8bdcbb9 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/ServiceRestMetadata.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/main/java/org/apache/dubbo/metadata/rest/ServiceRestMetadata.java
@@ -110,7 +110,7 @@ public class ServiceRestMetadata implements Serializable {
public void addRestMethodMetadata(RestMethodMetadata restMethodMetadata) {
PathMatcher pathMather = new PathMatcher(restMethodMetadata.getRequest().getPath(),
- this.getVersion(), this.getGroup(), this.getPort());
+ this.getVersion(), this.getGroup(), this.getPort(),restMethodMetadata.getRequest().getMethod());
addPathToServiceMap(pathMather, restMethodMetadata);
addMethodToServiceMap(restMethodMetadata);
getMeta().add(restMethodMetadata);
diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/api/JaxrsUsingService.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/api/JaxrsUsingService.java
new file mode 100644
index 0000000000..7490c75341
--- /dev/null
+++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/api/JaxrsUsingService.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.metadata.rest.api;
+
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+@Path("usingService")
+@Consumes({MediaType.APPLICATION_JSON})
+@Produces({MediaType.APPLICATION_JSON})
+public interface JaxrsUsingService {
+
+ @GET
+ Response getUsers();
+
+ @POST
+ Response createUser(Object user);
+
+ @GET
+ @Path("{uid}")
+ Response getUserByUid(@PathParam("uid") String uid);
+
+ @DELETE
+ @Path("{uid}")
+ Response deleteUserByUid(@PathParam("uid") String uid);
+}
diff --git a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/jaxrs/JaxrsRestDoubleCheckTest.java b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/jaxrs/JaxrsRestDoubleCheckTest.java
index 97ce07d5ec..3e8f129f20 100644
--- a/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/jaxrs/JaxrsRestDoubleCheckTest.java
+++ b/dubbo-metadata/dubbo-metadata-api/src/test/java/org/apache/dubbo/metadata/rest/jaxrs/JaxrsRestDoubleCheckTest.java
@@ -16,13 +16,18 @@
*/
package org.apache.dubbo.metadata.rest.jaxrs;
+import org.apache.dubbo.metadata.rest.PathMatcher;
+import org.apache.dubbo.metadata.rest.RestMethodMetadata;
import org.apache.dubbo.metadata.rest.ServiceRestMetadata;
import org.apache.dubbo.metadata.rest.api.JaxrsRestDoubleCheckContainsPathVariableService;
import org.apache.dubbo.metadata.rest.api.JaxrsRestDoubleCheckService;
+import org.apache.dubbo.metadata.rest.api.JaxrsUsingService;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import java.util.Map;
+
public class JaxrsRestDoubleCheckTest {
private JAXRSServiceRestMetadataResolver instance = new JAXRSServiceRestMetadataResolver(ApplicationModel.defaultModel());
@@ -45,4 +50,27 @@ public class JaxrsRestDoubleCheckTest {
}
+ @Test
+ void testSameHttpMethodException() {
+
+ Assertions.assertDoesNotThrow(() -> {
+ ServiceRestMetadata resolve = new ServiceRestMetadata();
+ resolve.setServiceInterface(JaxrsUsingService.class.getName());
+ instance.resolve(JaxrsUsingService.class, resolve);
+ });
+
+ ServiceRestMetadata resolve = new ServiceRestMetadata();
+ resolve.setServiceInterface(JaxrsUsingService.class.getName());
+ instance.resolve(JaxrsUsingService.class, resolve);
+
+ Map pathContainPathVariableToServiceMap = resolve.getPathContainPathVariableToServiceMap();
+
+
+ RestMethodMetadata restMethodMetadata = pathContainPathVariableToServiceMap.get(PathMatcher.getInvokeCreatePathMatcher("/usingService/aaa", null, null, null, "TEST"));
+
+ Assertions.assertNotNull(restMethodMetadata);
+
+
+ }
+
}
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
index 1d70dfb839..1d7aa3de05 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/PathAndInvokerMapper.java
@@ -65,10 +65,10 @@ public class PathAndInvokerMapper {
* @param port
* @return
*/
- public InvokerAndRestMethodMetadataPair getRestMethodMetadata(String path, String version, String group, Integer port) {
+ public InvokerAndRestMethodMetadataPair getRestMethodMetadata(String path, String version, String group, Integer port,String method) {
- PathMatcher pathMather = PathMatcher.getInvokeCreatePathMatcher(path, version, group, port);
+ PathMatcher pathMather = PathMatcher.getInvokeCreatePathMatcher(path, version, group, port,method);
// first search from pathToServiceMapNoPathVariable
if (pathToServiceMapNoPathVariable.containsKey(pathMather)) {
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestRPCInvocationUtil.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestRPCInvocationUtil.java
index 8388e1fe88..1d0221a587 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestRPCInvocationUtil.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/RestRPCInvocationUtil.java
@@ -134,8 +134,9 @@ public class RestRPCInvocationUtil {
String path = request.getPath();
String version = request.getHeader(RestHeaderEnum.VERSION.getHeader());
String group = request.getHeader(RestHeaderEnum.GROUP.getHeader());
+ String method = request.getMethod();
- return pathAndInvokerMapper.getRestMethodMetadata(path, version, group, null);
+ return pathAndInvokerMapper.getRestMethodMetadata(path, version, group, null, method);
}
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/handler/NettyHttpHandler.java b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/handler/NettyHttpHandler.java
index a3243685ab..7edf7c4ddf 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/handler/NettyHttpHandler.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/main/java/org/apache/dubbo/rpc/protocol/rest/handler/NettyHttpHandler.java
@@ -104,6 +104,16 @@ public class NettyHttpHandler implements HttpHandler returnType) {
+ private MediaType getAcceptMediaType(RequestFacade request, Class> returnType) {
String accept = request.getHeader(RestHeaderEnum.ACCEPT.getHeader());
MediaType mediaType = MediaTypeUtil.convertMediaType(returnType, accept);
return mediaType;
@@ -176,7 +186,7 @@ public class NettyHttpHandler implements HttpHandler returnType) {
try {
// media type judge
- getAcceptMediaType(requestFacade,returnType);
+ getAcceptMediaType(requestFacade, returnType);
} catch (UnSupportContentTypeException e) {
// return type judge
MediaType mediaType = HttpMessageCodecManager.typeSupport(returnType);
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java
index deca0a6937..c227ba68f2 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/JaxrsRestProtocolTest.java
@@ -46,8 +46,9 @@ import org.apache.dubbo.rpc.protocol.rest.rest.AnotherUserRestService;
import org.apache.dubbo.rpc.protocol.rest.rest.AnotherUserRestServiceImpl;
import org.apache.dubbo.rpc.protocol.rest.rest.HttpMethodService;
import org.apache.dubbo.rpc.protocol.rest.rest.HttpMethodServiceImpl;
-
import org.apache.dubbo.rpc.protocol.rest.rest.RestDemoForTestException;
+import org.apache.dubbo.rpc.protocol.rest.rest.RestDemoService;
+import org.apache.dubbo.rpc.protocol.rest.rest.RestDemoServiceImpl;
import org.hamcrest.CoreMatchers;
import org.jboss.resteasy.specimpl.MultivaluedMapImpl;
import org.junit.jupiter.api.AfterEach;
@@ -60,7 +61,6 @@ import java.util.HashMap;
import java.util.Map;
import static org.apache.dubbo.remoting.Constants.SERVER_KEY;
-import static org.apache.dubbo.rpc.protocol.rest.Constants.EXCEPTION_MAPPER_KEY;
import static org.apache.dubbo.rpc.protocol.rest.Constants.EXTENSION_KEY;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
@@ -561,6 +561,40 @@ class JaxrsRestProtocolTest {
}
}
+ @Test
+ void test405() {
+ int availablePort = NetUtils.getAvailablePort();
+ URL url = URL.valueOf("rest://127.0.0.1:" + availablePort
+ + "/?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.rest.RestDemoService&"
+ );
+
+ RestDemoServiceImpl server = new RestDemoServiceImpl();
+
+ url = this.registerProvider(url, server, RestDemoService.class);
+
+ Exporter exporter = protocol.export(proxy.getInvoker(server, RestDemoService.class, url));
+
+ URL consumer = URL.valueOf("rest://127.0.0.1:" + availablePort
+ + "/?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.rest.RestDemoForTestException&"
+ );
+
+ consumer = this.registerProvider(consumer, server, RestDemoForTestException.class);
+
+ Invoker invoker = protocol.refer(RestDemoForTestException.class, consumer);
+
+
+ RestDemoForTestException client = proxy.getProxy(invoker);
+
+ Assertions.assertThrows(RpcException.class, () -> {
+ client.testMethodDisallowed("aaa");
+
+ });
+
+
+ invoker.destroy();
+ exporter.unexport();
+ }
+
private URL registerProvider(URL url, Object impl, Class> interfaceClass) {
ServiceDescriptor serviceDescriptor = repository.registerService(interfaceClass);
ProviderModel providerModel = new ProviderModel(
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoForTestException.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoForTestException.java
index 718b665ce6..c6c36593f4 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoForTestException.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoForTestException.java
@@ -20,6 +20,7 @@ import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
@@ -36,4 +37,8 @@ public interface RestDemoForTestException {
@Consumes({MediaType.TEXT_PLAIN})
@Path("/hello")
Integer test400(@QueryParam("a")String a,@QueryParam("b") String b);
+
+ @POST
+ @Path("{uid}")
+ String testMethodDisallowed(@PathParam("uid") String uid);
}
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java
index 4f04bd3894..c423bbf3cf 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoService.java
@@ -42,4 +42,8 @@ public interface RestDemoService {
Long testFormBody(@FormParam("number") Long number);
boolean isCalled();
+
+ @DELETE
+ @Path("{uid}")
+ String deleteUserByUid(@PathParam("uid") String uid);
}
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java
index bed70b7657..3ba0858b23 100644
--- a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/rest/RestDemoServiceImpl.java
@@ -42,6 +42,11 @@ public class RestDemoServiceImpl implements RestDemoService {
return called;
}
+ @Override
+ public String deleteUserByUid(String uid) {
+ return uid;
+ }
+
@Override
public Integer hello(Integer a, Integer b) {
context = RpcContext.getServerAttachment().getObjectAttachments();
From e3600af21a55e42e4c8f106cdf18d24c95467a4a Mon Sep 17 00:00:00 2001
From: namelessssssssssss
<100946116+namelessssssssssss@users.noreply.github.com>
Date: Fri, 21 Apr 2023 22:08:43 +0800
Subject: [PATCH 22/59] Add metrics registration uts (#12134)
* Provide uts in metrics-api
* Provide uts in metrics-default
* Provide uts in metrics-default
* Update pom.xml
* Update pom.xml
* Remove 'import *'
* Remove 'import *'
* Merge remote branch
* Add license
* Update DefaultDubboClientObservationConventionTest.java
* Merge remote branch
* Add ut for METRIC_QPS
* Add ut for Provider/Consumer Metrics
* Add ut for Provider/Consumer Metrics
* Add ut for Provider/Consumer Metrics
* Update pom.xml for test
* Add test for p95 & p99
* Add test for p95 & p99
* Update pom.xml
* Update import
* Remove unused todo
* Update test
* Add ut for registration metrics
* Add ut for registration metrics
* Add metrics for application registration
* Add metrics for application registration
* Add test for service subscribe metrics
* Add test for service subscribe metrics
* Remove unused class and add license
* Replace 'Thread.sleep' to TimeController
* Fix wrong type name
* Trigger ci
---
.../collector/RegistryMetricsTest.java | 358 ++++++++++++++++++
.../collector/RegistryStatCompositeTest.java | 2 +-
2 files changed, 359 insertions(+), 1 deletion(-)
create mode 100644 dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsTest.java
diff --git a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsTest.java b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsTest.java
new file mode 100644
index 0000000000..553283c672
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryMetricsTest.java
@@ -0,0 +1,358 @@
+/*
+ * 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.registry.metrics.collector;
+
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.MetricsConfig;
+import org.apache.dubbo.config.context.ConfigManager;
+import org.apache.dubbo.config.nested.AggregationConfig;
+import org.apache.dubbo.metrics.model.key.MetricsKey;
+import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
+import org.apache.dubbo.metrics.registry.collector.RegistryMetricsCollector;
+import org.apache.dubbo.metrics.registry.event.RegistryEvent;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Optional;
+import java.util.concurrent.*;
+
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+
+
+public class RegistryMetricsTest {
+
+ ApplicationModel applicationModel;
+
+ RegistryMetricsCollector collector;
+
+ String REGISTER = "register";
+
+ @BeforeEach
+ void setUp() {
+ this.applicationModel = getApplicationModel();
+ this.collector = getTestCollector(this.applicationModel);
+ this.collector.setCollectEnabled(true);
+ }
+
+ @Test
+ void testRegisterRequestsCount() {
+
+ for (int i = 0; i < 10; i++) {
+ RegistryEvent event = applicationRegister();
+ if (i % 2 == 0) {
+ eventSuccess(event);
+ } else {
+ eventFailed(event);
+ }
+ }
+ List samples = collector.collect();
+
+ GaugeMetricSample> succeedRequests = getSample(MetricsKey.REGISTER_METRIC_REQUESTS_SUCCEED.getName(), samples);
+ GaugeMetricSample> failedRequests = getSample(MetricsKey.REGISTER_METRIC_REQUESTS_FAILED.getName(), samples);
+ GaugeMetricSample> totalRequests = getSample(MetricsKey.REGISTER_METRIC_REQUESTS.getName(), samples);
+
+ Assertions.assertEquals(5L, succeedRequests.applyAsLong());
+ Assertions.assertEquals(5L, failedRequests.applyAsLong());
+ Assertions.assertEquals(10L, totalRequests.applyAsLong());
+ }
+
+ @Test
+ void testLastResponseTime() {
+ long waitTime = 2000;
+
+ RegistryEvent event = applicationRegister();
+ await(waitTime);
+ eventSuccess(event);
+
+ GaugeMetricSample> sample = getSample(MetricsKey.METRIC_RT_LAST.getNameByType(REGISTER), collector.collect());
+ // 20% deviation is allowed
+ Assertions.assertTrue(considerEquals(waitTime, sample.applyAsLong(), 0.2));
+
+ RegistryEvent event1 = applicationRegister();
+ await(waitTime / 2);
+ eventSuccess(event1);
+
+ sample = getSample(MetricsKey.METRIC_RT_LAST.getNameByType(REGISTER), collector.collect());
+ Assertions.assertTrue(considerEquals((double) waitTime / 2, sample.applyAsLong(), 0.2));
+
+ RegistryEvent event2 = applicationRegister();
+ await(waitTime);
+ eventFailed(event2);
+
+ sample = getSample(MetricsKey.METRIC_RT_LAST.getNameByType(REGISTER), collector.collect());
+ Assertions.assertTrue(considerEquals((double) waitTime, sample.applyAsLong(), 0.2));
+ }
+
+ @Test
+ void testMinResponseTime() throws InterruptedException {
+ long waitTime = 2000L;
+
+ RegistryEvent event = applicationRegister();
+ await(waitTime);
+ eventSuccess(event);
+
+ RegistryEvent event1 = applicationRegister();
+ await(waitTime);
+
+ RegistryEvent event2 = applicationRegister();
+ await(waitTime);
+
+ eventSuccess(event1);
+ eventSuccess(event2);
+
+ GaugeMetricSample> sample = getSample(MetricsKey.METRIC_RT_MIN.getNameByType(REGISTER), collector.collect());
+ Assertions.assertTrue(considerEquals(waitTime, sample.applyAsLong(), 0.2));
+
+ RegistryEvent event3 = applicationRegister();
+ Thread.sleep(waitTime / 2);
+ eventSuccess(event3);
+
+ sample = getSample(MetricsKey.METRIC_RT_MIN.getNameByType(REGISTER), collector.collect());
+ Assertions.assertTrue(considerEquals((double) waitTime / 2, sample.applyAsLong(), 0.2));
+ }
+
+ @Test
+ void testMaxResponseTime() {
+ long waitTime = 1000L;
+
+ RegistryEvent event = applicationRegister();
+ await(waitTime);
+ eventSuccess(event);
+
+ GaugeMetricSample> sample = getSample(MetricsKey.METRIC_RT_MAX.getNameByType(REGISTER), collector.collect());
+ Assertions.assertTrue(considerEquals(waitTime, sample.applyAsLong(), 0.2));
+
+ RegistryEvent event1 = applicationRegister();
+ await(waitTime * 2);
+ eventSuccess(event1);
+
+ sample = getSample(MetricsKey.METRIC_RT_MAX.getNameByType(REGISTER), collector.collect());
+ Assertions.assertTrue(considerEquals(waitTime * 2, sample.applyAsLong(), 0.2));
+
+ sample = getSample(MetricsKey.METRIC_RT_MAX.getNameByType(REGISTER), collector.collect());
+ RegistryEvent event2 = applicationRegister();
+ eventSuccess(event2);
+ Assertions.assertTrue(considerEquals(waitTime * 2, sample.applyAsLong(), 0.2));
+ }
+
+ @Test
+ void testSumResponseTime() {
+ long waitTime = 1000;
+
+ RegistryEvent event = applicationRegister();
+ RegistryEvent event1 = applicationRegister();
+ RegistryEvent event2 = applicationRegister();
+
+ await(waitTime);
+
+ eventSuccess(event);
+ eventFailed(event1);
+
+ GaugeMetricSample> sample = getSample(MetricsKey.METRIC_RT_SUM.getNameByType(REGISTER), collector.collect());
+ Assertions.assertTrue(considerEquals(waitTime * 2, sample.applyAsLong(), 0.2));
+
+ await(waitTime);
+ eventSuccess(event2);
+
+ sample = getSample(MetricsKey.METRIC_RT_SUM.getNameByType(REGISTER), collector.collect());
+ Assertions.assertTrue(considerEquals(waitTime * 4, sample.applyAsLong(), 0.2));
+ }
+
+ @Test
+ void testAvgResponseTime() {
+ long waitTime = 1000;
+
+ RegistryEvent event = applicationRegister();
+ RegistryEvent event1 = applicationRegister();
+ RegistryEvent event2 = applicationRegister();
+
+ await(waitTime);
+
+ eventSuccess(event);
+ eventFailed(event1);
+
+ GaugeMetricSample> sample = getSample(MetricsKey.METRIC_RT_AVG.getNameByType(REGISTER), collector.collect());
+ Assertions.assertTrue(considerEquals(waitTime, sample.applyAsLong(), 0.2));
+
+ await(waitTime);
+ eventSuccess(event2);
+
+ sample = getSample(MetricsKey.METRIC_RT_AVG.getNameByType(REGISTER), collector.collect());
+ Assertions.assertTrue(considerEquals((double) waitTime * 4 / 3, sample.applyAsLong(), 0.2));
+ }
+
+ @Test
+ void testServiceRegisterCount() {
+
+ for (int i = 0; i < 10; i++) {
+ RegistryEvent event = serviceRegister();
+ if (i % 2 == 0) {
+ eventSuccess(event);
+ } else {
+ eventFailed(event);
+ }
+ }
+ List samples = collector.collect();
+
+ GaugeMetricSample> succeedRequests = getSample(MetricsKey.SERVICE_REGISTER_METRIC_REQUESTS_SUCCEED.getName(), samples);
+ GaugeMetricSample> failedRequests = getSample(MetricsKey.SERVICE_REGISTER_METRIC_REQUESTS_FAILED.getName(), samples);
+ GaugeMetricSample> totalRequests = getSample(MetricsKey.SERVICE_REGISTER_METRIC_REQUESTS.getName(), samples);
+
+ Assertions.assertEquals(5L, succeedRequests.applyAsLong());
+ Assertions.assertEquals(5L, failedRequests.applyAsLong());
+ Assertions.assertEquals(10L, totalRequests.applyAsLong());
+
+ }
+
+ @Test
+ void testServiceSubscribeCount() {
+
+ for (int i = 0; i < 10; i++) {
+ RegistryEvent event = serviceSubscribe();
+ if (i % 2 == 0) {
+ eventSuccess(event);
+ } else {
+ eventFailed(event);
+ }
+ }
+ List samples = collector.collect();
+
+ GaugeMetricSample> succeedRequests = getSample(MetricsKey.SUBSCRIBE_METRIC_NUM_SUCCEED.getName(), samples);
+ GaugeMetricSample> failedRequests = getSample(MetricsKey.SUBSCRIBE_METRIC_NUM_FAILED.getName(), samples);
+ GaugeMetricSample> totalRequests = getSample(MetricsKey.SUBSCRIBE_METRIC_NUM.getName(), samples);
+
+ Assertions.assertEquals(5L, succeedRequests.applyAsLong());
+ Assertions.assertEquals(5L, failedRequests.applyAsLong());
+ Assertions.assertEquals(10L, totalRequests.applyAsLong());
+ }
+
+
+ GaugeMetricSample> getSample(String name, List samples) {
+ return (GaugeMetricSample>) samples.stream().filter(metricSample -> metricSample.getName().equals(name)).findFirst().orElseThrow(NoSuchElementException::new);
+ }
+
+ RegistryEvent applicationRegister() {
+ RegistryEvent event = registerEvent();
+ collector.onEvent(event);
+ return event;
+ }
+
+ RegistryEvent serviceRegister() {
+ RegistryEvent event = rsEvent();
+ collector.onEvent(event);
+ return event;
+ }
+
+ RegistryEvent serviceSubscribe() {
+ RegistryEvent event = subscribeEvent();
+ collector.onEvent(event);
+ return event;
+ }
+
+ boolean considerEquals(double expected, double trueValue, double allowedErrorRatio) {
+ return Math.abs(1 - expected / trueValue) <= allowedErrorRatio;
+ }
+
+ void eventSuccess(RegistryEvent event) {
+ collector.onEventFinish(event);
+ }
+
+ void eventFailed(RegistryEvent event) {
+ collector.onEventError(event);
+ }
+
+ RegistryEvent registerEvent() {
+ RegistryEvent event = RegistryEvent.toRegisterEvent(applicationModel);
+ event.setAvailable(true);
+ return event;
+ }
+
+ RegistryEvent rsEvent() {
+ RegistryEvent event = RegistryEvent.toRsEvent(applicationModel, "TestServiceInterface1", 1);
+ event.setAvailable(true);
+ return event;
+ }
+
+ RegistryEvent subscribeEvent() {
+ RegistryEvent event = RegistryEvent.toSubscribeEvent(applicationModel);
+ event.setAvailable(true);
+ return event;
+ }
+
+ ApplicationModel getApplicationModel() {
+ return spy(new FrameworkModel().newApplication());
+ }
+
+ void await(long millis) {
+
+ CountDownLatch latch = new CountDownLatch(1);
+
+ ScheduledFuture> future = TimeController.executor.schedule(latch::countDown, millis, TimeUnit.MILLISECONDS);
+ try {
+ latch.await();
+ } catch (InterruptedException e) {
+ future.cancel(true);
+ Thread.currentThread().interrupt();
+ }
+ }
+
+ RegistryMetricsCollector getTestCollector(ApplicationModel applicationModel) {
+
+ ApplicationConfig applicationConfig = new ApplicationConfig("TestApp");
+ ConfigManager configManager = spy(new ConfigManager(applicationModel));
+ MetricsConfig metricsConfig = spy(new MetricsConfig());
+
+ configManager.setApplication(applicationConfig);
+ configManager.setMetrics(metricsConfig);
+
+ when(metricsConfig.getAggregation()).thenReturn(new AggregationConfig());
+ when(applicationModel.getApplicationConfigManager()).thenReturn(configManager);
+ when(applicationModel.NotExistApplicationConfig()).thenReturn(false);
+ when(configManager.getApplication()).thenReturn(Optional.of(applicationConfig));
+
+ return new RegistryMetricsCollector(applicationModel);
+ }
+
+
+ /**
+ * make the control of thread sleep time more precise
+ */
+ static class TimeController {
+
+ private static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
+
+ public static void sleep(long milliseconds) {
+ CountDownLatch latch = new CountDownLatch(1);
+ ScheduledFuture> future = executor.schedule(latch::countDown, milliseconds, TimeUnit.MILLISECONDS);
+ try {
+ latch.await();
+ } catch (InterruptedException e) {
+ future.cancel(true);
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
+
+}
diff --git a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java
index b1a34e7e32..1839b7d138 100644
--- a/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java
+++ b/dubbo-metrics/dubbo-metrics-registry/src/test/java/org/apache/dubbo/metrics/registry/metrics/collector/RegistryStatCompositeTest.java
@@ -60,7 +60,7 @@ public class RegistryStatCompositeTest {
@Test
void testInit() {
Assertions.assertEquals(statComposite.getApplicationStatComposite().getApplicationNumStats().size(), RegistryMetricsConstants.APP_LEVEL_KEYS.size());
- //(rt)5 * (register,subscribe,notify,register.service,subscribe.service)5
+ //(rt)5 * (applicationRegister,subscribe,notify,applicationRegister.service,subscribe.service)
Assertions.assertEquals(5 * 5, statComposite.getRtStatComposite().getRtStats().size());
statComposite.getApplicationStatComposite().getApplicationNumStats().values().forEach((v ->
Assertions.assertEquals(v, new ConcurrentHashMap<>())));
From 3fdd66752a66a5fcede2ec1df52276a6310e8ac7 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Mon, 24 Apr 2023 17:20:19 +0800
Subject: [PATCH 23/59] Bump jackson_version from 2.14.2 to 2.15.0 (#12178)
Bumps `jackson_version` from 2.14.2 to 2.15.0.
Updates `jackson-core` from 2.14.2 to 2.15.0
- [Release notes](https://github.com/FasterXML/jackson-core/releases)
- [Changelog](https://github.com/FasterXML/jackson-core/blob/jackson-core-2.15.0/release.properties)
- [Commits](https://github.com/FasterXML/jackson-core/compare/jackson-core-2.14.2...jackson-core-2.15.0)
Updates `jackson-databind` from 2.14.2 to 2.15.0
- [Release notes](https://github.com/FasterXML/jackson/releases)
- [Commits](https://github.com/FasterXML/jackson/commits)
Updates `jackson-datatype-jsr310` from 2.14.2 to 2.15.0
Updates `jackson-annotations` from 2.14.2 to 2.15.0
- [Release notes](https://github.com/FasterXML/jackson/releases)
- [Commits](https://github.com/FasterXML/jackson/commits)
---
updated-dependencies:
- dependency-name: com.fasterxml.jackson.core:jackson-core
dependency-type: direct:production
update-type: version-update:semver-minor
- dependency-name: com.fasterxml.jackson.core:jackson-databind
dependency-type: direct:production
update-type: version-update:semver-minor
- dependency-name: com.fasterxml.jackson.datatype:jackson-datatype-jsr310
dependency-type: direct:production
update-type: version-update:semver-minor
- dependency-name: com.fasterxml.jackson.core:jackson-annotations
dependency-type: direct:production
update-type: version-update:semver-minor
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
dubbo-dependencies-bom/pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml
index aa0c8841a9..15958a9ae1 100644
--- a/dubbo-dependencies-bom/pom.xml
+++ b/dubbo-dependencies-bom/pom.xml
@@ -184,7 +184,7 @@
2.0.6
5.4.3
2.10.1
- 2.14.2
+ 2.15.0
1.6
6.1.26
2.0
From 48bd823253abee73fa8c39dec0938b5affe7d374 Mon Sep 17 00:00:00 2001
From: conghuhu <56248584+conghuhu@users.noreply.github.com>
Date: Mon, 24 Apr 2023 19:50:05 +0800
Subject: [PATCH 24/59] =?UTF-8?q?chore:=20recover=20dubbo-spring-boot-obse?=
=?UTF-8?q?rvability-starter=20to=20compatible=20=E2=80=A6=20(#12124)?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* chore: recover dubbo-spring-boot-observability-starter to compatible with v3.2.0
* add license
* fix: change tracing
---
.artifacts | 3 +-
dubbo-distribution/dubbo-bom/pom.xml | 7 +++-
.../pom.xml | 2 +-
...bboMicrometerTracingAutoConfiguration.java | 0
.../DubboObservationAutoConfiguration.java | 0
.../autoconfigure/ObservabilityUtils.java | 0
.../ObservationHandlerGrouping.java | 0
.../ObservationRegistryPostProcessor.java | 0
.../ConditionalOnDubboTracingEnable.java | 0
.../brave/BraveAutoConfiguration.java | 15 +++++--
.../exporter/zipkin/HttpSender.java | 0
.../zipkin/ZipkinAutoConfiguration.java | 0
.../exporter/zipkin/ZipkinConfigurations.java | 0
.../zipkin/ZipkinRestTemplateSender.java | 0
.../zipkin/ZipkinWebClientSender.java | 0
.../ZipkinRestTemplateBuilderCustomizer.java | 0
.../ZipkinWebClientBuilderCustomizer.java | 0
.../otel/OpenTelemetryAutoConfiguration.java | 34 +++++++++-------
.../main/resources/META-INF/spring.factories | 0
...ot.autoconfigure.AutoConfiguration.imports | 0
...crometerTracingAutoConfigurationTests.java | 0
.../pom.xml | 40 +++++++++++++++++++
.../pom.xml | 2 +-
.../pom.xml | 2 +-
.../pom.xml | 3 +-
dubbo-spring-boot/pom.xml | 2 +-
dubbo-test/dubbo-dependencies-all/pom.xml | 4 ++
27 files changed, 90 insertions(+), 24 deletions(-)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/pom.xml (98%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/DubboMicrometerTracingAutoConfiguration.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/DubboObservationAutoConfiguration.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservabilityUtils.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservationHandlerGrouping.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservationRegistryPostProcessor.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/annotation/ConditionalOnDubboTracingEnable.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/brave/BraveAutoConfiguration.java (95%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/HttpSender.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinAutoConfiguration.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinConfigurations.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinRestTemplateSender.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinWebClientSender.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/customizer/ZipkinRestTemplateBuilderCustomizer.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/customizer/ZipkinWebClientBuilderCustomizer.java (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/otel/OpenTelemetryAutoConfiguration.java (86%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/resources/META-INF/spring.factories (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (100%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-observability-autoconfigure/src/test/java/org/apache/dubbo/spring/boot/observability/autoconfigure/observability/DubboMicrometerTracingAutoConfigurationTests.java (100%)
create mode 100644 dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-starter/pom.xml
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-tracing-brave-zipkin-starter/pom.xml (96%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/dubbo-spring-boot-tracing-otel-zipkin-starter/pom.xml (96%)
rename dubbo-spring-boot/{dubbo-spring-boot-observability-starter => dubbo-spring-boot-observability-starters}/pom.xml (96%)
diff --git a/.artifacts b/.artifacts
index 1e4e886012..e2302b5b1b 100644
--- a/.artifacts
+++ b/.artifacts
@@ -102,10 +102,11 @@ dubbo-spring-boot-actuator-compatible
dubbo-spring-boot-autoconfigure
dubbo-spring-boot-autoconfigure-compatible
dubbo-spring-boot-compatible
-dubbo-spring-boot-observability-starter
+dubbo-spring-boot-observability-starters
dubbo-spring-boot-observability-autoconfigure
dubbo-spring-boot-tracing-brave-zipkin-starter
dubbo-spring-boot-tracing-otel-zipkin-starter
+dubbo-spring-boot-observability-starter
dubbo-spring-boot-starter
dubbo-spring-security
dubbo-xds
diff --git a/dubbo-distribution/dubbo-bom/pom.xml b/dubbo-distribution/dubbo-bom/pom.xml
index 3678df9c02..7fff3c56cf 100644
--- a/dubbo-distribution/dubbo-bom/pom.xml
+++ b/dubbo-distribution/dubbo-bom/pom.xml
@@ -504,7 +504,7 @@
org.apache.dubbo
- dubbo-spring-boot-observability-starter
+ dubbo-spring-boot-observability-starters
${project.version}
@@ -522,6 +522,11 @@
dubbo-spring-boot-tracing-brave-zipkin-starter
${project.version}
+
+ org.apache.dubbo
+ dubbo-spring-boot-observability-starter
+ ${project.version}
+
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/pom.xml b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/pom.xml
similarity index 98%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/pom.xml
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/pom.xml
index 0103a93a3c..1f6e2f01b1 100644
--- a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/pom.xml
+++ b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/pom.xml
@@ -19,7 +19,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- dubbo-spring-boot-observability-starter
+ dubbo-spring-boot-observability-starters
org.apache.dubbo
${revision}
../pom.xml
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/DubboMicrometerTracingAutoConfiguration.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/DubboMicrometerTracingAutoConfiguration.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/DubboMicrometerTracingAutoConfiguration.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/DubboMicrometerTracingAutoConfiguration.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/DubboObservationAutoConfiguration.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/DubboObservationAutoConfiguration.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/DubboObservationAutoConfiguration.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/DubboObservationAutoConfiguration.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservabilityUtils.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservabilityUtils.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservabilityUtils.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservabilityUtils.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservationHandlerGrouping.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservationHandlerGrouping.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservationHandlerGrouping.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservationHandlerGrouping.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservationRegistryPostProcessor.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservationRegistryPostProcessor.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservationRegistryPostProcessor.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/ObservationRegistryPostProcessor.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/annotation/ConditionalOnDubboTracingEnable.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/annotation/ConditionalOnDubboTracingEnable.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/annotation/ConditionalOnDubboTracingEnable.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/annotation/ConditionalOnDubboTracingEnable.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/brave/BraveAutoConfiguration.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/brave/BraveAutoConfiguration.java
similarity index 95%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/brave/BraveAutoConfiguration.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/brave/BraveAutoConfiguration.java
index dbc6c61e00..24be95c4ec 100644
--- a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/brave/BraveAutoConfiguration.java
+++ b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/brave/BraveAutoConfiguration.java
@@ -16,6 +16,8 @@
*/
package org.apache.dubbo.spring.boot.observability.autoconfigure.brave;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.rpc.model.ModuleModel;
import org.apache.dubbo.spring.boot.autoconfigure.DubboConfigurationProperties;
import org.apache.dubbo.spring.boot.observability.autoconfigure.DubboMicrometerTracingAutoConfiguration;
import org.apache.dubbo.spring.boot.observability.autoconfigure.ObservabilityUtils;
@@ -31,7 +33,6 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
-import org.springframework.core.env.Environment;
import java.util.Collections;
import java.util.List;
@@ -54,6 +55,12 @@ public class BraveAutoConfiguration {
*/
private static final String DEFAULT_APPLICATION_NAME = "application";
+ private final ModuleModel moduleModel;
+
+ public BraveAutoConfiguration(ModuleModel moduleModel) {
+ this.moduleModel = moduleModel;
+ }
+
@Bean
@ConditionalOnMissingBean
@Order(Ordered.HIGHEST_PRECEDENCE)
@@ -66,10 +73,12 @@ public class BraveAutoConfiguration {
@Bean
@ConditionalOnMissingBean
- public brave.Tracing braveTracing(Environment environment, List spanHandlers,
+ public brave.Tracing braveTracing(List spanHandlers,
List tracingCustomizers, brave.propagation.CurrentTraceContext currentTraceContext,
brave.propagation.Propagation.Factory propagationFactory, brave.sampler.Sampler sampler) {
- String applicationName = environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME);
+ String applicationName = moduleModel.getApplicationModel().getApplicationConfigManager().getApplication()
+ .map(ApplicationConfig::getName)
+ .orElse(DEFAULT_APPLICATION_NAME);
brave.Tracing.Builder builder = brave.Tracing.newBuilder().currentTraceContext(currentTraceContext).traceId128Bit(true)
.supportsJoin(false).propagationFactory(propagationFactory).sampler(sampler)
.localServiceName(applicationName);
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/HttpSender.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/HttpSender.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/HttpSender.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/HttpSender.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinAutoConfiguration.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinAutoConfiguration.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinAutoConfiguration.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinAutoConfiguration.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinConfigurations.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinConfigurations.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinConfigurations.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinConfigurations.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinRestTemplateSender.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinRestTemplateSender.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinRestTemplateSender.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinRestTemplateSender.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinWebClientSender.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinWebClientSender.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinWebClientSender.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinWebClientSender.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/customizer/ZipkinRestTemplateBuilderCustomizer.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/customizer/ZipkinRestTemplateBuilderCustomizer.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/customizer/ZipkinRestTemplateBuilderCustomizer.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/customizer/ZipkinRestTemplateBuilderCustomizer.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/customizer/ZipkinWebClientBuilderCustomizer.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/customizer/ZipkinWebClientBuilderCustomizer.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/customizer/ZipkinWebClientBuilderCustomizer.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/customizer/ZipkinWebClientBuilderCustomizer.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/otel/OpenTelemetryAutoConfiguration.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/otel/OpenTelemetryAutoConfiguration.java
similarity index 86%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/otel/OpenTelemetryAutoConfiguration.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/otel/OpenTelemetryAutoConfiguration.java
index 5652d97f0c..85babb77f8 100644
--- a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/otel/OpenTelemetryAutoConfiguration.java
+++ b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/otel/OpenTelemetryAutoConfiguration.java
@@ -18,10 +18,12 @@ package org.apache.dubbo.spring.boot.observability.autoconfigure.otel;
import org.apache.dubbo.common.Version;
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.rpc.model.ModuleModel;
import org.apache.dubbo.spring.boot.autoconfigure.DubboConfigurationProperties;
-import org.apache.dubbo.spring.boot.observability.autoconfigure.annotation.ConditionalOnDubboTracingEnable ;
import org.apache.dubbo.spring.boot.observability.autoconfigure.DubboMicrometerTracingAutoConfiguration;
import org.apache.dubbo.spring.boot.observability.autoconfigure.ObservabilityUtils;
+import org.apache.dubbo.spring.boot.observability.autoconfigure.annotation.ConditionalOnDubboTracingEnable;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.AutoConfiguration;
@@ -31,7 +33,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
-import org.springframework.core.env.Environment;
import java.util.Collections;
import java.util.List;
@@ -43,8 +44,8 @@ import java.util.stream.Collectors;
@AutoConfiguration(before = DubboMicrometerTracingAutoConfiguration.class, afterName = "org.springframework.boot.actuate.autoconfigure.tracing.OpenTelemetryAutoConfiguration")
@ConditionalOnDubboTracingEnable
@ConditionalOnClass(name = {"io.micrometer.tracing.otel.bridge.OtelTracer",
- "io.opentelemetry.sdk.trace.SdkTracerProvider", "io.opentelemetry.api.OpenTelemetry"
- , "io.micrometer.tracing.SpanCustomizer"})
+ "io.opentelemetry.sdk.trace.SdkTracerProvider", "io.opentelemetry.api.OpenTelemetry"
+ , "io.micrometer.tracing.SpanCustomizer"})
@EnableConfigurationProperties(DubboConfigurationProperties.class)
public class OpenTelemetryAutoConfiguration {
@@ -55,24 +56,29 @@ public class OpenTelemetryAutoConfiguration {
private final DubboConfigurationProperties dubboConfigProperties;
- OpenTelemetryAutoConfiguration(DubboConfigurationProperties dubboConfigProperties) {
+ private final ModuleModel moduleModel;
+
+ OpenTelemetryAutoConfiguration(DubboConfigurationProperties dubboConfigProperties, ModuleModel moduleModel) {
this.dubboConfigProperties = dubboConfigProperties;
+ this.moduleModel = moduleModel;
}
@Bean
@ConditionalOnMissingBean
io.opentelemetry.api.OpenTelemetry openTelemetry(io.opentelemetry.sdk.trace.SdkTracerProvider sdkTracerProvider, io.opentelemetry.context.propagation.ContextPropagators contextPropagators) {
return io.opentelemetry.sdk.OpenTelemetrySdk.builder().setTracerProvider(sdkTracerProvider).setPropagators(contextPropagators)
- .build();
+ .build();
}
@Bean
@ConditionalOnMissingBean
- io.opentelemetry.sdk.trace.SdkTracerProvider otelSdkTracerProvider(Environment environment, ObjectProvider spanProcessors,
+ io.opentelemetry.sdk.trace.SdkTracerProvider otelSdkTracerProvider(ObjectProvider spanProcessors,
io.opentelemetry.sdk.trace.samplers.Sampler sampler) {
- String applicationName = environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME);
+ String applicationName = moduleModel.getApplicationModel().getApplicationConfigManager().getApplication()
+ .map(ApplicationConfig::getName)
+ .orElse(DEFAULT_APPLICATION_NAME);
io.opentelemetry.sdk.trace.SdkTracerProviderBuilder builder = io.opentelemetry.sdk.trace.SdkTracerProvider.builder().setSampler(sampler)
- .setResource(io.opentelemetry.sdk.resources.Resource.create(io.opentelemetry.api.common.Attributes.of(io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME, applicationName)));
+ .setResource(io.opentelemetry.sdk.resources.Resource.create(io.opentelemetry.api.common.Attributes.of(io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME, applicationName)));
spanProcessors.orderedStream().forEach(builder::addSpanProcessor);
return builder.build();
}
@@ -93,11 +99,11 @@ public class OpenTelemetryAutoConfiguration {
@Bean
@ConditionalOnMissingBean
io.opentelemetry.sdk.trace.SpanProcessor otelSpanProcessor(ObjectProvider spanExporters,
- ObjectProvider spanExportingPredicates, ObjectProvider spanReporters,
- ObjectProvider spanFilters) {
+ ObjectProvider spanExportingPredicates, ObjectProvider spanReporters,
+ ObjectProvider spanFilters) {
return io.opentelemetry.sdk.trace.export.BatchSpanProcessor.builder(new io.micrometer.tracing.otel.bridge.CompositeSpanExporter(spanExporters.orderedStream().collect(Collectors.toList()),
- spanExportingPredicates.orderedStream().collect(Collectors.toList()), spanReporters.orderedStream().collect(Collectors.toList()),
- spanFilters.orderedStream().collect(Collectors.toList()))).build();
+ spanExportingPredicates.orderedStream().collect(Collectors.toList()), spanReporters.orderedStream().collect(Collectors.toList()),
+ spanFilters.orderedStream().collect(Collectors.toList()))).build();
}
@Bean
@@ -163,7 +169,7 @@ public class OpenTelemetryAutoConfiguration {
io.opentelemetry.context.propagation.TextMapPropagator w3cTextMapPropagatorWithBaggage(io.micrometer.tracing.otel.bridge.OtelCurrentTraceContext otelCurrentTraceContext) {
List remoteFields = this.dubboConfigProperties.getTracing().getBaggage().getRemoteFields();
return io.opentelemetry.context.propagation.TextMapPropagator.composite(io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator.getInstance(),
- io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator.getInstance(), new io.micrometer.tracing.otel.propagation.BaggageTextMapPropagator(remoteFields,
+ io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator.getInstance(), new io.micrometer.tracing.otel.propagation.BaggageTextMapPropagator(remoteFields,
new io.micrometer.tracing.otel.bridge.OtelBaggageManager(otelCurrentTraceContext, remoteFields, Collections.emptyList())));
}
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/resources/META-INF/spring.factories b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/resources/META-INF/spring.factories
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/resources/META-INF/spring.factories
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/resources/META-INF/spring.factories
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/test/java/org/apache/dubbo/spring/boot/observability/autoconfigure/observability/DubboMicrometerTracingAutoConfigurationTests.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/test/java/org/apache/dubbo/spring/boot/observability/autoconfigure/observability/DubboMicrometerTracingAutoConfigurationTests.java
similarity index 100%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-observability-autoconfigure/src/test/java/org/apache/dubbo/spring/boot/observability/autoconfigure/observability/DubboMicrometerTracingAutoConfigurationTests.java
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/test/java/org/apache/dubbo/spring/boot/observability/autoconfigure/observability/DubboMicrometerTracingAutoConfigurationTests.java
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-starter/pom.xml b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-starter/pom.xml
new file mode 100644
index 0000000000..030298ed0d
--- /dev/null
+++ b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-starter/pom.xml
@@ -0,0 +1,40 @@
+
+
+
+
+ dubbo-spring-boot-observability-starters
+ org.apache.dubbo
+ ${revision}
+ ../pom.xml
+
+ 4.0.0
+
+ dubbo-spring-boot-observability-starter
+ jar
+ Apache Dubbo Spring Boot Observability Starter
+
+
+
+ org.apache.dubbo
+ dubbo-spring-boot-observability-autoconfigure
+ ${project.version}
+
+
+
\ No newline at end of file
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-tracing-brave-zipkin-starter/pom.xml b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-tracing-brave-zipkin-starter/pom.xml
similarity index 96%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-tracing-brave-zipkin-starter/pom.xml
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-tracing-brave-zipkin-starter/pom.xml
index 7dd89c5bf8..ac619e4302 100644
--- a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-tracing-brave-zipkin-starter/pom.xml
+++ b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-tracing-brave-zipkin-starter/pom.xml
@@ -19,7 +19,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- dubbo-spring-boot-observability-starter
+ dubbo-spring-boot-observability-starters
org.apache.dubbo
${revision}
../pom.xml
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-tracing-otel-zipkin-starter/pom.xml b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-tracing-otel-zipkin-starter/pom.xml
similarity index 96%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-tracing-otel-zipkin-starter/pom.xml
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-tracing-otel-zipkin-starter/pom.xml
index f09967097f..97279d2b73 100644
--- a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/dubbo-spring-boot-tracing-otel-zipkin-starter/pom.xml
+++ b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-tracing-otel-zipkin-starter/pom.xml
@@ -19,7 +19,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- dubbo-spring-boot-observability-starter
+ dubbo-spring-boot-observability-starters
org.apache.dubbo
${revision}
../pom.xml
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/pom.xml b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/pom.xml
similarity index 96%
rename from dubbo-spring-boot/dubbo-spring-boot-observability-starter/pom.xml
rename to dubbo-spring-boot/dubbo-spring-boot-observability-starters/pom.xml
index b4f34c65eb..8e334877a9 100644
--- a/dubbo-spring-boot/dubbo-spring-boot-observability-starter/pom.xml
+++ b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/pom.xml
@@ -27,12 +27,13 @@
4.0.0
pom
- dubbo-spring-boot-observability-starter
+ dubbo-spring-boot-observability-starters
dubbo-spring-boot-observability-autoconfigure
dubbo-spring-boot-tracing-otel-zipkin-starter
dubbo-spring-boot-tracing-brave-zipkin-starter
+ dubbo-spring-boot-observability-starter
diff --git a/dubbo-spring-boot/pom.xml b/dubbo-spring-boot/pom.xml
index 79997de27c..615e6c3b5b 100644
--- a/dubbo-spring-boot/pom.xml
+++ b/dubbo-spring-boot/pom.xml
@@ -36,7 +36,7 @@
dubbo-spring-boot-autoconfigure
dubbo-spring-boot-compatible
dubbo-spring-boot-starter
- dubbo-spring-boot-observability-starter
+ dubbo-spring-boot-observability-starters
diff --git a/dubbo-test/dubbo-dependencies-all/pom.xml b/dubbo-test/dubbo-dependencies-all/pom.xml
index e0b320e9d3..8b4ed7e5b0 100644
--- a/dubbo-test/dubbo-dependencies-all/pom.xml
+++ b/dubbo-test/dubbo-dependencies-all/pom.xml
@@ -343,6 +343,10 @@
org.apache.dubbo
dubbo-spring-boot-tracing-brave-zipkin-starter
+
+ org.apache.dubbo
+ dubbo-spring-boot-observability-starter
+
From 073a13dd9b7049638161c37ec3cd0dc0379e4694 Mon Sep 17 00:00:00 2001
From: Albumen Kevin
Date: Tue, 25 Apr 2023 09:30:46 +0800
Subject: [PATCH 25/59] Fix business exception (#12136)
* Fix business exception
* opt
* opt
* Catch exception
---
.../filter/MethodMetricsInterceptor.java | 12 ++++++----
.../dubbo/metrics/filter/MetricsFilter.java | 23 ++++++++++++++++---
2 files changed, 27 insertions(+), 8 deletions(-)
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
index 7c61eda55f..98620ccb7a 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
@@ -46,27 +46,28 @@ public class MethodMetricsInterceptor {
private String getSide(Invocation invocation) {
Optional extends Invoker>> invoker = Optional.ofNullable(invocation.getInvoker());
- String side = invoker.isPresent() ? invoker.get().getUrl().getSide() : PROVIDER_SIDE;
- return side;
+ return invoker.isPresent() ? invoker.get().getUrl().getSide() : PROVIDER_SIDE;
}
public void afterMethod(Invocation invocation, Result result) {
if (result.hasException()) {
- handleMethodException(invocation, result.getException());
+ handleMethodException(invocation, result.getException(), true);
} else {
sampler.incOnEvent(invocation, MetricsEvent.Type.SUCCEED.getNameByType(getSide(invocation)));
onCompleted(invocation);
}
}
- public void handleMethodException(Invocation invocation, Throwable throwable) {
+ public void handleMethodException(Invocation invocation, Throwable throwable, boolean isBusiness) {
if (throwable == null) {
return;
}
String side = getSide(invocation);
MetricsEvent.Type eventType = MetricsEvent.Type.UNKNOWN_FAILED;
- if (throwable instanceof RpcException) {
+ if (isBusiness) {
+ eventType = MetricsEvent.Type.BUSINESS_FAILED;
+ } else if (throwable instanceof RpcException) {
RpcException e = (RpcException) throwable;
if (e.isTimeout()) {
@@ -85,6 +86,7 @@ public class MethodMetricsInterceptor {
eventType = MetricsEvent.Type.NETWORK_EXCEPTION;
}
}
+
sampler.incOnEvent(invocation, eventType.getNameByType(side));
onCompleted(invocation);
sampler.incOnEvent(invocation, MetricsEvent.Type.TOTAL_FAILED.getNameByType(side));
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
index c981f62831..04265e4aff 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
@@ -17,6 +17,8 @@
package org.apache.dubbo.metrics.filter;
import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
import org.apache.dubbo.rpc.BaseFilter;
import org.apache.dubbo.rpc.Filter;
@@ -29,10 +31,13 @@ import org.apache.dubbo.rpc.model.ScopeModelAware;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER;
+import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
@Activate(group = {CONSUMER, PROVIDER}, order = -1)
public class MetricsFilter implements Filter, BaseFilter.Listener, ScopeModelAware {
+ private final static ErrorTypeAwareLogger LOGGER = LoggerFactory.getErrorTypeAwareLogger(MetricsFilter.class);
+
private DefaultMetricsCollector collector = null;
private MethodMetricsInterceptor metricsInterceptor;
@@ -51,7 +56,11 @@ public class MetricsFilter implements Filter, BaseFilter.Listener, ScopeModelAwa
return invoker.invoke(invocation);
}
- metricsInterceptor.beforeMethod(invocation);
+ try {
+ metricsInterceptor.beforeMethod(invocation);
+ } catch (Throwable t) {
+ LOGGER.warn(INTERNAL_ERROR, "", "", "Error occurred when beforeMethod.", t);
+ }
return invoker.invoke(invocation);
}
@@ -61,7 +70,11 @@ public class MetricsFilter implements Filter, BaseFilter.Listener, ScopeModelAwa
if (collector == null || !collector.isCollectEnabled()) {
return;
}
- metricsInterceptor.afterMethod(invocation, result);
+ try {
+ metricsInterceptor.afterMethod(invocation, result);
+ } catch (Throwable t) {
+ LOGGER.warn(INTERNAL_ERROR, "", "", "Error occurred when afterMethod.", t);
+ }
}
@Override
@@ -69,7 +82,11 @@ public class MetricsFilter implements Filter, BaseFilter.Listener, ScopeModelAwa
if (collector == null || !collector.isCollectEnabled()) {
return;
}
- metricsInterceptor.handleMethodException(invocation, t);
+ try {
+ metricsInterceptor.handleMethodException(invocation, t, false);
+ } catch (Throwable t1) {
+ LOGGER.warn(INTERNAL_ERROR, "", "", "Error occurred when handleMethodException.", t1);
+ }
}
}
From 1138b9749259398506c6cda88cfe5ceef32e48b7 Mon Sep 17 00:00:00 2001
From: PiteXChen <44110731+RapperCL@users.noreply.github.com>
Date: Tue, 25 Apr 2023 11:49:16 +0800
Subject: [PATCH 26/59] fix: Adjusting cluster invoker checks (#12139)
* Adjustment check(#12138)
* Unit test optimization
* Code optimization
---
.../support/AbstractClusterInvoker.java | 2 ++
.../support/BroadcastClusterInvoker.java | 1 -
.../support/FailbackClusterInvoker.java | 1 -
.../support/FailfastClusterInvoker.java | 1 -
.../support/FailoverClusterInvoker.java | 1 -
.../support/FailsafeClusterInvoker.java | 1 -
.../support/ForkingClusterInvoker.java | 1 -
.../support/MergeableClusterInvoker.java | 1 -
.../support/AvailableClusterInvokerTest.java | 2 +-
.../support/FailSafeClusterInvokerTest.java | 15 ++++++-----
.../support/FailbackClusterInvokerTest.java | 26 ++++++++++++-------
.../registry/ZoneAwareClusterInvokerTest.java | 2 ++
12 files changed, 30 insertions(+), 24 deletions(-)
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java
index b03447676d..43c9e9b294 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java
@@ -333,6 +333,8 @@ public abstract class AbstractClusterInvoker implements ClusterInvoker {
List> invokers = list(invocation);
InvocationProfilerUtils.releaseDetailProfiler(invocation);
+ checkInvokers(invokers, invocation);
+
LoadBalance loadbalance = initLoadBalance(invokers, invocation);
RpcUtils.attachInvocationIdIfAsync(getUrl(), invocation);
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/BroadcastClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/BroadcastClusterInvoker.java
index 02b8f6d2d0..bdf8f6f02e 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/BroadcastClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/BroadcastClusterInvoker.java
@@ -51,7 +51,6 @@ public class BroadcastClusterInvoker extends AbstractClusterInvoker {
@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Result doInvoke(final Invocation invocation, List> invokers, LoadBalance loadbalance) throws RpcException {
- checkInvokers(invokers, invocation);
RpcContext.getServiceContext().setInvokers((List) invokers);
RpcException exception = null;
Result result = null;
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvoker.java
index 608b00ef1f..368244a1df 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvoker.java
@@ -104,7 +104,6 @@ public class FailbackClusterInvoker extends AbstractClusterInvoker {
Invoker invoker = null;
URL consumerUrl = RpcContext.getServiceContext().getConsumerUrl();
try {
- checkInvokers(invokers, invocation);
invoker = select(loadbalance, invocation, invokers, null);
// Asynchronous call method must be used here, because failback will retry in the background.
// Then the serviceContext will be cleared after the call is completed.
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java
index 0b9f6bd2dc..22c708f824 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailfastClusterInvoker.java
@@ -41,7 +41,6 @@ public class FailfastClusterInvoker extends AbstractClusterInvoker {
@Override
public Result doInvoke(Invocation invocation, List> invokers, LoadBalance loadbalance) throws RpcException {
- checkInvokers(invokers, invocation);
Invoker invoker = select(loadbalance, invocation, invokers, null);
try {
return invokeWithContext(invoker, invocation);
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvoker.java
index 18583742ba..b0cdb8ff53 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailoverClusterInvoker.java
@@ -57,7 +57,6 @@ public class FailoverClusterInvoker extends AbstractClusterInvoker {
@SuppressWarnings({"unchecked", "rawtypes"})
public Result doInvoke(Invocation invocation, final List> invokers, LoadBalance loadbalance) throws RpcException {
List> copyInvokers = invokers;
- checkInvokers(copyInvokers, invocation);
String methodName = RpcUtils.getMethodName(invocation);
int len = calculateInvokeTimes(methodName);
// retry loop.
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailsafeClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailsafeClusterInvoker.java
index c09205bdbd..78c3540895 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailsafeClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailsafeClusterInvoker.java
@@ -47,7 +47,6 @@ public class FailsafeClusterInvoker extends AbstractClusterInvoker {
@Override
public Result doInvoke(Invocation invocation, List> invokers, LoadBalance loadbalance) throws RpcException {
try {
- checkInvokers(invokers, invocation);
Invoker invoker = select(loadbalance, invocation, invokers, null);
return invokeWithContext(invoker, invocation);
} catch (Throwable e) {
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java
index a1334aab07..b2f0d2e095 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/ForkingClusterInvoker.java
@@ -67,7 +67,6 @@ public class ForkingClusterInvoker extends AbstractClusterInvoker {
@SuppressWarnings({"unchecked", "rawtypes"})
public Result doInvoke(final Invocation invocation, List> invokers, LoadBalance loadbalance) throws RpcException {
try {
- checkInvokers(invokers, invocation);
final List> selected;
final int forks = getUrl().getParameter(FORKS_KEY, DEFAULT_FORKS);
final int timeout = getUrl().getParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java
index bdd2ddd6d2..6d61eb2194 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/MergeableClusterInvoker.java
@@ -59,7 +59,6 @@ public class MergeableClusterInvoker extends AbstractClusterInvoker {
@Override
protected Result doInvoke(Invocation invocation, List> invokers, LoadBalance loadbalance) throws RpcException {
- checkInvokers(invokers, invocation);
String merger = getUrl().getMethodParameter(invocation.getMethodName(), MERGER_KEY);
if (ConfigUtils.isEmpty(merger)) { // If a method doesn't have a merger, only invoke one Group
for (final Invoker invoker : invokers) {
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AvailableClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AvailableClusterInvokerTest.java
index 4218bd72d8..b3357e9ba4 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AvailableClusterInvokerTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/AvailableClusterInvokerTest.java
@@ -107,7 +107,7 @@ class AvailableClusterInvokerTest {
invoker.invoke(invocation);
fail();
} catch (RpcException e) {
- Assertions.assertTrue(e.getMessage().contains("No provider available in"));
+ Assertions.assertTrue(e.getMessage().contains("No provider available"));
assertFalse(e.getCause() instanceof RpcException);
}
}
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java
index 8887154a6c..dc05557f20 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailSafeClusterInvokerTest.java
@@ -17,7 +17,6 @@
package org.apache.dubbo.rpc.cluster.support;
import org.apache.dubbo.common.URL;
-import org.apache.dubbo.common.utils.LogUtil;
import org.apache.dubbo.rpc.AppResponse;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
@@ -25,6 +24,7 @@ import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.cluster.Directory;
import org.apache.dubbo.rpc.cluster.filter.DemoService;
+import org.apache.dubbo.rpc.RpcException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -33,7 +33,7 @@ import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
-import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -113,10 +113,13 @@ class FailSafeClusterInvokerTest {
resetInvokerToNoException();
FailsafeClusterInvoker invoker = new FailsafeClusterInvoker(dic);
- LogUtil.start();
- invoker.invoke(invocation);
- assertTrue(LogUtil.findMessage("No provider") > 0);
- LogUtil.stop();
+
+ try{
+ invoker.invoke(invocation);
+ } catch (RpcException e){
+ Assertions.assertTrue(e.getMessage().contains("No provider available"));
+ assertFalse(e.getCause() instanceof RpcException);
+ }
}
}
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java
index d7e48afef6..ed161d7c79 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvokerTest.java
@@ -24,9 +24,10 @@ import org.apache.dubbo.common.utils.LogUtil;
import org.apache.dubbo.rpc.AppResponse;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
-import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcInvocation;
+import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.cluster.Directory;
+import org.apache.dubbo.rpc.RpcException;
import org.apache.log4j.Level;
import org.junit.jupiter.api.AfterEach;
@@ -37,6 +38,7 @@ import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
+import org.junit.jupiter.api.function.Executable;
import java.lang.reflect.Field;
import java.util.ArrayList;
@@ -45,7 +47,7 @@ import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.apache.dubbo.common.constants.CommonConstants.RETRIES_KEY;
-import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -110,6 +112,9 @@ class FailbackClusterInvokerTest {
given(dic.getUrl()).willReturn(url);
given(dic.getConsumerUrl()).willReturn(url);
given(dic.getInterface()).willReturn(FailbackClusterInvokerTest.class);
+ given(dic.list(invocation)).willReturn(invokers);
+ given(invoker.getUrl()).willReturn(url);
+
FailbackClusterInvoker invoker = new FailbackClusterInvoker<>(dic);
invoker.invoke(invocation);
Assertions.assertNull(RpcContext.getServiceContext().getInvoker());
@@ -123,6 +128,9 @@ class FailbackClusterInvokerTest {
given(dic.getUrl()).willReturn(url);
given(dic.getConsumerUrl()).willReturn(url);
given(dic.getInterface()).willReturn(FailbackClusterInvokerTest.class);
+ given(dic.list(invocation)).willReturn(invokers);
+ given(invoker.getUrl()).willReturn(url);
+
FailbackClusterInvoker invoker = new FailbackClusterInvoker<>(dic);
invoker.invoke(invocation);
Assertions.assertNull(RpcContext.getServiceContext().getInvoker());
@@ -161,17 +169,15 @@ class FailbackClusterInvokerTest {
given(dic.getInterface()).willReturn(FailbackClusterInvokerTest.class);
invocation.setMethodName("method1");
-
invokers.add(invoker);
- resetInvokerToNoException();
-
FailbackClusterInvoker invoker = new FailbackClusterInvoker<>(dic);
- LogUtil.start();
- DubboAppender.clear();
- invoker.invoke(invocation);
- assertEquals(1, LogUtil.findMessage("Failback to invoke"));
- LogUtil.stop();
+ try{
+ invoker.invoke(invocation);
+ } catch (RpcException e){
+ Assertions.assertTrue(e.getMessage().contains("No provider available"));
+ assertFalse(e.getCause() instanceof RpcException);
+ }
}
@Disabled
diff --git a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvokerTest.java b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvokerTest.java
index 021f6facb4..9a20078935 100644
--- a/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvokerTest.java
+++ b/dubbo-cluster/src/test/java/org/apache/dubbo/rpc/cluster/support/registry/ZoneAwareClusterInvokerTest.java
@@ -24,6 +24,7 @@ import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.cluster.ClusterInvoker;
import org.apache.dubbo.rpc.cluster.Directory;
+import org.apache.dubbo.rpc.cluster.support.AbstractClusterInvoker;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -174,6 +175,7 @@ class ZoneAwareClusterInvokerTest {
given(directory.getUrl()).willReturn(url);
given(directory.getConsumerUrl()).willReturn(url);
given(directory.list(invocation)).willReturn(new ArrayList<>(0));
+ given(directory.getInterface()).willReturn(ZoneAwareClusterInvokerTest.class);
zoneAwareClusterInvoker = new ZoneAwareClusterInvoker<>(directory);
From 316d9536180d708527fc6aa4b2b4bdc6a0d42e1b Mon Sep 17 00:00:00 2001
From: MartinDai
Date: Tue, 25 Apr 2023 11:49:43 +0800
Subject: [PATCH 27/59] Lower MetricsFilter's order (#12157)
Co-authored-by: daming
---
.../java/org/apache/dubbo/metrics/filter/MetricsFilter.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
index 04265e4aff..bea8116a36 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MetricsFilter.java
@@ -33,7 +33,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
-@Activate(group = {CONSUMER, PROVIDER}, order = -1)
+@Activate(group = {CONSUMER, PROVIDER}, order = Integer.MIN_VALUE + 100)
public class MetricsFilter implements Filter, BaseFilter.Listener, ScopeModelAware {
private final static ErrorTypeAwareLogger LOGGER = LoggerFactory.getErrorTypeAwareLogger(MetricsFilter.class);
From 1a0c61ae23e0bf3ebdf40dbbf0db0ec356e8a9a0 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 25 Apr 2023 13:56:56 +0800
Subject: [PATCH 28/59] Bump spring-boot-dependencies from 2.7.10 to 2.7.11
(#12179)
Bumps [spring-boot-dependencies](https://github.com/spring-projects/spring-boot) from 2.7.10 to 2.7.11.
- [Release notes](https://github.com/spring-projects/spring-boot/releases)
- [Commits](https://github.com/spring-projects/spring-boot/compare/v2.7.10...v2.7.11)
---
updated-dependencies:
- dependency-name: org.springframework.boot:spring-boot-dependencies
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
dubbo-demo/dubbo-demo-spring-boot/pom.xml | 2 +-
dubbo-spring-boot/pom.xml | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/dubbo-demo/dubbo-demo-spring-boot/pom.xml b/dubbo-demo/dubbo-demo-spring-boot/pom.xml
index 69e1e07ed6..9c729c9ce9 100644
--- a/dubbo-demo/dubbo-demo-spring-boot/pom.xml
+++ b/dubbo-demo/dubbo-demo-spring-boot/pom.xml
@@ -36,7 +36,7 @@
8
8
true
- 2.7.10
+ 2.7.11
2.7.10
1.10.6
diff --git a/dubbo-spring-boot/pom.xml b/dubbo-spring-boot/pom.xml
index 615e6c3b5b..443343f9b4 100644
--- a/dubbo-spring-boot/pom.xml
+++ b/dubbo-spring-boot/pom.xml
@@ -40,7 +40,7 @@
- 2.7.10
+ 2.7.11
${revision}
2.20.0
From e13126edf63a6e9b9d284f8285bdb9078e8a82f3 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 25 Apr 2023 13:58:03 +0800
Subject: [PATCH 29/59] Bump spring-boot-maven-plugin from 2.7.10 to 2.7.11
(#12180)
Bumps [spring-boot-maven-plugin](https://github.com/spring-projects/spring-boot) from 2.7.10 to 2.7.11.
- [Release notes](https://github.com/spring-projects/spring-boot/releases)
- [Commits](https://github.com/spring-projects/spring-boot/compare/v2.7.10...v2.7.11)
---
updated-dependencies:
- dependency-name: org.springframework.boot:spring-boot-maven-plugin
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
dubbo-demo/dubbo-demo-annotation/pom.xml | 2 +-
dubbo-demo/dubbo-demo-api/pom.xml | 2 +-
dubbo-demo/dubbo-demo-spring-boot/pom.xml | 2 +-
dubbo-demo/dubbo-demo-xml/pom.xml | 2 +-
4 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/dubbo-demo/dubbo-demo-annotation/pom.xml b/dubbo-demo/dubbo-demo-annotation/pom.xml
index b84ceb28f7..fcdf966e93 100644
--- a/dubbo-demo/dubbo-demo-annotation/pom.xml
+++ b/dubbo-demo/dubbo-demo-annotation/pom.xml
@@ -30,7 +30,7 @@
true
- 2.7.10
+ 2.7.11
diff --git a/dubbo-demo/dubbo-demo-api/pom.xml b/dubbo-demo/dubbo-demo-api/pom.xml
index c68afe43a4..05db3e38db 100644
--- a/dubbo-demo/dubbo-demo-api/pom.xml
+++ b/dubbo-demo/dubbo-demo-api/pom.xml
@@ -36,7 +36,7 @@
true
- 2.7.10
+ 2.7.11
dubbo-demo-api
diff --git a/dubbo-demo/dubbo-demo-spring-boot/pom.xml b/dubbo-demo/dubbo-demo-spring-boot/pom.xml
index 9c729c9ce9..610ff36643 100644
--- a/dubbo-demo/dubbo-demo-spring-boot/pom.xml
+++ b/dubbo-demo/dubbo-demo-spring-boot/pom.xml
@@ -37,7 +37,7 @@
8
true
2.7.11
- 2.7.10
+ 2.7.11
1.10.6
diff --git a/dubbo-demo/dubbo-demo-xml/pom.xml b/dubbo-demo/dubbo-demo-xml/pom.xml
index 6e4c678b2e..bdc8ae0e1c 100644
--- a/dubbo-demo/dubbo-demo-xml/pom.xml
+++ b/dubbo-demo/dubbo-demo-xml/pom.xml
@@ -32,7 +32,7 @@
true
- 2.7.10
+ 2.7.11
From d37f4405288541745964f7653950fdc9e029121f Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 25 Apr 2023 13:58:28 +0800
Subject: [PATCH 30/59] Bump spring-boot-starter-test from 2.7.10 to 2.7.11
(#12181)
Bumps [spring-boot-starter-test](https://github.com/spring-projects/spring-boot) from 2.7.10 to 2.7.11.
- [Release notes](https://github.com/spring-projects/spring-boot/releases)
- [Commits](https://github.com/spring-projects/spring-boot/compare/v2.7.10...v2.7.11)
---
updated-dependencies:
- dependency-name: org.springframework.boot:spring-boot-starter-test
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
dubbo-config/dubbo-config-spring/pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dubbo-config/dubbo-config-spring/pom.xml b/dubbo-config/dubbo-config-spring/pom.xml
index 42247d25a5..25252f67b3 100644
--- a/dubbo-config/dubbo-config-spring/pom.xml
+++ b/dubbo-config/dubbo-config-spring/pom.xml
@@ -27,7 +27,7 @@
The spring config module of dubbo project
false
- 2.7.10
+ 2.7.11
From 6e2299653d0c39de9b6dfdc092050d0524d07c25 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 25 Apr 2023 13:58:45 +0800
Subject: [PATCH 31/59] Bump fastjson2 from 2.0.28 to 2.0.29 (#12177)
Bumps [fastjson2](https://github.com/alibaba/fastjson2) from 2.0.28 to 2.0.29.
- [Release notes](https://github.com/alibaba/fastjson2/releases)
- [Commits](https://github.com/alibaba/fastjson2/compare/2.0.28...2.0.29)
---
updated-dependencies:
- dependency-name: com.alibaba.fastjson2:fastjson2
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
dubbo-dependencies-bom/pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml
index 15958a9ae1..5399be5a43 100644
--- a/dubbo-dependencies-bom/pom.xml
+++ b/dubbo-dependencies-bom/pom.xml
@@ -102,7 +102,7 @@
4.5.14
4.4.16
1.2.83
- 2.0.28
+ 2.0.29
3.4.14
4.3.0
2.12.0
From 53133eacf08753d613c821bd57396709e83494d3 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 25 Apr 2023 13:58:59 +0800
Subject: [PATCH 32/59] Bump spring-boot.version from 2.7.10 to 2.7.11 (#12176)
Bumps `spring-boot.version` from 2.7.10 to 2.7.11.
Updates `spring-boot-starter` from 2.7.10 to 2.7.11
- [Release notes](https://github.com/spring-projects/spring-boot/releases)
- [Commits](https://github.com/spring-projects/spring-boot/compare/v2.7.10...v2.7.11)
Updates `spring-boot-autoconfigure` from 2.7.10 to 2.7.11
- [Release notes](https://github.com/spring-projects/spring-boot/releases)
- [Commits](https://github.com/spring-projects/spring-boot/compare/v2.7.10...v2.7.11)
Updates `spring-boot-starter-logging` from 2.7.10 to 2.7.11
- [Release notes](https://github.com/spring-projects/spring-boot/releases)
- [Commits](https://github.com/spring-projects/spring-boot/compare/v2.7.10...v2.7.11)
---
updated-dependencies:
- dependency-name: org.springframework.boot:spring-boot-starter
dependency-type: direct:production
update-type: version-update:semver-patch
- dependency-name: org.springframework.boot:spring-boot-autoconfigure
dependency-type: direct:production
update-type: version-update:semver-patch
- dependency-name: org.springframework.boot:spring-boot-starter-logging
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
.../dubbo-demo-spring-boot-consumer/pom.xml | 2 +-
.../dubbo-demo-spring-boot-provider/pom.xml | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/dubbo-demo/dubbo-demo-spring-boot/dubbo-demo-spring-boot-consumer/pom.xml b/dubbo-demo/dubbo-demo-spring-boot/dubbo-demo-spring-boot-consumer/pom.xml
index 2dcd2ef325..89b23d6ade 100644
--- a/dubbo-demo/dubbo-demo-spring-boot/dubbo-demo-spring-boot-consumer/pom.xml
+++ b/dubbo-demo/dubbo-demo-spring-boot/dubbo-demo-spring-boot-consumer/pom.xml
@@ -31,7 +31,7 @@
8
8
1.7.33
- 2.7.10
+ 2.7.11
true
diff --git a/dubbo-demo/dubbo-demo-spring-boot/dubbo-demo-spring-boot-provider/pom.xml b/dubbo-demo/dubbo-demo-spring-boot/dubbo-demo-spring-boot-provider/pom.xml
index fc82ed4e83..083a27bcd5 100644
--- a/dubbo-demo/dubbo-demo-spring-boot/dubbo-demo-spring-boot-provider/pom.xml
+++ b/dubbo-demo/dubbo-demo-spring-boot/dubbo-demo-spring-boot-provider/pom.xml
@@ -31,7 +31,7 @@
8
8
1.7.33
- 2.7.10
+ 2.7.11
true
From 8b323cd762acec8c9d1aa80a4ef07bb3bff90541 Mon Sep 17 00:00:00 2001
From: MartinDai
Date: Tue, 25 Apr 2023 14:11:17 +0800
Subject: [PATCH 33/59] Collect No Provider Request count (#12158)
* Collect No Provider Request count as dubbo.consumer.invoker.no.available.count Metrics
* ensure dubbo.consumer.invoker.no.available.count Metrics only collect in consumer side
---------
Co-authored-by: daming
---
.../java/org/apache/dubbo/metrics/event/MetricsEvent.java | 1 +
.../java/org/apache/dubbo/metrics/model/key/MetricsKey.java | 1 +
.../dubbo/metrics/collector/AggregateMetricsCollector.java | 2 ++
.../apache/dubbo/metrics/filter/MethodMetricsInterceptor.java | 4 ++++
.../metrics/collector/AggregateMetricsCollectorTest.java | 2 ++
5 files changed, 10 insertions(+)
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java
index 26396c8acd..490e36df2c 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java
@@ -103,6 +103,7 @@ public abstract class MetricsEvent {
NETWORK_EXCEPTION("NETWORK_EXCEPTION_%s"),
SERVICE_UNAVAILABLE("SERVICE_UNAVAILABLE_%s"),
CODEC_EXCEPTION("CODEC_EXCEPTION_%s"),
+ NO_INVOKER_AVAILABLE("NO_INVOKER_AVAILABLE_%s"),
;
private final String name;
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKey.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKey.java
index 0d993bb4cd..b142365fad 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKey.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKey.java
@@ -118,6 +118,7 @@ public enum MetricsKey {
METADATA_GIT_COMMITID_METRIC("git.commit.id", "Git Commit Id Metrics"),
// consumer metrics key
+ INVOKER_NO_AVAILABLE_COUNT("dubbo.consumer.invoker.no.available.count", "Request Throw No Invoker Available Exception Count"),
;
private String name;
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
index 286fa1dcd0..fad110a90a 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
@@ -139,6 +139,7 @@ public class AggregateMetricsCollector implements MetricsCollector, MetricsListe
collectMethod(list, MetricsEvent.Type.NETWORK_EXCEPTION.getNameByType(side), MetricsKey.METRIC_REQUESTS_TOTAL_NETWORK_FAILED_AGG);
collectMethod(list, MetricsEvent.Type.CODEC_EXCEPTION.getNameByType(side), MetricsKey.METRIC_REQUESTS_TOTAL_CODEC_FAILED_AGG);
collectMethod(list, MetricsEvent.Type.SERVICE_UNAVAILABLE.getNameByType(side), MetricsKey.METRIC_REQUESTS_TOTAL_SERVICE_UNAVAILABLE_FAILED_AGG);
+ collectMethod(list, MetricsEvent.Type.NO_INVOKER_AVAILABLE.getNameByType(side), MetricsKey.INVOKER_NO_AVAILABLE_COUNT);
}
private void collectMethod(List list, String eventType, MetricsKey metricsKey) {
@@ -179,6 +180,7 @@ public class AggregateMetricsCollector implements MetricsCollector, MetricsListe
methodTypeCounter.put(MetricsEvent.Type.SERVICE_UNAVAILABLE.getNameByType(side), new ConcurrentHashMap<>());
methodTypeCounter.put(MetricsEvent.Type.NETWORK_EXCEPTION.getNameByType(side), new ConcurrentHashMap<>());
methodTypeCounter.put(MetricsEvent.Type.CODEC_EXCEPTION.getNameByType(side), new ConcurrentHashMap<>());
+ methodTypeCounter.put(MetricsEvent.Type.NO_INVOKER_AVAILABLE.getNameByType(side), new ConcurrentHashMap<>());
}
private void registerListener() {
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
index 98620ccb7a..21da540c1e 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
@@ -17,6 +17,7 @@
package org.apache.dubbo.metrics.filter;
+import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.metrics.collector.sample.MethodMetricsSampler;
import org.apache.dubbo.metrics.event.MetricsEvent;
import org.apache.dubbo.rpc.Invocation;
@@ -85,6 +86,9 @@ public class MethodMetricsInterceptor {
if (e.isNetwork()) {
eventType = MetricsEvent.Type.NETWORK_EXCEPTION;
}
+ if (e.isNoInvokerAvailableAfterFilter() && CommonConstants.CONSUMER_SIDE.equals(side)) {
+ eventType = MetricsEvent.Type.NO_INVOKER_AVAILABLE;
+ }
}
sampler.incOnEvent(invocation, eventType.getNameByType(side));
diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
index 7bc56579b6..d4fbd1e571 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
@@ -156,6 +156,7 @@ class AggregateMetricsCollectorTest {
methodMetricsCountSampler.incOnEvent(invocation, MetricsEvent.Type.NETWORK_EXCEPTION.getNameByType(side));
methodMetricsCountSampler.incOnEvent(invocation, MetricsEvent.Type.SERVICE_UNAVAILABLE.getNameByType(side));
methodMetricsCountSampler.incOnEvent(invocation, MetricsEvent.Type.CODEC_EXCEPTION.getNameByType(side));
+ methodMetricsCountSampler.incOnEvent(invocation, MetricsEvent.Type.NO_INVOKER_AVAILABLE.getNameByType(side));
List samples = collector.collect();
@@ -181,6 +182,7 @@ class AggregateMetricsCollectorTest {
Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_REQUESTS_TOTAL_NETWORK_FAILED_AGG.getNameByType(side)), 1L);
Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_REQUESTS_TOTAL_CODEC_FAILED_AGG.getNameByType(side)), 1L);
Assertions.assertEquals(sampleMap.get(MetricsKey.METRIC_REQUESTS_TOTAL_SERVICE_UNAVAILABLE_FAILED_AGG.getNameByType(side)), 1L);
+ Assertions.assertEquals(sampleMap.get(MetricsKey.INVOKER_NO_AVAILABLE_COUNT.getNameByType(side)), 1L);
Assertions.assertTrue(sampleMap.containsKey(MetricsKey.METRIC_QPS.getNameByType(side)));
}
From 9687e48949792f5580847b12ff8680b93b85bd2f Mon Sep 17 00:00:00 2001
From: ShenFeng312 <49786112+ShenFeng312@users.noreply.github.com>
Date: Tue, 25 Apr 2023 14:16:56 +0800
Subject: [PATCH 34/59] Polish comments and logs (#12169)
---
.../java/org/apache/dubbo/config/DubboShutdownHook.java | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java
index e32a3f4fd3..c946fa4390 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java
@@ -92,11 +92,11 @@ public class DubboShutdownHook extends Thread {
int timeout = ConfigurationUtils.getServerShutdownTimeout(applicationModel);
if (timeout > 0) {
long start = System.currentTimeMillis();
- /**
- * To avoid shutdown conflicts between Dubbo and Spring,
- * wait for the modules bound to Spring to be handled by Spring util timeout.
+ /*
+ To avoid shutdown conflicts between Dubbo and Spring,
+ wait for the modules bound to Spring to be handled by Spring until timeout.
*/
- logger.info("Waiting for modules managed by Spring to be shut down.");
+ logger.info("Waiting for modules managed by Spring to be shutdown.");
while (!applicationModel.isDestroyed() && hasModuleBindSpring
&& (System.currentTimeMillis() - start) < timeout) {
try {
From fb00a8a391195472a32695674a6f506636b90927 Mon Sep 17 00:00:00 2001
From: Albumen Kevin
Date: Tue, 25 Apr 2023 14:18:39 +0800
Subject: [PATCH 35/59] Follow #11262, disable cache (#12171)
---
.../registry/nacos/NacosConnectionManager.java | 17 +++++++----------
1 file changed, 7 insertions(+), 10 deletions(-)
diff --git a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosConnectionManager.java b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosConnectionManager.java
index 913809a5a6..2082abd2b0 100644
--- a/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosConnectionManager.java
+++ b/dubbo-registry/dubbo-registry-nacos/src/main/java/org/apache/dubbo/registry/nacos/NacosConnectionManager.java
@@ -16,13 +16,6 @@
*/
package org.apache.dubbo.registry.nacos;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-import java.util.concurrent.ThreadLocalRandom;
-
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.LoggerCodeConstants;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
@@ -35,7 +28,13 @@ import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
-import static com.alibaba.nacos.api.PropertyKeyConst.NAMING_LOAD_CACHE_AT_START;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ThreadLocalRandom;
+
import static com.alibaba.nacos.api.PropertyKeyConst.PASSWORD;
import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR;
import static com.alibaba.nacos.api.PropertyKeyConst.USERNAME;
@@ -199,8 +198,6 @@ public class NacosConnectionManager {
if (StringUtils.isNotEmpty(url.getPassword())) {
properties.put(PASSWORD, url.getPassword());
}
-
- putPropertyIfAbsent(url, properties, NAMING_LOAD_CACHE_AT_START, "true");
}
private void putPropertyIfAbsent(URL url, Properties properties, String propertyName, String defaultValue) {
From a1a83873d674bfd61c64d01f8833f58bfc771cea Mon Sep 17 00:00:00 2001
From: suncairong163 <105478245+suncairong163@users.noreply.github.com>
Date: Tue, 25 Apr 2023 14:34:21 +0800
Subject: [PATCH 36/59] Feature/dubbo3.2 rest demo (#12183)
* add jaxrs rest demo
* add jaxrs rest demo
---
dubbo-demo/dubbo-demo-interface/pom.xml | 13 +-
.../dubbo/demo/rest/api/CurlService.java | 34 ++++
.../api/DubboServiceAnnotationService.java | 32 ++++
.../demo/rest/api/ExceptionMapperService.java | 31 ++++
.../demo/rest/api/HttpMethodService.java | 71 ++++++++
...tpRequestAndResponseRPCContextService.java | 47 ++++++
.../demo/rest/api/JaxRsRestDemoService.java | 120 ++++++++++++++
.../demo/rest/api/SpringRestDemoService.java | 79 +++++++++
.../src/main/java/po/User.java | 78 +++++++++
.../dubbo-demo-jaxrs-rest-consumer/pom.xml | 153 ++++++++++++++++++
.../dubbo/demo/rest/api/RestConsumer.java | 107 ++++++++++++
.../rest/api/SpringControllerService.java | 31 ++++
.../demo/rest/api/config/DubboConfig.java | 23 +++
.../main/resources/spring/rest-consumer.xml | 48 ++++++
.../dubbo-demo-jaxrs-rest-provider/pom.xml | 153 ++++++++++++++++++
.../dubbo/demo/rest/api/RestProvider.java | 43 +++++
.../demo/rest/api/config/DubboConfig.java | 23 +++
.../api/extension/ExceptionMapperForTest.java | 29 ++++
.../demo/rest/api/impl/CurlServiceImpl.java | 31 ++++
.../DubboServiceAnnotationServiceImpl.java | 28 ++++
.../api/impl/ExceptionMapperServiceImpl.java | 32 ++++
.../rest/api/impl/HttpMethodServiceImpl.java | 60 +++++++
...questAndResponseRPCContextServiceImpl.java | 53 ++++++
.../api/impl/JaxRsRestDemoServiceImpl.java | 105 ++++++++++++
.../main/resources/spring/rest-provider.xml | 59 +++++++
dubbo-demo/dubbo-demo-xml/pom.xml | 2 +
26 files changed, 1478 insertions(+), 7 deletions(-)
create mode 100644 dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/CurlService.java
create mode 100644 dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/DubboServiceAnnotationService.java
create mode 100644 dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/ExceptionMapperService.java
create mode 100644 dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/HttpMethodService.java
create mode 100644 dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/HttpRequestAndResponseRPCContextService.java
create mode 100644 dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/JaxRsRestDemoService.java
create mode 100644 dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/SpringRestDemoService.java
create mode 100644 dubbo-demo/dubbo-demo-interface/src/main/java/po/User.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/pom.xml
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/RestConsumer.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringControllerService.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/resources/spring/rest-consumer.xml
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/pom.xml
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/RestProvider.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/extension/ExceptionMapperForTest.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/CurlServiceImpl.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/DubboServiceAnnotationServiceImpl.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/ExceptionMapperServiceImpl.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/HttpMethodServiceImpl.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/HttpRequestAndResponseRPCContextServiceImpl.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/JaxRsRestDemoServiceImpl.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/resources/spring/rest-provider.xml
diff --git a/dubbo-demo/dubbo-demo-interface/pom.xml b/dubbo-demo/dubbo-demo-interface/pom.xml
index 7cc0c57707..c906f1d1d2 100644
--- a/dubbo-demo/dubbo-demo-interface/pom.xml
+++ b/dubbo-demo/dubbo-demo-interface/pom.xml
@@ -36,18 +36,17 @@
dubbo-rpc-rest
-
-
- org.springframework
- spring-web
- test
-
-
org.springframework
spring-context
test
+
+
+ org.springframework
+ spring-web
+
+
diff --git a/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/CurlService.java b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/CurlService.java
new file mode 100644
index 0000000000..09a202ab58
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/CurlService.java
@@ -0,0 +1,34 @@
+/*
+ * 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.demo.rest.api;
+
+
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
+@Path("/curl")
+public interface CurlService {
+ // curl -X GET http://localhost:8888/services/curl
+ // http://localhost:8888/services/curl
+ @GET
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ String curl();
+
+}
+
diff --git a/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/DubboServiceAnnotationService.java b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/DubboServiceAnnotationService.java
new file mode 100644
index 0000000000..0ef70bf293
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/DubboServiceAnnotationService.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.demo.rest.api;
+
+
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
+@Path("/annotation")
+public interface DubboServiceAnnotationService {
+ @GET
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ String annotation();
+
+}
+
diff --git a/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/ExceptionMapperService.java b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/ExceptionMapperService.java
new file mode 100644
index 0000000000..313159be0a
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/ExceptionMapperService.java
@@ -0,0 +1,31 @@
+/*
+ * 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.demo.rest.api;
+
+
+
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+
+@Path("/exception/mapper")
+public interface ExceptionMapperService {
+
+ @POST
+ @Path("/exception")
+ String exception(String message);
+
+}
diff --git a/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/HttpMethodService.java b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/HttpMethodService.java
new file mode 100644
index 0000000000..ebbc1b10f3
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/HttpMethodService.java
@@ -0,0 +1,71 @@
+/*
+ * 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.demo.rest.api;
+
+
+import io.swagger.jaxrs.PATCH;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.HEAD;
+import javax.ws.rs.OPTIONS;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.QueryParam;
+
+@Path("/demoService")
+public interface HttpMethodService {
+
+ @POST
+ @Path("/sayPost")
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ String sayHelloPost(String hello);
+
+ @DELETE
+ @Path("/sayDelete")
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ String sayHelloDelete(@QueryParam("name") String hello);
+
+ @HEAD
+ @Path("/sayHead")
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ String sayHelloHead(@QueryParam("name") String hello);
+
+ @GET
+ @Path("/sayGet")
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ String sayHelloGet(@QueryParam("name") String hello);
+
+ @PUT
+ @Path("/sayPut")
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ String sayHelloPut(@QueryParam("name") String hello);
+
+ @PATCH
+ @Path("/sayPatch")
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ String sayHelloPatch(@QueryParam("name") String hello);
+
+ @OPTIONS
+ @Path("/sayOptions")
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ String sayHelloOptions(@QueryParam("name") String hello);
+
+}
+
diff --git a/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/HttpRequestAndResponseRPCContextService.java b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/HttpRequestAndResponseRPCContextService.java
new file mode 100644
index 0000000000..e1df730e20
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/HttpRequestAndResponseRPCContextService.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.demo.rest.api;
+
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.QueryParam;
+import java.util.List;
+
+@Path("/demoService")
+public interface HttpRequestAndResponseRPCContextService {
+
+ @POST
+ @Path("/httpRequestParam")
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ String httpRequestParam(@QueryParam("name") String hello);
+
+ @POST
+ @Path("/httpRequestHeader")
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ String httpRequestHeader(@HeaderParam("header") String hello);
+
+ @POST
+ @Path("/httpResponseHeader")
+ @Consumes({javax.ws.rs.core.MediaType.TEXT_PLAIN})
+ List httpResponseHeader(@HeaderParam("response") String hello);
+
+
+}
+
diff --git a/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/JaxRsRestDemoService.java b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/JaxRsRestDemoService.java
new file mode 100644
index 0000000000..198304a5b6
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/JaxRsRestDemoService.java
@@ -0,0 +1,120 @@
+/*
+ * 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.demo.rest.api;
+
+
+import po.User;
+
+import javax.ws.rs.Consumes;
+import javax.ws.rs.FormParam;
+import javax.ws.rs.GET;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ *
+ * @Consumers & @Produces can be not used ,we will make sure the content-type of request by arg type
+ * but the Request method is forbidden disappear
+ * parameters which annotation are not present , it is from the body (jaxrs anntation is diffrent from spring web from param(only request param can ignore anntation))
+ *
+ * Every method only one param from body
+ *
+ * the path annotation must present in class & method
+ */
+
+@Path("/jaxrs/demo/service")
+public interface JaxRsRestDemoService {
+ @GET
+ @Path("/hello")
+ Integer hello(@QueryParam("a") Integer a, @QueryParam("b") Integer b);
+
+ @GET
+ @Path("/error")
+ String error();
+
+ @POST
+ @Path("/say")
+ String sayHello(String name);
+
+
+
+
+
+ @POST
+ @Path("/testFormBody")
+ Long testFormBody(@FormParam("number") Long number);
+
+ @POST
+ @Path("/testJavaBeanBody")
+ @Consumes({MediaType.APPLICATION_JSON})
+ User testJavaBeanBody(User user);
+
+
+
+ @GET
+ @Path("/primitive")
+ int primitiveInt(@QueryParam("a") int a, @QueryParam("b") int b);
+
+ @GET
+ @Path("/primitiveLong")
+ long primitiveLong(@QueryParam("a") long a, @QueryParam("b") Long b);
+
+ @GET
+ @Path("/primitiveByte")
+ long primitiveByte(@QueryParam("a") byte a, @QueryParam("b") Long b);
+
+ @POST
+ @Path("/primitiveShort")
+ long primitiveShort(@QueryParam("a") short a, @QueryParam("b") Long b, int c);
+
+ @GET
+ @Path("testMapParam")
+ @Produces({MediaType.TEXT_PLAIN})
+ @Consumes({MediaType.TEXT_PLAIN})
+ String testMapParam(@QueryParam("test") Map params);
+
+ @GET
+ @Path("testMapHeader")
+ @Produces({MediaType.TEXT_PLAIN})
+ @Consumes({MediaType.TEXT_PLAIN})
+ String testMapHeader(@HeaderParam("test") Map headers);
+
+ @POST
+ @Path("testMapForm")
+ @Produces({MediaType.APPLICATION_JSON})
+ @Consumes({MediaType.APPLICATION_FORM_URLENCODED})
+ List testMapForm(MultivaluedMap params);
+
+ @POST
+ @Path("/header")
+ @Consumes({MediaType.TEXT_PLAIN})
+ String header(@HeaderParam("header") String header);
+
+ @POST
+ @Path("/headerInt")
+ @Consumes({MediaType.TEXT_PLAIN})
+ int headerInt(@HeaderParam("header") int header);
+
+
+}
diff --git a/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/SpringRestDemoService.java b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/SpringRestDemoService.java
new file mode 100644
index 0000000000..d5da64403e
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-interface/src/main/java/org/apache/dubbo/demo/rest/api/SpringRestDemoService.java
@@ -0,0 +1,79 @@
+/*
+ * 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.demo.rest.api;
+
+
+import org.springframework.http.MediaType;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestHeader;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
+import po.User;
+
+import java.util.List;
+import java.util.Map;
+
+@RequestMapping("/spring/demo/service")
+public interface SpringRestDemoService {
+
+ @RequestMapping(method = RequestMethod.GET, value = "/hello")
+ Integer hello(@RequestParam("a") Integer a, @RequestParam("b") Integer b);
+
+ @RequestMapping(method = RequestMethod.GET, value = "/error")
+ String error();
+
+ @RequestMapping(method = RequestMethod.POST, value = "/say")
+ String sayHello(@RequestBody String name);
+
+ @RequestMapping(method = RequestMethod.POST, value = "/testFormBody", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
+ Long testFormBody(@RequestBody Long number);
+
+ @RequestMapping(method = RequestMethod.POST, value = "/testJavaBeanBody", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
+ User testJavaBeanBody(@RequestBody User user);
+
+
+ @RequestMapping(method = RequestMethod.GET, value = "/primitive")
+ int primitiveInt(@RequestParam("a") int a, @RequestParam("b") int b);
+
+ @RequestMapping(method = RequestMethod.GET, value = "/primitiveLong")
+ long primitiveLong(@RequestParam("a") long a, @RequestParam("b") Long b);
+
+ @RequestMapping(method = RequestMethod.GET, value = "/primitiveByte")
+ long primitiveByte(@RequestParam("a") byte a, @RequestParam("b") Long b);
+
+
+ @RequestMapping(method = RequestMethod.POST, value = "/primitiveShort")
+ long primitiveShort(@RequestParam("a") short a, @RequestParam("b") Long b, @RequestBody int c);
+
+
+ @RequestMapping(method = RequestMethod.GET, value = "/testMapParam")
+ String testMapParam(@RequestParam Map params);
+
+ @RequestMapping(method = RequestMethod.GET, value = "/testMapHeader")
+ String testMapHeader(@RequestHeader Map headers);
+
+ @RequestMapping(method = RequestMethod.POST, value = "/testMapForm", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
+ List testMapForm(MultiValueMap params);
+
+
+ @RequestMapping(method = RequestMethod.GET, value = "/headerInt")
+ int headerInt(@RequestHeader("header") int header);
+
+
+}
diff --git a/dubbo-demo/dubbo-demo-interface/src/main/java/po/User.java b/dubbo-demo/dubbo-demo-interface/src/main/java/po/User.java
new file mode 100644
index 0000000000..2c58d9d3f2
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-interface/src/main/java/po/User.java
@@ -0,0 +1,78 @@
+/*
+ * 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 po;
+
+import java.io.Serializable;
+import java.util.Objects;
+
+
+public class User implements Serializable {
+
+
+ private Long id;
+
+ private String name;
+
+ public User() {
+ }
+
+ public User(Long id, String name) {
+ this.id = id;
+ this.name = name;
+ }
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public static User getInstance() {
+ return new User(1l, "dubbo-rest");
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ User user = (User) o;
+ return Objects.equals(id, user.id) && Objects.equals(name, user.name);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(id, name);
+ }
+
+ @Override
+ public String toString() {
+ return "User (" +
+ "id=" + id +
+ ", name='" + name + '\'' +
+ ')';
+ }
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/pom.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/pom.xml
new file mode 100644
index 0000000000..f004076824
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/pom.xml
@@ -0,0 +1,153 @@
+
+
+
+ 4.0.0
+
+ org.apache.dubbo
+ dubbo-demo-xml
+ ${revision}
+ ../pom.xml
+
+
+ dubbo-demo-jaxrs-rest-consumer
+
+ jar
+ Dubbo Rest Demo
+ ${project.artifactId}
+
+
+ true
+ 1.7.33
+
+
+
+ org.springframework
+ spring-context
+
+
+ org.springframework
+ spring-web
+
+
+
+ org.apache.dubbo
+ dubbo-registry-multicast
+
+
+ org.apache.dubbo
+ dubbo-registry-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-registry-nacos
+
+
+ com.alibaba.nacos
+ nacos-client
+
+
+ org.apache.dubbo
+ dubbo-configcenter-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-configcenter-nacos
+
+
+ org.apache.dubbo
+ dubbo-metadata-report-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-metadata-report-nacos
+
+
+ org.apache.dubbo
+ dubbo-rpc-dubbo
+
+
+ org.apache.dubbo
+ dubbo-rpc-rest
+
+
+ org.apache.dubbo
+ dubbo-config-spring
+
+
+ org.apache.dubbo
+ dubbo-remoting-netty4
+
+
+ org.apache.dubbo
+ dubbo-serialization-hessian2
+
+
+ org.apache.dubbo
+ dubbo-serialization-fastjson2
+
+
+ org.apache.dubbo
+ dubbo-serialization-jdk
+
+
+ org.slf4j
+ slf4j-api
+
+
+ org.slf4j
+ slf4j-log4j12
+ ${slf4j-log4j12.version}
+
+
+ log4j
+ log4j
+
+
+
+ org.springframework
+ spring-test
+ test
+
+
+
+ org.apache.dubbo
+ dubbo-demo-interface
+ ${project.version}
+
+
+
+
+
+
+ javax.annotation
+
+ [1.11,)
+
+
+
+ javax.annotation
+ javax.annotation-api
+ 1.3.2
+
+
+
+
+
+
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/RestConsumer.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/RestConsumer.java
new file mode 100644
index 0000000000..b90d221f3c
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/RestConsumer.java
@@ -0,0 +1,107 @@
+/*
+ * 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.demo.rest.api;
+
+import org.jboss.resteasy.specimpl.MultivaluedMapImpl;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import po.User;
+
+import java.util.Arrays;
+
+public class RestConsumer {
+
+ public static void main(String[] args) {
+ consumerService();
+ }
+
+ public static void consumerService() {
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/rest-consumer.xml"});
+ context.start();
+ System.out.println("rest consumer start");
+ testExceptionMapperService(context);
+ testHttpMethodService(context);
+ httpRPCContextTest(context);
+ jaxRsRestDemoServiceTest(context);
+ System.out.println("rest consumer test success");
+ }
+
+ private static void jaxRsRestDemoServiceTest(ClassPathXmlApplicationContext context) {
+ JaxRsRestDemoService jaxRsRestDemoService = context.getBean("jaxRsRestDemoService", JaxRsRestDemoService.class);
+ String hello = jaxRsRestDemoService.sayHello("hello");
+ assertEquals("Hello, hello", hello);
+ Integer result = jaxRsRestDemoService.primitiveInt(1, 2);
+ Long resultLong = jaxRsRestDemoService.primitiveLong(1, 2l);
+ long resultByte = jaxRsRestDemoService.primitiveByte((byte) 1, 2l);
+ long resultShort = jaxRsRestDemoService.primitiveShort((short) 1, 2l, 1);
+
+ assertEquals(result, 3);
+ assertEquals(resultShort, 3l);
+ assertEquals(resultLong, 3l);
+ assertEquals(resultByte, 3l);
+
+ assertEquals(Long.valueOf(1l), jaxRsRestDemoService.testFormBody(1l));
+
+ MultivaluedMapImpl forms = new MultivaluedMapImpl<>();
+ forms.put("form", Arrays.asList("F1"));
+
+ assertEquals(Arrays.asList("F1"), jaxRsRestDemoService.testMapForm(forms));
+ assertEquals(User.getInstance(), jaxRsRestDemoService.testJavaBeanBody(User.getInstance()));
+ }
+
+
+ private static void testExceptionMapperService(ClassPathXmlApplicationContext context) {
+ String returnStr = "exception";
+ String paramStr = "exception";
+ ExceptionMapperService exceptionMapperService = context.getBean("exceptionMapperService", ExceptionMapperService.class);
+ assertEquals(returnStr, exceptionMapperService.exception(paramStr));
+ }
+
+ private static void httpRPCContextTest(ClassPathXmlApplicationContext context) {
+
+ HttpRequestAndResponseRPCContextService requestAndResponseRPCContextService = context.getBean("httpRequestAndResponseRPCContextService", HttpRequestAndResponseRPCContextService.class);
+ String returnStr = "hello";
+ String paramStr = "hello";
+ assertEquals(returnStr, requestAndResponseRPCContextService.httpRequestHeader(paramStr));
+ assertEquals(returnStr, requestAndResponseRPCContextService.httpRequestParam(paramStr));
+ assertEquals(returnStr, requestAndResponseRPCContextService.httpResponseHeader(paramStr).get(0));
+ }
+
+
+ private static void testHttpMethodService(ClassPathXmlApplicationContext context) {
+ HttpMethodService httpMethodService = context.getBean("httpMethodService", HttpMethodService.class);
+ String returnStr = "hello";
+ String paramStr = "hello";
+// assertEquals(null, httpMethodService.sayHelloHead(paramStr));
+ assertEquals(returnStr, httpMethodService.sayHelloGet(paramStr));
+ assertEquals(returnStr, httpMethodService.sayHelloDelete(paramStr));
+ assertEquals(returnStr, httpMethodService.sayHelloPut(paramStr));
+ assertEquals(returnStr, httpMethodService.sayHelloOptions(paramStr));
+// Assert.assertEquals(returnStr, httpMethodService.sayHelloPatch(paramStr));
+ assertEquals(returnStr, httpMethodService.sayHelloPost(paramStr));
+ }
+
+ private static void assertEquals(Object returnStr, Object exception) {
+ boolean equal = returnStr != null && returnStr.equals(exception);
+
+ if (equal) {
+ return;
+ } else {
+ throw new RuntimeException();
+ }
+ }
+
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringControllerService.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringControllerService.java
new file mode 100644
index 0000000000..58841def65
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringControllerService.java
@@ -0,0 +1,31 @@
+/*
+ * 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.demo.rest.api;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/controller")
+public class SpringControllerService {
+
+ @GetMapping("/sayHello")
+ public String sayHello(String hello) {
+ return hello;
+ }
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
new file mode 100644
index 0000000000..a5d0edfacf
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
@@ -0,0 +1,23 @@
+/*
+ * 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.demo.rest.api.config;
+
+import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
+
+@DubboComponentScan("org.apache.dubbo.demo.rest")
+public class DubboConfig {
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/resources/spring/rest-consumer.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/resources/spring/rest-consumer.xml
new file mode 100644
index 0000000000..ea6a63b04f
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/resources/spring/rest-consumer.xml
@@ -0,0 +1,48 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/pom.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/pom.xml
new file mode 100644
index 0000000000..f15f8b3c92
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/pom.xml
@@ -0,0 +1,153 @@
+
+
+
+ 4.0.0
+
+ org.apache.dubbo
+ dubbo-demo-xml
+ ${revision}
+ ../pom.xml
+
+
+ dubbo-demo-jaxrs-rest-provider
+
+ war
+ Dubbo Rest Demo
+ ${project.artifactId}
+
+
+ true
+ 1.7.33
+
+
+
+ org.springframework
+ spring-context
+
+
+ org.springframework
+ spring-web
+
+
+
+ org.apache.dubbo
+ dubbo-registry-multicast
+
+
+ org.apache.dubbo
+ dubbo-registry-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-registry-nacos
+
+
+ com.alibaba.nacos
+ nacos-client
+
+
+ org.apache.dubbo
+ dubbo-configcenter-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-configcenter-nacos
+
+
+ org.apache.dubbo
+ dubbo-metadata-report-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-metadata-report-nacos
+
+
+ org.apache.dubbo
+ dubbo-rpc-dubbo
+
+
+ org.apache.dubbo
+ dubbo-rpc-rest
+
+
+ org.apache.dubbo
+ dubbo-config-spring
+
+
+ org.apache.dubbo
+ dubbo-remoting-netty4
+
+
+ org.apache.dubbo
+ dubbo-serialization-hessian2
+
+
+ org.apache.dubbo
+ dubbo-serialization-fastjson2
+
+
+ org.apache.dubbo
+ dubbo-serialization-jdk
+
+
+ org.slf4j
+ slf4j-api
+
+
+ org.slf4j
+ slf4j-log4j12
+ ${slf4j-log4j12.version}
+
+
+ log4j
+ log4j
+
+
+
+ org.springframework
+ spring-test
+ test
+
+
+
+ org.apache.dubbo
+ dubbo-demo-interface
+ ${project.version}
+
+
+
+
+
+
+ javax.annotation
+
+ [1.11,)
+
+
+
+ javax.annotation
+ javax.annotation-api
+ 1.3.2
+
+
+
+
+
+
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/RestProvider.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/RestProvider.java
new file mode 100644
index 0000000000..ebc2ccfe73
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/RestProvider.java
@@ -0,0 +1,43 @@
+/*
+ * 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.demo.rest.api;
+
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+
+public class RestProvider {
+
+ public static void main(String[] args) throws Exception {
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/rest-provider.xml"});
+
+ context.refresh();
+
+// SpringControllerService springControllerService = context.getBean(SpringControllerService.class);
+// ServiceConfig serviceConfig = new ServiceConfig<>();
+// serviceConfig.setInterface(SpringControllerService.class);
+// serviceConfig.setProtocol(new ProtocolConfig("rest", 8888));
+// serviceConfig.setRef(springControllerService);
+// serviceConfig.export();
+
+
+ System.out.println("dubbo service started");
+
+ System.in.read();
+ }
+
+
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
new file mode 100644
index 0000000000..a5d0edfacf
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
@@ -0,0 +1,23 @@
+/*
+ * 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.demo.rest.api.config;
+
+import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
+
+@DubboComponentScan("org.apache.dubbo.demo.rest")
+public class DubboConfig {
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/extension/ExceptionMapperForTest.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/extension/ExceptionMapperForTest.java
new file mode 100644
index 0000000000..49099a606a
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/extension/ExceptionMapperForTest.java
@@ -0,0 +1,29 @@
+/*
+ * 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.demo.rest.api.extension;
+
+
+import org.apache.dubbo.rpc.protocol.rest.exception.mapper.ExceptionHandler;
+
+public class ExceptionMapperForTest implements ExceptionHandler {
+
+
+ @Override
+ public Object result(RuntimeException exception) {
+ return exception.getMessage();
+ }
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/CurlServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/CurlServiceImpl.java
new file mode 100644
index 0000000000..64dacac29f
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/CurlServiceImpl.java
@@ -0,0 +1,31 @@
+/*
+ * 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.demo.rest.api.impl;
+
+import org.apache.dubbo.config.annotation.DubboService;
+import org.apache.dubbo.demo.rest.api.CurlService;
+
+@DubboService( interfaceClass = CurlService.class,protocol = "rest")
+public class CurlServiceImpl implements CurlService {
+
+
+
+ @Override
+ public String curl() {
+ return "hello,dubbo rest curl request";
+ }
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/DubboServiceAnnotationServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/DubboServiceAnnotationServiceImpl.java
new file mode 100644
index 0000000000..823af088f7
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/DubboServiceAnnotationServiceImpl.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.demo.rest.api.impl;
+
+import org.apache.dubbo.config.annotation.DubboService;
+import org.apache.dubbo.demo.rest.api.DubboServiceAnnotationService;
+
+@DubboService(protocol = "rest")
+public class DubboServiceAnnotationServiceImpl implements DubboServiceAnnotationService {
+ @Override
+ public String annotation() {
+ return "Dubbo Service Annotation service demo!";
+ }
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/ExceptionMapperServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/ExceptionMapperServiceImpl.java
new file mode 100644
index 0000000000..26a69ff7fb
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/ExceptionMapperServiceImpl.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.demo.rest.api.impl;
+
+
+import org.apache.dubbo.demo.rest.api.ExceptionMapperService;
+import org.springframework.stereotype.Service;
+
+@Service("exceptionMapperService")
+public class ExceptionMapperServiceImpl implements ExceptionMapperService {
+
+ @Override
+ public String exception(String message) {
+
+ throw new RuntimeException(message);
+ }
+
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/HttpMethodServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/HttpMethodServiceImpl.java
new file mode 100644
index 0000000000..cc1e832ca1
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/HttpMethodServiceImpl.java
@@ -0,0 +1,60 @@
+/*
+ * 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.demo.rest.api.impl;
+
+import org.apache.dubbo.demo.rest.api.HttpMethodService;
+import org.springframework.stereotype.Service;
+
+@Service("httpMethodService")
+public class HttpMethodServiceImpl implements HttpMethodService {
+
+ @Override
+ public String sayHelloPost(String hello) {
+ return hello;
+ }
+
+ @Override
+ public String sayHelloDelete(String hello) {
+ return hello;
+ }
+
+ @Override
+ public String sayHelloHead(String hello) {
+ return hello;
+ }
+
+ @Override
+ public String sayHelloGet(String hello) {
+ return hello;
+ }
+
+ @Override
+ public String sayHelloPut(String hello) {
+ return hello;
+ }
+
+ @Override
+ public String sayHelloPatch(String hello) {
+ return hello;
+ }
+
+ @Override
+ public String sayHelloOptions(String hello) {
+ return hello;
+ }
+}
+
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/HttpRequestAndResponseRPCContextServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/HttpRequestAndResponseRPCContextServiceImpl.java
new file mode 100644
index 0000000000..0aa6d69e6e
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/HttpRequestAndResponseRPCContextServiceImpl.java
@@ -0,0 +1,53 @@
+/*
+ * 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.demo.rest.api.impl;
+
+import org.apache.dubbo.rpc.RpcContext;
+import org.apache.dubbo.rpc.protocol.rest.netty.NettyHttpResponse;
+import org.apache.dubbo.rpc.protocol.rest.request.RequestFacade;
+import org.apache.dubbo.demo.rest.api.HttpRequestAndResponseRPCContextService;
+import org.springframework.stereotype.Service;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+
+@Service("httpRequestAndResponseRPCContextService")
+public class HttpRequestAndResponseRPCContextServiceImpl implements HttpRequestAndResponseRPCContextService {
+ @Override
+ public String httpRequestParam(String hello) {
+ Object request = RpcContext.getServerAttachment().getRequest();
+ return ((RequestFacade) request).getParameter("name");
+ }
+
+ @Override
+ public String httpRequestHeader(String hello) {
+ Object request = RpcContext.getServerAttachment().getRequest();
+ return ((RequestFacade) request).getHeader("header");
+ }
+
+ @Override
+ public List httpResponseHeader(String hello) {
+ Object response = RpcContext.getServerAttachment().getResponse();
+ Map> outputHeaders = ((NettyHttpResponse) response).getOutputHeaders();
+ String responseKey = "response";
+ outputHeaders.put(responseKey, Arrays.asList(hello));
+
+
+ return outputHeaders.get(responseKey);
+ }
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/JaxRsRestDemoServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/JaxRsRestDemoServiceImpl.java
new file mode 100644
index 0000000000..ad177d0940
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/JaxRsRestDemoServiceImpl.java
@@ -0,0 +1,105 @@
+/*
+ * 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.demo.rest.api.impl;
+
+
+import org.apache.dubbo.demo.rest.api.JaxRsRestDemoService;
+import org.springframework.stereotype.Service;
+import po.User;
+
+import javax.ws.rs.core.MultivaluedMap;
+import java.util.List;
+import java.util.Map;
+@Service("jaxRsRestDemoService")
+public class JaxRsRestDemoServiceImpl implements JaxRsRestDemoService {
+
+ @Override
+ public String sayHello(String name) {
+ return "Hello, " + name;
+ }
+
+ @Override
+ public Long testFormBody(Long number) {
+ return number;
+ }
+
+ @Override
+ public User testJavaBeanBody(User user) {
+ return user;
+ }
+
+
+ @Override
+ public int primitiveInt(int a, int b) {
+ return a + b;
+ }
+
+ @Override
+ public long primitiveLong(long a, Long b) {
+ return a + b;
+ }
+
+ @Override
+ public long primitiveByte(byte a, Long b) {
+ return a + b;
+ }
+
+ @Override
+ public long primitiveShort(short a, Long b, int c) {
+ return a + b;
+ }
+
+
+
+ @Override
+ public String testMapParam(Map params) {
+ return params.get("param");
+ }
+
+ @Override
+ public String testMapHeader(Map headers) {
+ return headers.get("header");
+ }
+
+ @Override
+ public List testMapForm(MultivaluedMap params) {
+ return params.get("form");
+ }
+
+ @Override
+ public String header(String header) {
+ return header;
+ }
+
+ @Override
+ public int headerInt(int header) {
+ return header;
+ }
+
+
+ @Override
+ public Integer hello(Integer a, Integer b) {
+ return a + b;
+ }
+
+
+ @Override
+ public String error() {
+ throw new RuntimeException("test error");
+ }
+
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/resources/spring/rest-provider.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/resources/spring/rest-provider.xml
new file mode 100644
index 0000000000..dfbe817895
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/resources/spring/rest-provider.xml
@@ -0,0 +1,59 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/dubbo-demo/dubbo-demo-xml/pom.xml b/dubbo-demo/dubbo-demo-xml/pom.xml
index bdc8ae0e1c..895c0cbf62 100644
--- a/dubbo-demo/dubbo-demo-xml/pom.xml
+++ b/dubbo-demo/dubbo-demo-xml/pom.xml
@@ -38,6 +38,8 @@
dubbo-demo-xml-provider
dubbo-demo-xml-consumer
+ dubbo-demo-jaxrs-rest-consumer
+ dubbo-demo-jaxrs-rest-provider
From ace46c6465b2ac770956bc5272e3e8fd0da47817 Mon Sep 17 00:00:00 2001
From: conghuhu <56248584+conghuhu@users.noreply.github.com>
Date: Tue, 25 Apr 2023 14:49:38 +0800
Subject: [PATCH 37/59] fix: load zipkin after springboot3 (#12133)
---
.../exporter/zipkin/ZipkinAutoConfiguration.java | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinAutoConfiguration.java b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinAutoConfiguration.java
index 1559854138..604e7af5da 100644
--- a/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinAutoConfiguration.java
+++ b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/dubbo-spring-boot-observability-autoconfigure/src/main/java/org/apache/dubbo/spring/boot/observability/autoconfigure/exporter/zipkin/ZipkinAutoConfiguration.java
@@ -44,10 +44,12 @@ import static org.apache.dubbo.spring.boot.observability.autoconfigure.Observabi
* {@link EnableAutoConfiguration Auto-configuration} for Zipkin.
*
* It uses imports on {@link ZipkinConfigurations} to guarantee the correct configuration ordering.
+ * Create Zipkin sender and exporter when you are using Boot < 3.0 or you are not using spring-boot-starter-actuator.
+ * When you use SpringBoot 3.*, priority should be given to loading S3 related configurations. Dubbo related zipkin configurations are invalid.
*
* @since 3.2.1
*/
-@AutoConfiguration(after = RestTemplateAutoConfiguration.class)
+@AutoConfiguration(after = RestTemplateAutoConfiguration.class, afterName = "org.springframework.boot.actuate.autoconfigure.tracing.zipkin")
@ConditionalOnClass(Sender.class)
@Import({SenderConfiguration.class,
ReporterConfiguration.class, BraveConfiguration.class,
From c9f8ae90d9cd1bf51d39f616ebe4b2638ae9ee59 Mon Sep 17 00:00:00 2001
From: namelessssssssssss
<100946116+namelessssssssssss@users.noreply.github.com>
Date: Tue, 25 Apr 2023 19:15:03 +0800
Subject: [PATCH 38/59] Add test for metadata metrics (#12162)
Co-authored-by: songxiaosheng
---
.../MetadataMetricsCollectorTest.java | 78 +++++++++++++++----
1 file changed, 61 insertions(+), 17 deletions(-)
diff --git a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java
index dd2662db64..1496d427e4 100644
--- a/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java
+++ b/dubbo-metrics/dubbo-metrics-metadata/src/test/java/org/apache/dubbo/metrics/metadata/MetadataMetricsCollectorTest.java
@@ -22,14 +22,13 @@ import org.apache.dubbo.metrics.event.MetricsDispatcher;
import org.apache.dubbo.metrics.event.MetricsEventBus;
import org.apache.dubbo.metrics.metadata.collector.MetadataMetricsCollector;
import org.apache.dubbo.metrics.metadata.event.MetadataEvent;
+import org.apache.dubbo.metrics.model.TimePair;
import org.apache.dubbo.metrics.model.key.MetricsKey;
import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
-import org.apache.dubbo.metrics.model.TimePair;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
-
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -37,19 +36,20 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
+import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME;
-import static org.apache.dubbo.metrics.metadata.MetadataMetricsConstants.OP_TYPE_PUSH;
-import static org.apache.dubbo.metrics.metadata.MetadataMetricsConstants.OP_TYPE_STORE_PROVIDER_INTERFACE;
-import static org.apache.dubbo.metrics.metadata.MetadataMetricsConstants.OP_TYPE_SUBSCRIBE;
+import static org.apache.dubbo.metrics.metadata.MetadataMetricsConstants.*;
class MetadataMetricsCollectorTest {
private ApplicationModel applicationModel;
+ private MetadataMetricsCollector collector;
+
@BeforeEach
public void setup() {
FrameworkModel frameworkModel = FrameworkModel.defaultModel();
@@ -58,7 +58,10 @@ class MetadataMetricsCollectorTest {
config.setName("MockMetrics");
applicationModel.getApplicationConfigManager().setApplication(config);
+ applicationModel.getBeanFactory().getOrRegisterBean(MetricsDispatcher.class);
+ collector = applicationModel.getBeanFactory().getOrRegisterBean(MetadataMetricsCollector.class);
+ collector.setCollectEnabled(true);
}
@AfterEach
@@ -68,10 +71,7 @@ class MetadataMetricsCollectorTest {
@Test
void testPushMetrics() {
-
- applicationModel.getBeanFactory().getOrRegisterBean(MetricsDispatcher.class);
- MetadataMetricsCollector collector = applicationModel.getBeanFactory().getOrRegisterBean(MetadataMetricsCollector.class);
- collector.setCollectEnabled(true);
+// MetadataMetricsCollector collector = getCollector();
MetadataEvent pushEvent = MetadataEvent.toPushEvent(applicationModel);
MetricsEventBus.post(pushEvent,
@@ -129,10 +129,7 @@ class MetadataMetricsCollectorTest {
@Test
void testSubscribeMetrics() {
-
- applicationModel.getBeanFactory().getOrRegisterBean(MetricsDispatcher.class);
- MetadataMetricsCollector collector = applicationModel.getBeanFactory().getOrRegisterBean(MetadataMetricsCollector.class);
- collector.setCollectEnabled(true);
+// MetadataMetricsCollector collector = getCollector();
MetadataEvent subscribeEvent = MetadataEvent.toSubscribeEvent(applicationModel);
MetricsEventBus.post(subscribeEvent,
@@ -191,10 +188,7 @@ class MetadataMetricsCollectorTest {
@Test
void testStoreProviderMetadataMetrics() {
-
- applicationModel.getBeanFactory().getOrRegisterBean(MetricsDispatcher.class);
- MetadataMetricsCollector collector = applicationModel.getBeanFactory().getOrRegisterBean(MetadataMetricsCollector.class);
- collector.setCollectEnabled(true);
+// MetadataMetricsCollector collector = getCollector();
String serviceKey = "store.provider.test";
MetadataEvent metadataEvent = MetadataEvent.toServiceSubscribeEvent(applicationModel, serviceKey);
@@ -252,4 +246,54 @@ class MetadataMetricsCollectorTest {
Assertions.assertEquals(sampleMap.get(new MetricsKeyWrapper(MetricsKey.METRIC_RT_SUM, OP_TYPE_STORE_PROVIDER_INTERFACE).targetKey()), c1 + c2);
}
+ @Test
+ void testMetadataPushNum() {
+
+ for (int i = 0; i < 10; i++) {
+ MetadataEvent event = MetadataEvent.toPushEvent(applicationModel);
+ if(i %2 ==0 ) {
+ MetricsEventBus.post(event,() -> true, r -> r);
+ }else {
+ MetricsEventBus.post(event,() -> false, r -> r);
+ }
+ }
+
+ List samples = collector.collect();
+
+ GaugeMetricSample> totalNum = getSample(MetricsKey.METADATA_PUSH_METRIC_NUM.getName(), samples);
+ GaugeMetricSample> succeedNum = getSample(MetricsKey.METADATA_PUSH_METRIC_NUM_SUCCEED.getName(), samples);
+ GaugeMetricSample> failedNum = getSample(MetricsKey.METADATA_PUSH_METRIC_NUM_FAILED.getName(), samples);
+
+ Assertions.assertEquals(10,totalNum.applyAsLong());
+ Assertions.assertEquals(5,succeedNum.applyAsLong());
+ Assertions.assertEquals(5,failedNum.applyAsLong());
+ }
+
+ @Test
+ void testSubscribeSum(){
+
+ for (int i = 0; i < 10; i++) {
+ MetadataEvent event = MetadataEvent.toSubscribeEvent(applicationModel);
+ if(i %2 ==0 ) {
+ MetricsEventBus.post(event,() -> true, r -> r);
+ }else {
+ MetricsEventBus.post(event,() -> false, r -> r);
+ }
+ }
+
+ List samples = collector.collect();
+
+ GaugeMetricSample> totalNum = getSample(MetricsKey.METADATA_SUBSCRIBE_METRIC_NUM.getName(), samples);
+ GaugeMetricSample> succeedNum = getSample(MetricsKey.METADATA_SUBSCRIBE_METRIC_NUM_SUCCEED.getName(), samples);
+ GaugeMetricSample> failedNum = getSample(MetricsKey.METADATA_SUBSCRIBE_METRIC_NUM_FAILED.getName(), samples);
+
+ Assertions.assertEquals(10,totalNum.applyAsLong());
+ Assertions.assertEquals(5,succeedNum.applyAsLong());
+ Assertions.assertEquals(5,failedNum.applyAsLong());
+ }
+
+ GaugeMetricSample> getSample(String name, List samples) {
+ return (GaugeMetricSample>) samples.stream().filter(metricSample -> metricSample.getName().equals(name)).findFirst().orElseThrow(NoSuchElementException::new);
+ }
+
}
From 335733b4fc82ab7168f3286d4e15c92eccdce601 Mon Sep 17 00:00:00 2001
From: Albumen Kevin
Date: Tue, 25 Apr 2023 20:51:26 +0800
Subject: [PATCH 39/59] Disable Aggregation by default (#12184)
* Disable Aggregation by default
* Fix uts
---
.../collector/AggregateMetricsCollector.java | 2 +-
.../AggregateMetricsCollectorTest.java | 17 ++++++++++-------
2 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
index fad110a90a..dc2ce7bb5f 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
@@ -66,7 +66,7 @@ public class AggregateMetricsCollector implements MetricsCollector, MetricsListe
this.applicationModel = applicationModel;
ConfigManager configManager = applicationModel.getApplicationConfigManager();
MetricsConfig config = configManager.getMetrics().orElse(null);
- if (config != null && config.getAggregation() != null && (config.getAggregation().getEnabled() == null || Boolean.TRUE.equals(config.getAggregation().getEnabled()))) {
+ if (config != null && config.getAggregation() != null && (Boolean.TRUE.equals(config.getAggregation().getEnabled()))) {
// only registered when aggregation is enabled.
registerListener();
diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
index d4fbd1e571..fe1a8c4a06 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollectorTest.java
@@ -22,9 +22,7 @@ import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.utils.ReflectionUtils;
import org.apache.dubbo.config.ApplicationConfig;
-
import org.apache.dubbo.config.MetricsConfig;
-
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.config.nested.AggregationConfig;
import org.apache.dubbo.metrics.TestMetricsInvoker;
@@ -39,27 +37,30 @@ import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.model.ApplicationModel;
+
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
-import java.util.Collections;
import java.util.concurrent.ConcurrentHashMap;
-
import java.util.stream.Collectors;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
-import static org.apache.dubbo.common.constants.MetricsConstants.*;
+import static org.apache.dubbo.common.constants.MetricsConstants.TAG_GROUP_KEY;
+import static org.apache.dubbo.common.constants.MetricsConstants.TAG_INTERFACE_KEY;
+import static org.apache.dubbo.common.constants.MetricsConstants.TAG_METHOD_KEY;
+import static org.apache.dubbo.common.constants.MetricsConstants.TAG_VERSION_KEY;
import static org.apache.dubbo.metrics.model.MetricsCategory.QPS;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
class AggregateMetricsCollectorTest {
@@ -93,7 +94,9 @@ class AggregateMetricsCollectorTest {
configManager.setMetrics(metricsConfig);
- when(metricsConfig.getAggregation()).thenReturn(new AggregationConfig());
+ AggregationConfig aggregationConfig = spy(new AggregationConfig());
+ when(aggregationConfig.getEnabled()).thenReturn(true);
+ when(metricsConfig.getAggregation()).thenReturn(aggregationConfig);
when(applicationModel.getApplicationConfigManager()).thenReturn(configManager);
ScopeBeanFactory beanFactory = mock(ScopeBeanFactory.class);
From 4f86a3bbfc87745a532803ed6a970f4a9f639ae2 Mon Sep 17 00:00:00 2001
From: suncairong163 <105478245+suncairong163@users.noreply.github.com>
Date: Wed, 26 Apr 2023 09:56:11 +0800
Subject: [PATCH 40/59] add spring mvc rest demo (#12188)
* add spring mvc rest demo
* FIX VERSION
---
.../dubbo-demo-jaxrs-rest-consumer/pom.xml | 2 +-
.../dubbo/demo/rest/api/RestConsumer.java | 7 +
...DubboServiceAnnotationServiceConsumer.java | 33 ++++
.../dubbo-demo-jaxrs-rest-provider/pom.xml | 4 +-
.../main/resources/spring/rest-provider.xml | 1 -
.../pom.xml | 154 ++++++++++++++++++
.../demo/rest/api/SpringMvcRestConsumer.java | 74 +++++++++
.../demo/rest/api/config/DubboConfig.java | 25 +++
.../SpringRestDemoServiceConsumer.java | 29 ++++
.../main/resources/spring/rest-consumer.xml | 37 +++++
.../pom.xml | 153 +++++++++++++++++
.../demo/rest/api/SpringMvcRestProvider.java} | 23 ++-
.../demo/rest/api/config/DubboConfig.java | 23 +++
.../api/impl/SpringRestDemoServiceImpl.java | 101 ++++++++++++
.../main/resources/spring/rest-provider.xml | 38 +++++
dubbo-demo/dubbo-demo-xml/pom.xml | 2 +
16 files changed, 693 insertions(+), 13 deletions(-)
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/annotation/DubboServiceAnnotationServiceConsumer.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/pom.xml
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringMvcRestConsumer.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/consumer/SpringRestDemoServiceConsumer.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/resources/spring/rest-consumer.xml
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/pom.xml
rename dubbo-demo/dubbo-demo-xml/{dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringControllerService.java => dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/SpringMvcRestProvider.java} (66%)
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/SpringRestDemoServiceImpl.java
create mode 100644 dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/resources/spring/rest-provider.xml
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/pom.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/pom.xml
index f004076824..78fc871305 100644
--- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/pom.xml
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/pom.xml
@@ -29,7 +29,7 @@
dubbo-demo-jaxrs-rest-consumer
jar
- Dubbo Rest Demo
+ Dubbo JAXRS Rest Consumer Demo
${project.artifactId}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/RestConsumer.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/RestConsumer.java
index b90d221f3c..1977f19efa 100644
--- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/RestConsumer.java
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/RestConsumer.java
@@ -16,6 +16,7 @@
*/
package org.apache.dubbo.demo.rest.api;
+import org.apache.dubbo.demo.rest.api.annotation.DubboServiceAnnotationServiceConsumer;
import org.jboss.resteasy.specimpl.MultivaluedMapImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import po.User;
@@ -36,9 +37,15 @@ public class RestConsumer {
testHttpMethodService(context);
httpRPCContextTest(context);
jaxRsRestDemoServiceTest(context);
+ annotationTest(context);
System.out.println("rest consumer test success");
}
+ private static void annotationTest(ClassPathXmlApplicationContext context) {
+ DubboServiceAnnotationServiceConsumer bean = context.getBean(DubboServiceAnnotationServiceConsumer.class);
+ bean.invokeAnnotationService();
+ }
+
private static void jaxRsRestDemoServiceTest(ClassPathXmlApplicationContext context) {
JaxRsRestDemoService jaxRsRestDemoService = context.getBean("jaxRsRestDemoService", JaxRsRestDemoService.class);
String hello = jaxRsRestDemoService.sayHello("hello");
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/annotation/DubboServiceAnnotationServiceConsumer.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/annotation/DubboServiceAnnotationServiceConsumer.java
new file mode 100644
index 0000000000..bda5281231
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/annotation/DubboServiceAnnotationServiceConsumer.java
@@ -0,0 +1,33 @@
+/*
+ * 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.demo.rest.api.annotation;
+
+import org.apache.dubbo.config.annotation.DubboReference;
+import org.apache.dubbo.demo.rest.api.DubboServiceAnnotationService;
+import org.springframework.stereotype.Component;
+
+@Component
+public class DubboServiceAnnotationServiceConsumer {
+
+ @DubboReference(interfaceClass = DubboServiceAnnotationService.class)
+ DubboServiceAnnotationService dubboServiceAnnotationService;
+
+ public void invokeAnnotationService() {
+ String annotation = dubboServiceAnnotationService.annotation();
+ System.out.println(annotation);
+ }
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/pom.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/pom.xml
index f15f8b3c92..9989d45b7a 100644
--- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/pom.xml
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/pom.xml
@@ -28,8 +28,8 @@
dubbo-demo-jaxrs-rest-provider
- war
- Dubbo Rest Demo
+ jar
+ Dubbo JAXRS Rest Provider Demo
${project.artifactId}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/resources/spring/rest-provider.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/resources/spring/rest-provider.xml
index dfbe817895..f0471d1dc7 100644
--- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/resources/spring/rest-provider.xml
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-provider/src/main/resources/spring/rest-provider.xml
@@ -33,7 +33,6 @@
-
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/pom.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/pom.xml
new file mode 100644
index 0000000000..63c51491c7
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/pom.xml
@@ -0,0 +1,154 @@
+
+
+
+
+ dubbo-demo-xml
+ org.apache.dubbo
+ ${revision}
+ ../pom.xml
+
+ 4.0.0
+
+ dubbo-demo-spring-mvc-rest-consumer
+
+ jar
+ Dubbo Spring MVC Rest Consumer Demo
+ ${project.artifactId}
+
+
+ true
+ 1.7.33
+
+
+
+ org.springframework
+ spring-context
+
+
+ org.springframework
+ spring-web
+
+
+
+ org.apache.dubbo
+ dubbo-registry-multicast
+
+
+ org.apache.dubbo
+ dubbo-registry-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-registry-nacos
+
+
+ com.alibaba.nacos
+ nacos-client
+
+
+ org.apache.dubbo
+ dubbo-configcenter-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-configcenter-nacos
+
+
+ org.apache.dubbo
+ dubbo-metadata-report-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-metadata-report-nacos
+
+
+ org.apache.dubbo
+ dubbo-rpc-dubbo
+
+
+ org.apache.dubbo
+ dubbo-rpc-rest
+
+
+ org.apache.dubbo
+ dubbo-config-spring
+
+
+ org.apache.dubbo
+ dubbo-remoting-netty4
+
+
+ org.apache.dubbo
+ dubbo-serialization-hessian2
+
+
+ org.apache.dubbo
+ dubbo-serialization-fastjson2
+
+
+ org.apache.dubbo
+ dubbo-serialization-jdk
+
+
+ org.slf4j
+ slf4j-api
+
+
+ org.slf4j
+ slf4j-log4j12
+ ${slf4j-log4j12.version}
+
+
+ log4j
+ log4j
+
+
+
+ org.springframework
+ spring-test
+ test
+
+
+
+ org.apache.dubbo
+ dubbo-demo-interface
+ ${project.version}
+
+
+
+
+
+
+ javax.annotation
+
+ [1.11,)
+
+
+
+ javax.annotation
+ javax.annotation-api
+ 1.3.2
+
+
+
+
+
+
+
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringMvcRestConsumer.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringMvcRestConsumer.java
new file mode 100644
index 0000000000..43e1dc5854
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringMvcRestConsumer.java
@@ -0,0 +1,74 @@
+/*
+ * 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.demo.rest.api;
+
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import po.User;
+
+import java.util.Arrays;
+
+public class SpringMvcRestConsumer {
+
+ public static void main(String[] args) {
+ consumerService();
+ }
+
+ public static void consumerService() {
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/rest-consumer.xml"});
+ context.start();
+ System.out.println("spring mvc rest consumer start");
+ springMvcRestDemoServiceTest(context);
+ System.out.println("spring mvc rest consumer test success");
+ }
+
+ private static void springMvcRestDemoServiceTest(ClassPathXmlApplicationContext context) {
+ SpringRestDemoService springRestDemoService = context.getBean("springRestDemoService", SpringRestDemoService.class);
+ String hello = springRestDemoService.sayHello("hello");
+ assertEquals("Hello, hello", hello);
+ Integer result = springRestDemoService.primitiveInt(1, 2);
+ Long resultLong = springRestDemoService.primitiveLong(1, 2l);
+ long resultByte = springRestDemoService.primitiveByte((byte) 1, 2l);
+ long resultShort = springRestDemoService.primitiveShort((short) 1, 2l, 1);
+
+ assertEquals(result, 3);
+ assertEquals(resultShort, 3l);
+ assertEquals(resultLong, 3l);
+ assertEquals(resultByte, 3l);
+
+ assertEquals(Long.valueOf(1l), springRestDemoService.testFormBody(1l));
+
+ MultiValueMap forms = new LinkedMultiValueMap<>();
+ forms.put("form", Arrays.asList("F1"));
+
+ assertEquals(Arrays.asList("F1"), springRestDemoService.testMapForm(forms));
+ assertEquals(User.getInstance(), springRestDemoService.testJavaBeanBody(User.getInstance()));
+ }
+
+
+ private static void assertEquals(Object returnStr, Object exception) {
+ boolean equal = returnStr != null && returnStr.equals(exception);
+
+ if (equal) {
+ return;
+ } else {
+ throw new RuntimeException();
+ }
+ }
+
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
new file mode 100644
index 0000000000..9710c76921
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
@@ -0,0 +1,25 @@
+/*
+ * 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.demo.rest.api.config;
+
+import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
+
+@DubboComponentScan("org.apache.dubbo.demo.rest")
+public class DubboConfig {
+
+
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/consumer/SpringRestDemoServiceConsumer.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/consumer/SpringRestDemoServiceConsumer.java
new file mode 100644
index 0000000000..180947cd2d
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/consumer/SpringRestDemoServiceConsumer.java
@@ -0,0 +1,29 @@
+/*
+ * 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.demo.rest.api.consumer;
+
+import org.apache.dubbo.config.annotation.DubboReference;
+import org.apache.dubbo.demo.rest.api.SpringRestDemoService;
+import org.springframework.stereotype.Component;
+
+@Component
+public class SpringRestDemoServiceConsumer {
+ @DubboReference(interfaceClass = SpringRestDemoService.class )
+ SpringRestDemoService springRestDemoService;
+
+
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/resources/spring/rest-consumer.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/resources/spring/rest-consumer.xml
new file mode 100644
index 0000000000..b028879160
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-consumer/src/main/resources/spring/rest-consumer.xml
@@ -0,0 +1,37 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/pom.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/pom.xml
new file mode 100644
index 0000000000..8c042046e2
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/pom.xml
@@ -0,0 +1,153 @@
+
+
+
+
+ dubbo-demo-xml
+ org.apache.dubbo
+ ${revision}
+ ../pom.xml
+
+ 4.0.0
+
+ dubbo-demo-spring-mvc-rest-provider
+
+ jar
+ Dubbo Spring MVC Rest Provider Demo
+ ${project.artifactId}
+
+
+ true
+ 1.7.33
+
+
+
+ org.springframework
+ spring-context
+
+
+ org.springframework
+ spring-web
+
+
+
+ org.apache.dubbo
+ dubbo-registry-multicast
+
+
+ org.apache.dubbo
+ dubbo-registry-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-registry-nacos
+
+
+ com.alibaba.nacos
+ nacos-client
+
+
+ org.apache.dubbo
+ dubbo-configcenter-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-configcenter-nacos
+
+
+ org.apache.dubbo
+ dubbo-metadata-report-zookeeper
+
+
+ org.apache.dubbo
+ dubbo-metadata-report-nacos
+
+
+ org.apache.dubbo
+ dubbo-rpc-dubbo
+
+
+ org.apache.dubbo
+ dubbo-rpc-rest
+
+
+ org.apache.dubbo
+ dubbo-config-spring
+
+
+ org.apache.dubbo
+ dubbo-remoting-netty4
+
+
+ org.apache.dubbo
+ dubbo-serialization-hessian2
+
+
+ org.apache.dubbo
+ dubbo-serialization-fastjson2
+
+
+ org.apache.dubbo
+ dubbo-serialization-jdk
+
+
+ org.slf4j
+ slf4j-api
+
+
+ org.slf4j
+ slf4j-log4j12
+ ${slf4j-log4j12.version}
+
+
+ log4j
+ log4j
+
+
+
+ org.springframework
+ spring-test
+ test
+
+
+
+ org.apache.dubbo
+ dubbo-demo-interface
+ ${project.version}
+
+
+
+
+
+
+ javax.annotation
+
+ [1.11,)
+
+
+
+ javax.annotation
+ javax.annotation-api
+ 1.3.2
+
+
+
+
+
+
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringControllerService.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/SpringMvcRestProvider.java
similarity index 66%
rename from dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringControllerService.java
rename to dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/SpringMvcRestProvider.java
index 58841def65..70b2fe9a41 100644
--- a/dubbo-demo/dubbo-demo-xml/dubbo-demo-jaxrs-rest-consumer/src/main/java/org/apache/dubbo/demo/rest/api/SpringControllerService.java
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/SpringMvcRestProvider.java
@@ -16,16 +16,21 @@
*/
package org.apache.dubbo.demo.rest.api;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
-@RestController
-@RequestMapping("/controller")
-public class SpringControllerService {
- @GetMapping("/sayHello")
- public String sayHello(String hello) {
- return hello;
+public class SpringMvcRestProvider {
+
+ public static void main(String[] args) throws Exception {
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"spring/rest-provider.xml"});
+
+ context.refresh();
+
+
+ System.out.println("spring mvc rest provider started");
+
+ System.in.read();
}
+
+
}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
new file mode 100644
index 0000000000..a5d0edfacf
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/config/DubboConfig.java
@@ -0,0 +1,23 @@
+/*
+ * 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.demo.rest.api.config;
+
+import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
+
+@DubboComponentScan("org.apache.dubbo.demo.rest")
+public class DubboConfig {
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/SpringRestDemoServiceImpl.java b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/SpringRestDemoServiceImpl.java
new file mode 100644
index 0000000000..2798e89aa3
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/java/org/apache/dubbo/demo/rest/api/impl/SpringRestDemoServiceImpl.java
@@ -0,0 +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.demo.rest.api.impl;
+
+
+import org.apache.dubbo.config.annotation.DubboService;
+import org.apache.dubbo.demo.rest.api.SpringRestDemoService;
+import org.springframework.util.MultiValueMap;
+import po.User;
+
+import java.util.List;
+import java.util.Map;
+
+@DubboService(interfaceClass = SpringRestDemoService.class ,protocol = "rest")
+public class SpringRestDemoServiceImpl implements SpringRestDemoService {
+
+ @Override
+ public String sayHello(String name) {
+ return "Hello, " + name;
+ }
+
+ @Override
+ public Long testFormBody(Long number) {
+ return number;
+ }
+
+ @Override
+ public User testJavaBeanBody(User user) {
+ return user;
+ }
+
+
+ @Override
+ public int primitiveInt(int a, int b) {
+ return a + b;
+ }
+
+ @Override
+ public long primitiveLong(long a, Long b) {
+ return a + b;
+ }
+
+ @Override
+ public long primitiveByte(byte a, Long b) {
+ return a + b;
+ }
+
+ @Override
+ public long primitiveShort(short a, Long b, int c) {
+ return a + b;
+ }
+
+
+ @Override
+ public String testMapParam(Map params) {
+ return params.get("param");
+ }
+
+ @Override
+ public String testMapHeader(Map headers) {
+ return headers.get("header");
+ }
+
+ @Override
+ public List testMapForm(MultiValueMap params) {
+ return params.get("form");
+ }
+
+
+ @Override
+ public int headerInt(int header) {
+ return header;
+ }
+
+
+ @Override
+ public Integer hello(Integer a, Integer b) {
+ return a + b;
+ }
+
+
+ @Override
+ public String error() {
+ throw new RuntimeException("test error");
+ }
+
+}
diff --git a/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/resources/spring/rest-provider.xml b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/resources/spring/rest-provider.xml
new file mode 100644
index 0000000000..8c6be976f7
--- /dev/null
+++ b/dubbo-demo/dubbo-demo-xml/dubbo-demo-spring-mvc-rest-provider/src/main/resources/spring/rest-provider.xml
@@ -0,0 +1,38 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/dubbo-demo/dubbo-demo-xml/pom.xml b/dubbo-demo/dubbo-demo-xml/pom.xml
index 895c0cbf62..0514421d5b 100644
--- a/dubbo-demo/dubbo-demo-xml/pom.xml
+++ b/dubbo-demo/dubbo-demo-xml/pom.xml
@@ -40,6 +40,8 @@
dubbo-demo-xml-consumer
dubbo-demo-jaxrs-rest-consumer
dubbo-demo-jaxrs-rest-provider
+ dubbo-demo-spring-mvc-rest-consumer
+ dubbo-demo-spring-mvc-rest-provider
From 69bd2639f55976e3a248e08d40af2de327f4ead5 Mon Sep 17 00:00:00 2001
From: jojocodeX <571943037@qq.com>
Date: Thu, 27 Apr 2023 11:51:19 +0800
Subject: [PATCH 41/59] Spring-security codec ignore error (#12192)
---
.../ContextHolderAuthenticationPrepareFilter.java | 9 ++++++++-
.../ContextHolderAuthenticationResolverFilter.java | 6 ++++++
.../spring/security/jackson/ObjectMapperCodec.java | 14 ++++++++++----
3 files changed, 24 insertions(+), 5 deletions(-)
diff --git a/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/filter/ContextHolderAuthenticationPrepareFilter.java b/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/filter/ContextHolderAuthenticationPrepareFilter.java
index 5d70dc9858..e2a8fe0bc6 100644
--- a/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/filter/ContextHolderAuthenticationPrepareFilter.java
+++ b/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/filter/ContextHolderAuthenticationPrepareFilter.java
@@ -18,6 +18,7 @@ package org.apache.dubbo.spring.security.filter;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
@@ -52,6 +53,12 @@ public class ContextHolderAuthenticationPrepareFilter implements ClusterFilter{
Authentication authentication = context.getAuthentication();
- invocation.setObjectAttachment(SecurityNames.SECURITY_AUTHENTICATION_CONTEXT_KEY, mapper.serialize(authentication));
+ String content = mapper.serialize(authentication);
+
+ if (StringUtils.isBlank(content)) {
+ return;
+ }
+
+ invocation.setObjectAttachment(SecurityNames.SECURITY_AUTHENTICATION_CONTEXT_KEY, content);
}
}
diff --git a/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/filter/ContextHolderAuthenticationResolverFilter.java b/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/filter/ContextHolderAuthenticationResolverFilter.java
index 092cfe018f..acd5026409 100644
--- a/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/filter/ContextHolderAuthenticationResolverFilter.java
+++ b/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/filter/ContextHolderAuthenticationResolverFilter.java
@@ -53,7 +53,13 @@ public class ContextHolderAuthenticationResolverFilter implements Filter {
if (StringUtils.isBlank(authenticationJSON)) {
return;
}
+
Authentication authentication = mapper.deserialize(authenticationJSON, Authentication.class);
+
+ if (authentication == null) {
+ return;
+ }
+
SecurityContextHolder.getContext().setAuthentication(authentication);
}
diff --git a/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/jackson/ObjectMapperCodec.java b/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/jackson/ObjectMapperCodec.java
index a97e520d65..97d1048bba 100644
--- a/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/jackson/ObjectMapperCodec.java
+++ b/dubbo-plugin/dubbo-spring-security/src/main/java/org/apache/dubbo/spring/security/jackson/ObjectMapperCodec.java
@@ -20,6 +20,9 @@ package org.apache.dubbo.spring.security.jackson;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
+import org.apache.dubbo.common.constants.LoggerCodeConstants;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.springframework.security.jackson2.CoreJackson2Module;
@@ -30,6 +33,8 @@ import java.util.function.Consumer;
public class ObjectMapperCodec {
+ private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(ObjectMapperCodec.class);
+
private final ObjectMapper mapper = new ObjectMapper();
public ObjectMapperCodec() {
@@ -38,7 +43,6 @@ public class ObjectMapperCodec {
public T deserialize(byte[] bytes, Class clazz) {
try {
-
if (bytes == null || bytes.length == 0) {
return null;
}
@@ -46,9 +50,9 @@ public class ObjectMapperCodec {
return mapper.readValue(bytes, clazz);
} catch (Exception exception) {
- throw new RuntimeException(
- String.format("objectMapper! deserialize error %s", exception));
+ logger.warn(LoggerCodeConstants.COMMON_JSON_CONVERT_EXCEPTION, "objectMapper! deserialize error, you can try to customize the ObjectMapperCodecCustomer.","","", exception);
}
+ return null;
}
public T deserialize(String content, Class clazz) {
@@ -68,8 +72,10 @@ public class ObjectMapperCodec {
return mapper.writeValueAsString(object);
} catch (Exception ex) {
- throw new RuntimeException(String.format("objectMapper! serialize error %s", ex));
+ logger.warn(LoggerCodeConstants.COMMON_JSON_CONVERT_EXCEPTION, "objectMapper! serialize error, you can try to customize the ObjectMapperCodecCustomer.","","", ex);
+
}
+ return null;
}
public ObjectMapperCodec addModule(SimpleModule simpleModule) {
From 83240f5083833f502070d2fc2aa0cf4d0b04a1e4 Mon Sep 17 00:00:00 2001
From: huazhongming
Date: Thu, 27 Apr 2023 11:51:39 +0800
Subject: [PATCH 42/59] Fix npe occurs during client graceful offline. (#12190)
Signed-off-by: crazyhzm
---
.../dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java
index 90b32cdf87..ef3b89bb31 100644
--- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java
+++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DecodeableRpcResult.java
@@ -124,7 +124,14 @@ public class DecodeableRpcResult extends AppResponse implements Codec, Decodeabl
if (!hasDecoded && channel != null && inputStream != null) {
try {
if (invocation != null) {
- Configuration systemConfiguration = ConfigurationUtils.getSystemConfiguration(channel.getUrl().getScopeModel());
+ Configuration systemConfiguration = null;
+ try {
+ systemConfiguration = ConfigurationUtils.getSystemConfiguration(channel.getUrl().getScopeModel());
+ } catch (Exception e) {
+ // Because the Environment may be destroyed during the offline process, the configuration cannot be obtained.
+ // Exceptions are ignored here, and normal decoding is guaranteed.
+ }
+
if (systemConfiguration == null || systemConfiguration.getBoolean(SERIALIZATION_SECURITY_CHECK_KEY, true)) {
Object serializationTypeObj = invocation.get(SERIALIZATION_ID_KEY);
if (serializationTypeObj != null) {
From fa8e09201f6bb8bdd7f52d1b833c35294e1a6871 Mon Sep 17 00:00:00 2001
From: TomlongTK <1120170646@qq.com>
Date: Fri, 28 Apr 2023 10:55:01 +0800
Subject: [PATCH 43/59] packable method extension (#12199)
* packable method extension
* support content-type
* packable method extension
* support content-type
* get packable factory type from configuration
* Add new SPI to dubbo-all(dubbo-distribution/dubbo-all/pom.xml in shade plugin) to being transformed
* At present, Use CONTENT_PROTO as content-type, while support customize content-type, need fix it
* remove unused import class
---------
Co-authored-by: longqiang02
---
.../common/constants/CommonConstants.java | 5 +++
.../java/org/apache/dubbo/rpc/model/Pack.java | 29 ++++++++++++
.../dubbo/rpc/model/PackableMethod.java | 45 +++----------------
.../rpc/model/PackableMethodFactory.java | 29 ++++++++++++
.../org/apache/dubbo/rpc/model/UnPack.java | 29 ++++++++++++
.../apache/dubbo/rpc/model/WrapperUnPack.java | 28 ++++++++++++
dubbo-distribution/dubbo-all/pom.xml | 6 +++
.../integration/RegistryProtocol.java | 3 +-
.../tri/DefaultPackableMethodFactory.java | 32 +++++++++++++
.../dubbo/rpc/protocol/tri/PbArrayPacker.java | 41 +++++++++++++++++
.../dubbo/rpc/protocol/tri/PbUnpack.java | 4 +-
.../tri/ReflectionPackableMethod.java | 22 ++-------
.../dubbo/rpc/protocol/tri/TripleInvoker.java | 7 ++-
.../protocol/tri/call/AbstractServerCall.java | 4 +-
.../call/ReflectionAbstractServerCall.java | 23 +++++++---
.../tri/call/StubAbstractServerCall.java | 3 +-
...ache.dubbo.rpc.model.PackableMethodFactory | 1 +
.../protocol/tri/call/StubServerCallTest.java | 3 +-
18 files changed, 238 insertions(+), 76 deletions(-)
create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/rpc/model/Pack.java
create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/rpc/model/PackableMethodFactory.java
create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/rpc/model/UnPack.java
create mode 100644 dubbo-common/src/main/java/org/apache/dubbo/rpc/model/WrapperUnPack.java
create mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/DefaultPackableMethodFactory.java
create mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/PbArrayPacker.java
create mode 100644 dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.model.PackableMethodFactory
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
index 625ddfb2f8..f066044043 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
@@ -628,4 +628,9 @@ public interface CommonConstants {
String DUBBO_METRICS_CONFIGCENTER_ENABLE = "dubbo.metrics.configcenter.enable";
Integer TRI_EXCEPTION_CODE_NOT_EXISTS = 0;
+
+ String PACKABLE_METHOD_FACTORY_KEY = "serialize.packable.factory";
+
+ String DUBBO_PACKABLE_METHOD_FACTORY = "dubbo.application.parameters." + PACKABLE_METHOD_FACTORY_KEY;
+
}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/Pack.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/Pack.java
new file mode 100644
index 0000000000..50f46c5f8b
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/Pack.java
@@ -0,0 +1,29 @@
+/*
+ * 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.rpc.model;
+
+public interface Pack {
+
+ /**
+ * @param obj instance
+ * @return byte array
+ * @throws Exception when error occurs
+ */
+ byte[] pack(Object obj) throws Exception;
+
+}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/PackableMethod.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/PackableMethod.java
index f21cc4d690..a9707bc073 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/PackableMethod.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/PackableMethod.java
@@ -17,56 +17,21 @@
package org.apache.dubbo.rpc.model;
-import java.io.IOException;
-
/**
* A packable method is used to customize serialization for methods. It can provide a common wrapper
* for RESP / Protobuf.
*/
public interface PackableMethod {
- interface Pack {
-
- /**
- * @param obj instance
- * @return byte array
- * @throws IOException when error occurs
- */
- byte[] pack(Object obj) throws IOException;
- }
-
- interface WrapperUnPack extends UnPack {
-
- default Object unpack(byte[] data) throws IOException, ClassNotFoundException {
- return unpack(data, false);
- }
-
- Object unpack(byte[] data, boolean isReturnTriException) throws IOException, ClassNotFoundException;
-
-
- }
-
- interface UnPack {
-
- /**
- * @param data byte array
- * @return object instance
- * @throws IOException IOException
- * @throws ClassNotFoundException when no class found
- */
- Object unpack(byte[] data) throws IOException, ClassNotFoundException;
-
- }
-
- default Object parseRequest(byte[] data) throws IOException, ClassNotFoundException {
+ default Object parseRequest(byte[] data) throws Exception {
return getRequestUnpack().unpack(data);
}
- default Object parseResponse(byte[] data) throws IOException, ClassNotFoundException {
+ default Object parseResponse(byte[] data) throws Exception {
return parseResponse(data, false);
}
- default Object parseResponse(byte[] data, boolean isReturnTriException) throws IOException, ClassNotFoundException {
+ default Object parseResponse(byte[] data, boolean isReturnTriException) throws Exception {
UnPack unPack = getResponseUnpack();
if (unPack instanceof WrapperUnPack) {
return ((WrapperUnPack) unPack).unpack(data, isReturnTriException);
@@ -74,11 +39,11 @@ public interface PackableMethod {
return unPack.unpack(data);
}
- default byte[] packRequest(Object request) throws IOException {
+ default byte[] packRequest(Object request) throws Exception {
return getRequestPack().pack(request);
}
- default byte[] packResponse(Object response) throws IOException {
+ default byte[] packResponse(Object response) throws Exception {
return getResponsePack().pack(response);
}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/PackableMethodFactory.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/PackableMethodFactory.java
new file mode 100644
index 0000000000..c3961a78c5
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/PackableMethodFactory.java
@@ -0,0 +1,29 @@
+/*
+ * 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.rpc.model;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.extension.ExtensionScope;
+import org.apache.dubbo.common.extension.SPI;
+
+@SPI(scope = ExtensionScope.FRAMEWORK)
+public interface PackableMethodFactory {
+
+ PackableMethod create(MethodDescriptor methodDescriptor, URL url, String contentType);
+
+}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/UnPack.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/UnPack.java
new file mode 100644
index 0000000000..b214e50ce0
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/UnPack.java
@@ -0,0 +1,29 @@
+/*
+ * 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.rpc.model;
+
+public interface UnPack {
+
+ /**
+ * @param data byte array
+ * @return object instance
+ * @throws Exception exception
+ */
+ Object unpack(byte[] data) throws Exception;
+
+}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/WrapperUnPack.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/WrapperUnPack.java
new file mode 100644
index 0000000000..444309689e
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/WrapperUnPack.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.rpc.model;
+
+public interface WrapperUnPack extends UnPack {
+
+ default Object unpack(byte[] data) throws Exception {
+ return unpack(data, false);
+ }
+
+ Object unpack(byte[] data, boolean isReturnTriException) throws Exception;
+
+}
diff --git a/dubbo-distribution/dubbo-all/pom.xml b/dubbo-distribution/dubbo-all/pom.xml
index 66c0103bf6..3951a9fd9d 100644
--- a/dubbo-distribution/dubbo-all/pom.xml
+++ b/dubbo-distribution/dubbo-all/pom.xml
@@ -1323,6 +1323,12 @@
+
+
+ META-INF/dubbo/internal/org.apache.dubbo.rpc.model.PackableMethodFactory
+
+
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java
index c19cae36d3..da87f87318 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java
@@ -86,6 +86,7 @@ import static org.apache.dubbo.common.constants.CommonConstants.IPV6_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.LOADBALANCE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.MONITOR_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.PACKABLE_METHOD_FACTORY_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PATH_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.PROTOCOL_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.REGISTRY_PROTOCOL_LISTENER_KEY;
@@ -144,7 +145,7 @@ public class RegistryProtocol implements Protocol, ScopeModelAware {
public static final String[] DEFAULT_REGISTER_PROVIDER_KEYS = {
APPLICATION_KEY, CODEC_KEY, EXCHANGER_KEY, SERIALIZATION_KEY, PREFER_SERIALIZATION_KEY, CLUSTER_KEY, CONNECTIONS_KEY, DEPRECATED_KEY,
GROUP_KEY, LOADBALANCE_KEY, MOCK_KEY, PATH_KEY, TIMEOUT_KEY, TOKEN_KEY, VERSION_KEY, WARMUP_KEY,
- WEIGHT_KEY, DUBBO_VERSION_KEY, RELEASE_KEY, SIDE_KEY, IPV6_KEY
+ WEIGHT_KEY, DUBBO_VERSION_KEY, RELEASE_KEY, SIDE_KEY, IPV6_KEY, PACKABLE_METHOD_FACTORY_KEY
};
public static final String[] DEFAULT_REGISTER_CONSUMER_KEYS = {
diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/DefaultPackableMethodFactory.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/DefaultPackableMethodFactory.java
new file mode 100644
index 0000000000..36fb31f736
--- /dev/null
+++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/DefaultPackableMethodFactory.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dubbo.rpc.protocol.tri;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.rpc.model.MethodDescriptor;
+import org.apache.dubbo.rpc.model.PackableMethod;
+import org.apache.dubbo.rpc.model.PackableMethodFactory;
+
+public class DefaultPackableMethodFactory implements PackableMethodFactory {
+
+ @Override
+ public PackableMethod create(MethodDescriptor methodDescriptor, URL url, String contentType) {
+ return ReflectionPackableMethod.init(methodDescriptor, url);
+ }
+
+}
diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/PbArrayPacker.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/PbArrayPacker.java
new file mode 100644
index 0000000000..9a20d29f40
--- /dev/null
+++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/PbArrayPacker.java
@@ -0,0 +1,41 @@
+/*
+ * 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.rpc.protocol.tri;
+
+import com.google.protobuf.Message;
+import org.apache.dubbo.rpc.model.Pack;
+
+public class PbArrayPacker implements Pack {
+
+ private static final Pack PB_PACK = o -> ((Message) o).toByteArray();
+
+ private final boolean singleArgument;
+
+ public PbArrayPacker(boolean singleArgument) {
+ this.singleArgument = singleArgument;
+ }
+
+ @Override
+ public byte[] pack(Object obj) throws Exception {
+ if (!singleArgument) {
+ obj = ((Object[]) obj)[0];
+ }
+ return PB_PACK.pack(obj);
+ }
+
+}
diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/PbUnpack.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/PbUnpack.java
index 51d5ea8227..f2aaa69dd0 100644
--- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/PbUnpack.java
+++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/PbUnpack.java
@@ -17,12 +17,12 @@
package org.apache.dubbo.rpc.protocol.tri;
-import org.apache.dubbo.rpc.model.PackableMethod;
+import org.apache.dubbo.rpc.model.UnPack;
import java.io.ByteArrayInputStream;
import java.io.IOException;
-public class PbUnpack implements PackableMethod.UnPack {
+public class PbUnpack implements UnPack {
private final Class clz;
diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/ReflectionPackableMethod.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/ReflectionPackableMethod.java
index ae647e5ca6..da3e67dfa4 100644
--- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/ReflectionPackableMethod.java
+++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/ReflectionPackableMethod.java
@@ -25,9 +25,12 @@ import org.apache.dubbo.config.Constants;
import org.apache.dubbo.remoting.utils.UrlUtils;
import org.apache.dubbo.remoting.transport.CodecSupport;
import org.apache.dubbo.rpc.model.MethodDescriptor;
+import org.apache.dubbo.rpc.model.Pack;
import org.apache.dubbo.rpc.model.PackableMethod;
import com.google.protobuf.Message;
+import org.apache.dubbo.rpc.model.UnPack;
+import org.apache.dubbo.rpc.model.WrapperUnPack;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@@ -115,11 +118,11 @@ public class ReflectionPackableMethod implements PackableMethod {
}
public static ReflectionPackableMethod init(MethodDescriptor methodDescriptor, URL url) {
- final String serializeName = UrlUtils.serializationOrDefault(url);
Object stored = methodDescriptor.getAttribute(METHOD_ATTR_PACK);
if (stored != null) {
return (ReflectionPackableMethod) stored;
}
+ final String serializeName = UrlUtils.serializationOrDefault(url);
final Collection allSerialize = UrlUtils.allSerializations(url);
ReflectionPackableMethod reflectionPackableMethod = new ReflectionPackableMethod(
methodDescriptor, url, serializeName, allSerialize);
@@ -448,23 +451,6 @@ public class ReflectionPackableMethod implements PackableMethod {
}
- private static class PbArrayPacker implements Pack {
-
- private final boolean singleArgument;
-
- private PbArrayPacker(boolean singleArgument) {
- this.singleArgument = singleArgument;
- }
-
- @Override
- public byte[] pack(Object obj) throws IOException {
- if (!singleArgument) {
- obj = ((Object[]) obj)[0];
- }
- return PB_PACK.pack(obj);
- }
- }
-
private class WrapRequestUnpack implements WrapperUnPack {
private final MultipleSerialization serialization;
diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/TripleInvoker.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/TripleInvoker.java
index 94713b3498..b6bad67e5f 100644
--- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/TripleInvoker.java
+++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/TripleInvoker.java
@@ -42,6 +42,7 @@ import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ConsumerModel;
import org.apache.dubbo.rpc.model.MethodDescriptor;
import org.apache.dubbo.rpc.model.PackableMethod;
+import org.apache.dubbo.rpc.model.PackableMethodFactory;
import org.apache.dubbo.rpc.model.ScopeModelUtil;
import org.apache.dubbo.rpc.model.ServiceDescriptor;
import org.apache.dubbo.rpc.model.StubMethodDescriptor;
@@ -66,6 +67,8 @@ import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.ReentrantLock;
+import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PACKABLE_METHOD_FACTORY;
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.TIMEOUT_KEY;
@@ -260,7 +263,9 @@ public class TripleInvoker extends AbstractInvoker {
if (methodDescriptor instanceof PackableMethod) {
meta.packableMethod = (PackableMethod) methodDescriptor;
} else {
- meta.packableMethod = ReflectionPackableMethod.init(methodDescriptor, url);
+ meta.packableMethod = url.getOrDefaultFrameworkModel().getExtensionLoader(PackableMethodFactory.class)
+ .getExtension(ConfigurationUtils.getGlobalConfiguration(url.getApplicationModel()).getString(DUBBO_PACKABLE_METHOD_FACTORY, DEFAULT_KEY))
+ .create(methodDescriptor, url, TripleConstant.CONTENT_PROTO);
}
meta.convertNoLowerHeader = TripleProtocol.CONVERT_NO_LOWER_HEADER;
meta.ignoreDefaultVersion = TripleProtocol.IGNORE_1_0_0_VERSION;
diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/AbstractServerCall.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/AbstractServerCall.java
index 026031c8d4..623e731ec4 100644
--- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/AbstractServerCall.java
+++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/AbstractServerCall.java
@@ -45,7 +45,6 @@ import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http2.DefaultHttp2Headers;
import io.netty.util.concurrent.Future;
-import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
@@ -225,8 +224,7 @@ public abstract class AbstractServerCall implements ServerCall, ServerStream.Lis
}
}
- protected abstract Object parseSingleMessage(byte[] data)
- throws IOException, ClassNotFoundException;
+ protected abstract Object parseSingleMessage(byte[] data) throws Exception;
@Override
public final void onCancelByRemote(TriRpcStatus status) {
diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/ReflectionAbstractServerCall.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/ReflectionAbstractServerCall.java
index 214edbe3ea..7789474a70 100644
--- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/ReflectionAbstractServerCall.java
+++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/ReflectionAbstractServerCall.java
@@ -17,7 +17,9 @@
package org.apache.dubbo.rpc.protocol.tri.call;
+import io.netty.handler.codec.http.HttpHeaderNames;
import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.rpc.HeaderFilter;
@@ -27,19 +29,21 @@ import org.apache.dubbo.rpc.TriRpcStatus;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.model.MethodDescriptor;
import org.apache.dubbo.rpc.model.MethodDescriptor.RpcType;
+import org.apache.dubbo.rpc.model.PackableMethodFactory;
import org.apache.dubbo.rpc.model.ProviderModel;
import org.apache.dubbo.rpc.model.ServiceDescriptor;
import org.apache.dubbo.rpc.protocol.tri.ClassLoadUtil;
-import org.apache.dubbo.rpc.protocol.tri.ReflectionPackableMethod;
import org.apache.dubbo.rpc.protocol.tri.TripleCustomerProtocolWapper;
import org.apache.dubbo.rpc.protocol.tri.stream.ServerStream;
import org.apache.dubbo.rpc.service.ServiceDescriptorInternalCache;
-import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
+import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_KEY;
+import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PACKABLE_METHOD_FACTORY;
+
public class ReflectionAbstractServerCall extends AbstractServerCall {
private final List headerFilters;
@@ -116,7 +120,10 @@ public class ReflectionAbstractServerCall extends AbstractServerCall {
}
}
if (methodDescriptor != null) {
- packableMethod = ReflectionPackableMethod.init(methodDescriptor, invoker.getUrl());
+ final URL url = invoker.getUrl();
+ packableMethod = frameworkModel.getExtensionLoader(PackableMethodFactory.class)
+ .getExtension(ConfigurationUtils.getGlobalConfiguration(url.getApplicationModel()).getString(DUBBO_PACKABLE_METHOD_FACTORY, DEFAULT_KEY))
+ .create(methodDescriptor, url, (String) requestMetadata.get(HttpHeaderNames.CONTENT_TYPE.toString()));
}
trySetListener();
if (listener == null) {
@@ -148,8 +155,7 @@ public class ReflectionAbstractServerCall extends AbstractServerCall {
}
@Override
- protected Object parseSingleMessage(byte[] data)
- throws IOException, ClassNotFoundException {
+ protected Object parseSingleMessage(byte[] data) throws Exception {
trySetMethodDescriptor(data);
trySetListener();
if (isClosed()) {
@@ -161,7 +167,7 @@ public class ReflectionAbstractServerCall extends AbstractServerCall {
}
- private void trySetMethodDescriptor(byte[] data) throws IOException {
+ private void trySetMethodDescriptor(byte[] data) {
if (methodDescriptor != null) {
return;
}
@@ -185,7 +191,10 @@ public class ReflectionAbstractServerCall extends AbstractServerCall {
+ serviceDescriptor.getInterfaceName()), null);
return;
}
- packableMethod = ReflectionPackableMethod.init(methodDescriptor, invoker.getUrl());
+ final URL url = invoker.getUrl();
+ packableMethod = frameworkModel.getExtensionLoader(PackableMethodFactory.class)
+ .getExtension(ConfigurationUtils.getGlobalConfiguration(url.getApplicationModel()).getString(DUBBO_PACKABLE_METHOD_FACTORY, DEFAULT_KEY))
+ .create(methodDescriptor, url, (String) requestMetadata.get(HttpHeaderNames.CONTENT_TYPE.toString()));
}
}
diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/StubAbstractServerCall.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/StubAbstractServerCall.java
index 20b5f80a2c..6f5027abfa 100644
--- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/StubAbstractServerCall.java
+++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/call/StubAbstractServerCall.java
@@ -25,7 +25,6 @@ import org.apache.dubbo.rpc.model.StubMethodDescriptor;
import org.apache.dubbo.rpc.protocol.tri.stream.ServerStream;
import org.apache.dubbo.rpc.stub.StubSuppliers;
-import java.io.IOException;
import java.util.concurrent.Executor;
public class StubAbstractServerCall extends AbstractServerCall {
@@ -58,7 +57,7 @@ public class StubAbstractServerCall extends AbstractServerCall {
}
@Override
- protected Object parseSingleMessage(byte[] data) throws IOException, ClassNotFoundException {
+ protected Object parseSingleMessage(byte[] data) throws Exception {
return packableMethod.parseRequest(data);
}
diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.model.PackableMethodFactory b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.model.PackableMethodFactory
new file mode 100644
index 0000000000..31e8c7ef9c
--- /dev/null
+++ b/dubbo-rpc/dubbo-rpc-triple/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.rpc.model.PackableMethodFactory
@@ -0,0 +1 @@
+default=org.apache.dubbo.rpc.protocol.tri.DefaultPackableMethodFactory
diff --git a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/call/StubServerCallTest.java b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/call/StubServerCallTest.java
index 98949a9237..e957092140 100644
--- a/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/call/StubServerCallTest.java
+++ b/dubbo-rpc/dubbo-rpc-triple/src/test/java/org/apache/dubbo/rpc/protocol/tri/call/StubServerCallTest.java
@@ -30,7 +30,6 @@ import io.netty.util.concurrent.ImmediateEventExecutor;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
-import java.io.IOException;
import java.util.Collections;
import static org.mockito.ArgumentMatchers.any;
@@ -40,7 +39,7 @@ import static org.mockito.Mockito.when;
class StubServerCallTest {
@Test
- void doStartCall() throws IOException, ClassNotFoundException {
+ void doStartCall() throws Exception {
Invoker> invoker = Mockito.mock(Invoker.class);
TripleServerStream tripleServerStream = Mockito.mock(TripleServerStream.class);
ProviderModel providerModel = Mockito.mock(ProviderModel.class);
From 3552347d44d700b7413650dc279c90a91cf25504 Mon Sep 17 00:00:00 2001
From: Albumen Kevin
Date: Fri, 28 Apr 2023 14:21:47 +0800
Subject: [PATCH 44/59] Support Shutdown Gracefully (#12118)
---
.../apache/dubbo/rpc/model/ProviderModel.java | 10 ++
.../java/com/alibaba/dubbo/rpc/Exporter.java | 7 +
.../apache/dubbo/config/ServiceConfig.java | 55 ++++++++
.../deploy/DefaultApplicationDeployer.java | 35 ++++-
.../config/deploy/DefaultModuleDeployer.java | 32 ++++-
.../dubbo/monitor/support/MonitorFilter.java | 7 +
.../integration/RegistryProtocol.java | 126 ++++++++++--------
.../java/org/apache/dubbo/rpc/Exporter.java | 5 +
.../rpc/listener/ListenerExporterWrapper.java | 5 +
.../dubbo/rpc/protocol/AbstractExporter.java | 5 +
10 files changed, 226 insertions(+), 61 deletions(-)
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java
index 775d5c93c6..d578cef6fe 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/rpc/model/ProviderModel.java
@@ -40,6 +40,8 @@ public class ProviderModel extends ServiceModel {
*/
private List serviceUrls = new ArrayList<>();
+ private volatile long lastInvokeTime = 0;
+
public ProviderModel(String serviceKey,
Object serviceInstance,
ServiceDescriptor serviceDescriptor,
@@ -176,6 +178,14 @@ public class ProviderModel extends ServiceModel {
}
+ public long getLastInvokeTime() {
+ return lastInvokeTime;
+ }
+
+ public void updateLastInvokeTime() {
+ this.lastInvokeTime = System.currentTimeMillis();
+ }
+
@Override
public boolean equals(Object o) {
if (this == o) {
diff --git a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Exporter.java b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Exporter.java
index c1fadeb9d8..5da68a3b20 100644
--- a/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Exporter.java
+++ b/dubbo-compatible/src/main/java/com/alibaba/dubbo/rpc/Exporter.java
@@ -23,6 +23,8 @@ public interface Exporter extends org.apache.dubbo.rpc.Exporter {
@Override
Invoker getInvoker();
+ default void unregister() {}
+
class CompatibleExporter implements Exporter {
private org.apache.dubbo.rpc.Exporter delegate;
@@ -40,5 +42,10 @@ public interface Exporter extends org.apache.dubbo.rpc.Exporter {
public void unexport() {
delegate.unexport();
}
+
+ @Override
+ public void unregister() {
+ delegate.unregister();
+ }
}
}
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
index 61784867c3..291296240b 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/ServiceConfig.java
@@ -19,6 +19,7 @@ package org.apache.dubbo.config;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.common.Version;
+import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
@@ -86,6 +87,7 @@ import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_NO_ME
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_SERVER_DISCONNECTED;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_UNEXPORT_ERROR;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_USE_RANDOM_PORT;
+import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.SERVICE_REGISTRY_PROTOCOL;
import static org.apache.dubbo.common.utils.NetUtils.getAvailablePort;
@@ -194,6 +196,14 @@ public class ServiceConfig extends ServiceConfigBase {
return;
}
if (!exporters.isEmpty()) {
+ for (Exporter> exporter : exporters) {
+ try {
+ exporter.unregister();
+ } catch (Throwable t) {
+ logger.warn(CONFIG_UNEXPORT_ERROR, "", "", "Unexpected error occurred when unexport " + exporter, t);
+ }
+ }
+ waitForIdle();
for (Exporter> exporter : exporters) {
try {
exporter.unexport();
@@ -209,6 +219,51 @@ public class ServiceConfig extends ServiceConfigBase {
repository.unregisterProvider(providerModel);
}
+ private void waitForIdle() {
+ int timeout = ConfigurationUtils.getServerShutdownTimeout(getScopeModel());
+
+ long idleTime = System.currentTimeMillis() - providerModel.getLastInvokeTime();
+
+ // 1. if service has idle for 10s(shutdown time), un-export directly
+ if (idleTime > timeout) {
+ return;
+ }
+
+ // 2. if service has idle for more than 6.7s(2/3 of shutdown time), wait for the rest time, then un-export directly
+ int tick = timeout / 3;
+ if (timeout - idleTime < tick) {
+ logger.info("Service " + getUniqueServiceName() + " has idle for " + idleTime + " ms, wait for " + (timeout - idleTime) + " ms to un-export");
+ try {
+ Thread.sleep(timeout - idleTime);
+ } catch (InterruptedException e) {
+ logger.warn(INTERNAL_ERROR, "unknown error in registry module", "", e.getMessage(), e);
+ Thread.currentThread().interrupt();
+ }
+ return;
+ }
+
+ // 3. Wait for 3.33s(1/3 of shutdown time), if service has idle for 3.33s(1/3 of shutdown time), un-export directly,
+ // otherwise wait for the rest time until idle for 3.33s(1/3 of shutdown time). The max wait time is 10s(shutdown time).
+ idleTime = 0;
+ long startTime = System.currentTimeMillis();
+ while (idleTime < tick) {
+ // service idle time.
+ idleTime = System.currentTimeMillis() - Math.max(providerModel.getLastInvokeTime(), startTime);
+ if (idleTime >= tick || System.currentTimeMillis() - startTime > timeout) {
+ return;
+ }
+ // idle rest time or timeout rest time
+ long waitTime = Math.min(tick - idleTime, timeout + startTime - System.currentTimeMillis());
+ logger.info("Service " + getUniqueServiceName() + " has idle for " + idleTime + " ms, wait for " + waitTime + " ms to un-export");
+ try {
+ Thread.sleep(waitTime);
+ } catch (InterruptedException e) {
+ logger.warn(INTERNAL_ERROR, "unknown error in registry module", "", e.getMessage(), e);
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
+
/**
* for early init serviceMetadata
*/
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
index b7fd2e347e..faf8123717 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultApplicationDeployer.java
@@ -58,10 +58,14 @@ import org.apache.dubbo.metrics.registry.event.RegistryEvent;
import org.apache.dubbo.metrics.report.MetricsReporter;
import org.apache.dubbo.metrics.report.MetricsReporterFactory;
import org.apache.dubbo.metrics.service.MetricsServiceExporter;
+import org.apache.dubbo.registry.Registry;
+import org.apache.dubbo.registry.RegistryFactory;
import org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils;
import org.apache.dubbo.registry.support.RegistryManager;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ModuleModel;
+import org.apache.dubbo.rpc.model.ModuleServiceRepository;
+import org.apache.dubbo.rpc.model.ProviderModel;
import org.apache.dubbo.rpc.model.ScopeModel;
import org.apache.dubbo.rpc.model.ScopeModelUtil;
@@ -931,10 +935,12 @@ public class DefaultApplicationDeployer extends AbstractDeployer exportedServices = serviceRepository.getExportedServices();
+ for (ProviderModel exportedService : exportedServices) {
+ List statedUrls = exportedService.getStatedUrl();
+ for (ProviderModel.RegisterStatedURL statedURL : statedUrls) {
+ if (statedURL.isRegistered()) {
+ doOffline(statedURL);
+ }
+ }
+ }
+ }
+ } catch (Throwable t) {
+ logger.error(LoggerCodeConstants.INTERNAL_ERROR, "", "", "Exceptions occurred when unregister services.", t);
+ }
+ }
+
+ private void doOffline(ProviderModel.RegisterStatedURL statedURL) {
+ RegistryFactory registryFactory =
+ statedURL.getRegistryUrl().getOrDefaultApplicationModel().getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
+ Registry registry = registryFactory.getRegistry(statedURL.getRegistryUrl());
+ registry.unregister(statedURL.getProviderUrl());
+ statedURL.setRegistered(false);
+ }
+
@Override
public void postDestroy() {
synchronized (destroyLock) {
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultModuleDeployer.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultModuleDeployer.java
index c611892073..a29fbaf197 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultModuleDeployer.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/deploy/DefaultModuleDeployer.java
@@ -17,6 +17,7 @@
package org.apache.dubbo.config.deploy;
import org.apache.dubbo.common.config.ReferenceCache;
+import org.apache.dubbo.common.constants.LoggerCodeConstants;
import org.apache.dubbo.common.deploy.AbstractDeployer;
import org.apache.dubbo.common.deploy.ApplicationDeployer;
import org.apache.dubbo.common.deploy.DeployListener;
@@ -35,6 +36,8 @@ import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.ServiceConfigBase;
import org.apache.dubbo.config.context.ModuleConfigManager;
import org.apache.dubbo.config.utils.SimpleReferenceCache;
+import org.apache.dubbo.registry.Registry;
+import org.apache.dubbo.registry.RegistryFactory;
import org.apache.dubbo.rpc.model.ConsumerModel;
import org.apache.dubbo.rpc.model.ModuleModel;
import org.apache.dubbo.rpc.model.ModuleServiceRepository;
@@ -213,6 +216,33 @@ public class DefaultModuleDeployer extends AbstractDeployer impleme
return;
}
onModuleStopping();
+
+ offline();
+ }
+
+ private void offline() {
+ try {
+ ModuleServiceRepository serviceRepository = moduleModel.getServiceRepository();
+ List exportedServices = serviceRepository.getExportedServices();
+ for (ProviderModel exportedService : exportedServices) {
+ List statedUrls = exportedService.getStatedUrl();
+ for (ProviderModel.RegisterStatedURL statedURL : statedUrls) {
+ if (statedURL.isRegistered()) {
+ doOffline(statedURL);
+ }
+ }
+ }
+ } catch (Throwable t) {
+ logger.error(LoggerCodeConstants.INTERNAL_ERROR, "", "", "Exceptions occurred when unregister services.", t);
+ }
+ }
+
+ private void doOffline(ProviderModel.RegisterStatedURL statedURL) {
+ RegistryFactory registryFactory =
+ statedURL.getRegistryUrl().getOrDefaultApplicationModel().getExtensionLoader(RegistryFactory.class).getAdaptiveExtension();
+ Registry registry = registryFactory.getRegistry(statedURL.getRegistryUrl());
+ registry.unregister(statedURL.getProviderUrl());
+ statedURL.setRegistered(false);
}
@Override
@@ -436,7 +466,7 @@ public class DefaultModuleDeployer extends AbstractDeployer impleme
exportFuture = CompletableFuture.allOf(asyncExportingFutures.toArray(new CompletableFuture[0]));
exportFuture.get();
} catch (Throwable e) {
- logger.warn(CONFIG_FAILED_EXPORT_SERVICE, "","",getIdentifier() + " export services occurred an exception: " + e.toString());
+ logger.warn(CONFIG_FAILED_EXPORT_SERVICE, "", "", getIdentifier() + " export services occurred an exception: " + e.toString());
} finally {
logger.info(getIdentifier() + " export services finished.");
asyncExportingFutures.clear();
diff --git a/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MonitorFilter.java b/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MonitorFilter.java
index 7e50cc800b..f8afc4a588 100644
--- a/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MonitorFilter.java
+++ b/dubbo-monitor/dubbo-monitor-api/src/main/java/org/apache/dubbo/monitor/support/MonitorFilter.java
@@ -31,6 +31,8 @@ import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
+import org.apache.dubbo.rpc.model.ProviderModel;
+import org.apache.dubbo.rpc.model.ServiceModel;
import org.apache.dubbo.rpc.support.RpcUtils;
import java.util.concurrent.ConcurrentHashMap;
@@ -97,6 +99,11 @@ public class MonitorFilter implements Filter, Filter.Listener {
// count up
getConcurrent(invoker, invocation).incrementAndGet();
}
+ ServiceModel serviceModel = invoker.getUrl().getServiceModel();
+ if (serviceModel instanceof ProviderModel) {
+ ((ProviderModel) serviceModel).updateLastInvokeTime();
+ }
+
// proceed invocation chain
return invoker.invoke(invocation);
}
diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java
index da87f87318..caa52f3b6f 100644
--- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java
+++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/integration/RegistryProtocol.java
@@ -16,18 +16,7 @@
*/
package org.apache.dubbo.registry.integration;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
-import java.util.concurrent.ScheduledExecutorService;
-import java.util.concurrent.TimeUnit;
-
import org.apache.dubbo.common.URL;
-import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.config.configcenter.DynamicConfiguration;
import org.apache.dubbo.common.deploy.ApplicationDeployer;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
@@ -71,11 +60,21 @@ import org.apache.dubbo.rpc.model.ScopeModelUtil;
import org.apache.dubbo.rpc.protocol.InvokerWrapper;
import org.apache.dubbo.rpc.support.ProtocolUtils;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
import static org.apache.dubbo.common.constants.CommonConstants.APPLICATION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.COMMA_SPLIT_PATTERN;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER;
-import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_SERVER_SHUTDOWN_TIMEOUT;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_VERSION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ENABLED_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.EXTRA_KEYS_KEY;
@@ -702,6 +701,11 @@ public class RegistryProtocol implements Protocol, ScopeModelAware {
public void unexport() {
exporter.unexport();
}
+
+ @Override
+ public void unregister() {
+ exporter.unregister();
+ }
}
/**
@@ -906,6 +910,7 @@ public class RegistryProtocol implements Protocol, ScopeModelAware {
private URL registerUrl;
private NotifyListener notifyListener;
+ private final AtomicBoolean unregistered = new AtomicBoolean(false);
public ExporterChangeableWrapper(Exporter exporter, Invoker originInvoker) {
this.exporter = exporter;
@@ -929,57 +934,60 @@ public class RegistryProtocol implements Protocol, ScopeModelAware {
}
@Override
- public void unexport() {
- String key = getCacheKey(this.originInvoker);
- bounds.remove(key);
-
- Registry registry = RegistryProtocol.this.getRegistry(getRegistryUrl(originInvoker));
- try {
- registry.unregister(registerUrl);
- } catch (Throwable t) {
- logger.warn(INTERNAL_ERROR, "unknown error in registry module", "", t.getMessage(), t);
- }
- try {
- if (subscribeUrl != null) {
- Map> overrideListeners = getProviderConfigurationListener(subscribeUrl).getOverrideListeners();
- Set listeners = overrideListeners.get(subscribeUrl);
- if(listeners != null){
- if (listeners.remove(notifyListener)) {
- if (!registry.isServiceDiscovery()) {
- registry.unsubscribe(subscribeUrl, notifyListener);
- }
- ApplicationModel applicationModel = getApplicationModel(registerUrl.getScopeModel());
- if (applicationModel.getModelEnvironment().getConfiguration().convert(Boolean.class, ENABLE_CONFIGURATION_LISTEN, true)) {
- for (ModuleModel moduleModel : applicationModel.getPubModuleModels()) {
- if (moduleModel.getServiceRepository().getExportedServices().size() > 0) {
- moduleModel.getExtensionLoader(GovernanceRuleRepository.class).getDefaultExtension()
- .removeListener(subscribeUrl.getServiceKey() + CONFIGURATORS_SUFFIX,
- serviceConfigurationListeners.remove(subscribeUrl.getServiceKey()));
- }
- }
- }
- }
- if (listeners.isEmpty()) {
- overrideListeners.remove(subscribeUrl);
- }
- }
- }
- } catch (Throwable t) {
- logger.warn(INTERNAL_ERROR, "unknown error in registry module", "", t.getMessage(), t);
- }
-
- //TODO wait for shutdown timeout is a bit strange
- int timeout = DEFAULT_SERVER_SHUTDOWN_TIMEOUT;
- if (subscribeUrl != null) {
- timeout = ConfigurationUtils.getServerShutdownTimeout(subscribeUrl.getScopeModel());
- }
- executor.schedule(() -> {
+ public synchronized void unregister() {
+ if (unregistered.compareAndSet(false, true)) {
+ Registry registry = RegistryProtocol.this.getRegistry(getRegistryUrl(originInvoker));
try {
- exporter.unexport();
+ registry.unregister(registerUrl);
} catch (Throwable t) {
logger.warn(INTERNAL_ERROR, "unknown error in registry module", "", t.getMessage(), t);
}
- }, timeout, TimeUnit.MILLISECONDS);
+ try {
+ if (subscribeUrl != null) {
+ Map> overrideListeners = getProviderConfigurationListener(subscribeUrl).getOverrideListeners();
+ Set listeners = overrideListeners.get(subscribeUrl);
+ if (listeners != null) {
+ if (listeners.remove(notifyListener)) {
+ if (!registry.isServiceDiscovery()) {
+ registry.unsubscribe(subscribeUrl, notifyListener);
+ }
+ ApplicationModel applicationModel = getApplicationModel(registerUrl.getScopeModel());
+ if (applicationModel.getModelEnvironment().getConfiguration().convert(Boolean.class, ENABLE_CONFIGURATION_LISTEN, true)) {
+ for (ModuleModel moduleModel : applicationModel.getPubModuleModels()) {
+ if (moduleModel.getServiceRepository().getExportedServices().size() > 0) {
+ moduleModel.getExtensionLoader(GovernanceRuleRepository.class).getDefaultExtension()
+ .removeListener(subscribeUrl.getServiceKey() + CONFIGURATORS_SUFFIX,
+ serviceConfigurationListeners.remove(subscribeUrl.getServiceKey()));
+ }
+ }
+ }
+ }
+ if (listeners.isEmpty()) {
+ overrideListeners.remove(subscribeUrl);
+ }
+ }
+ }
+ } catch (Throwable t) {
+ logger.warn(INTERNAL_ERROR, "unknown error in registry module", "", t.getMessage(), t);
+ }
+ }
+ }
+
+ @Override
+ public synchronized void unexport() {
+ String key = getCacheKey(this.originInvoker);
+ bounds.remove(key);
+
+ unregister();
+ doUnExport();
+ }
+
+ private void doUnExport() {
+ try {
+ exporter.unexport();
+ } catch (Throwable t) {
+ logger.warn(INTERNAL_ERROR, "unknown error in registry module", "", t.getMessage(), t);
+ }
}
public void setSubscribeUrl(URL subscribeUrl) {
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Exporter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Exporter.java
index d02a4050dc..9b87ddb5d9 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Exporter.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/Exporter.java
@@ -41,4 +41,9 @@ public interface Exporter {
*/
void unexport();
+ /**
+ * unregister from registry
+ */
+ void unregister();
+
}
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java
index 972f5cbc56..025459588d 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/listener/ListenerExporterWrapper.java
@@ -62,6 +62,11 @@ public class ListenerExporterWrapper implements Exporter {
}
}
+ @Override
+ public void unregister() {
+ exporter.unregister();
+ }
+
private void listenerEvent(Consumer consumer) {
if (CollectionUtils.isNotEmpty(listeners)) {
RuntimeException exception = null;
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractExporter.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractExporter.java
index 2378b2458b..b7f6c03e30 100644
--- a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractExporter.java
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/protocol/AbstractExporter.java
@@ -60,6 +60,11 @@ public abstract class AbstractExporter implements Exporter {
afterUnExport();
}
+ @Override
+ public void unregister() {
+
+ }
+
/**
* subclasses need to override this method to destroy resources.
*/
From 830c460c0a2db7ad6d5caed9a0b7e2890952da77 Mon Sep 17 00:00:00 2001
From: Albumen Kevin
Date: Fri, 28 Apr 2023 17:14:01 +0800
Subject: [PATCH 45/59] Fix port unification channel leak (#12212)
---
.../transport/netty4/NettyChannelHandler.java | 77 +++++++++++++++++++
.../netty4/NettyPortUnificationServer.java | 7 +-
.../NettyPortUnificationServerHandler.java | 19 +----
3 files changed, 83 insertions(+), 20 deletions(-)
create mode 100644 dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannelHandler.java
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannelHandler.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannelHandler.java
new file mode 100644
index 0000000000..cfedb14371
--- /dev/null
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyChannelHandler.java
@@ -0,0 +1,77 @@
+/*
+ * 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.remoting.transport.netty4;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.logger.Logger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.remoting.Channel;
+import org.apache.dubbo.remoting.ChannelHandler;
+
+import io.netty.channel.ChannelHandlerContext;
+import io.netty.channel.ChannelInboundHandlerAdapter;
+
+import java.net.InetSocketAddress;
+import java.util.Map;
+
+public class NettyChannelHandler extends ChannelInboundHandlerAdapter {
+ private static final Logger logger = LoggerFactory.getLogger(NettyChannelHandler.class);
+
+ private final Map dubboChannels;
+
+ private final URL url;
+ private final ChannelHandler handler;
+
+ public NettyChannelHandler(Map dubboChannels, URL url, ChannelHandler handler) {
+ this.dubboChannels = dubboChannels;
+ this.url = url;
+ this.handler = handler;
+ }
+
+ @Override
+ public void channelActive(ChannelHandlerContext ctx) throws Exception {
+ super.channelActive(ctx);
+ NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
+ if (channel != null) {
+ dubboChannels.put(NetUtils.toAddressString((InetSocketAddress) ctx.channel().remoteAddress()), channel);
+ handler.connected(channel);
+
+ if (logger.isInfoEnabled()) {
+ logger.info("The connection of " + channel.getRemoteAddress() + " -> " + channel.getLocalAddress() + " is established.");
+ }
+ }
+ }
+
+ @Override
+ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
+ super.channelInactive(ctx);
+ NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
+ try {
+ dubboChannels.remove(NetUtils.toAddressString((InetSocketAddress) ctx.channel().remoteAddress()));
+ if (channel != null) {
+ handler.disconnected(channel);
+ if (logger.isInfoEnabled()) {
+ logger.info("The connection of " + channel.getRemoteAddress() + " -> " + channel.getLocalAddress() + " is disconnected.");
+ }
+ }
+ } finally {
+ NettyChannel.removeChannel(ctx.channel());
+ }
+ }
+
+}
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServer.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServer.java
index 239367320a..42d8d89895 100644
--- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServer.java
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServer.java
@@ -122,10 +122,11 @@ public class NettyPortUnificationServer extends AbstractPortUnificationServer {
protected void initChannel(SocketChannel ch) throws Exception {
// Do not add idle state handler here, because it should be added in the protocol handler.
final ChannelPipeline p = ch.pipeline();
- final NettyPortUnificationServerHandler puHandler;
- puHandler = new NettyPortUnificationServerHandler(getUrl(), true, getProtocols(),
- NettyPortUnificationServer.this, NettyPortUnificationServer.this.dubboChannels,
+ NettyChannelHandler nettyChannelHandler = new NettyChannelHandler(dubboChannels, getUrl(), NettyPortUnificationServer.this);
+ NettyPortUnificationServerHandler puHandler = new NettyPortUnificationServerHandler(getUrl(), true, getProtocols(),
+ NettyPortUnificationServer.this,
getSupportedUrls(), getSupportedHandlers());
+ p.addLast("channel-handler", nettyChannelHandler);
p.addLast("negotiation-protocol", puHandler);
}
});
diff --git a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServerHandler.java b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServerHandler.java
index 5d02ea4e16..cb7e672c67 100644
--- a/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServerHandler.java
+++ b/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyPortUnificationServerHandler.java
@@ -22,8 +22,6 @@ import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.ssl.CertManager;
import org.apache.dubbo.common.ssl.ProviderCert;
-import org.apache.dubbo.common.utils.NetUtils;
-import org.apache.dubbo.remoting.Channel;
import org.apache.dubbo.remoting.ChannelHandler;
import org.apache.dubbo.remoting.api.ProtocolDetector;
import org.apache.dubbo.remoting.api.WireProtocol;
@@ -39,7 +37,6 @@ import io.netty.handler.ssl.SslHandler;
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
import javax.net.ssl.SSLSession;
-import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -54,19 +51,17 @@ public class NettyPortUnificationServerHandler extends ByteToMessageDecoder {
private final ChannelHandler handler;
private final boolean detectSsl;
private final List protocols;
- private final Map dubboChannels;
private final Map urlMapper;
private final Map handlerMapper;
public NettyPortUnificationServerHandler(URL url, boolean detectSsl,
List protocols, ChannelHandler handler,
- Map dubboChannels, Map urlMapper, Map handlerMapper) {
+ Map urlMapper, Map handlerMapper) {
this.url = url;
this.protocols = protocols;
this.detectSsl = detectSsl;
this.handler = handler;
- this.dubboChannels = dubboChannels;
this.urlMapper = urlMapper;
this.handlerMapper = handlerMapper;
}
@@ -76,16 +71,6 @@ public class NettyPortUnificationServerHandler extends ByteToMessageDecoder {
LOGGER.error(INTERNAL_ERROR, "unknown error in remoting module", "", "Unexpected exception from downstream before protocol detected.", cause);
}
- @Override
- public void channelActive(ChannelHandlerContext ctx) throws Exception {
- super.channelActive(ctx);
- NettyChannel channel = NettyChannel.getOrAddChannel(ctx.channel(), url, handler);
- if (channel != null) {
- // this is needed by some test cases
- dubboChannels.put(NetUtils.toAddressString((InetSocketAddress) ctx.channel().remoteAddress()), channel);
- }
- }
-
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof SslHandshakeCompletionEvent) {
@@ -162,7 +147,7 @@ public class NettyPortUnificationServerHandler extends ByteToMessageDecoder {
p.addLast("ssl", sslContext.newHandler(ctx.alloc()));
p.addLast("unificationA",
new NettyPortUnificationServerHandler(url, false, protocols,
- handler, dubboChannels, urlMapper, handlerMapper));
+ handler, urlMapper, handlerMapper));
p.remove(this);
}
From 25a08a2b40d9c49ae2ddbe65a7146f3a2ba819f4 Mon Sep 17 00:00:00 2001
From: Albumen Kevin
Date: Tue, 2 May 2023 21:25:11 +0800
Subject: [PATCH 46/59] Support offline notify (#12211)
* Support offline notify
* Add log
---
.../common/constants/CommonConstants.java | 1 +
.../dubbo/config/DubboShutdownHook.java | 8 ++
.../qos/command/impl/GracefulShutdown.java | 47 +++++++++++
.../org.apache.dubbo.qos.api.BaseCommand | 1 +
.../qos/command/util/CommandHelperTest.java | 2 +
.../support/header/HeaderExchangeHandler.java | 6 ++
.../apache/dubbo/rpc/GracefulShutdown.java | 31 +++++++
.../protocol/dubbo/DubboGracefulShutdown.java | 81 +++++++++++++++++++
.../rpc/protocol/dubbo/DubboProtocol.java | 1 +
9 files changed, 178 insertions(+)
create mode 100644 dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/GracefulShutdown.java
create mode 100644 dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/GracefulShutdown.java
create mode 100644 dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboGracefulShutdown.java
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
index f066044043..978d053031 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
@@ -313,6 +313,7 @@ public interface CommonConstants {
String HEARTBEAT_EVENT = null;
String MOCK_HEARTBEAT_EVENT = "H";
String READONLY_EVENT = "R";
+ String WRITEABLE_EVENT = "W";
String REFERENCE_FILTER_KEY = "reference.filter";
diff --git a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java
index c946fa4390..3172525e72 100644
--- a/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java
+++ b/dubbo-config/dubbo-config-api/src/main/java/org/apache/dubbo/config/DubboShutdownHook.java
@@ -22,9 +22,11 @@ import org.apache.dubbo.common.constants.LoggerCodeConstants;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.Assert;
+import org.apache.dubbo.rpc.GracefulShutdown;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ModuleModel;
+import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -80,6 +82,12 @@ public class DubboShutdownHook extends Thread {
}
private void doDestroy() {
+ // send readonly for shutdown hook
+ List gracefulShutdowns = GracefulShutdown.getGracefulShutdowns(applicationModel.getFrameworkModel());
+ for (GracefulShutdown gracefulShutdown : gracefulShutdowns) {
+ gracefulShutdown.readonly();
+ }
+
boolean hasModuleBindSpring = false;
// check if any modules are bound to Spring
for (ModuleModel module: applicationModel.getModuleModels()) {
diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/GracefulShutdown.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/GracefulShutdown.java
new file mode 100644
index 0000000000..c4fa69557d
--- /dev/null
+++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/GracefulShutdown.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.qos.command.impl;
+
+import org.apache.dubbo.qos.api.BaseCommand;
+import org.apache.dubbo.qos.api.Cmd;
+import org.apache.dubbo.qos.api.CommandContext;
+import org.apache.dubbo.qos.api.PermissionLevel;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+
+@Cmd(name = "gracefulShutdown",
+ summary = "Gracefully shutdown servers",
+ example = {"gracefulShutdown"},
+ requiredPermissionLevel = PermissionLevel.PRIVATE)
+public class GracefulShutdown implements BaseCommand {
+ private final Offline offline;
+ private final FrameworkModel frameworkModel;
+
+ public GracefulShutdown(FrameworkModel frameworkModel) {
+ this.offline = new Offline(frameworkModel);
+ this.frameworkModel = frameworkModel;
+ }
+
+ @Override
+ public String execute(CommandContext commandContext, String[] args) {
+ offline.execute(commandContext, new String[0]);
+ for (org.apache.dubbo.rpc.GracefulShutdown gracefulShutdown :
+ org.apache.dubbo.rpc.GracefulShutdown.getGracefulShutdowns(frameworkModel)) {
+ gracefulShutdown.readonly();
+ }
+ return "OK";
+ }
+}
diff --git a/dubbo-plugin/dubbo-qos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.qos.api.BaseCommand b/dubbo-plugin/dubbo-qos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.qos.api.BaseCommand
index 78825644ed..d575733b46 100644
--- a/dubbo-plugin/dubbo-qos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.qos.api.BaseCommand
+++ b/dubbo-plugin/dubbo-qos/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.qos.api.BaseCommand
@@ -36,3 +36,4 @@ serializeCheckStatus=org.apache.dubbo.qos.command.impl.SerializeCheckStatus
serializeWarnedClasses=org.apache.dubbo.qos.command.impl.SerializeWarnedClasses
getConfig=org.apache.dubbo.qos.command.impl.GetConfig
getAddress=org.apache.dubbo.qos.command.impl.GetAddress
+gracefulShutdown=org.apache.dubbo.qos.command.impl.GracefulShutdown
diff --git a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java
index a06a76408c..0370e519ef 100644
--- a/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java
+++ b/dubbo-plugin/dubbo-qos/src/test/java/org/apache/dubbo/qos/command/util/CommandHelperTest.java
@@ -30,6 +30,7 @@ import org.apache.dubbo.qos.command.impl.GetConfig;
import org.apache.dubbo.qos.command.impl.GetEnabledRouterSnapshot;
import org.apache.dubbo.qos.command.impl.GetRecentRouterSnapshot;
import org.apache.dubbo.qos.command.impl.GetRouterSnapshot;
+import org.apache.dubbo.qos.command.impl.GracefulShutdown;
import org.apache.dubbo.qos.command.impl.Help;
import org.apache.dubbo.qos.command.impl.InvokeTelnet;
import org.apache.dubbo.qos.command.impl.Live;
@@ -123,6 +124,7 @@ class CommandHelperTest {
expectedClasses.add(SerializeWarnedClasses.class);
expectedClasses.add(GetConfig.class);
expectedClasses.add(GetAddress.class);
+ expectedClasses.add(GracefulShutdown.class);
assertThat(classes, containsInAnyOrder(expectedClasses.toArray(new Class>[0])));
}
diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java
index ef0420f8f4..fd9d660d05 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/exchange/support/header/HeaderExchangeHandler.java
@@ -38,6 +38,7 @@ import java.net.InetSocketAddress;
import java.util.concurrent.CompletionStage;
import static org.apache.dubbo.common.constants.CommonConstants.READONLY_EVENT;
+import static org.apache.dubbo.common.constants.CommonConstants.WRITEABLE_EVENT;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.TRANSPORT_FAILED_RESPONSE;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.TRANSPORT_UNSUPPORTED_MESSAGE;
@@ -75,6 +76,11 @@ public class HeaderExchangeHandler implements ChannelHandlerDelegate {
void handlerEvent(Channel channel, Request req) throws RemotingException {
if (req.getData() != null && req.getData().equals(READONLY_EVENT)) {
channel.setAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY, Boolean.TRUE);
+ logger.info("ChannelReadOnly set true for channel: " + channel);
+ }
+ if (req.getData() != null && req.getData().equals(WRITEABLE_EVENT)) {
+ channel.removeAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY);
+ logger.info("ChannelReadOnly set false for channel: " + channel);
}
}
diff --git a/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/GracefulShutdown.java b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/GracefulShutdown.java
new file mode 100644
index 0000000000..e8bdb38044
--- /dev/null
+++ b/dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/GracefulShutdown.java
@@ -0,0 +1,31 @@
+/*
+ * 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.rpc;
+
+import org.apache.dubbo.rpc.model.FrameworkModel;
+
+import java.util.List;
+
+public interface GracefulShutdown {
+ void readonly();
+
+ void writeable();
+
+ static List getGracefulShutdowns(FrameworkModel frameworkModel) {
+ return frameworkModel.getBeanFactory().getBeansOfType(GracefulShutdown.class);
+ }
+}
diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboGracefulShutdown.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboGracefulShutdown.java
new file mode 100644
index 0000000000..f1a324e01a
--- /dev/null
+++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboGracefulShutdown.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.rpc.protocol.dubbo;
+
+import org.apache.dubbo.common.Version;
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.remoting.Channel;
+import org.apache.dubbo.remoting.Constants;
+import org.apache.dubbo.remoting.RemotingException;
+import org.apache.dubbo.remoting.exchange.Request;
+import org.apache.dubbo.rpc.GracefulShutdown;
+import org.apache.dubbo.rpc.ProtocolServer;
+
+import java.nio.channels.ClosedChannelException;
+import java.util.Collection;
+
+import static org.apache.dubbo.common.constants.CommonConstants.READONLY_EVENT;
+import static org.apache.dubbo.common.constants.CommonConstants.WRITEABLE_EVENT;
+import static org.apache.dubbo.common.constants.LoggerCodeConstants.TRANSPORT_FAILED_CLOSE_STREAM;
+
+public class DubboGracefulShutdown implements GracefulShutdown {
+ private static final ErrorTypeAwareLogger logger = LoggerFactory.getErrorTypeAwareLogger(DubboGracefulShutdown.class);
+ private final DubboProtocol dubboProtocol;
+
+ public DubboGracefulShutdown(DubboProtocol dubboProtocol) {
+ this.dubboProtocol = dubboProtocol;
+ }
+
+ @Override
+ public void readonly() {
+ sendEvent(READONLY_EVENT);
+ }
+
+ @Override
+ public void writeable() {
+ sendEvent(WRITEABLE_EVENT);
+ }
+
+ private void sendEvent(String event) {
+ try {
+ for (ProtocolServer server : dubboProtocol.getServers()) {
+ Collection channels = server.getRemotingServer().getChannels();
+ Request request = new Request();
+ request.setEvent(event);
+ request.setTwoWay(false);
+ request.setVersion(Version.getProtocolVersion());
+
+ for (Channel channel : channels) {
+ try {
+ if (channel.isConnected()) {
+ channel.send(request, channel.getUrl().getParameter(Constants.CHANNEL_READONLYEVENT_SENT_KEY, true));
+ }
+ } catch (RemotingException e) {
+ if (e.getCause() instanceof ClosedChannelException) {
+ // ignore ClosedChannelException which means the connection has been closed.
+ continue;
+ }
+ logger.warn(TRANSPORT_FAILED_CLOSE_STREAM, "", "", "send cannot write message error.", e);
+ }
+ }
+ }
+ } catch (Throwable e) {
+ logger.warn(TRANSPORT_FAILED_CLOSE_STREAM, "", "", "send cannot write message error.", e);
+ }
+ }
+}
diff --git a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java
index 0e10031e4a..3c0138e0d6 100644
--- a/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java
+++ b/dubbo-rpc/dubbo-rpc-dubbo/src/main/java/org/apache/dubbo/rpc/protocol/dubbo/DubboProtocol.java
@@ -235,6 +235,7 @@ public class DubboProtocol extends AbstractProtocol {
}
};
this.frameworkModel = frameworkModel;
+ this.frameworkModel.getBeanFactory().registerBean(new DubboGracefulShutdown(this));
}
/**
From a59a378de5166356c14439ba566fc7eeb70c36f7 Mon Sep 17 00:00:00 2001
From: KamTo Hung
Date: Tue, 2 May 2023 21:41:04 +0800
Subject: [PATCH 47/59] Fix consumer startup failure (#12204)
* fix #12099
* fix #12099
* add UT
* fix #12099
---
.../dubbo/config/AbstractInterfaceConfig.java | 3 +-
.../reference/ReferenceBeanSupport.java | 4 +-
.../schema/DubboBeanDefinitionParser.java | 5 +-
.../consumer/DubboXmlConsumerTest.java | 36 +++++++++++++
.../registryNA/consumer/dubbo-consumer.xml | 33 ++++++++++++
.../consumer/dubbo-registryNA-consumer.xml | 33 ++++++++++++
.../provider/DubboXmlProviderTest.java | 51 +++++++++++++++++++
.../registryNA/provider/dubbo-provider.xml | 39 ++++++++++++++
8 files changed, 199 insertions(+), 5 deletions(-)
create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/DubboXmlConsumerTest.java
create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/dubbo-consumer.xml
create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/dubbo-registryNA-consumer.xml
create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/provider/DubboXmlProviderTest.java
create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/provider/dubbo-provider.xml
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java
index aae01b667d..30be87558e 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java
@@ -688,7 +688,7 @@ public abstract class AbstractInterfaceConfig extends AbstractMethodConfig {
}
public void setRegistry(RegistryConfig registry) {
- List registries = new ArrayList(1);
+ List registries = new ArrayList<>(1);
registries.add(registry);
setRegistries(registries);
}
@@ -716,7 +716,6 @@ public abstract class AbstractInterfaceConfig extends AbstractMethodConfig {
return methods;
}
- @SuppressWarnings("unchecked")
public void setMethods(List extends MethodConfig> methods) {
this.methods = (methods != null) ? new ArrayList<>(methods) : null;
}
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/reference/ReferenceBeanSupport.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/reference/ReferenceBeanSupport.java
index d571b4596f..e828b6f720 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/reference/ReferenceBeanSupport.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/reference/ReferenceBeanSupport.java
@@ -51,7 +51,7 @@ import static org.apache.dubbo.common.utils.StringUtils.join;
public class ReferenceBeanSupport {
- private static List IGNORED_ATTRS = Arrays.asList(ReferenceAttributes.ID, ReferenceAttributes.GROUP,
+ private static final List IGNORED_ATTRS = Arrays.asList(ReferenceAttributes.ID, ReferenceAttributes.GROUP,
ReferenceAttributes.VERSION, ReferenceAttributes.INTERFACE, ReferenceAttributes.INTERFACE_NAME,
ReferenceAttributes.INTERFACE_CLASS);
@@ -65,7 +65,7 @@ public class ReferenceBeanSupport {
if (interfaceName == null) {
Object interfaceClassValue = attributes.get(ReferenceAttributes.INTERFACE_CLASS);
if (interfaceClassValue instanceof Class) {
- interfaceName = ((Class) interfaceClassValue).getName();
+ interfaceName = ((Class>) interfaceClassValue).getName();
} else if (interfaceClassValue instanceof String) {
if (interfaceClassValue.equals("void")) {
attributes.remove(ReferenceAttributes.INTERFACE_CLASS);
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java
index 8f63565b21..9f0c8c7d90 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboBeanDefinitionParser.java
@@ -175,7 +175,10 @@ public class DubboBeanDefinitionParser implements BeanDefinitionParser {
if ("registry".equals(property) && RegistryConfig.NO_AVAILABLE.equalsIgnoreCase(value)) {
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress(RegistryConfig.NO_AVAILABLE);
- beanDefinition.getPropertyValues().addPropertyValue(beanProperty, registryConfig);
+ // see AbstractInterfaceConfig#registries, It will be invoker setRegistries method when BeanDefinition is registered,
+ beanDefinition.getPropertyValues().addPropertyValue("registries", registryConfig);
+ // If registry is N/A, don't init it until the reference is invoked
+ beanDefinition.setLazyInit(true);
} else if ("provider".equals(property) || "registry".equals(property) || ("protocol".equals(property) && AbstractServiceConfig.class.isAssignableFrom(beanClass))) {
/**
* For 'provider' 'protocol' 'registry', keep literal value (should be id/name) and set the value to 'registryIds' 'providerIds' protocolIds'
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/DubboXmlConsumerTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/DubboXmlConsumerTest.java
new file mode 100644
index 0000000000..af17e997cc
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/DubboXmlConsumerTest.java
@@ -0,0 +1,36 @@
+/*
+ * 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.reference.registryNA.consumer;
+
+import org.apache.dubbo.config.spring.api.HelloService;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+class DubboXmlConsumerTest {
+
+
+ @Test
+ void testConsumer() {
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/org/apache/dubbo/config/spring/reference/registryNA/consumer/dubbo-registryNA-consumer.xml");
+ context.start();
+ HelloService helloService = context.getBean("helloService", HelloService.class);
+ IllegalStateException exception = Assertions.assertThrows(IllegalStateException.class, () -> helloService.sayHello("dubbo"));
+ Assertions.assertTrue(exception.getMessage().contains("No such any registry to reference org.apache.dubbo.config.spring.api.HelloService"));
+ }
+
+}
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/dubbo-consumer.xml b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/dubbo-consumer.xml
new file mode 100644
index 0000000000..ca6485632a
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/dubbo-consumer.xml
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/dubbo-registryNA-consumer.xml b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/dubbo-registryNA-consumer.xml
new file mode 100644
index 0000000000..0bfc310ba4
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/consumer/dubbo-registryNA-consumer.xml
@@ -0,0 +1,33 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/provider/DubboXmlProviderTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/provider/DubboXmlProviderTest.java
new file mode 100644
index 0000000000..b45a649901
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/provider/DubboXmlProviderTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.reference.registryNA.provider;
+
+import org.apache.dubbo.config.spring.api.HelloService;
+import org.apache.dubbo.rpc.RpcException;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @author KamTo Hung
+ */
+public class DubboXmlProviderTest {
+
+ @Test
+ void testProvider() {
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/org/apache/dubbo/config/spring/reference/registryNA/provider/dubbo-provider.xml");
+ context.start();
+ Object bean = context.getBean("helloService");
+ Assertions.assertNotNull(bean);
+ }
+
+ @Test
+ void testProvider2() {
+ ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:/org/apache/dubbo/config/spring/reference/registryNA/provider/dubbo-provider.xml");
+ context.start();
+ Assertions.assertNotNull(context.getBean("helloService"));
+ ClassPathXmlApplicationContext context2 = new ClassPathXmlApplicationContext("classpath:/org/apache/dubbo/config/spring/reference/registryNA/consumer/dubbo-consumer.xml");
+ context2.start();
+ HelloService helloService = context2.getBean("helloService", HelloService.class);
+ Assertions.assertNotNull(helloService);
+ RpcException exception = Assertions.assertThrows(RpcException.class, () -> helloService.sayHello("dubbo"));
+ Assertions.assertTrue(exception.getMessage().contains("Failed to invoke the method sayHello in the service org.apache.dubbo.config.spring.api.HelloService. No provider available for the service org.apache.dubbo.config.spring.api.HelloService"));
+ }
+
+}
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/provider/dubbo-provider.xml b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/provider/dubbo-provider.xml
new file mode 100644
index 0000000000..09a71c3aca
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/reference/registryNA/provider/dubbo-provider.xml
@@ -0,0 +1,39 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
From ab85403d472cd9c4512e219caf7577e988e3d28a Mon Sep 17 00:00:00 2001
From: suncairong163 <105478245+suncairong163@users.noreply.github.com>
Date: Tue, 2 May 2023 21:42:07 +0800
Subject: [PATCH 48/59] rest provider interface judge modify (#12163)
* rest impl class support by changing service config interface judge
* add test System.exit(1)
* remove test System.exit(1)
* add Override annotation
* add comment to containsRestProtocol
* refacor method name
---
.../dubbo/config/AbstractInterfaceConfig.java | 13 ++-
.../org/apache/dubbo/config/Constants.java | 2 +
.../dubbo/config/ServiceConfigBase.java | 32 +++++--
.../spring/ControllerServiceConfigTest.java | 42 +++++++++
.../spring/api/SpringControllerService.java | 29 ++++++
.../rpc/protocol/rest/ServiceConfigTest.java | 91 +++++++++++++++++++
.../rest/mvc/SpringControllerService.java | 29 ++++++
7 files changed, 229 insertions(+), 9 deletions(-)
create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ControllerServiceConfigTest.java
create mode 100644 dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/api/SpringControllerService.java
create mode 100644 dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/ServiceConfigTest.java
create mode 100644 dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringControllerService.java
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java
index 30be87558e..414b2a4b25 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractInterfaceConfig.java
@@ -314,7 +314,8 @@ public abstract class AbstractInterfaceConfig extends AbstractMethodConfig {
// There may be no interface class when generic call
return;
}
- if (!interfaceClass.isInterface()) {
+
+ if (!interfaceClass.isInterface() && !canSkipInterfaceCheck()) {
throw new IllegalStateException(interfaceName + " is not an interface");
}
@@ -374,6 +375,15 @@ public abstract class AbstractInterfaceConfig extends AbstractMethodConfig {
}
+ /**
+ * it is used for skipping the check of interface since dubbo 3.2
+ * rest protocol allow the service is implement class
+ * @return
+ */
+ protected boolean canSkipInterfaceCheck() {
+ return false;
+ }
+
protected boolean verifyMethodConfig(MethodConfig methodConfig, Class> interfaceClass, boolean ignoreInvalidMethodConfig) {
String methodName = methodConfig.getName();
if (StringUtils.isEmpty(methodName)) {
@@ -922,4 +932,5 @@ public abstract class AbstractInterfaceConfig extends AbstractMethodConfig {
public void setInterfaceClassLoader(ClassLoader interfaceClassLoader) {
this.interfaceClassLoader = interfaceClassLoader;
}
+
}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/Constants.java b/dubbo-common/src/main/java/org/apache/dubbo/config/Constants.java
index cd65fc4119..ed2e821368 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/Constants.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/Constants.java
@@ -150,4 +150,6 @@ public interface Constants {
String SERVER_THREAD_POOL_NAME = "DubboServerHandler";
String CLIENT_THREAD_POOL_NAME = "DubboClientHandler";
+
+ String REST_PROTOCOL="rest";
}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/ServiceConfigBase.java b/dubbo-common/src/main/java/org/apache/dubbo/config/ServiceConfigBase.java
index 57f850c18c..78e6bf1c16 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/ServiceConfigBase.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/ServiceConfigBase.java
@@ -51,7 +51,6 @@ public abstract class ServiceConfigBase extends AbstractServiceConfig {
private static final long serialVersionUID = 3033787999037024738L;
-
/**
* The interface class of the exported service
*/
@@ -173,8 +172,8 @@ public abstract class ServiceConfigBase extends AbstractServiceConfig {
convertProviderIdToProvider();
if (provider == null) {
provider = getModuleConfigManager()
- .getDefaultProvider()
- .orElseThrow(() -> new IllegalStateException("Default provider is not initialized"));
+ .getDefaultProvider()
+ .orElseThrow(() -> new IllegalStateException("Default provider is not initialized"));
}
// try set properties from `dubbo.service` if not set in current config
refreshWithPrefixes(super.getPrefixes(), ConfigMode.OVERRIDE_IF_ABSENT);
@@ -228,7 +227,7 @@ public abstract class ServiceConfigBase extends AbstractServiceConfig {
protected void convertProviderIdToProvider() {
if (provider == null && StringUtils.hasText(providerIds)) {
provider = getModuleConfigManager().getProvider(providerIds)
- .orElseThrow(() -> new IllegalStateException("Provider config not found: " + providerIds));
+ .orElseThrow(() -> new IllegalStateException("Provider config not found: " + providerIds));
}
}
@@ -250,7 +249,7 @@ public abstract class ServiceConfigBase extends AbstractServiceConfig {
if (globalProtocol.isPresent()) {
tmpProtocols.add(globalProtocol.get());
} else {
- throw new IllegalStateException("Protocol not found: "+id);
+ throw new IllegalStateException("Protocol not found: " + id);
}
}
setProtocols(tmpProtocols);
@@ -267,7 +266,7 @@ public abstract class ServiceConfigBase extends AbstractServiceConfig {
try {
if (StringUtils.isNotEmpty(interfaceName)) {
this.interfaceClass = Class.forName(interfaceName, true, Thread.currentThread()
- .getContextClassLoader());
+ .getContextClassLoader());
}
} catch (ClassNotFoundException t) {
throw new IllegalStateException(t.getMessage(), t);
@@ -285,9 +284,9 @@ public abstract class ServiceConfigBase extends AbstractServiceConfig {
}
-
public void setInterface(Class> interfaceClass) {
- if (interfaceClass != null && !interfaceClass.isInterface()) {
+ // rest protocol allow set impl class
+ if (interfaceClass != null && !interfaceClass.isInterface() && !canSkipInterfaceCheck()) {
throw new IllegalStateException("The interface class " + interfaceClass + " is not a interface!");
}
this.interfaceClass = interfaceClass;
@@ -297,6 +296,23 @@ public abstract class ServiceConfigBase extends AbstractServiceConfig {
}
}
+ @Override
+ public boolean canSkipInterfaceCheck() {
+ // for multipart protocol so for each contain
+ List protocols = getProtocols();
+
+ if (protocols == null) {
+ return false;
+ }
+
+ for (ProtocolConfig protocol : protocols) {
+ if (Constants.REST_PROTOCOL.equals(protocol.getName())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
@Transient
public T getRef() {
return ref;
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ControllerServiceConfigTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ControllerServiceConfigTest.java
new file mode 100644
index 0000000000..6f059143a8
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/ControllerServiceConfigTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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;
+
+import org.apache.dubbo.config.ApplicationConfig;
+import org.apache.dubbo.config.ProtocolConfig;
+import org.apache.dubbo.config.ServiceConfig;
+import org.apache.dubbo.config.spring.api.SpringControllerService;
+import org.junit.jupiter.api.Test;
+
+public class ControllerServiceConfigTest {
+
+ @Test
+ void testServiceConfig() {
+
+ ServiceConfig serviceServiceConfig = new ServiceConfig<>();
+ ApplicationConfig applicationConfig = new ApplicationConfig();
+ applicationConfig.setName("dubbo");
+ serviceServiceConfig.setApplication(applicationConfig);
+ serviceServiceConfig.setProtocol(new ProtocolConfig("rest",8080));
+ serviceServiceConfig.setRef(new SpringControllerService());
+ serviceServiceConfig.setInterface(SpringControllerService.class.getName());
+ serviceServiceConfig.export();
+ serviceServiceConfig.unexport();
+
+
+ }
+}
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/api/SpringControllerService.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/api/SpringControllerService.java
new file mode 100644
index 0000000000..da2866f85f
--- /dev/null
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/api/SpringControllerService.java
@@ -0,0 +1,29 @@
+/*
+ * 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.api;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+@RequestMapping("/controller")
+public class SpringControllerService {
+
+ @GetMapping("/sayHello")
+ public String sayHello(String say) {
+ return say;
+ }
+}
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/ServiceConfigTest.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/ServiceConfigTest.java
new file mode 100644
index 0000000000..e37f646c08
--- /dev/null
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/ServiceConfigTest.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.dubbo.rpc.protocol.rest;
+
+import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.extension.ExtensionLoader;
+import org.apache.dubbo.common.utils.NetUtils;
+import org.apache.dubbo.remoting.http.RequestTemplate;
+import org.apache.dubbo.remoting.http.config.HttpClientConfig;
+import org.apache.dubbo.remoting.http.restclient.OKHttpRestClient;
+import org.apache.dubbo.rpc.Exporter;
+import org.apache.dubbo.rpc.Protocol;
+import org.apache.dubbo.rpc.ProxyFactory;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.rpc.model.FrameworkModel;
+import org.apache.dubbo.rpc.model.ModuleServiceRepository;
+import org.apache.dubbo.rpc.model.ProviderModel;
+import org.apache.dubbo.rpc.model.ServiceDescriptor;
+import org.apache.dubbo.rpc.protocol.rest.constans.RestConstant;
+import org.apache.dubbo.rpc.protocol.rest.mvc.SpringControllerService;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+
+public class ServiceConfigTest {
+
+ private final Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest");
+ private final ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
+ private final ModuleServiceRepository repository = ApplicationModel.defaultModel().getDefaultModule().getServiceRepository();
+
+ @AfterEach
+ public void tearDown() {
+ protocol.destroy();
+ FrameworkModel.destroyAll();
+ }
+
+ @Test
+ void testControllerService() throws Exception {
+
+ int availablePort = NetUtils.getAvailablePort();
+ URL url = URL.valueOf("rest://127.0.0.1:" + availablePort + "/?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.mvc.SpringControllerService");
+
+ SpringControllerService server = new SpringControllerService();
+
+ url = this.registerProvider(url, server, SpringControllerService.class);
+
+ Exporter exporter = protocol.export(proxy.getInvoker(server, SpringControllerService.class, url));
+
+ OKHttpRestClient okHttpRestClient = new OKHttpRestClient(new HttpClientConfig());
+
+ RequestTemplate requestTemplate = new RequestTemplate(null, "GET", "127.0.0.1:" + availablePort);
+ requestTemplate.path("/controller/sayHello?say=dubbo");
+ requestTemplate.addHeader(RestConstant.CONTENT_TYPE, "text/plain");
+ requestTemplate.addHeader(RestConstant.ACCEPT, "text/plain");
+ requestTemplate.addHeader(RestHeaderEnum.VERSION.getHeader(), "1.0.0");
+
+ byte[] body = okHttpRestClient.send(requestTemplate).get().getBody();
+
+
+ Assertions.assertEquals("dubbo", new String(body));
+ exporter.unexport();
+ }
+
+
+ private URL registerProvider(URL url, Object impl, Class> interfaceClass) {
+ ServiceDescriptor serviceDescriptor = repository.registerService(interfaceClass);
+ ProviderModel providerModel = new ProviderModel(
+ url.getServiceKey(),
+ impl,
+ serviceDescriptor,
+ null,
+ null);
+ repository.registerProvider(providerModel);
+ return url.setServiceModel(providerModel);
+ }
+}
diff --git a/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringControllerService.java b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringControllerService.java
new file mode 100644
index 0000000000..bf761196d4
--- /dev/null
+++ b/dubbo-rpc/dubbo-rpc-rest/src/test/java/org/apache/dubbo/rpc/protocol/rest/mvc/SpringControllerService.java
@@ -0,0 +1,29 @@
+/*
+ * 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.rpc.protocol.rest.mvc;
+
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+
+@RequestMapping("/controller")
+public class SpringControllerService {
+
+ @GetMapping("/sayHello")
+ public String sayHello(String say) {
+ return say;
+ }
+}
From 868622e23724e41a43ec0640e9b9cad0edfab032 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 2 May 2023 21:47:33 +0800
Subject: [PATCH 49/59] Bump junit-platform-launcher from 1.9.2 to 1.9.3
(#12215)
Bumps [junit-platform-launcher](https://github.com/junit-team/junit5) from 1.9.2 to 1.9.3.
- [Release notes](https://github.com/junit-team/junit5/releases)
- [Commits](https://github.com/junit-team/junit5/commits)
---
updated-dependencies:
- dependency-name: org.junit.platform:junit-platform-launcher
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
dubbo-test/dubbo-test-check/pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dubbo-test/dubbo-test-check/pom.xml b/dubbo-test/dubbo-test-check/pom.xml
index 35cf2ace79..30116dd7e1 100644
--- a/dubbo-test/dubbo-test-check/pom.xml
+++ b/dubbo-test/dubbo-test-check/pom.xml
@@ -36,7 +36,7 @@
3.4.14
4.2.0
1.23.0
- 1.9.2
+ 1.9.3
1.3
2.12.3
From 2c1ede9db0e230cae3ec232d21645c0f91bbb1ff Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 2 May 2023 21:47:42 +0800
Subject: [PATCH 50/59] Bump junit_jupiter_version from 5.9.2 to 5.9.3 (#12219)
Bumps `junit_jupiter_version` from 5.9.2 to 5.9.3.
Updates `junit-jupiter-engine` from 5.9.2 to 5.9.3
- [Release notes](https://github.com/junit-team/junit5/releases)
- [Commits](https://github.com/junit-team/junit5/compare/r5.9.2...r5.9.3)
Updates `junit-jupiter-api` from 5.9.2 to 5.9.3
- [Release notes](https://github.com/junit-team/junit5/releases)
- [Commits](https://github.com/junit-team/junit5/compare/r5.9.2...r5.9.3)
Updates `junit-jupiter-params` from 5.9.2 to 5.9.3
- [Release notes](https://github.com/junit-team/junit5/releases)
- [Commits](https://github.com/junit-team/junit5/compare/r5.9.2...r5.9.3)
Updates `junit-vintage-engine` from 5.9.2 to 5.9.3
- [Release notes](https://github.com/junit-team/junit5/releases)
- [Commits](https://github.com/junit-team/junit5/compare/r5.9.2...r5.9.3)
---
updated-dependencies:
- dependency-name: org.junit.jupiter:junit-jupiter-engine
dependency-type: direct:production
update-type: version-update:semver-patch
- dependency-name: org.junit.jupiter:junit-jupiter-api
dependency-type: direct:development
update-type: version-update:semver-patch
- dependency-name: org.junit.jupiter:junit-jupiter-params
dependency-type: direct:production
update-type: version-update:semver-patch
- dependency-name: org.junit.vintage:junit-vintage-engine
dependency-type: direct:development
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
index 503b21fdbb..a84311593d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -88,7 +88,7 @@
- 5.9.2
+ 5.9.3
4.2.0
3.12.13
2.2
From 120db0bf15e7a94c225ece528221903b5c8f04d4 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 2 May 2023 21:47:51 +0800
Subject: [PATCH 51/59] Bump jacoco-maven-plugin from 0.8.8 to 0.8.10 (#12218)
Bumps [jacoco-maven-plugin](https://github.com/jacoco/jacoco) from 0.8.8 to 0.8.10.
- [Release notes](https://github.com/jacoco/jacoco/releases)
- [Commits](https://github.com/jacoco/jacoco/compare/v0.8.8...v0.8.10)
---
updated-dependencies:
- dependency-name: org.jacoco:jacoco-maven-plugin
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/pom.xml b/pom.xml
index a84311593d..1f1e5e83db 100644
--- a/pom.xml
+++ b/pom.xml
@@ -121,7 +121,7 @@
3.5.0
9.4.51.v20230217
3.2.1
- 0.8.8
+ 0.8.10
1.4.1
3.3.0
3.1.0
From 87bc7e69d1f79578a0ab8ccd9a74694227ae5e5a Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 2 May 2023 21:48:01 +0800
Subject: [PATCH 52/59] Bump zipkin-reporter-bom from 2.16.3 to 2.16.4 (#12217)
Bumps [zipkin-reporter-bom](https://github.com/openzipkin/zipkin-reporter-java) from 2.16.3 to 2.16.4.
- [Release notes](https://github.com/openzipkin/zipkin-reporter-java/releases)
- [Changelog](https://github.com/openzipkin/zipkin-reporter-java/blob/master/RELEASE.md)
- [Commits](https://github.com/openzipkin/zipkin-reporter-java/compare/2.16.3...2.16.4)
---
updated-dependencies:
- dependency-name: io.zipkin.reporter2:zipkin-reporter-bom
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
.../dubbo-spring-boot-observability-starters/pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dubbo-spring-boot/dubbo-spring-boot-observability-starters/pom.xml b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/pom.xml
index 8e334877a9..25ea3640fd 100644
--- a/dubbo-spring-boot/dubbo-spring-boot-observability-starters/pom.xml
+++ b/dubbo-spring-boot/dubbo-spring-boot-observability-starters/pom.xml
@@ -40,7 +40,7 @@
1.10.6
1.0.4
1.25.0
- 2.16.3
+ 2.16.4
0.16.0
From ac3509c4f107516a08291dd26030037e975ef79f Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 2 May 2023 21:48:15 +0800
Subject: [PATCH 53/59] Bump jedis from 3.9.0 to 3.10.0 (#12216)
Bumps [jedis](https://github.com/redis/jedis) from 3.9.0 to 3.10.0.
- [Release notes](https://github.com/redis/jedis/releases)
- [Commits](https://github.com/redis/jedis/compare/v3.9.0...v3.10.0)
---
updated-dependencies:
- dependency-name: redis.clients:jedis
dependency-type: direct:production
update-type: version-update:semver-minor
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
dubbo-dependencies-bom/pom.xml | 2 +-
dubbo-metadata/dubbo-metadata-report-redis/pom.xml | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml
index 5399be5a43..1abd67df62 100644
--- a/dubbo-dependencies-bom/pom.xml
+++ b/dubbo-dependencies-bom/pom.xml
@@ -106,7 +106,7 @@
3.4.14
4.3.0
2.12.0
- 3.9.0
+ 3.10.0
1.4.5
2.2.1
1.5.3
diff --git a/dubbo-metadata/dubbo-metadata-report-redis/pom.xml b/dubbo-metadata/dubbo-metadata-report-redis/pom.xml
index 91fd5690ef..708178b432 100644
--- a/dubbo-metadata/dubbo-metadata-report-redis/pom.xml
+++ b/dubbo-metadata/dubbo-metadata-report-redis/pom.xml
@@ -24,7 +24,7 @@
dubbo-metadata-report-redis
- 3.9.0
+ 3.10.0
From d711c0ca85b5fa0cc28bc33b5891201b04c5f534 Mon Sep 17 00:00:00 2001
From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com>
Date: Tue, 2 May 2023 21:48:26 +0800
Subject: [PATCH 54/59] Bump netty4_version from 4.1.91.Final to 4.1.92.Final
(#12214)
Bumps `netty4_version` from 4.1.91.Final to 4.1.92.Final.
Updates `netty-all` from 4.1.91.Final to 4.1.92.Final
- [Release notes](https://github.com/netty/netty/releases)
- [Commits](https://github.com/netty/netty/compare/netty-4.1.91.Final...netty-4.1.92.Final)
Updates `netty-bom` from 4.1.91.Final to 4.1.92.Final
- [Release notes](https://github.com/netty/netty/releases)
- [Commits](https://github.com/netty/netty/compare/netty-4.1.91.Final...netty-4.1.92.Final)
---
updated-dependencies:
- dependency-name: io.netty:netty-all
dependency-type: direct:development
update-type: version-update:semver-patch
- dependency-name: io.netty:netty-bom
dependency-type: direct:production
update-type: version-update:semver-patch
...
Signed-off-by: dependabot[bot]
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
---
dubbo-dependencies-bom/pom.xml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml
index 1abd67df62..469c30b205 100644
--- a/dubbo-dependencies-bom/pom.xml
+++ b/dubbo-dependencies-bom/pom.xml
@@ -96,7 +96,7 @@
3.29.2-GA
1.14.4
3.2.10.Final
- 4.1.91.Final
+ 4.1.92.Final
2.2.1
2.4.4
4.5.14
From acd4212f597813411b6634377f96ac2bb3888a07 Mon Sep 17 00:00:00 2001
From: wxbty <38374721+wxbty@users.noreply.github.com>
Date: Tue, 2 May 2023 21:56:54 +0800
Subject: [PATCH 55/59] refactor metrics defaultCollector&&agg (#12206)
* remove unused generic
* use request event
* fix test
* add licence
* bugfix
* code opt
* code opt
* code opt
* code opt
* code opt
* code opt
* fix ci
* fix
* revert
* opt
* opt
* bugfix
---------
Co-authored-by: x-shadow-man <1494445739@qq.com>
---
.../filter/support/MetricsClusterFilter.java | 21 +-
.../apache/dubbo/common/lang/Nullable.java | 26 +-
.../dubbo/common/utils/ReflectionUtils.java | 53 +++-
.../dubbo/metrics/MetricsConstants.java | 5 +
.../collector/CombMetricsCollector.java | 30 ++-
.../MethodMetricsCollector.java} | 32 +--
.../collector/ServiceMetricsCollector.java | 10 +-
.../data/ApplicationStatComposite.java | 13 +-
.../dubbo/metrics/data/BaseStatComposite.java | 48 +++-
.../metrics/data/MethodStatComposite.java | 71 +++++
.../dubbo/metrics/data/RtStatComposite.java | 23 +-
.../metrics/data/ServiceStatComposite.java | 34 +--
.../dubbo/metrics/event/EmptyEvent.java | 2 +-
.../dubbo/metrics/event/MethodEvent.java | 47 ----
.../dubbo/metrics/event/MetricsEvent.java | 13 +-
.../dubbo/metrics/event/MetricsEventBus.java | 81 ++++--
.../dubbo/metrics/event/TimeCounterEvent.java | 5 +-
.../listener/AbstractMetricsKeyListener.java | 83 ++++++
.../listener/AbstractMetricsListener.java | 53 +---
.../listener/MetricsApplicationListener.java | 17 +-
.../metrics/listener/MetricsListener.java | 10 +-
.../listener/MetricsServiceListener.java | 30 +--
.../dubbo/metrics/model/MethodMetric.java | 32 ++-
.../dubbo/metrics/model/MetricsSupport.java | 133 ++++++++++
.../metrics/model/key/CategoryOverall.java | 9 +-
.../dubbo/metrics/model/key/MetricsCat.java | 19 +-
.../dubbo/metrics/model/key/MetricsKey.java | 6 +-
.../metrics/model/key/MetricsKeyWrapper.java | 59 ++++-
.../dubbo/metrics/model/key/MetricsLevel.java | 2 +-
...sPlaceType.java => MetricsPlaceValue.java} | 29 ++-
.../dubbo/metrics/model/key/TypeWrapper.java | 1 +
.../model/sample/CounterMetricSample.java | 11 +-
.../model/sample/GaugeMetricSample.java | 5 +
.../dubbo/metrics/report/MetricsExport.java | 4 +-
.../SimpleMetricsEventMulticasterTest.java | 11 +-
.../ConfigCenterMetricsCollector.java | 13 +-
.../config/event/ConfigCenterEvent.java | 3 +-
...er.java => ConfigCenterSubDispatcher.java} | 8 +-
.../dubbo/metrics/DefaultConstants.java | 70 +++++
.../metrics/MetricsScopeModelInitializer.java | 2 -
.../collector/AggregateMetricsCollector.java | 160 +++++++-----
.../collector/DefaultMetricsCollector.java | 77 +++---
.../collector/HistogramMetricsCollector.java | 40 ++-
.../sample/MethodMetricsSampler.java | 133 ----------
.../sample/MetricsCountSampleConfigurer.java | 10 -
.../collector/sample/MetricsCountSampler.java | 19 --
.../sample/SimpleMetricsCountSampler.java | 144 -----------
.../metrics/event/DefaultSubDispatcher.java | 113 ++++++++
.../metrics/event/RequestBeforeEvent.java | 48 ++++
.../dubbo/metrics/event/RequestEvent.java | 71 +++++
.../filter/MethodMetricsInterceptor.java | 110 --------
.../dubbo/metrics/filter/MetricsFilter.java | 57 ++--
...e.dubbo.metrics.collector.MetricsCollector | 1 +
.../AggregateMetricsCollectorTest.java | 182 +++++++------
.../collector/DefaultCollectorTest.java | 244 ++++++++++++++++++
.../metrics/filter/MetricsFilterTest.java | 14 +-
.../DefaultMetricsCollectorTest.java | 209 ---------------
.../metrics/sampler/CountSamplerTest.java | 192 --------------
.../metrics/sampler/MethodMetricsTest.java | 132 ----------
.../metadata/MetadataMetricsConstants.java | 15 +-
.../collector/MetadataMetricsCollector.java | 25 +-
.../metrics/metadata/event/MetadataEvent.java | 3 +-
...caster.java => MetadataSubDispatcher.java} | 4 +-
.../MetadataMetricsCollectorTest.java | 15 +-
.../metadata/MetadataStatCompositeTest.java | 13 +-
.../registry/RegistryMetricsConstants.java | 33 ++-
.../collector/RegistryMetricsCollector.java | 26 +-
.../metrics/registry/event/RegistryEvent.java | 5 +-
...caster.java => RegistrySubDispatcher.java} | 15 +-
.../RegistryMetricsCollectorTest.java | 44 +++-
.../collector/RegistryStatCompositeTest.java | 44 ++--
71 files changed, 1716 insertions(+), 1606 deletions(-)
rename dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/event/RTEventTest.java => dubbo-common/src/main/java/org/apache/dubbo/common/lang/Nullable.java (56%)
rename dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/{event/RTEvent.java => collector/MethodMetricsCollector.java} (58%)
create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/MethodStatComposite.java
delete mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MethodEvent.java
create mode 100644 dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/AbstractMetricsKeyListener.java
rename dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/{MetricsPlaceType.java => MetricsPlaceValue.java} (58%)
rename dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/event/{ConfigCenterMetricsDispatcher.java => ConfigCenterSubDispatcher.java} (87%)
create mode 100644 dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/DefaultConstants.java
delete mode 100644 dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java
create mode 100644 dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/event/DefaultSubDispatcher.java
create mode 100644 dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/event/RequestBeforeEvent.java
create mode 100644 dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/event/RequestEvent.java
delete mode 100644 dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/filter/MethodMetricsInterceptor.java
create mode 100644 dubbo-metrics/dubbo-metrics-default/src/main/resources/META-INF/dubbo/internal/org.apache.dubbo.metrics.collector.MetricsCollector
create mode 100644 dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/collector/DefaultCollectorTest.java
delete mode 100644 dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/collector/DefaultMetricsCollectorTest.java
delete mode 100644 dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/sampler/CountSamplerTest.java
delete mode 100644 dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/sampler/MethodMetricsTest.java
rename dubbo-metrics/dubbo-metrics-metadata/src/main/java/org/apache/dubbo/metrics/metadata/event/{MetadataMetricsEventMulticaster.java => MetadataSubDispatcher.java} (96%)
rename dubbo-metrics/dubbo-metrics-registry/src/main/java/org/apache/dubbo/metrics/registry/event/{RegistryMetricsEventMulticaster.java => RegistrySubDispatcher.java} (91%)
diff --git a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/support/MetricsClusterFilter.java b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/support/MetricsClusterFilter.java
index 2a4d36dcfc..9cc8f19535 100644
--- a/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/support/MetricsClusterFilter.java
+++ b/dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/filter/support/MetricsClusterFilter.java
@@ -20,7 +20,8 @@ package org.apache.dubbo.rpc.cluster.filter.support;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
-import org.apache.dubbo.metrics.event.MetricsEvent;
+import org.apache.dubbo.metrics.event.MetricsEventBus;
+import org.apache.dubbo.metrics.event.RequestBeforeEvent;
import org.apache.dubbo.rpc.BaseFilter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
@@ -30,19 +31,17 @@ import org.apache.dubbo.rpc.cluster.filter.ClusterFilter;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ScopeModelAware;
-
-import java.util.Optional;
-
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER;
-import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
-@Activate(group = CONSUMER,onClass = "org.apache.dubbo.metrics.collector.DefaultMetricsCollector")
+@Activate(group = CONSUMER, onClass = "org.apache.dubbo.metrics.collector.DefaultMetricsCollector")
public class MetricsClusterFilter implements ClusterFilter, BaseFilter.Listener, ScopeModelAware {
+ private ApplicationModel applicationModel;
private DefaultMetricsCollector collector;
@Override
public void setApplicationModel(ApplicationModel applicationModel) {
+ this.applicationModel = applicationModel;
this.collector = applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class);
}
@@ -65,18 +64,12 @@ public class MetricsClusterFilter implements ClusterFilter, BaseFilter.Listener,
if (collector == null || !collector.isCollectEnabled()) {
return;
}
- if (t != null && t instanceof RpcException) {
+ if (t instanceof RpcException) {
RpcException e = (RpcException) t;
if (e.isForbidden()) {
- collector.getMethodSampler().incOnEvent(invocation,
- MetricsEvent.Type.SERVICE_UNAVAILABLE.getNameByType(getSide(invocation)));
+ MetricsEventBus.publish(RequestBeforeEvent.toEvent(applicationModel, invocation));
}
}
}
- private String getSide(Invocation invocation) {
- Optional extends Invoker>> invoker = Optional.ofNullable(invocation.getInvoker());
- String side = invoker.isPresent() ? invoker.get().getUrl().getSide() : PROVIDER_SIDE;
- return side;
- }
}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/event/RTEventTest.java b/dubbo-common/src/main/java/org/apache/dubbo/common/lang/Nullable.java
similarity index 56%
rename from dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/event/RTEventTest.java
rename to dubbo-common/src/main/java/org/apache/dubbo/common/lang/Nullable.java
index a69c166318..1114492cc1 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/test/java/org/apache/dubbo/metrics/metrics/event/RTEventTest.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/lang/Nullable.java
@@ -15,23 +15,17 @@
* limitations under the License.
*/
-package org.apache.dubbo.metrics.metrics.event;
+package org.apache.dubbo.common.lang;
-import org.apache.dubbo.metrics.event.RTEvent;
-import org.apache.dubbo.metrics.model.MethodMetric;
-import org.apache.dubbo.rpc.model.ApplicationModel;
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
-class RTEventTest {
- @Test
- void testNewEvent() {
- MethodMetric metric = new MethodMetric();
- Long rt = 5L;
- RTEvent event = new RTEvent(ApplicationModel.defaultModel(), metric, rt);
-
- Assertions.assertEquals(event.getSource(), ApplicationModel.defaultModel());
- Assertions.assertEquals(event.getRt(), rt);
- }
+@Target({ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface Nullable {
}
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectionUtils.java b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectionUtils.java
index b659dcb97b..b6b686e50b 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectionUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ReflectionUtils.java
@@ -18,7 +18,12 @@ package org.apache.dubbo.common.utils;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
import java.util.Arrays;
+import java.util.List;
+import java.util.stream.Collectors;
/**
* A utility class that provides methods for accessing and manipulating private fields and methods of an object.
@@ -28,7 +33,8 @@ import java.util.Arrays;
*/
public class ReflectionUtils {
- private ReflectionUtils(){}
+ private ReflectionUtils() {
+ }
/**
* Retrieves the value of the specified field from the given object.
@@ -92,7 +98,50 @@ public class ReflectionUtils {
return true;
}
- public static class ReflectionException extends RuntimeException{
+ /**
+ * Returns a list of distinct {@link Class} objects representing the generics of the given class that implement the
+ * given interface.
+ *
+ * @param clazz the class to retrieve the generics for
+ * @param interfaceClass the interface to retrieve the generics for
+ * @return a list of distinct {@link Class} objects representing the generics of the given class that implement the
+ * given interface
+ */
+ public static List> getClassGenerics(Class> clazz, Class> interfaceClass) {
+ List> generics = new ArrayList<>();
+ Type[] genericInterfaces = clazz.getGenericInterfaces();
+ for (Type genericInterface : genericInterfaces) {
+ if (genericInterface instanceof ParameterizedType) {
+ ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
+ Type rawType = parameterizedType.getRawType();
+ if (rawType instanceof Class && interfaceClass.isAssignableFrom((Class>) rawType)) {
+ Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
+ for (Type actualTypeArgument : actualTypeArguments) {
+ if (actualTypeArgument instanceof Class) {
+ generics.add((Class>) actualTypeArgument);
+ }
+ }
+ }
+ }
+ }
+ Type genericSuperclass = clazz.getGenericSuperclass();
+ if (genericSuperclass instanceof ParameterizedType) {
+ ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
+ Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
+ for (Type actualTypeArgument : actualTypeArguments) {
+ if (actualTypeArgument instanceof Class) {
+ generics.add((Class>) actualTypeArgument);
+ }
+ }
+ }
+ Class> superclass = clazz.getSuperclass();
+ if (superclass != null) {
+ generics.addAll(getClassGenerics(superclass, interfaceClass));
+ }
+ return generics.stream().distinct().collect(Collectors.toList());
+ }
+
+ public static class ReflectionException extends RuntimeException {
public ReflectionException(Throwable cause) {
super(cause);
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricsConstants.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricsConstants.java
index a4c7516321..3c3871c0d7 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricsConstants.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/MetricsConstants.java
@@ -19,6 +19,11 @@ package org.apache.dubbo.metrics;
public interface MetricsConstants {
+ String INVOCATION = "metric_filter_invocation";
+ String INVOCATION_METRICS_COUNTER = "metric_filter_invocation_counter";
+
+ String INVOCATION_SIDE = "metric_filter_side";
+
String ATTACHMENT_KEY_SERVICE = "serviceKey";
String ATTACHMENT_KEY_SIZE = "size";
String ATTACHMENT_KEY_LAST_NUM_MAP = "lastNumMap";
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/CombMetricsCollector.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/CombMetricsCollector.java
index f9eb9ac5c6..03f9478052 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/CombMetricsCollector.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/CombMetricsCollector.java
@@ -20,15 +20,18 @@ package org.apache.dubbo.metrics.collector;
import org.apache.dubbo.metrics.data.BaseStatComposite;
import org.apache.dubbo.metrics.event.MetricsEventMulticaster;
import org.apache.dubbo.metrics.event.TimeCounterEvent;
+import org.apache.dubbo.metrics.listener.AbstractMetricsListener;
import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.key.MetricsKey;
-import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
+import org.apache.dubbo.rpc.Invocation;
import java.util.List;
import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;
-public abstract class CombMetricsCollector implements ApplicationMetricsCollector, ServiceMetricsCollector {
+public abstract class CombMetricsCollector extends AbstractMetricsListener implements ApplicationMetricsCollector, ServiceMetricsCollector, MethodMetricsCollector {
private final BaseStatComposite stats;
private MetricsEventMulticaster eventMulticaster;
@@ -43,7 +46,7 @@ public abstract class CombMetricsCollector implement
}
@Override
- public void setNum(MetricsKey metricsKey, String applicationName, String serviceKey, int num) {
+ public void setNum(MetricsKeyWrapper metricsKey, String applicationName, String serviceKey, int num) {
this.stats.setServiceKey(metricsKey, applicationName, serviceKey, num);
}
@@ -52,8 +55,8 @@ public abstract class CombMetricsCollector implement
this.stats.incrementApp(metricsKey, applicationName, SELF_INCREMENT_SIZE);
}
- public void increment(String applicationName, String serviceKey, MetricsKey metricsKey, int size) {
- this.stats.incrementServiceKey(metricsKey, applicationName, serviceKey, size);
+ public void increment(String applicationName, String serviceKey, MetricsKeyWrapper metricsKeyWrapper, int size) {
+ this.stats.incrementServiceKey(metricsKeyWrapper, applicationName, serviceKey, size);
}
@Override
@@ -65,11 +68,24 @@ public abstract class CombMetricsCollector implement
stats.calcServiceKeyRt(applicationName, serviceKey, registryOpType, responseTime);
}
- @SuppressWarnings({"rawtypes"})
- protected List export(MetricsCategory category) {
+ @Override
+ public void increment(String applicationName, Invocation invocation, MetricsKeyWrapper wrapper, int size) {
+ this.stats.incrementMethodKey(wrapper, applicationName, invocation, size);
+ }
+
+ @Override
+ public void addRt(String applicationName, Invocation invocation, String registryOpType, Long responseTime) {
+ stats.calcMethodKeyRt(applicationName, invocation, registryOpType, responseTime);
+ }
+
+ protected List export(MetricsCategory category) {
return stats.export(category);
}
+ public MetricsEventMulticaster getEventMulticaster() {
+ return eventMulticaster;
+ }
+
@Override
public void onEvent(TimeCounterEvent event) {
eventMulticaster.publishEvent(event);
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/RTEvent.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/MethodMetricsCollector.java
similarity index 58%
rename from dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/RTEvent.java
rename to dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/MethodMetricsCollector.java
index 18256ffda4..25732f3416 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/RTEvent.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/MethodMetricsCollector.java
@@ -15,33 +15,19 @@
* limitations under the License.
*/
-package org.apache.dubbo.metrics.event;
+package org.apache.dubbo.metrics.collector;
-import org.apache.dubbo.rpc.model.ApplicationModel;
+import org.apache.dubbo.metrics.event.TimeCounterEvent;
+import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
+import org.apache.dubbo.rpc.Invocation;
/**
- * RtEvent.
+ * Method-level metrics collection for rpc invocation scenarios
*/
-public class RTEvent extends MetricsEvent {
- private Long rt;
- private final Object metric;
+public interface MethodMetricsCollector extends MetricsCollector {
- public RTEvent(ApplicationModel applicationModel, Object metric, Long rt) {
- super(applicationModel);
- this.rt = rt;
- this.metric = metric;
- setAvailable(true);
- }
+ void increment(String applicationName, Invocation invocation, MetricsKeyWrapper wrapper, int size);
- public Long getRt() {
- return rt;
- }
-
- public void setRt(Long rt) {
- this.rt = rt;
- }
-
- public Object getMetric() {
- return metric;
- }
+ void addRt(String applicationName, Invocation invocation, String registryOpType, Long responseTime);
}
+
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ServiceMetricsCollector.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ServiceMetricsCollector.java
index b4876716bf..af0aabc635 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ServiceMetricsCollector.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/collector/ServiceMetricsCollector.java
@@ -18,19 +18,17 @@
package org.apache.dubbo.metrics.collector;
import org.apache.dubbo.metrics.event.TimeCounterEvent;
-import org.apache.dubbo.metrics.model.key.MetricsKey;
+import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
/**
- * Application-level collector.
+ * Service-level collector.
* registration center, configuration center and other scenarios
- *
- * @Params metrics type
*/
public interface ServiceMetricsCollector extends MetricsCollector {
- void increment(String applicationName, String serviceKey, MetricsKey metricsKey, int size);
+ void increment(String applicationName, String serviceKey, MetricsKeyWrapper wrapper, int size);
- void setNum(MetricsKey metricsKey, String applicationName, String serviceKey, int num);
+ void setNum(MetricsKeyWrapper metricsKey, String applicationName, String serviceKey, int num);
void addRt(String applicationName, String serviceKey, String registryOpType, Long responseTime);
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ApplicationStatComposite.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ApplicationStatComposite.java
index 087334bc09..72b23eb234 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ApplicationStatComposite.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ApplicationStatComposite.java
@@ -22,6 +22,7 @@ import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.MetricsSupport;
import org.apache.dubbo.metrics.model.key.MetricsKey;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.metrics.report.MetricsExport;
import java.util.ArrayList;
@@ -48,17 +49,9 @@ public class ApplicationStatComposite implements MetricsExport {
applicationNumStats.get(metricsKey).computeIfAbsent(applicationName, k -> new AtomicLong(0L)).getAndAdd(size);
}
- public void setApplicationKey(MetricsKey metricsKey, String applicationName, int num) {
- if (!applicationNumStats.containsKey(metricsKey)) {
- return;
- }
- applicationNumStats.get(metricsKey).computeIfAbsent(applicationName, k -> new AtomicLong(0L)).set(num);
- }
-
- @SuppressWarnings({"rawtypes"})
- public List export(MetricsCategory category) {
- List list = new ArrayList<>();
+ public List export(MetricsCategory category) {
+ List list = new ArrayList<>();
for (MetricsKey type : applicationNumStats.keySet()) {
Map stringAtomicLongMap = applicationNumStats.get(type);
for (String applicationName : stringAtomicLongMap.keySet()) {
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/BaseStatComposite.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/BaseStatComposite.java
index 0805c9e090..7f99f5e85c 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/BaseStatComposite.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/BaseStatComposite.java
@@ -20,9 +20,10 @@ package org.apache.dubbo.metrics.data;
import org.apache.dubbo.metrics.collector.MetricsCollector;
import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.key.MetricsKey;
-import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
-
+import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.metrics.report.MetricsExport;
+import org.apache.dubbo.rpc.Invocation;
import java.util.ArrayList;
import java.util.List;
@@ -37,14 +38,31 @@ public abstract class BaseStatComposite implements MetricsExport {
private final ApplicationStatComposite applicationStatComposite = new ApplicationStatComposite();
private final ServiceStatComposite serviceStatComposite = new ServiceStatComposite();
+
+ private final MethodStatComposite methodStatComposite = new MethodStatComposite();
private final RtStatComposite rtStatComposite = new RtStatComposite();
public BaseStatComposite() {
- init(applicationStatComposite, serviceStatComposite, rtStatComposite);
+ init(applicationStatComposite);
+ init(serviceStatComposite);
+ init(methodStatComposite);
+ init(rtStatComposite);
}
- protected abstract void init(ApplicationStatComposite applicationStatComposite, ServiceStatComposite serviceStatComposite, RtStatComposite rtStatComposite);
+
+ protected void init(ApplicationStatComposite applicationStatComposite) {
+ }
+
+ protected void init(ServiceStatComposite serviceStatComposite) {
+
+ }
+
+ protected void init(MethodStatComposite methodStatComposite) {
+ }
+
+ protected void init(RtStatComposite rtStatComposite) {
+ }
public void calcApplicationRt(String applicationName, String registryOpType, Long responseTime) {
rtStatComposite.calcApplicationRt(applicationName, registryOpType, responseTime);
@@ -54,29 +72,33 @@ public abstract class BaseStatComposite implements MetricsExport {
rtStatComposite.calcServiceKeyRt(applicationName, serviceKey, registryOpType, responseTime);
}
- public void setServiceKey(MetricsKey metricsKey, String applicationName, String serviceKey, int num) {
- serviceStatComposite.setServiceKey(metricsKey, applicationName, serviceKey, num);
+ public void calcMethodKeyRt(String applicationName, Invocation invocation, String registryOpType, Long responseTime) {
+ rtStatComposite.calcMethodKeyRt(applicationName, invocation, registryOpType, responseTime);
}
- public void setApplicationKey(MetricsKey metricsKey, String applicationName, int num) {
- applicationStatComposite.setApplicationKey(metricsKey, applicationName, num);
+ public void setServiceKey(MetricsKeyWrapper metricsKey, String applicationName, String serviceKey, int num) {
+ serviceStatComposite.setServiceKey(metricsKey, applicationName, serviceKey, num);
}
public void incrementApp(MetricsKey metricsKey, String applicationName, int size) {
applicationStatComposite.incrementSize(metricsKey, applicationName, size);
}
- public void incrementServiceKey(MetricsKey metricsKey, String applicationName, String attServiceKey, int size) {
- serviceStatComposite.incrementServiceKey(metricsKey, applicationName, attServiceKey, size);
+ public void incrementServiceKey(MetricsKeyWrapper metricsKeyWrapper, String applicationName, String attServiceKey, int size) {
+ serviceStatComposite.incrementServiceKey(metricsKeyWrapper, applicationName, attServiceKey, size);
+ }
+
+ public void incrementMethodKey(MetricsKeyWrapper metricsKeyWrapper, String applicationName, Invocation invocation, int size) {
+ methodStatComposite.incrementMethodKey(metricsKeyWrapper, applicationName, invocation, size);
}
@Override
- @SuppressWarnings({"rawtypes"})
- public List export(MetricsCategory category) {
- List list = new ArrayList<>();
+ public List export(MetricsCategory category) {
+ List list = new ArrayList<>();
list.addAll(applicationStatComposite.export(category));
list.addAll(rtStatComposite.export(category));
list.addAll(serviceStatComposite.export(category));
+ list.addAll(methodStatComposite.export(category));
return list;
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/MethodStatComposite.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/MethodStatComposite.java
new file mode 100644
index 0000000000..ad099221bb
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/MethodStatComposite.java
@@ -0,0 +1,71 @@
+/*
+ * 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.data;
+
+import org.apache.dubbo.common.utils.CollectionUtils;
+import org.apache.dubbo.metrics.model.MethodMetric;
+import org.apache.dubbo.metrics.model.MetricsCategory;
+import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
+import org.apache.dubbo.metrics.model.sample.CounterMetricSample;
+import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
+import org.apache.dubbo.metrics.report.MetricsExport;
+import org.apache.dubbo.rpc.Invocation;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+public class MethodStatComposite implements MetricsExport {
+
+ private final Map> methodNumStats = new ConcurrentHashMap<>();
+
+ public void initWrapper(List metricsKeyWrappers) {
+ if (CollectionUtils.isEmpty(metricsKeyWrappers)) {
+ return;
+ }
+ metricsKeyWrappers.forEach(appKey -> methodNumStats.put(appKey, new ConcurrentHashMap<>()));
+ }
+
+ public void incrementMethodKey(MetricsKeyWrapper wrapper, String applicationName, Invocation invocation, int size) {
+ if (!methodNumStats.containsKey(wrapper)) {
+ return;
+ }
+ methodNumStats.get(wrapper).computeIfAbsent(new MethodMetric(applicationName, invocation), k -> new AtomicLong(0L)).getAndAdd(size);
+ }
+
+ public List export(MetricsCategory category) {
+ List list = new ArrayList<>();
+ for (MetricsKeyWrapper wrapper : methodNumStats.keySet()) {
+ Map stringAtomicLongMap = methodNumStats.get(wrapper);
+ for (MethodMetric methodMetric : stringAtomicLongMap.keySet()) {
+ if (methodMetric.getSampleType() == MetricSample.Type.GAUGE) {
+ list.add(new CounterMetricSample<>(wrapper,
+ methodMetric.getTags(), category, stringAtomicLongMap.get(methodMetric)));
+ } else {
+ list.add(new GaugeMetricSample<>(wrapper, methodMetric.getTags(), category, stringAtomicLongMap, value -> value.get(methodMetric).get()));
+ }
+
+ }
+ }
+ return list;
+ }
+
+}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/RtStatComposite.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/RtStatComposite.java
index bf1bd09179..27ba8fe97a 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/RtStatComposite.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/RtStatComposite.java
@@ -24,9 +24,11 @@ import org.apache.dubbo.metrics.model.container.AtomicLongContainer;
import org.apache.dubbo.metrics.model.container.LongAccumulatorContainer;
import org.apache.dubbo.metrics.model.container.LongContainer;
import org.apache.dubbo.metrics.model.key.MetricsKey;
-import org.apache.dubbo.metrics.model.key.MetricsPlaceType;
+import org.apache.dubbo.metrics.model.key.MetricsPlaceValue;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.metrics.report.MetricsExport;
+import org.apache.dubbo.rpc.Invocation;
import java.util.ArrayList;
import java.util.Arrays;
@@ -36,18 +38,19 @@ import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import java.util.stream.Collectors;
+@SuppressWarnings({"rawtypes", "unchecked"})
public class RtStatComposite implements MetricsExport {
private final List> rtStats = new ArrayList<>();
- public void init(MetricsPlaceType... placeValues) {
+ public void init(MetricsPlaceValue... placeValues) {
if (placeValues == null) {
return;
}
Arrays.stream(placeValues).forEach(metricsPlaceType -> rtStats.addAll(initStats(metricsPlaceType)));
}
- private List> initStats(MetricsPlaceType placeValue) {
+ private List> initStats(MetricsPlaceValue placeValue) {
List> singleRtStats = new ArrayList<>();
singleRtStats.add(new AtomicLongContainer(new MetricsKeyWrapper(MetricsKey.METRIC_RT_LAST, placeValue)));
singleRtStats.add(new LongAccumulatorContainer(new MetricsKeyWrapper(MetricsKey.METRIC_RT_MIN, placeValue), new LongAccumulator(Long::min, Long.MAX_VALUE)));
@@ -65,7 +68,6 @@ public class RtStatComposite implements MetricsExport {
return singleRtStats;
}
- @SuppressWarnings({"rawtypes", "unchecked"})
public void calcApplicationRt(String applicationName, String registryOpType, Long responseTime) {
for (LongContainer container : rtStats.stream().filter(longContainer -> longContainer.specifyType(registryOpType)).collect(Collectors.toList())) {
Number current = (Number) ConcurrentHashMapUtils.computeIfAbsent(container, applicationName, container.getInitFunc());
@@ -73,7 +75,6 @@ public class RtStatComposite implements MetricsExport {
}
}
- @SuppressWarnings({"rawtypes", "unchecked"})
public void calcServiceKeyRt(String applicationName, String serviceKey, String registryOpType, Long responseTime) {
for (LongContainer container : rtStats.stream().filter(longContainer -> longContainer.specifyType(registryOpType)).collect(Collectors.toList())) {
Number current = (Number) ConcurrentHashMapUtils.computeIfAbsent(container, applicationName + "_" + serviceKey, container.getInitFunc());
@@ -81,9 +82,15 @@ public class RtStatComposite implements MetricsExport {
}
}
- @SuppressWarnings({"rawtypes"})
- public List export(MetricsCategory category) {
- List list = new ArrayList<>();
+ public void calcMethodKeyRt(String applicationName, Invocation invocation, String registryOpType, Long responseTime) {
+ for (LongContainer container : rtStats.stream().filter(longContainer -> longContainer.specifyType(registryOpType)).collect(Collectors.toList())) {
+ Number current = (Number) ConcurrentHashMapUtils.computeIfAbsent(container, applicationName + "_" + invocation.getServiceName() + "_" + invocation.getMethodName(), container.getInitFunc());
+ container.getConsumerFunc().accept(responseTime, current);
+ }
+ }
+
+ public List export(MetricsCategory category) {
+ List list = new ArrayList<>();
for (LongContainer extends Number> rtContainer : rtStats) {
MetricsKeyWrapper metricsKeyWrapper = rtContainer.getMetricsKeyWrapper();
for (Map.Entry entry : rtContainer.entrySet()) {
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ServiceStatComposite.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ServiceStatComposite.java
index 7b46751392..f1bfc32b1c 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ServiceStatComposite.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/data/ServiceStatComposite.java
@@ -20,8 +20,9 @@ package org.apache.dubbo.metrics.data;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.ServiceKeyMetric;
-import org.apache.dubbo.metrics.model.key.MetricsKey;
+import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.metrics.report.MetricsExport;
import java.util.ArrayList;
@@ -32,36 +33,35 @@ import java.util.concurrent.atomic.AtomicLong;
public class ServiceStatComposite implements MetricsExport {
- private final Map> serviceNumStats = new ConcurrentHashMap<>();
+ private final Map> serviceWrapperNumStats = new ConcurrentHashMap<>();
- public void init(List serviceKeys) {
- if (CollectionUtils.isEmpty(serviceKeys)) {
+ public void initWrapper(List metricsKeyWrappers) {
+ if (CollectionUtils.isEmpty(metricsKeyWrappers)) {
return;
}
- serviceKeys.forEach(appKey -> serviceNumStats.put(appKey, new ConcurrentHashMap<>()));
+ metricsKeyWrappers.forEach(appKey -> serviceWrapperNumStats.put(appKey, new ConcurrentHashMap<>()));
}
- public void incrementServiceKey(MetricsKey metricsKey, String applicationName, String serviceKey, int size) {
- if (!serviceNumStats.containsKey(metricsKey)) {
+ public void incrementServiceKey(MetricsKeyWrapper wrapper, String applicationName, String serviceKey, int size) {
+ if (!serviceWrapperNumStats.containsKey(wrapper)) {
return;
}
- serviceNumStats.get(metricsKey).computeIfAbsent(new ServiceKeyMetric(applicationName, serviceKey), k -> new AtomicLong(0L)).getAndAdd(size);
+ serviceWrapperNumStats.get(wrapper).computeIfAbsent(new ServiceKeyMetric(applicationName, serviceKey), k -> new AtomicLong(0L)).getAndAdd(size);
}
- public void setServiceKey(MetricsKey type, String applicationName, String serviceKey, int num) {
- if (!serviceNumStats.containsKey(type)) {
+ public void setServiceKey(MetricsKeyWrapper wrapper, String applicationName, String serviceKey, int num) {
+ if (!serviceWrapperNumStats.containsKey(wrapper)) {
return;
}
- serviceNumStats.get(type).computeIfAbsent(new ServiceKeyMetric(applicationName, serviceKey), k -> new AtomicLong(0L)).set(num);
+ serviceWrapperNumStats.get(wrapper).computeIfAbsent(new ServiceKeyMetric(applicationName, serviceKey), k -> new AtomicLong(0L)).set(num);
}
- @SuppressWarnings({"rawtypes"})
- public List export(MetricsCategory category) {
- List list = new ArrayList<>();
- for (MetricsKey type : serviceNumStats.keySet()) {
- Map stringAtomicLongMap = serviceNumStats.get(type);
+ public List export(MetricsCategory category) {
+ List list = new ArrayList<>();
+ for (MetricsKeyWrapper wrapper : serviceWrapperNumStats.keySet()) {
+ Map stringAtomicLongMap = serviceWrapperNumStats.get(wrapper);
for (ServiceKeyMetric serviceKeyMetric : stringAtomicLongMap.keySet()) {
- list.add(new GaugeMetricSample<>(type, serviceKeyMetric.getTags(), category, stringAtomicLongMap, value -> value.get(serviceKeyMetric).get()));
+ list.add(new GaugeMetricSample<>(wrapper, serviceKeyMetric.getTags(), category, stringAtomicLongMap, value -> value.get(serviceKeyMetric).get()));
}
}
return list;
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/EmptyEvent.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/EmptyEvent.java
index 7cc8e3e1a2..70c9764dd3 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/EmptyEvent.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/EmptyEvent.java
@@ -27,7 +27,7 @@ public class EmptyEvent extends MetricsEvent {
private static final EmptyEvent empty = new EmptyEvent(null);
private EmptyEvent(ApplicationModel source) {
- super(source);
+ super(source, null);
}
public static EmptyEvent instance() {
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MethodEvent.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MethodEvent.java
deleted file mode 100644
index 3686c03197..0000000000
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MethodEvent.java
+++ /dev/null
@@ -1,47 +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.event;
-
-import org.apache.dubbo.metrics.model.MethodMetric;
-import org.apache.dubbo.rpc.model.ApplicationModel;
-
-public class MethodEvent extends MetricsEvent {
- private String type;
- private final MethodMetric methodMetric;
-
- public MethodEvent(ApplicationModel applicationModel, MethodMetric methodMetric, String type) {
- super(applicationModel);
- this.type = type;
- this.methodMetric = methodMetric;
- setAvailable(true);
- }
-
- public MethodMetric getMethodMetric() {
- return methodMetric;
- }
-
- public String getType() {
- return type;
- }
-
- public void setType(String type) {
- this.type = type;
- }
-
-
-}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java
index 490e36df2c..baa5d812fd 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEvent.java
@@ -35,11 +35,12 @@ public abstract class MetricsEvent {
*/
protected transient ApplicationModel source;
private boolean available = true;
- protected TypeWrapper typeWrapper;
+ private final TypeWrapper typeWrapper;
private final Map attachment = new HashMap<>(8);
- public MetricsEvent(ApplicationModel source) {
+ public MetricsEvent(ApplicationModel source, TypeWrapper typeWrapper) {
+ this.typeWrapper = typeWrapper;
if (source == null) {
this.source = ApplicationModel.defaultModel();
// Appears only in unit tests
@@ -51,8 +52,8 @@ public abstract class MetricsEvent {
@SuppressWarnings("unchecked")
public T getAttachmentValue(String key) {
- if (!attachment.containsKey(key)) {
- throw new MetricsNeverHappenException("Attachment key [" + key + "] not found");
+ if (key == null) {
+ throw new MetricsNeverHappenException("Attachment key is null");
}
return (T) attachment.get(key);
}
@@ -82,6 +83,10 @@ public abstract class MetricsEvent {
return getSource().getApplicationName();
}
+ public TypeWrapper getTypeWrapper() {
+ return typeWrapper;
+ }
+
public boolean isAssignableFrom(Object type) {
return typeWrapper.isAssignableFrom(type);
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEventBus.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEventBus.java
index 193f7feb37..8a4121baa6 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEventBus.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/MetricsEventBus.java
@@ -73,48 +73,77 @@ public class MetricsEventBus {
* @return Biz result
*/
public static T post(MetricsEvent event, Supplier targetSupplier, Function trFunction) {
- if (event.getSource() == null) {
- return targetSupplier.get();
- }
- ApplicationModel applicationModel = event.getSource();
- if (applicationModel.isDestroyed()) {
- return targetSupplier.get();
- }
- ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
- if (beanFactory.isDestroyed()) {
- return targetSupplier.get();
- }
- MetricsDispatcher dispatcher = beanFactory.getBean(MetricsDispatcher.class);
- if (dispatcher == null) {
- return targetSupplier.get();
- }
- dispatcher.publishEvent(event);
- if (!(event instanceof TimeCounterEvent)) {
- return targetSupplier.get();
- }
- TimeCounterEvent timeCounterEvent = (TimeCounterEvent) event;
T result;
+ before(event);
if (trFunction == null) {
try {
result = targetSupplier.get();
} catch (Throwable e) {
- dispatcher.publishErrorEvent(timeCounterEvent);
+ error(event);
throw e;
}
- event.customAfterPost(result);
- dispatcher.publishFinishEvent(timeCounterEvent);
+ after(event, result);
} else {
// Custom failure status
result = targetSupplier.get();
if (trFunction.apply(result)) {
- event.customAfterPost(result);
- dispatcher.publishFinishEvent(timeCounterEvent);
+ after(event, result);
} else {
- dispatcher.publishErrorEvent(timeCounterEvent);
+ error(event);
}
}
return result;
}
+ public static void before(MetricsEvent event) {
+ before(event, null);
+ }
+ /**
+ * Applicable to the scene where execution and return are separated,
+ * eventSaveRunner saves the event, so that the calculation rt is introverted
+ */
+ public static void before(MetricsEvent event, Runnable eventSaveRunner) {
+ MetricsDispatcher dispatcher = validate(event);
+ if (dispatcher == null) return;
+ dispatcher.publishEvent(event);
+ if (eventSaveRunner != null) {
+ eventSaveRunner.run();
+ }
+ }
+
+ public static void after(MetricsEvent event, Object result) {
+ MetricsDispatcher dispatcher = validate(event);
+ if (dispatcher == null) return;
+ event.customAfterPost(result);
+ dispatcher.publishFinishEvent((TimeCounterEvent) event);
+ }
+
+ public static void error(MetricsEvent event) {
+ MetricsDispatcher dispatcher = validate(event);
+ if (dispatcher == null) return;
+ dispatcher.publishErrorEvent((TimeCounterEvent) event);
+ }
+
+ private static MetricsDispatcher validate(MetricsEvent event) {
+ if (event.getSource() == null) {
+ return null;
+ }
+ ApplicationModel applicationModel = event.getSource();
+ if (applicationModel.isDestroyed()) {
+ return null;
+ }
+ ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
+ if (beanFactory.isDestroyed()) {
+ return null;
+ }
+ MetricsDispatcher dispatcher = beanFactory.getBean(MetricsDispatcher.class);
+ if (dispatcher == null) {
+ return null;
+ }
+ if (!(event instanceof TimeCounterEvent)) {
+ return null;
+ }
+ return dispatcher;
+ }
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/TimeCounterEvent.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/TimeCounterEvent.java
index b4a0db1322..c8f91df47e 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/TimeCounterEvent.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/event/TimeCounterEvent.java
@@ -18,6 +18,7 @@
package org.apache.dubbo.metrics.event;
import org.apache.dubbo.metrics.model.TimePair;
+import org.apache.dubbo.metrics.model.key.TypeWrapper;
import org.apache.dubbo.rpc.model.ApplicationModel;
/**
@@ -27,8 +28,8 @@ public abstract class TimeCounterEvent extends MetricsEvent {
private final TimePair timePair;
- public TimeCounterEvent(ApplicationModel source) {
- super(source);
+ public TimeCounterEvent(ApplicationModel source, TypeWrapper typeWrapper) {
+ super(source, typeWrapper);
this.timePair = TimePair.start();
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/AbstractMetricsKeyListener.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/AbstractMetricsKeyListener.java
new file mode 100644
index 0000000000..04c18f5263
--- /dev/null
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/AbstractMetricsKeyListener.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.metrics.listener;
+
+import org.apache.dubbo.metrics.event.MetricsEvent;
+import org.apache.dubbo.metrics.event.MetricsEventBus;
+import org.apache.dubbo.metrics.event.TimeCounterEvent;
+import org.apache.dubbo.metrics.model.key.MetricsKey;
+
+import java.util.function.Consumer;
+
+/**
+ * According to the event template of {@link MetricsEventBus},
+ * build a consistent static method for general and custom monitoring consume methods
+ */
+public abstract class AbstractMetricsKeyListener extends AbstractMetricsListener implements MetricsLifeListener {
+
+ private final MetricsKey metricsKey;
+
+ public AbstractMetricsKeyListener(MetricsKey metricsKey) {
+ this.metricsKey = metricsKey;
+ }
+
+ /**
+ * The MetricsKey type determines whether events are supported
+ */
+ @Override
+ public boolean isSupport(MetricsEvent event) {
+ return super.isSupport(event) && event.isAssignableFrom(metricsKey);
+ }
+
+ @Override
+ public void onEvent(TimeCounterEvent event) {
+
+ }
+
+ public static AbstractMetricsKeyListener onEvent(MetricsKey metricsKey, Consumer postFunc) {
+
+ return new AbstractMetricsKeyListener(metricsKey) {
+ @Override
+ public void onEvent(TimeCounterEvent event) {
+ postFunc.accept(event);
+ }
+ };
+ }
+
+ public static AbstractMetricsKeyListener onFinish(MetricsKey metricsKey, Consumer finishFunc) {
+
+ return new AbstractMetricsKeyListener(metricsKey) {
+ @Override
+ public void onEventFinish(TimeCounterEvent event) {
+ finishFunc.accept(event);
+ }
+ };
+ }
+
+ public static AbstractMetricsKeyListener onError(MetricsKey metricsKey, Consumer errorFunc) {
+
+ return new AbstractMetricsKeyListener(metricsKey) {
+ @Override
+ public void onEventError(TimeCounterEvent event) {
+ errorFunc.accept(event);
+ }
+ };
+ }
+
+
+}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/AbstractMetricsListener.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/AbstractMetricsListener.java
index c588a33ade..5c0247ad6a 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/AbstractMetricsListener.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/AbstractMetricsListener.java
@@ -17,54 +17,21 @@
package org.apache.dubbo.metrics.listener;
+import org.apache.dubbo.common.utils.ReflectionUtils;
import org.apache.dubbo.metrics.event.MetricsEvent;
-import org.apache.dubbo.metrics.event.TimeCounterEvent;
-import org.apache.dubbo.metrics.model.key.MetricsKey;
-import java.util.function.Consumer;
+import java.util.List;
-public abstract class AbstractMetricsListener implements MetricsLifeListener {
+public abstract class AbstractMetricsListener implements MetricsListener {
- private final MetricsKey metricsKey;
-
- public AbstractMetricsListener(MetricsKey metricsKey) {
- this.metricsKey = metricsKey;
+ /**
+ * Whether to support the general determination of event points depends on the event type
+ */
+ public boolean isSupport(MetricsEvent event) {
+ List> eventTypes = ReflectionUtils.getClassGenerics(getClass(), AbstractMetricsListener.class);
+ return event.isAvailable() && eventTypes.stream().allMatch(clazz -> clazz.isInstance(event));
}
@Override
- public boolean isSupport(MetricsEvent event) {
- return event.isAvailable() && event.isAssignableFrom(metricsKey);
- }
-
- public static AbstractMetricsListener onEvent(MetricsKey metricsKey, Consumer postFunc) {
-
- return new AbstractMetricsListener(metricsKey) {
- @Override
- public void onEvent(TimeCounterEvent event) {
- postFunc.accept(event);
- }
- };
- }
-
- public static AbstractMetricsListener onFinish(MetricsKey metricsKey, Consumer finishFunc) {
-
- return new AbstractMetricsListener(metricsKey) {
- @Override
- public void onEventFinish(TimeCounterEvent event) {
- finishFunc.accept(event);
- }
- };
- }
-
- public static AbstractMetricsListener onError(MetricsKey metricsKey, Consumer errorFunc) {
-
- return new AbstractMetricsListener(metricsKey) {
- @Override
- public void onEventError(TimeCounterEvent event) {
- errorFunc.accept(event);
- }
- };
- }
-
-
+ public abstract void onEvent(E event);
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsApplicationListener.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsApplicationListener.java
index d5b6d77ec0..956b518c26 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsApplicationListener.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsApplicationListener.java
@@ -18,24 +18,23 @@
package org.apache.dubbo.metrics.listener;
import org.apache.dubbo.metrics.collector.CombMetricsCollector;
-import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.model.key.MetricsKey;
-import org.apache.dubbo.metrics.model.key.MetricsPlaceType;
+import org.apache.dubbo.metrics.model.key.MetricsPlaceValue;
-public class MetricsApplicationListener extends AbstractMetricsListener {
+public class MetricsApplicationListener extends AbstractMetricsKeyListener {
public MetricsApplicationListener(MetricsKey metricsKey) {
super(metricsKey);
}
- public static AbstractMetricsListener onPostEventBuild(MetricsKey metricsKey, CombMetricsCollector collector) {
- return AbstractMetricsListener.onEvent(metricsKey,
+ public static AbstractMetricsKeyListener onPostEventBuild(MetricsKey metricsKey, CombMetricsCollector collector) {
+ return AbstractMetricsKeyListener.onEvent(metricsKey,
event -> collector.increment(event.appName(), metricsKey)
);
}
- public static AbstractMetricsListener onFinishEventBuild(MetricsKey metricsKey, MetricsPlaceType placeType, CombMetricsCollector collector) {
- return AbstractMetricsListener.onFinish(metricsKey,
+ public static AbstractMetricsKeyListener onFinishEventBuild(MetricsKey metricsKey, MetricsPlaceValue placeType, CombMetricsCollector collector) {
+ return AbstractMetricsKeyListener.onFinish(metricsKey,
event -> {
collector.increment(event.appName(), metricsKey);
collector.addRt(event.appName(), placeType.getType(), event.getTimePair().calc());
@@ -43,8 +42,8 @@ public class MetricsApplicationListener extends AbstractMetricsListener {
);
}
- public static AbstractMetricsListener onErrorEventBuild(MetricsKey metricsKey, MetricsPlaceType placeType, CombMetricsCollector collector) {
- return AbstractMetricsListener.onError(metricsKey,
+ public static AbstractMetricsKeyListener onErrorEventBuild(MetricsKey metricsKey, MetricsPlaceValue placeType, CombMetricsCollector collector) {
+ return AbstractMetricsKeyListener.onError(metricsKey,
event -> {
collector.increment(event.appName(), metricsKey);
collector.addRt(event.appName(), placeType.getType(), event.getTimePair().calc());
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsListener.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsListener.java
index 8f0926796e..4e355802db 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsListener.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsListener.java
@@ -19,21 +19,21 @@ package org.apache.dubbo.metrics.listener;
import org.apache.dubbo.metrics.event.MetricsEvent;
+
/**
* Metrics Listener.
*/
public interface MetricsListener {
- default boolean isSupport(MetricsEvent event) {
- return event.isAvailable();
- }
+
+ boolean isSupport(MetricsEvent event);
/**
* notify event.
*
* @param event BaseMetricsEvent
*/
- default void onEvent(E event) {
- }
+ void onEvent(E event);
+
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsServiceListener.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsServiceListener.java
index 07a76ab19b..18989d78b4 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsServiceListener.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/listener/MetricsServiceListener.java
@@ -19,38 +19,32 @@ package org.apache.dubbo.metrics.listener;
import org.apache.dubbo.metrics.collector.ServiceMetricsCollector;
import org.apache.dubbo.metrics.event.TimeCounterEvent;
+import org.apache.dubbo.metrics.model.MetricsSupport;
import org.apache.dubbo.metrics.model.key.MetricsKey;
-import org.apache.dubbo.metrics.model.key.MetricsPlaceType;
+import org.apache.dubbo.metrics.model.key.MetricsPlaceValue;
-import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SERVICE;
-import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;
-
-public class MetricsServiceListener extends AbstractMetricsListener {
+public class MetricsServiceListener extends AbstractMetricsKeyListener {
public MetricsServiceListener(MetricsKey metricsKey) {
super(metricsKey);
}
- public static AbstractMetricsListener onPostEventBuild(MetricsKey metricsKey, ServiceMetricsCollector collector) {
- return AbstractMetricsListener.onEvent(metricsKey,
- event -> collector.increment(event.appName(), event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), metricsKey, SELF_INCREMENT_SIZE)
+ public static AbstractMetricsKeyListener onPostEventBuild(MetricsKey metricsKey, MetricsPlaceValue placeType, ServiceMetricsCollector collector) {
+ return AbstractMetricsKeyListener.onEvent(metricsKey,
+ event -> MetricsSupport.increment(metricsKey, placeType, collector, event)
);
}
- public static AbstractMetricsListener onFinishEventBuild(MetricsKey metricsKey, MetricsPlaceType placeType, ServiceMetricsCollector collector) {
- return AbstractMetricsListener.onFinish(metricsKey,
- event -> incrAndAddRt(metricsKey, placeType, collector, event)
+ public static AbstractMetricsKeyListener onFinishEventBuild(MetricsKey metricsKey, MetricsPlaceValue placeType, ServiceMetricsCollector collector) {
+ return AbstractMetricsKeyListener.onFinish(metricsKey,
+ event -> MetricsSupport.incrAndAddRt(metricsKey, placeType, collector, event)
);
}
- public static AbstractMetricsListener onErrorEventBuild(MetricsKey metricsKey, MetricsPlaceType placeType, ServiceMetricsCollector collector) {
- return AbstractMetricsListener.onError(metricsKey,
- event -> incrAndAddRt(metricsKey, placeType, collector, event)
+ public static AbstractMetricsKeyListener onErrorEventBuild(MetricsKey metricsKey, MetricsPlaceValue placeType, ServiceMetricsCollector collector) {
+ return AbstractMetricsKeyListener.onError(metricsKey,
+ event -> MetricsSupport.incrAndAddRt(metricsKey, placeType, collector, event)
);
}
- private static void incrAndAddRt(MetricsKey metricsKey, MetricsPlaceType placeType, ServiceMetricsCollector collector, TimeCounterEvent event) {
- collector.increment(event.appName(), event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), metricsKey, SELF_INCREMENT_SIZE);
- collector.addRt(event.appName(), event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), placeType.getType(), event.getTimePair().calc());
- }
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java
index 35b7a6bf71..3ab9a91110 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MethodMetric.java
@@ -17,6 +17,7 @@
package org.apache.dubbo.metrics.model;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.RpcInvocation;
@@ -38,6 +39,7 @@ import static org.apache.dubbo.common.constants.MetricsConstants.TAG_METHOD_KEY;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_VERSION_KEY;
import static org.apache.dubbo.common.utils.NetUtils.getLocalHost;
import static org.apache.dubbo.common.utils.NetUtils.getLocalHostName;
+import static org.apache.dubbo.metrics.MetricsConstants.INVOCATION_METRICS_COUNTER;
import static org.apache.dubbo.rpc.support.RpcUtils.isGenericCall;
/**
@@ -51,13 +53,21 @@ public class MethodMetric implements Metric {
private String group;
private String version;
- public MethodMetric() {}
+ private MetricSample.Type sampleType;
+
+ public MethodMetric() {
+ }
public MethodMetric(String applicationName, Invocation invocation) {
this.applicationName = applicationName;
+ this.sampleType = (MetricSample.Type) invocation.get(INVOCATION_METRICS_COUNTER);
init(invocation);
}
+ public MetricSample.Type getSampleType() {
+ return sampleType;
+ }
+
public String getInterfaceName() {
return interfaceName;
}
@@ -106,9 +116,9 @@ public class MethodMetric implements Metric {
String serviceUniqueName = invocation.getTargetServiceUniqueName();
String methodName = invocation.getMethodName();
if (invocation instanceof RpcInvocation
- && isGenericCall(((RpcInvocation) invocation).getParameterTypesDesc(), methodName)
- && invocation.getArguments() != null
- && invocation.getArguments().length == 3) {
+ && isGenericCall(((RpcInvocation) invocation).getParameterTypesDesc(), methodName)
+ && invocation.getArguments() != null
+ && invocation.getArguments().length == 3) {
methodName = ((String) invocation.getArguments()[0]).trim();
}
String group = null;
@@ -150,13 +160,13 @@ public class MethodMetric implements Metric {
@Override
public String toString() {
return "MethodMetric{" +
- "applicationName='" + applicationName + '\'' +
- ", side='" + side + '\'' +
- ", interfaceName='" + interfaceName + '\'' +
- ", methodName='" + methodName + '\'' +
- ", group='" + group + '\'' +
- ", version='" + version + '\'' +
- '}';
+ "applicationName='" + applicationName + '\'' +
+ ", side='" + side + '\'' +
+ ", interfaceName='" + interfaceName + '\'' +
+ ", methodName='" + methodName + '\'' +
+ ", group='" + group + '\'' +
+ ", version='" + version + '\'' +
+ '}';
}
@Override
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java
index 18b40fc265..2029e6501d 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/MetricsSupport.java
@@ -18,19 +18,36 @@
package org.apache.dubbo.metrics.model;
import org.apache.dubbo.common.Version;
+import org.apache.dubbo.metrics.collector.MethodMetricsCollector;
+import org.apache.dubbo.metrics.collector.ServiceMetricsCollector;
+import org.apache.dubbo.metrics.event.MetricsEvent;
+import org.apache.dubbo.metrics.event.TimeCounterEvent;
import org.apache.dubbo.metrics.exception.MetricsNeverHappenException;
import org.apache.dubbo.metrics.model.key.MetricsKey;
+import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
+import org.apache.dubbo.metrics.model.key.MetricsPlaceValue;
+import org.apache.dubbo.rpc.Invocation;
+import org.apache.dubbo.rpc.Invoker;
+import org.apache.dubbo.rpc.RpcException;
import java.util.HashMap;
import java.util.Map;
+import java.util.Optional;
+import static org.apache.dubbo.common.constants.CommonConstants.GROUP_CHAR_SEPARATOR;
+import static org.apache.dubbo.common.constants.CommonConstants.PATH_SEPARATOR;
+import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_NAME;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_APPLICATION_VERSION_KEY;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_HOSTNAME;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_INTERFACE_KEY;
import static org.apache.dubbo.common.constants.MetricsConstants.TAG_IP;
+import static org.apache.dubbo.common.constants.MetricsConstants.TAG_METHOD_KEY;
import static org.apache.dubbo.common.utils.NetUtils.getLocalHost;
import static org.apache.dubbo.common.utils.NetUtils.getLocalHostName;
+import static org.apache.dubbo.metrics.MetricsConstants.ATTACHMENT_KEY_SERVICE;
+import static org.apache.dubbo.metrics.MetricsConstants.INVOCATION;
+import static org.apache.dubbo.metrics.MetricsConstants.SELF_INCREMENT_SIZE;
public class MetricsSupport {
@@ -56,4 +73,120 @@ public class MetricsSupport {
tags.put(TAG_INTERFACE_KEY, keys[1]);
return tags;
}
+
+ public static Map methodTags(String names) {
+ String[] keys = names.split("_");
+ if (keys.length != 3) {
+ throw new MetricsNeverHappenException("Error names: " + names);
+ }
+ Map tags = applicationTags(keys[0]);
+ tags.put(TAG_INTERFACE_KEY, keys[1]);
+ tags.put(TAG_METHOD_KEY, keys[2]);
+ return tags;
+ }
+
+ public static MetricsKey getMetricsKey(RpcException e) {
+ MetricsKey targetKey;
+ targetKey = MetricsKey.METRIC_REQUESTS_FAILED;
+ if (e.isTimeout()) {
+ targetKey = MetricsKey.METRIC_REQUESTS_TIMEOUT;
+ }
+ if (e.isLimitExceed()) {
+ targetKey = MetricsKey.METRIC_REQUESTS_LIMIT;
+ }
+ if (e.isBiz()) {
+ targetKey = MetricsKey.METRIC_REQUEST_BUSINESS_FAILED;
+ }
+ if (e.isSerialization()) {
+ targetKey = MetricsKey.METRIC_REQUESTS_CODEC_FAILED;
+ }
+ if (e.isNetwork()) {
+ targetKey = MetricsKey.METRIC_REQUESTS_NETWORK_FAILED;
+ }
+ return targetKey;
+ }
+
+ public static MetricsKey getAggMetricsKey(RpcException e) {
+ MetricsKey targetKey;
+ targetKey = MetricsKey.METRIC_REQUESTS_FAILED_AGG;
+ if (e.isTimeout()) {
+ targetKey = MetricsKey.METRIC_REQUESTS_TIMEOUT_AGG;
+ }
+ if (e.isLimitExceed()) {
+ targetKey = MetricsKey.METRIC_REQUESTS_LIMIT_AGG;
+ }
+ if (e.isBiz()) {
+ targetKey = MetricsKey.METRIC_REQUEST_BUSINESS_FAILED_AGG;
+ }
+ if (e.isSerialization()) {
+ targetKey = MetricsKey.METRIC_REQUESTS_CODEC_FAILED_AGG;
+ }
+ if (e.isNetwork()) {
+ targetKey = MetricsKey.METRIC_REQUESTS_NETWORK_FAILED_AGG;
+ }
+ return targetKey;
+ }
+
+ public static String getSide(Invocation invocation) {
+ Optional extends Invoker>> invoker = Optional.ofNullable(invocation.getInvoker());
+ return invoker.isPresent() ? invoker.get().getUrl().getSide() : PROVIDER_SIDE;
+ }
+
+
+ public static String getInterfaceName(Invocation invocation) {
+ String serviceUniqueName = invocation.getTargetServiceUniqueName();
+ String interfaceAndVersion;
+ String[] arr = serviceUniqueName.split(PATH_SEPARATOR);
+ if (arr.length == 2) {
+ interfaceAndVersion = arr[1];
+ } else {
+ interfaceAndVersion = arr[0];
+ }
+ String[] ivArr = interfaceAndVersion.split(GROUP_CHAR_SEPARATOR);
+ return ivArr[0];
+ }
+
+ /**
+ * Incr service num
+ */
+ public static void increment(MetricsKey metricsKey, MetricsPlaceValue placeType, ServiceMetricsCollector collector, MetricsEvent event) {
+ collector.increment(event.appName(), event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE);
+ }
+
+ /**
+ * Dec service num
+ */
+ public static void dec(MetricsKey metricsKey, MetricsPlaceValue placeType, ServiceMetricsCollector collector, MetricsEvent event) {
+ collector.increment(event.appName(), event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), new MetricsKeyWrapper(metricsKey, placeType), -SELF_INCREMENT_SIZE);
+ }
+
+ /**
+ * Incr service num&&rt
+ */
+ public static void incrAndAddRt(MetricsKey metricsKey, MetricsPlaceValue placeType, ServiceMetricsCollector collector, TimeCounterEvent event) {
+ collector.increment(event.appName(), event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE);
+ collector.addRt(event.appName(), event.getAttachmentValue(ATTACHMENT_KEY_SERVICE), placeType.getType(), event.getTimePair().calc());
+ }
+
+ /**
+ * Incr method num
+ */
+ public static void increment(MetricsKey metricsKey, MetricsPlaceValue placeType, MethodMetricsCollector collector, MetricsEvent event) {
+ collector.increment(event.appName(), event.getAttachmentValue(INVOCATION), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE);
+ }
+
+ /**
+ * Dec method num
+ */
+ public static void dec(MetricsKey metricsKey, MetricsPlaceValue placeType, MethodMetricsCollector collector, MetricsEvent event) {
+ collector.increment(event.appName(), event.getAttachmentValue(INVOCATION), new MetricsKeyWrapper(metricsKey, placeType), -SELF_INCREMENT_SIZE);
+ }
+
+ /**
+ * Incr method num&&rt
+ */
+ public static void incrAndAddRt(MetricsKey metricsKey, MetricsPlaceValue placeType, MethodMetricsCollector collector, TimeCounterEvent event) {
+ collector.increment(event.appName(), event.getAttachmentValue(INVOCATION), new MetricsKeyWrapper(metricsKey, placeType), SELF_INCREMENT_SIZE);
+ collector.addRt(event.appName(), event.getAttachmentValue(INVOCATION), placeType.getType(), event.getTimePair().calc());
+ }
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/CategoryOverall.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/CategoryOverall.java
index 007a904b72..c13973d17a 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/CategoryOverall.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/CategoryOverall.java
@@ -19,13 +19,20 @@ package org.apache.dubbo.metrics.model.key;
import io.micrometer.common.lang.Nullable;
+/**
+ * The overall event set, including the event processing functions in three stages
+ */
public class CategoryOverall {
private final MetricsCat post;
private MetricsCat finish;
private MetricsCat error;
- public CategoryOverall(MetricsPlaceType placeType, MetricsCat post, @Nullable MetricsCat finish, @Nullable MetricsCat error) {
+ /**
+ * @param placeType When placeType is null, it means that placeType is obtained dynamically
+ * @param post Statistics of the number of events, as long as it occurs, it will take effect, so it cannot be null
+ */
+ public CategoryOverall(@Nullable MetricsPlaceValue placeType, MetricsCat post, @Nullable MetricsCat finish, @Nullable MetricsCat error) {
this.post = post.setPlaceType(placeType);
if (finish != null) {
this.finish = finish.setPlaceType(placeType);
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsCat.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsCat.java
index a6301bbd85..88b19cd2f8 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsCat.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsCat.java
@@ -18,31 +18,34 @@
package org.apache.dubbo.metrics.model.key;
import org.apache.dubbo.metrics.collector.CombMetricsCollector;
-import org.apache.dubbo.metrics.event.TimeCounterEvent;
-import org.apache.dubbo.metrics.listener.AbstractMetricsListener;
+import org.apache.dubbo.metrics.listener.AbstractMetricsKeyListener;
import java.util.function.BiFunction;
import java.util.function.Function;
public class MetricsCat {
- private MetricsPlaceType placeType;
- private final Function, AbstractMetricsListener> eventFunc;
+ private MetricsPlaceValue placeType;
+ private final Function eventFunc;
- public MetricsCat(MetricsKey metricsKey, BiFunction, AbstractMetricsListener> biFunc) {
+ public MetricsCat(MetricsKey metricsKey, BiFunction biFunc) {
this.eventFunc = collector -> biFunc.apply(metricsKey, collector);
}
- public MetricsCat(MetricsKey metricsKey, TpFunction, AbstractMetricsListener> tpFunc) {
+ /**
+ * @param metricsKey The key that the current category listens to,not necessarily the export key(export key may be dynamic)
+ * @param tpFunc Build the func that outputs the MetricsListener by listen metricsKey
+ */
+ public MetricsCat(MetricsKey metricsKey, TpFunction tpFunc) {
this.eventFunc = collector -> tpFunc.apply(metricsKey, placeType, collector);
}
- public MetricsCat setPlaceType(MetricsPlaceType placeType) {
+ public MetricsCat setPlaceType(MetricsPlaceValue placeType) {
this.placeType = placeType;
return this;
}
- public Function, AbstractMetricsListener> getEventFunc() {
+ public Function getEventFunc() {
return eventFunc;
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKey.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKey.java
index b142365fad..b6158be79a 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKey.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKey.java
@@ -39,12 +39,12 @@ public enum MetricsKey {
METRIC_REQUESTS_TOTAL_AGG("dubbo.%s.requests.total.aggregate", "Aggregated Total Requests"),
METRIC_REQUESTS_SUCCEED_AGG("dubbo.%s.requests.succeed.aggregate", "Aggregated Succeed Requests"),
METRIC_REQUESTS_FAILED_AGG("dubbo.%s.requests.failed.aggregate", "Aggregated Failed Requests"),
- METRIC_REQUESTS_BUSINESS_FAILED_AGG("dubbo.%s.requests.business.failed.aggregate", "Aggregated Business Failed Requests"),
+ METRIC_REQUEST_BUSINESS_FAILED_AGG("dubbo.%s.requests.business.failed.aggregate", "Aggregated Business Failed Requests"),
METRIC_REQUESTS_TIMEOUT_AGG("dubbo.%s.requests.timeout.failed.aggregate", "Aggregated timeout Failed Requests"),
METRIC_REQUESTS_LIMIT_AGG("dubbo.%s.requests.limit.aggregate", "Aggregated limit Requests"),
METRIC_REQUESTS_TOTAL_FAILED_AGG("dubbo.%s.requests.failed.total.aggregate", "Aggregated failed total Requests"),
- METRIC_REQUESTS_TOTAL_NETWORK_FAILED_AGG("dubbo.%s.requests.failed.network.total.aggregate", "Aggregated failed network total Requests"),
- METRIC_REQUESTS_TOTAL_CODEC_FAILED_AGG("dubbo.%s.requests.failed.codec.total.aggregate", "Aggregated failed codec total Requests"),
+ METRIC_REQUESTS_NETWORK_FAILED_AGG("dubbo.%s.requests.failed.network.total.aggregate", "Aggregated failed network total Requests"),
+ METRIC_REQUESTS_CODEC_FAILED_AGG("dubbo.%s.requests.failed.codec.total.aggregate", "Aggregated failed codec total Requests"),
METRIC_REQUESTS_TOTAL_SERVICE_UNAVAILABLE_FAILED_AGG("dubbo.%s.requests.failed.service.unavailable.total.aggregate", "Aggregated failed codec total Requests"),
METRIC_QPS("dubbo.%s.qps.total", "Query Per Seconds"),
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKeyWrapper.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKeyWrapper.java
index c3862f5f38..e213be48cb 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKeyWrapper.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsKeyWrapper.java
@@ -17,9 +17,11 @@
package org.apache.dubbo.metrics.model.key;
+import io.micrometer.common.lang.Nullable;
import org.apache.dubbo.metrics.model.MetricsSupport;
import java.util.Map;
+import java.util.Objects;
/**
* Let {@link MetricsKey MetricsKey} output dynamic, custom string content
@@ -31,15 +33,21 @@ public class MetricsKeyWrapper {
*/
private final MetricsKey metricsKey;
+ /**
+ * The value corresponding to the MetricsKey placeholder (if exist)
+ */
+ private final MetricsPlaceValue placeType;
- private final MetricsPlaceType placeType;
-
- public MetricsKeyWrapper(MetricsKey metricsKey, MetricsPlaceType placeType) {
+ /**
+ * When the MetricsPlaceType is null, it is equivalent to a single MetricsKey.
+ * Use the decorator mode to share a container with MetricsKey
+ */
+ public MetricsKeyWrapper(MetricsKey metricsKey, @Nullable MetricsPlaceValue placeType) {
this.metricsKey = metricsKey;
this.placeType = placeType;
}
- public MetricsPlaceType getPlaceType() {
+ public MetricsPlaceValue getPlaceType() {
return placeType;
}
@@ -55,11 +63,14 @@ public class MetricsKeyWrapper {
return metricsKey == getMetricsKey() && registryOpType.equals(getType());
}
- public boolean isServiceLevel() {
- return getPlaceType().getMetricsLevel().equals(MetricsLevel.SERVICE);
+ public MetricsLevel getLevel() {
+ return getPlaceType().getMetricsLevel();
}
public String targetKey() {
+ if (placeType == null) {
+ return metricsKey.getName();
+ }
try {
return String.format(metricsKey.getName(), getType());
} catch (Exception ignore) {
@@ -68,6 +79,9 @@ public class MetricsKeyWrapper {
}
public String targetDesc() {
+ if (placeType == null) {
+ return metricsKey.getDescription();
+ }
try {
return String.format(metricsKey.getDescription(), getType());
} catch (Exception ignore) {
@@ -76,6 +90,37 @@ public class MetricsKeyWrapper {
}
public Map tagName(String key) {
- return isServiceLevel() ? MetricsSupport.serviceTags(key) : MetricsSupport.applicationTags(key);
+ MetricsLevel level = getLevel();
+ switch (level) {
+ case APP:
+ return MetricsSupport.applicationTags(key);
+ case SERVICE:
+ return MetricsSupport.serviceTags(key);
+ case METHOD:
+ return MetricsSupport.methodTags(key);
+ }
+ return MetricsSupport.applicationTags(key);
+ }
+
+ public static MetricsKeyWrapper wrapper(MetricsKey metricsKey) {
+ return new MetricsKeyWrapper(metricsKey, null);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ MetricsKeyWrapper wrapper = (MetricsKeyWrapper) o;
+
+ if (metricsKey != wrapper.metricsKey) return false;
+ return Objects.equals(placeType, wrapper.placeType);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = metricsKey != null ? metricsKey.hashCode() : 0;
+ result = 31 * result + (placeType != null ? placeType.hashCode() : 0);
+ return result;
}
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsLevel.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsLevel.java
index 9711f5ae7b..783de99948 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsLevel.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsLevel.java
@@ -18,5 +18,5 @@
package org.apache.dubbo.metrics.model.key;
public enum MetricsLevel {
- APP,SERVICE,CONFIG
+ APP, SERVICE, METHOD, CONFIG
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsPlaceType.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsPlaceValue.java
similarity index 58%
rename from dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsPlaceType.java
rename to dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsPlaceValue.java
index 744cd55007..30542f73bb 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsPlaceType.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/MetricsPlaceValue.java
@@ -17,18 +17,21 @@
package org.apache.dubbo.metrics.model.key;
-public class MetricsPlaceType {
+/**
+ * The value corresponding to the placeholder in {@link MetricsKey}
+ */
+public class MetricsPlaceValue {
private final String type;
private final MetricsLevel metricsLevel;
- private MetricsPlaceType(String type, MetricsLevel metricsLevel) {
+ private MetricsPlaceValue(String type, MetricsLevel metricsLevel) {
this.type = type;
this.metricsLevel = metricsLevel;
}
- public static MetricsPlaceType of(String type, MetricsLevel metricsLevel) {
- return new MetricsPlaceType(type, metricsLevel);
+ public static MetricsPlaceValue of(String type, MetricsLevel metricsLevel) {
+ return new MetricsPlaceValue(type, metricsLevel);
}
public String getType() {
@@ -38,4 +41,22 @@ public class MetricsPlaceType {
public MetricsLevel getMetricsLevel() {
return metricsLevel;
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+
+ MetricsPlaceValue that = (MetricsPlaceValue) o;
+
+ if (!type.equals(that.type)) return false;
+ return metricsLevel == that.metricsLevel;
+ }
+
+ @Override
+ public int hashCode() {
+ int result = type.hashCode();
+ result = 31 * result + metricsLevel.hashCode();
+ return result;
+ }
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/TypeWrapper.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/TypeWrapper.java
index a46faa6471..76c37a5899 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/TypeWrapper.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/key/TypeWrapper.java
@@ -44,4 +44,5 @@ public class TypeWrapper {
Assert.notNull(type, "Type can not be null");
return type.equals(postType) || type.equals(finishType) || type.equals(errorType);
}
+
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/CounterMetricSample.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/CounterMetricSample.java
index 46ce0d50f9..860f1f63b7 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/CounterMetricSample.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/CounterMetricSample.java
@@ -17,23 +17,22 @@
package org.apache.dubbo.metrics.model.sample;
import org.apache.dubbo.metrics.model.MetricsCategory;
+import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
import java.util.Map;
-public class CounterMetricSample extends MetricSample {
+public class CounterMetricSample extends MetricSample {
private final T value;
public CounterMetricSample(String name, String description, Map tags,
- MetricsCategory category, T value ) {
+ MetricsCategory category, T value) {
super(name, description, tags, Type.COUNTER, category);
this.value = value;
}
- public CounterMetricSample(String name, String description, Map tags, MetricsCategory category,
- String baseUnit, T value) {
- super(name, description, tags, Type.COUNTER, category, baseUnit);
- this.value = value;
+ public CounterMetricSample(MetricsKeyWrapper metricsKeyWrapper, Map tags, MetricsCategory category, T value) {
+ this(metricsKeyWrapper.targetKey(), metricsKeyWrapper.targetDesc(), tags, category, value);
}
public T getValue() {
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java
index b795207070..5a3401d282 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/model/sample/GaugeMetricSample.java
@@ -19,6 +19,7 @@ package org.apache.dubbo.metrics.model.sample;
import org.apache.dubbo.metrics.model.MetricsCategory;
import org.apache.dubbo.metrics.model.key.MetricsKey;
+import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
import java.util.Map;
import java.util.Objects;
@@ -37,6 +38,10 @@ public class GaugeMetricSample extends MetricSample {
this(metricsKey.getName(), metricsKey.getDescription(), tags, category, null, value, apply);
}
+ public GaugeMetricSample(MetricsKeyWrapper metricsKeyWrapper, Map tags, MetricsCategory category, T value, ToDoubleFunction apply) {
+ this(metricsKeyWrapper.targetKey(), metricsKeyWrapper.targetDesc(), tags, category, null, value, apply);
+ }
+
public GaugeMetricSample(String name, String description, Map tags, MetricsCategory category, T value, ToDoubleFunction apply) {
this(name, description, tags, category, null, value, apply);
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/report/MetricsExport.java b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/report/MetricsExport.java
index 82db29a1cc..d2b7c9eca5 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/report/MetricsExport.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/main/java/org/apache/dubbo/metrics/report/MetricsExport.java
@@ -18,7 +18,7 @@
package org.apache.dubbo.metrics.report;
import org.apache.dubbo.metrics.model.MetricsCategory;
-import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
import java.util.List;
@@ -31,6 +31,6 @@ public interface MetricsExport {
/**
* export all.
*/
- List export(MetricsCategory category);
+ List export(MetricsCategory category);
}
diff --git a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/event/SimpleMetricsEventMulticasterTest.java b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/event/SimpleMetricsEventMulticasterTest.java
index 16ad2edf05..c18627b584 100644
--- a/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/event/SimpleMetricsEventMulticasterTest.java
+++ b/dubbo-metrics/dubbo-metrics-api/src/test/java/org/apache/dubbo/metrics/event/SimpleMetricsEventMulticasterTest.java
@@ -19,8 +19,8 @@ package org.apache.dubbo.metrics.event;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.context.ConfigManager;
+import org.apache.dubbo.metrics.listener.AbstractMetricsListener;
import org.apache.dubbo.metrics.listener.MetricsLifeListener;
-import org.apache.dubbo.metrics.listener.MetricsListener;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
@@ -39,7 +39,7 @@ public class SimpleMetricsEventMulticasterTest {
public void setup() {
eventMulticaster = new SimpleMetricsEventMulticaster();
objects = new Object[]{obj};
- eventMulticaster.addListener(new MetricsListener() {
+ eventMulticaster.addListener(new AbstractMetricsListener() {
@Override
public void onEvent(MetricsEvent event) {
objects[0] = new Object();
@@ -52,7 +52,7 @@ public class SimpleMetricsEventMulticasterTest {
ConfigManager configManager = new ConfigManager(applicationModel);
configManager.setApplication(applicationConfig);
applicationModel.setConfigManager(configManager);
- requestEvent = new TimeCounterEvent(applicationModel) {
+ requestEvent = new TimeCounterEvent(applicationModel,null) {
};
}
@@ -77,6 +77,11 @@ public class SimpleMetricsEventMulticasterTest {
//do onEventFinish with MetricsLifeListener
eventMulticaster.addListener((new MetricsLifeListener() {
+ @Override
+ public boolean isSupport(MetricsEvent event) {
+ return event instanceof TimeCounterEvent;
+ }
+
@Override
public void onEvent(TimeCounterEvent event) {
diff --git a/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/collector/ConfigCenterMetricsCollector.java b/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/collector/ConfigCenterMetricsCollector.java
index 17b3098710..d0dd1a7d7a 100644
--- a/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/collector/ConfigCenterMetricsCollector.java
+++ b/dubbo-metrics/dubbo-metrics-config-center/src/main/java/org/apache/dubbo/metrics/config/collector/ConfigCenterMetricsCollector.java
@@ -22,9 +22,7 @@ import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.metrics.collector.CombMetricsCollector;
import org.apache.dubbo.metrics.collector.MetricsCollector;
import org.apache.dubbo.metrics.config.event.ConfigCenterEvent;
-import org.apache.dubbo.metrics.config.event.ConfigCenterMetricsDispatcher;
-import org.apache.dubbo.metrics.event.MetricsEvent;
-import org.apache.dubbo.metrics.event.TimeCounterEvent;
+import org.apache.dubbo.metrics.config.event.ConfigCenterSubDispatcher;
import org.apache.dubbo.metrics.model.ConfigCenterMetric;
import org.apache.dubbo.metrics.model.key.MetricsKey;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
@@ -45,7 +43,7 @@ import static org.apache.dubbo.metrics.model.MetricsCategory.CONFIGCENTER;
* Config center implementation of {@link MetricsCollector}
*/
@Activate
-public class ConfigCenterMetricsCollector extends CombMetricsCollector {
+public class ConfigCenterMetricsCollector extends CombMetricsCollector {
private Boolean collectEnabled = null;
private final ApplicationModel applicationModel;
@@ -55,7 +53,7 @@ public class ConfigCenterMetricsCollector extends CombMetricsCollector METHOD_LEVEL_KEYS = Arrays.asList(
+ new MetricsKeyWrapper(METRIC_REQUESTS, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_PROCESSING, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_PROCESSING, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_SUCCEED, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_SUCCEED, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUEST_BUSINESS_FAILED, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUEST_BUSINESS_FAILED, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_TIMEOUT, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_TIMEOUT, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_LIMIT, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_LIMIT, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_FAILED, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_FAILED, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_TOTAL_FAILED, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_TOTAL_FAILED, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_NETWORK_FAILED, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_NETWORK_FAILED, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_SERVICE_UNAVAILABLE_FAILED, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_SERVICE_UNAVAILABLE_FAILED, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_CODEC_FAILED, MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD)),
+ new MetricsKeyWrapper(METRIC_REQUESTS_CODEC_FAILED, MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD))
+ );
+}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/MetricsScopeModelInitializer.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/MetricsScopeModelInitializer.java
index 15b2806cc6..660fcc835d 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/MetricsScopeModelInitializer.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/MetricsScopeModelInitializer.java
@@ -18,7 +18,6 @@
package org.apache.dubbo.metrics;
import org.apache.dubbo.common.beans.factory.ScopeBeanFactory;
-import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
import org.apache.dubbo.metrics.event.MetricsDispatcher;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
@@ -35,7 +34,6 @@ public class MetricsScopeModelInitializer implements ScopeModelInitializer {
@Override
public void initializeApplicationModel(ApplicationModel applicationModel) {
ScopeBeanFactory beanFactory = applicationModel.getBeanFactory();
- beanFactory.registerBean(DefaultMetricsCollector.class);
beanFactory.registerBean(MetricsDispatcher.class);
}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
index dc2ce7bb5f..4dc18f896e 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/AggregateMetricsCollector.java
@@ -21,26 +21,32 @@ import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
import org.apache.dubbo.config.MetricsConfig;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.config.nested.AggregationConfig;
+import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.aggregate.TimeWindowCounter;
import org.apache.dubbo.metrics.aggregate.TimeWindowQuantile;
-import org.apache.dubbo.metrics.event.MethodEvent;
import org.apache.dubbo.metrics.event.MetricsEvent;
-import org.apache.dubbo.metrics.event.RTEvent;
-import org.apache.dubbo.metrics.listener.MetricsListener;
+import org.apache.dubbo.metrics.event.RequestEvent;
import org.apache.dubbo.metrics.model.MethodMetric;
+import org.apache.dubbo.metrics.model.MetricsSupport;
import org.apache.dubbo.metrics.model.key.MetricsKey;
+import org.apache.dubbo.metrics.model.key.MetricsKeyWrapper;
+import org.apache.dubbo.metrics.model.key.MetricsLevel;
+import org.apache.dubbo.metrics.model.key.MetricsPlaceValue;
import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
+import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.rpc.model.ApplicationModel;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
+import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
+import static org.apache.dubbo.metrics.DefaultConstants.METRIC_THROWABLE;
import static org.apache.dubbo.metrics.model.MetricsCategory.QPS;
import static org.apache.dubbo.metrics.model.MetricsCategory.REQUESTS;
import static org.apache.dubbo.metrics.model.MetricsCategory.RT;
@@ -49,68 +55,101 @@ import static org.apache.dubbo.metrics.model.MetricsCategory.RT;
* Aggregation metrics collector implementation of {@link MetricsCollector}.
* This collector only enabled when metrics aggregation config is enabled.
*/
-public class AggregateMetricsCollector implements MetricsCollector, MetricsListener {
+public class AggregateMetricsCollector implements MetricsCollector {
private int bucketNum;
private int timeWindowSeconds;
- private final Map> methodTypeCounter = new ConcurrentHashMap<>();
+ private final Map> methodTypeCounter = new ConcurrentHashMap<>();
private final ConcurrentMap rt = new ConcurrentHashMap<>();
private final ConcurrentHashMap qps = new ConcurrentHashMap<>();
private final ApplicationModel applicationModel;
private static final Integer DEFAULT_COMPRESSION = 100;
private static final Integer DEFAULT_BUCKET_NUM = 10;
private static final Integer DEFAULT_TIME_WINDOW_SECONDS = 120;
+ private Boolean collectEnabled = null;
public AggregateMetricsCollector(ApplicationModel applicationModel) {
- this.registryEventTypeHandler();
-
this.applicationModel = applicationModel;
ConfigManager configManager = applicationModel.getApplicationConfigManager();
- MetricsConfig config = configManager.getMetrics().orElse(null);
- if (config != null && config.getAggregation() != null && (Boolean.TRUE.equals(config.getAggregation().getEnabled()))) {
+ if (isCollectEnabled()) {
// only registered when aggregation is enabled.
- registerListener();
-
- AggregationConfig aggregation = config.getAggregation();
- this.bucketNum = aggregation.getBucketNum() == null ? DEFAULT_BUCKET_NUM : aggregation.getBucketNum();
- this.timeWindowSeconds = aggregation.getTimeWindowSeconds() == null ? DEFAULT_TIME_WINDOW_SECONDS : aggregation.getTimeWindowSeconds();
+ Optional optional = configManager.getMetrics();
+ if (optional.isPresent()) {
+ registerListener();
+ AggregationConfig aggregation = optional.get().getAggregation();
+ this.bucketNum = aggregation.getBucketNum() == null ? DEFAULT_BUCKET_NUM : aggregation.getBucketNum();
+ this.timeWindowSeconds = aggregation.getTimeWindowSeconds() == null ? DEFAULT_TIME_WINDOW_SECONDS : aggregation.getTimeWindowSeconds();
+ }
}
}
+ public void setCollectEnabled(Boolean collectEnabled) {
+ if (collectEnabled != null) {
+ this.collectEnabled = collectEnabled;
+ }
+ }
+
+
+ @Override
+ public boolean isCollectEnabled() {
+ if (collectEnabled == null) {
+ ConfigManager configManager = applicationModel.getApplicationConfigManager();
+ configManager.getMetrics().ifPresent(metricsConfig -> setCollectEnabled(metricsConfig.getAggregation().getEnabled()));
+ }
+ return Optional.ofNullable(collectEnabled).orElse(true);
+ }
+
@Override
- public void onEvent(MetricsEvent event) {
- if (event instanceof RTEvent) {
- onRTEvent((RTEvent) event);
- } else if (event instanceof MethodEvent) {
- onRequestEvent((MethodEvent) event);
- }
+ public boolean isSupport(MetricsEvent event) {
+ return event instanceof RequestEvent;
}
- private void onRTEvent(RTEvent event) {
- MethodMetric metric = (MethodMetric) event.getMetric();
- Long responseTime = event.getRt();
+ @Override
+ public void onEvent(RequestEvent event) {
+ MethodMetric metric = calcWindowCounter(event, MetricsKey.METRIC_REQUESTS);
+ TimeWindowCounter qpsCounter = ConcurrentHashMapUtils.computeIfAbsent(qps, metric, methodMetric -> new TimeWindowCounter(bucketNum, timeWindowSeconds));
+ qpsCounter.increment();
+ }
+
+ @Override
+ public void onEventFinish(RequestEvent event) {
+ MetricsKey targetKey = MetricsKey.METRIC_REQUESTS_SUCCEED;
+ Object throwableObj = event.getAttachmentValue(METRIC_THROWABLE);
+ if (throwableObj != null) {
+ targetKey = MetricsSupport.getAggMetricsKey((RpcException) throwableObj);
+ }
+ calcWindowCounter(event, targetKey);
+ onRTEvent(event);
+ }
+
+ @Override
+ public void onEventError(RequestEvent event) {
+ MetricsKey targetKey = MetricsKey.METRIC_REQUESTS_FAILED;
+ Object throwableObj = event.getAttachmentValue(METRIC_THROWABLE);
+ if (throwableObj != null) {
+ targetKey = MetricsSupport.getAggMetricsKey((RpcException) throwableObj);
+ }
+ calcWindowCounter(event, targetKey);
+ onRTEvent(event);
+ }
+
+ private void onRTEvent(RequestEvent event) {
+ MethodMetric metric = new MethodMetric(applicationModel.getApplicationName(), event.getAttachmentValue(MetricsConstants.INVOCATION));
+ long responseTime = event.getTimePair().calc();
TimeWindowQuantile quantile = ConcurrentHashMapUtils.computeIfAbsent(rt, metric, k -> new TimeWindowQuantile(DEFAULT_COMPRESSION, bucketNum, timeWindowSeconds));
quantile.add(responseTime);
}
- private void onRequestEvent(MethodEvent event) {
- MethodMetric metric = event.getMethodMetric();
+ private MethodMetric calcWindowCounter(RequestEvent event, MetricsKey targetKey) {
+ MetricsPlaceValue placeType = MetricsPlaceValue.of(event.getAttachmentValue(MetricsConstants.INVOCATION_SIDE), MetricsLevel.SERVICE);
+ MetricsKeyWrapper metricsKeyWrapper = new MetricsKeyWrapper(targetKey, placeType);
+ MethodMetric metric = new MethodMetric(applicationModel.getApplicationName(), event.getAttachmentValue(MetricsConstants.INVOCATION));
- String type = event.getType();
+ ConcurrentMap counter = methodTypeCounter.computeIfAbsent(metricsKeyWrapper, k -> new ConcurrentHashMap<>());
- ConcurrentMap counter = methodTypeCounter.get(type);
-
- if (counter == null) {
- return;
- }
TimeWindowCounter windowCounter = ConcurrentHashMapUtils.computeIfAbsent(counter, metric, methodMetric -> new TimeWindowCounter(bucketNum, timeWindowSeconds));
-
- if (MetricsEvent.Type.TOTAL.getNameByType(PROVIDER_SIDE).equals(type)
- || MetricsEvent.Type.TOTAL.getNameByType(CONSUMER_SIDE).equals(type)) {
- TimeWindowCounter qpsCounter = ConcurrentHashMapUtils.computeIfAbsent(qps, metric, methodMetric -> new TimeWindowCounter(bucketNum, timeWindowSeconds));
- qpsCounter.increment();
- }
windowCounter.increment();
+ return metric;
}
@Override
@@ -129,21 +168,21 @@ public class AggregateMetricsCollector implements MetricsCollector, MetricsListe
}
private void collectBySide(List list, String side) {
- collectMethod(list, MetricsEvent.Type.TOTAL.getNameByType(side), MetricsKey.METRIC_REQUESTS_TOTAL_AGG);
- collectMethod(list, MetricsEvent.Type.SUCCEED.getNameByType(side), MetricsKey.METRIC_REQUESTS_SUCCEED_AGG);
- collectMethod(list, MetricsEvent.Type.UNKNOWN_FAILED.getNameByType(side), MetricsKey.METRIC_REQUESTS_FAILED_AGG);
- collectMethod(list, MetricsEvent.Type.BUSINESS_FAILED.getNameByType(side), MetricsKey.METRIC_REQUESTS_BUSINESS_FAILED_AGG);
- collectMethod(list, MetricsEvent.Type.REQUEST_TIMEOUT.getNameByType(side), MetricsKey.METRIC_REQUESTS_TIMEOUT_AGG);
- collectMethod(list, MetricsEvent.Type.REQUEST_LIMIT.getNameByType(side), MetricsKey.METRIC_REQUESTS_LIMIT_AGG);
- collectMethod(list, MetricsEvent.Type.TOTAL_FAILED.getNameByType(side), MetricsKey.METRIC_REQUESTS_TOTAL_FAILED_AGG);
- collectMethod(list, MetricsEvent.Type.NETWORK_EXCEPTION.getNameByType(side), MetricsKey.METRIC_REQUESTS_TOTAL_NETWORK_FAILED_AGG);
- collectMethod(list, MetricsEvent.Type.CODEC_EXCEPTION.getNameByType(side), MetricsKey.METRIC_REQUESTS_TOTAL_CODEC_FAILED_AGG);
- collectMethod(list, MetricsEvent.Type.SERVICE_UNAVAILABLE.getNameByType(side), MetricsKey.METRIC_REQUESTS_TOTAL_SERVICE_UNAVAILABLE_FAILED_AGG);
- collectMethod(list, MetricsEvent.Type.NO_INVOKER_AVAILABLE.getNameByType(side), MetricsKey.INVOKER_NO_AVAILABLE_COUNT);
+ collectMethod(list, side, MetricsKey.METRIC_REQUESTS_TOTAL_AGG);
+ collectMethod(list, side, MetricsKey.METRIC_REQUESTS_SUCCEED_AGG);
+ collectMethod(list, side, MetricsKey.METRIC_REQUESTS_FAILED_AGG);
+ collectMethod(list, side, MetricsKey.METRIC_REQUEST_BUSINESS_FAILED_AGG);
+ collectMethod(list, side, MetricsKey.METRIC_REQUESTS_TIMEOUT_AGG);
+ collectMethod(list, side, MetricsKey.METRIC_REQUESTS_LIMIT_AGG);
+ collectMethod(list, side, MetricsKey.METRIC_REQUESTS_TOTAL_FAILED_AGG);
+ collectMethod(list, side, MetricsKey.METRIC_REQUESTS_NETWORK_FAILED_AGG);
+ collectMethod(list, side, MetricsKey.METRIC_REQUESTS_CODEC_FAILED_AGG);
+ collectMethod(list, side, MetricsKey.METRIC_REQUESTS_TOTAL_SERVICE_UNAVAILABLE_FAILED_AGG);
}
- private void collectMethod(List list, String eventType, MetricsKey metricsKey) {
- ConcurrentHashMap windowCounter = methodTypeCounter.get(eventType);
+ private void collectMethod(List list, String side, MetricsKey metricsKey) {
+ MetricsKeyWrapper metricsKeyWrapper = new MetricsKeyWrapper(metricsKey, MetricsPlaceValue.of(side, MetricsLevel.SERVICE));
+ ConcurrentHashMap windowCounter = methodTypeCounter.get(metricsKeyWrapper);
if (windowCounter != null) {
windowCounter.forEach((k, v) -> list.add(new GaugeMetricSample<>(metricsKey.getNameByType(k.getSide()),
metricsKey.getDescription(), k.getTags(), REQUESTS, v, TimeWindowCounter::get)));
@@ -164,27 +203,8 @@ public class AggregateMetricsCollector implements MetricsCollector, MetricsListe
});
}
- private void registryEventTypeHandler() {
- registryBySide(PROVIDER_SIDE);
- registryBySide(CONSUMER_SIDE);
- }
-
- private void registryBySide(String side) {
- methodTypeCounter.put(MetricsEvent.Type.TOTAL.getNameByType(side), new ConcurrentHashMap<>());
- methodTypeCounter.put(MetricsEvent.Type.SUCCEED.getNameByType(side), new ConcurrentHashMap<>());
- methodTypeCounter.put(MetricsEvent.Type.UNKNOWN_FAILED.getNameByType(side), new ConcurrentHashMap<>());
- methodTypeCounter.put(MetricsEvent.Type.BUSINESS_FAILED.getNameByType(side), new ConcurrentHashMap<>());
- methodTypeCounter.put(MetricsEvent.Type.REQUEST_TIMEOUT.getNameByType(side), new ConcurrentHashMap<>());
- methodTypeCounter.put(MetricsEvent.Type.REQUEST_LIMIT.getNameByType(side), new ConcurrentHashMap<>());
- methodTypeCounter.put(MetricsEvent.Type.TOTAL_FAILED.getNameByType(side), new ConcurrentHashMap<>());
- methodTypeCounter.put(MetricsEvent.Type.SERVICE_UNAVAILABLE.getNameByType(side), new ConcurrentHashMap<>());
- methodTypeCounter.put(MetricsEvent.Type.NETWORK_EXCEPTION.getNameByType(side), new ConcurrentHashMap<>());
- methodTypeCounter.put(MetricsEvent.Type.CODEC_EXCEPTION.getNameByType(side), new ConcurrentHashMap<>());
- methodTypeCounter.put(MetricsEvent.Type.NO_INVOKER_AVAILABLE.getNameByType(side), new ConcurrentHashMap<>());
- }
-
private void registerListener() {
- applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class).addListener(this);
+ applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class).getEventMulticaster().addListener(this);
}
}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java
index 953cc075e6..937893310c 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/DefaultMetricsCollector.java
@@ -16,48 +16,71 @@
*/
package org.apache.dubbo.metrics.collector;
-import org.apache.dubbo.metrics.collector.sample.MethodMetricsSampler;
+
+import org.apache.dubbo.common.constants.CommonConstants;
+import org.apache.dubbo.common.extension.Activate;
+import org.apache.dubbo.metrics.DefaultConstants;
import org.apache.dubbo.metrics.collector.sample.MetricsCountSampleConfigurer;
import org.apache.dubbo.metrics.collector.sample.MetricsSampler;
import org.apache.dubbo.metrics.collector.sample.SimpleMetricsCountSampler;
import org.apache.dubbo.metrics.collector.sample.ThreadPoolMetricsSampler;
+import org.apache.dubbo.metrics.data.BaseStatComposite;
+import org.apache.dubbo.metrics.data.MethodStatComposite;
+import org.apache.dubbo.metrics.data.RtStatComposite;
+import org.apache.dubbo.metrics.event.DefaultSubDispatcher;
import org.apache.dubbo.metrics.event.MetricsEvent;
-import org.apache.dubbo.metrics.event.SimpleMetricsEventMulticaster;
-import org.apache.dubbo.metrics.listener.MetricsListener;
+import org.apache.dubbo.metrics.event.RequestBeforeEvent;
+import org.apache.dubbo.metrics.event.RequestEvent;
import org.apache.dubbo.metrics.model.ApplicationMetric;
+import org.apache.dubbo.metrics.model.MetricsCategory;
+import org.apache.dubbo.metrics.model.key.MetricsLevel;
+import org.apache.dubbo.metrics.model.key.MetricsPlaceValue;
import org.apache.dubbo.metrics.model.sample.CounterMetricSample;
import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.rpc.model.ApplicationModel;
+
import java.util.ArrayList;
import java.util.List;
+
import static org.apache.dubbo.metrics.model.MetricsCategory.APPLICATION;
import static org.apache.dubbo.metrics.model.key.MetricsKey.APPLICATION_METRIC_INFO;
/**
* Default implementation of {@link MetricsCollector}
*/
-public class DefaultMetricsCollector implements MetricsCollector {
+@Activate
+public class DefaultMetricsCollector extends CombMetricsCollector {
private boolean collectEnabled = false;
- private volatile boolean threadpoolCollectEnabled=false;
- private final SimpleMetricsEventMulticaster eventMulticaster;
- private final MethodMetricsSampler methodSampler = new MethodMetricsSampler(this);
+ private volatile boolean threadpoolCollectEnabled = false;
private final ThreadPoolMetricsSampler threadPoolSampler = new ThreadPoolMetricsSampler(this);
private String applicationName;
private ApplicationModel applicationModel;
private final List samplers = new ArrayList<>();
public DefaultMetricsCollector() {
- this.eventMulticaster = new SimpleMetricsEventMulticaster();
- samplers.add(methodSampler);
+ super(new BaseStatComposite() {
+ @Override
+ protected void init(MethodStatComposite methodStatComposite) {
+ methodStatComposite.initWrapper(DefaultConstants.METHOD_LEVEL_KEYS);
+ }
+
+ @Override
+ protected void init(RtStatComposite rtStatComposite) {
+ rtStatComposite.init(MetricsPlaceValue.of(CommonConstants.PROVIDER, MetricsLevel.METHOD),
+ MetricsPlaceValue.of(CommonConstants.CONSUMER, MetricsLevel.METHOD));
+ }
+ });
+ super.setEventMulticaster(new DefaultSubDispatcher(this));
samplers.add(applicationSampler);
samplers.add(threadPoolSampler);
}
- public void addSampler(MetricsSampler sampler){
+ public void addSampler(MetricsSampler sampler) {
samplers.add(sampler);
}
+
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
@@ -70,10 +93,6 @@ public class DefaultMetricsCollector implements MetricsCollector {
return this.applicationModel;
}
- public SimpleMetricsEventMulticaster getEventMulticaster() {
- return this.eventMulticaster;
- }
-
public void setCollectEnabled(Boolean collectEnabled) {
this.collectEnabled = collectEnabled;
}
@@ -90,14 +109,6 @@ public class DefaultMetricsCollector implements MetricsCollector {
this.threadpoolCollectEnabled = threadpoolCollectEnabled;
}
- public MethodMetricsSampler getMethodSampler() {
- return this.methodSampler;
- }
-
- public ThreadPoolMetricsSampler getThreadPoolSampler() {
- return this.threadPoolSampler;
- }
-
public void collectApplication(ApplicationModel applicationModel) {
this.setApplicationName(applicationModel.getApplicationName());
this.applicationModel = applicationModel;
@@ -111,15 +122,21 @@ public class DefaultMetricsCollector implements MetricsCollector {
@Override
public List collect() {
List list = new ArrayList<>();
+ if (!isCollectEnabled()) {
+ return list;
+ }
+
for (MetricsSampler sampler : samplers) {
List sample = sampler.sample();
list.addAll(sample);
}
+ list.addAll(super.export(MetricsCategory.REQUESTS));
return list;
}
- public void addListener(MetricsListener listener) {
- this.eventMulticaster.addListener(listener);
+ @Override
+ public boolean isSupport(MetricsEvent event) {
+ return event instanceof RequestEvent || event instanceof RequestBeforeEvent;
}
public SimpleMetricsCountSampler applicationSampler = new SimpleMetricsCountSampler() {
@@ -127,17 +144,17 @@ public class DefaultMetricsCollector implements MetricsCollector {
public List sample() {
List samples = new ArrayList<>();
this.getCount(MetricsEvent.Type.APPLICATION_INFO).filter(e -> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) ->
- samples.add(new CounterMetricSample<>(APPLICATION_METRIC_INFO.getName(),
- APPLICATION_METRIC_INFO.getDescription(),
- k.getTags(), APPLICATION, v)))
- );
+ .ifPresent(map -> map.forEach((k, v) ->
+ samples.add(new CounterMetricSample<>(APPLICATION_METRIC_INFO.getName(),
+ APPLICATION_METRIC_INFO.getDescription(),
+ k.getTags(), APPLICATION, v)))
+ );
return samples;
}
@Override
protected void countConfigure(
- MetricsCountSampleConfigurer sampleConfigure) {
+ MetricsCountSampleConfigurer sampleConfigure) {
sampleConfigure.configureMetrics(configure -> new ApplicationMetric(sampleConfigure.getSource()));
}
};
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/HistogramMetricsCollector.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/HistogramMetricsCollector.java
index 28792e8b60..9979707364 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/HistogramMetricsCollector.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/HistogramMetricsCollector.java
@@ -22,22 +22,25 @@ import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
import org.apache.dubbo.config.MetricsConfig;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.config.nested.HistogramConfig;
+import org.apache.dubbo.metrics.MetricsConstants;
import org.apache.dubbo.metrics.MetricsGlobalRegistry;
-import org.apache.dubbo.metrics.event.MetricsEvent;
-import org.apache.dubbo.metrics.event.RTEvent;
-import org.apache.dubbo.metrics.listener.MetricsListener;
+import org.apache.dubbo.metrics.event.RequestEvent;
+import org.apache.dubbo.metrics.listener.AbstractMetricsListener;
import org.apache.dubbo.metrics.model.MethodMetric;
import org.apache.dubbo.metrics.model.key.MetricsKey;
+import org.apache.dubbo.metrics.model.sample.MetricSample;
import org.apache.dubbo.metrics.register.HistogramMetricRegister;
import org.apache.dubbo.metrics.sample.HistogramMetricSample;
import org.apache.dubbo.rpc.model.ApplicationModel;
+import java.util.ArrayList;
+import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import static org.apache.dubbo.metrics.model.MetricsCategory.RT;
-public class HistogramMetricsCollector implements MetricsListener {
+public class HistogramMetricsCollector extends AbstractMetricsListener implements MetricsCollector {
private final ConcurrentHashMap rt = new ConcurrentHashMap<>();
private HistogramMetricRegister metricRegister;
@@ -63,20 +66,28 @@ public class HistogramMetricsCollector implements MetricsListener {
}
private void registerListener() {
- applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class).addListener(this);
+ applicationModel.getBeanFactory().getBean(DefaultMetricsCollector.class).getEventMulticaster().addListener(this);
}
@Override
- public void onEvent(MetricsEvent event) {
- if (event instanceof RTEvent) {
- onRTEvent((RTEvent) event);
- }
+ public void onEvent(RequestEvent event) {
+
}
- private void onRTEvent(RTEvent event) {
+ @Override
+ public void onEventFinish(RequestEvent event) {
+ onRTEvent(event);
+ }
+
+ @Override
+ public void onEventError(RequestEvent event) {
+ onRTEvent(event);
+ }
+
+ private void onRTEvent(RequestEvent event) {
if (metricRegister != null) {
- MethodMetric metric = (MethodMetric) event.getMetric();
- Long responseTime = event.getRt();
+ MethodMetric metric = new MethodMetric(applicationModel.getApplicationName(), event.getAttachmentValue(MetricsConstants.INVOCATION));
+ long responseTime = event.getTimePair().calc();
HistogramMetricSample sample = new HistogramMetricSample(MetricsKey.METRIC_RT_HISTOGRAM.getNameByType(metric.getSide()),
MetricsKey.METRIC_RT_HISTOGRAM.getDescription(), metric.getTags(), RT);
@@ -85,4 +96,9 @@ public class HistogramMetricsCollector implements MetricsListener {
timer.record(responseTime, TimeUnit.MILLISECONDS);
}
}
+
+ @Override
+ public List collect() {
+ return new ArrayList<>();
+ }
}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java
deleted file mode 100644
index 29a14e1ed5..0000000000
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MethodMetricsSampler.java
+++ /dev/null
@@ -1,133 +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.collector.sample;
-
-import org.apache.dubbo.metrics.collector.DefaultMetricsCollector;
-import org.apache.dubbo.metrics.event.MethodEvent;
-import org.apache.dubbo.metrics.event.MetricsEvent;
-import org.apache.dubbo.metrics.event.RTEvent;
-import org.apache.dubbo.metrics.model.MethodMetric;
-import org.apache.dubbo.metrics.model.Metric;
-import org.apache.dubbo.metrics.model.MetricsCategory;
-import org.apache.dubbo.metrics.model.key.MetricsKey;
-import org.apache.dubbo.metrics.model.sample.CounterMetricSample;
-import org.apache.dubbo.metrics.model.sample.GaugeMetricSample;
-import org.apache.dubbo.metrics.model.sample.MetricSample;
-import org.apache.dubbo.rpc.Invocation;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.atomic.AtomicLong;
-import java.util.function.ToDoubleFunction;
-
-import static org.apache.dubbo.common.constants.CommonConstants.CONSUMER_SIDE;
-import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
-
-public class MethodMetricsSampler extends SimpleMetricsCountSampler {
-
- private final DefaultMetricsCollector collector;
-
- public MethodMetricsSampler(DefaultMetricsCollector collector) {
- this.collector = collector;
- }
-
- @Override
- protected void countConfigure(
- MetricsCountSampleConfigurer sampleConfigure) {
- sampleConfigure.configureMetrics(configure -> new MethodMetric(collector.getApplicationName(), configure.getSource()));
- sampleConfigure.configureEventHandler(configure -> collector.getEventMulticaster().publishEvent(new MethodEvent(collector.getApplicationModel(), configure.getMetric(),
- configure.getMetricName())));
- }
-
- @Override
- public void rtConfigure(
- MetricsCountSampleConfigurer sampleConfigure) {
- sampleConfigure.configureMetrics(configure -> new MethodMetric(collector.getApplicationName(), configure.getSource()));
- sampleConfigure.configureEventHandler(configure -> collector.getEventMulticaster().publishEvent(new RTEvent(collector.getApplicationModel(), configure.getMetric(), configure.getRt())));
- }
-
- @Override
- public List sample() {
- List metricSamples = new ArrayList<>();
-
- collect(metricSamples);
- metricSamples.addAll(
- this.collectRT(new MetricSampleFactory>() {
- @Override
- public GaugeMetricSample> newInstance(MetricsKey key, MethodMetric metric, T value, ToDoubleFunction apply) {
- return createGaugeMetricSample(key, metric, MetricsCategory.RT, value, apply);
- }
- }));
-
- return metricSamples;
- }
-
- private void collect(List list) {
- collectBySide(list, PROVIDER_SIDE);
- collectBySide(list, CONSUMER_SIDE);
- }
-
- private void collectBySide(List list, String side) {
- count(list, MetricsEvent.Type.TOTAL.getNameByType(side), MetricSample.Type.COUNTER, MetricsKey.METRIC_REQUESTS);
- count(list, MetricsEvent.Type.SUCCEED.getNameByType(side), MetricSample.Type.COUNTER, MetricsKey.METRIC_REQUESTS_SUCCEED);
- count(list, MetricsEvent.Type.UNKNOWN_FAILED.getNameByType(side), MetricSample.Type.COUNTER, MetricsKey.METRIC_REQUESTS_FAILED);
- count(list, MetricsEvent.Type.PROCESSING.getNameByType(side), MetricSample.Type.GAUGE, MetricsKey.METRIC_REQUESTS_PROCESSING);
- count(list, MetricsEvent.Type.BUSINESS_FAILED.getNameByType(side), MetricSample.Type.COUNTER, MetricsKey.METRIC_REQUEST_BUSINESS_FAILED);
- count(list, MetricsEvent.Type.REQUEST_TIMEOUT.getNameByType(side), MetricSample.Type.COUNTER, MetricsKey.METRIC_REQUESTS_TIMEOUT);
- count(list, MetricsEvent.Type.REQUEST_LIMIT.getNameByType(side), MetricSample.Type.COUNTER, MetricsKey.METRIC_REQUESTS_LIMIT);
- count(list, MetricsEvent.Type.TOTAL_FAILED.getNameByType(side), MetricSample.Type.COUNTER, MetricsKey.METRIC_REQUESTS_TOTAL_FAILED);
- count(list, MetricsEvent.Type.NETWORK_EXCEPTION.getNameByType(side), MetricSample.Type.COUNTER, MetricsKey.METRIC_REQUESTS_NETWORK_FAILED);
- count(list, MetricsEvent.Type.SERVICE_UNAVAILABLE.getNameByType(side), MetricSample.Type.COUNTER, MetricsKey.METRIC_REQUESTS_SERVICE_UNAVAILABLE_FAILED);
- count(list, MetricsEvent.Type.CODEC_EXCEPTION.getNameByType(side), MetricSample.Type.COUNTER, MetricsKey.METRIC_REQUESTS_CODEC_FAILED);
- }
-
-
- private GaugeMetricSample createGaugeMetricSample(MetricsKey metricsKey,
- MethodMetric methodMetric,
- MetricsCategory metricsCategory,
- T value,
- ToDoubleFunction apply) {
- return new GaugeMetricSample<>(
- metricsKey.getNameByType(methodMetric.getSide()),
- metricsKey.getDescription(),
- methodMetric.getTags(),
- metricsCategory,
- value,
- apply);
- }
-
- private void count(List list, String eventType, MetricSample.Type type, MetricsKey metricsKey) {
- getCount(eventType).filter(e -> !e.isEmpty())
- .ifPresent(map -> map.forEach((k, v) -> {
- if(type == MetricSample.Type.COUNTER){
- list.add(createCounterMetricSample(metricsKey, k, MetricsCategory.REQUESTS, v));
- }else if(type == MetricSample.Type.GAUGE){
- list.add(createGaugeMetricSample(metricsKey, k, MetricsCategory.REQUESTS, v, AtomicLong::get));
- }
- }
- ));
- }
-
- private MetricSample createCounterMetricSample(MetricsKey metricsKey, MethodMetric methodMetric, MetricsCategory metricsCategory, AtomicLong value) {
- return new CounterMetricSample<>(metricsKey.getNameByType(methodMetric.getSide()),
- metricsKey.getDescription(),
- methodMetric.getTags(), metricsCategory, value);
-
-
- }
-}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampleConfigurer.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampleConfigurer.java
index 439b04eabf..d66ba6021e 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampleConfigurer.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampleConfigurer.java
@@ -52,20 +52,10 @@ public class MetricsCountSampleConfigurer {
return this;
}
- public MetricsCountSampleConfigurer configureEventHandler(
- Consumer> fireEventHandler){
- this.fireEventHandler = fireEventHandler;
- return this;
- }
-
public S getSource() {
return source;
}
- public K getMetricName() {
- return metricName;
- }
-
public M getMetric() {
return metric;
}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampler.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampler.java
index 8bcd5a976b..0b561f54c0 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampler.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/MetricsCountSampler.java
@@ -18,36 +18,17 @@
package org.apache.dubbo.metrics.collector.sample;
import org.apache.dubbo.metrics.model.Metric;
-import org.apache.dubbo.metrics.model.key.MetricsKey;
-import org.apache.dubbo.metrics.model.sample.MetricSample;
-import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;
-import java.util.function.ToDoubleFunction;
public interface MetricsCountSampler extends MetricsSampler {
void inc(S source, K metricName);
- void dec(S source, K metricName);
-
void incOnEvent(S source, K metricName);
- void decOnEvent(S source, K metricName);
-
- void addRT(S source, Long rt);
-
- void addRT(S source, K metricName, Long rt);
-
Optional> getCount(K metricName);
- List collectRT(MetricSampleFactory factory);
-
- List collectRT(MetricSampleFactory factory, K metricName);
-
- interface MetricSampleFactory {
- R newInstance(MetricsKey key, M metric, T value, ToDoubleFunction apply);
- }
}
diff --git a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/SimpleMetricsCountSampler.java b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/SimpleMetricsCountSampler.java
index 167fbb383b..7f01dcd222 100644
--- a/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/SimpleMetricsCountSampler.java
+++ b/dubbo-metrics/dubbo-metrics-default/src/main/java/org/apache/dubbo/metrics/collector/sample/SimpleMetricsCountSampler.java
@@ -18,20 +18,13 @@
package org.apache.dubbo.metrics.collector.sample;
import org.apache.dubbo.common.utils.Assert;
-import org.apache.dubbo.common.utils.ConcurrentHashMapUtils;
import org.apache.dubbo.metrics.model.Metric;
-import org.apache.dubbo.metrics.model.key.MetricsKey;
-import org.apache.dubbo.metrics.model.sample.MetricSample;
-import java.util.ArrayList;
-import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;
-import java.util.concurrent.atomic.AtomicLongArray;
-import java.util.concurrent.atomic.LongAccumulator;
import java.util.function.Function;
/**
@@ -45,14 +38,6 @@ public abstract class SimpleMetricsCountSampler
private final ConcurrentMap EMPTY_COUNT = new ConcurrentHashMap<>();
private final Map> metricCounter = new ConcurrentHashMap<>();
- // lastRT, totalRT, rtCount, avgRT share a container, can utilize the system cache line
- private final ConcurrentMap rtSample = new ConcurrentHashMap<>();
- private final ConcurrentMap minRT = new ConcurrentHashMap<>();
- private final ConcurrentMap maxRT = new ConcurrentHashMap<>();
-
- private final ConcurrentMap> rtGroupSample = new ConcurrentHashMap<>();
- private final ConcurrentMap> groupMinRT = new ConcurrentHashMap<>();
- private final ConcurrentMap> groupMaxRT = new ConcurrentHashMap<>();
@Override
public void inc(S source, K metricName) {
@@ -62,14 +47,6 @@ public abstract class SimpleMetricsCountSampler
});
}
- @Override
- public void dec(S source, K metricName) {
- doExecute(source, metricName, counter -> {
- counter.decrementAndGet();
- return false;
- });
- }
-
@Override
public void incOnEvent(S source, K metricName) {
doExecute(source, metricName, counter -> {
@@ -78,91 +55,6 @@ public abstract class SimpleMetricsCountSampler
});
}
- @Override
- public void decOnEvent(S source, K metricName) {
- doExecute(source, metricName, counter -> {
- counter.decrementAndGet();
- return true;
- });
- }
-
- @Override
- public void addRT(S source, Long rt) {
- MetricsCountSampleConfigurer sampleConfigure = new MetricsCountSampleConfigurer<>();
- sampleConfigure.setSource(source);
-
- this.rtConfigure(sampleConfigure);
-
- M metric = sampleConfigure.getMetric();
-
- AtomicLongArray rtCalculator = ConcurrentHashMapUtils.computeIfAbsent(this.rtSample, metric, k -> new AtomicLongArray(4));
-
- // set lastRT
- rtCalculator.set(0, rt);
-
- // add to totalRT
- rtCalculator.addAndGet(1, rt);
-
- // add to rtCount
- rtCalculator.incrementAndGet(2);
-
- // calc avgRT. In order to reduce the amount of calculation, calculated when collect
- //rtArray.set(3, Math.floorDiv(rtArray.get(1), rtArray.get(2)));
-
- LongAccumulator min = ConcurrentHashMapUtils.computeIfAbsent(minRT, metric, k -> new LongAccumulator(Long::min, Long.MAX_VALUE));
- min.accumulate(rt);
-
- LongAccumulator max = ConcurrentHashMapUtils.computeIfAbsent(maxRT, metric, k -> new LongAccumulator(Long::max, Long.MIN_VALUE));
- max.accumulate(rt);
-
- sampleConfigure.setRt(rt);
-
- sampleConfigure.getFireEventHandler().accept(sampleConfigure);
- }
-
- @Override
- public void addRT(S source, K metricName, Long rt) {
- MetricsCountSampleConfigurer sampleConfigure = new MetricsCountSampleConfigurer<>();
- sampleConfigure.setSource(source);
- sampleConfigure.setMetricsName(metricName);
-
- this.rtConfigure(sampleConfigure);
-
- M metric = sampleConfigure.getMetric();
-
- ConcurrentMap nameToCalculator = rtGroupSample.get(metricName);
-
- if (nameToCalculator == null) {
- ConcurrentHashMap calculator = new ConcurrentHashMap<>();
- calculator.put(metric, new AtomicLongArray(4));
-
- rtGroupSample.put(metricName, calculator);
-
- nameToCalculator = rtGroupSample.get(metricName);
- }
- AtomicLongArray calculator = nameToCalculator.get(metric);
-
- // set lastRT
- calculator.set(0, rt);
-
- // add to totalRT
- calculator.addAndGet(1, rt);
-
- // add to rtCount
- calculator.incrementAndGet(2);
-
- ConcurrentMap minRT = ConcurrentHashMapUtils.computeIfAbsent(groupMinRT, metricName, k -> new ConcurrentHashMap<>());
- LongAccumulator min = ConcurrentHashMapUtils.computeIfAbsent(minRT, metric, k -> new LongAccumulator(Long::min, Long.MAX_VALUE));
- min.accumulate(rt);
-
- ConcurrentMap maxRT = ConcurrentHashMapUtils.computeIfAbsent(groupMaxRT, metricName, k -> new ConcurrentHashMap<>());
- LongAccumulator max = ConcurrentHashMapUtils.computeIfAbsent(maxRT, metric, k -> new LongAccumulator(Long::max, Long.MIN_VALUE));
- max.accumulate(rt);
- sampleConfigure.setRt(rt);
-
- sampleConfigure.getFireEventHandler().accept(sampleConfigure);
- }
-
@Override
public Optional> getCount(K metricName) {
return Optional.ofNullable(metricCounter.get(metricName) == null ?
@@ -170,42 +62,6 @@ public abstract class SimpleMetricsCountSampler
metricCounter.get(metricName));
}
- @Override
- public List collectRT(MetricSampleFactory factory) {
- return collect(factory, rtSample, this.minRT, this.maxRT);
- }
-
- @Override
- public List collectRT(MetricSampleFactory factory, K metricName) {
- return collect(factory, rtGroupSample.get(metricName), groupMinRT.get(metricName), groupMaxRT.get(metricName));
- }
-
- private List collect(MetricSampleFactory