This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 5a3d52202 [type:refactor] add CommonMetaDataSubscriber and refactor
dubbo part (#3766)
5a3d52202 is described below
commit 5a3d522023e87ffe8ada761d09c21f9464663a66
Author: dragon-zhang <[email protected]>
AuthorDate: Thu Jul 28 15:29:47 2022 +0800
[type:refactor] add CommonMetaDataSubscriber and refactor dubbo part (#3766)
* [type:refactor] add CommonMetaDataSubscriber and refactor dubbo part
* fix code style
* fix test case
* fix test case
* fix name
* remove unuse imports
* rollback modify
---
.../base/cache/CommonMetaDataSubscriber.java | 65 +++++++++++++++++++
.../plugin/base/handler/MetaDataHandler.java | 54 ++++++++++++++++
.../dubbo/handler/AlibabaDubboMetaDataHandler.java | 50 +++++++++++++++
.../subscriber/AlibabaDubboMetaDataSubscriber.java | 65 -------------------
.../AlibabaDubboMetaDataHandlerTest.java} | 23 ++++---
.../dubbo/handler/ApacheDubboMetaDataHandler.java | 50 +++++++++++++++
.../subscriber/ApacheDubboMetaDataSubscriber.java | 65 -------------------
.../ApacheDubboMetaDataHandlerTest.java} | 20 +++---
.../handler/AbstractDubboMetaDataHandler.java | 72 ++++++++++++++++++++++
.../starter/gateway/ShenyuConfiguration.java | 19 +++++-
.../dubbo/AlibabaDubboPluginConfiguration.java | 12 ++--
.../dubbo/AlibabaDubboPluginConfigurationTest.java | 8 +--
.../dubbo/ApacheDubboPluginConfiguration.java | 12 ++--
.../dubbo/ApacheDubboPluginConfigurationTest.java | 8 +--
14 files changed, 348 insertions(+), 175 deletions(-)
diff --git
a/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/cache/CommonMetaDataSubscriber.java
b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/cache/CommonMetaDataSubscriber.java
new file mode 100644
index 000000000..316e2ab5b
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/cache/CommonMetaDataSubscriber.java
@@ -0,0 +1,65 @@
+/*
+ * 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.shenyu.plugin.base.cache;
+
+import org.apache.commons.collections4.MapUtils;
+import org.apache.shenyu.common.dto.MetaData;
+import org.apache.shenyu.plugin.base.handler.MetaDataHandler;
+import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+/**
+ * The type common meta data subscriber.
+ */
+public class CommonMetaDataSubscriber implements MetaDataSubscriber {
+
+ private final Map<String, MetaDataHandler> handlerMap;
+
+ /**
+ * Instantiates a new Common meta data subscriber.
+ *
+ * @param metaDataHandlerList the plugin data handler list
+ */
+ public CommonMetaDataSubscriber(final List<MetaDataHandler>
metaDataHandlerList) {
+ this.handlerMap =
metaDataHandlerList.stream().collect(Collectors.toConcurrentMap(MetaDataHandler::rpcType,
e -> e));
+ }
+
+ @Override
+ public void onSubscribe(final MetaData metaData) {
+ Optional.ofNullable(handlerMap.get(metaData.getRpcType()))
+ .ifPresent(handler -> handler.handle(metaData));
+ }
+
+ @Override
+ public void unSubscribe(final MetaData metaData) {
+ Optional.ofNullable(handlerMap.get(metaData.getRpcType()))
+ .ifPresent(handler -> handler.remove(metaData));
+ }
+
+ @Override
+ public void refresh() {
+ if (MapUtils.isEmpty(handlerMap)) {
+ return;
+ }
+ handlerMap.forEach((k, v) -> v.refresh());
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/handler/MetaDataHandler.java
b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/handler/MetaDataHandler.java
new file mode 100644
index 000000000..539a34924
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/handler/MetaDataHandler.java
@@ -0,0 +1,54 @@
+/*
+ * 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.shenyu.plugin.base.handler;
+
+import org.apache.shenyu.common.dto.MetaData;
+
+/**
+ * The interface meta data handler.
+ */
+public interface MetaDataHandler {
+
+ /**
+ * Handle metaData.
+ *
+ * @param metaData the meta data
+ */
+ void handle(MetaData metaData);
+
+ /**
+ * Remove metaData.
+ *
+ * @param metaData the meta data
+ */
+ void remove(MetaData metaData);
+
+ /**
+ * Refresh.
+ */
+ default void refresh() {
+ }
+
+ /**
+ * rpc type string.
+ *
+ * @return the rpc type string
+ * @see org.apache.shenyu.common.enums.RpcTypeEnum#getName()
+ */
+ String rpcType();
+}
diff --git
a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/handler/AlibabaDubboMetaDataHandler.java
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/handler/AlibabaDubboMetaDataHandler.java
new file mode 100644
index 000000000..42d34b789
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/handler/AlibabaDubboMetaDataHandler.java
@@ -0,0 +1,50 @@
+/*
+ * 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.shenyu.plugin.alibaba.dubbo.handler;
+
+import org.apache.shenyu.common.dto.MetaData;
+import org.apache.shenyu.plugin.alibaba.dubbo.cache.AlibabaDubboConfigCache;
+import
org.apache.shenyu.plugin.dubbo.common.handler.AbstractDubboMetaDataHandler;
+
+import java.util.Objects;
+
+/**
+ * The type Alibaba dubbo meta data handler.
+ */
+public class AlibabaDubboMetaDataHandler extends AbstractDubboMetaDataHandler {
+
+ @Override
+ protected boolean isInitialized(final MetaData metaData) {
+ return
Objects.nonNull(AlibabaDubboConfigCache.getInstance().get(metaData.getPath()));
+ }
+
+ @Override
+ protected void initReference(final MetaData metaData) {
+ AlibabaDubboConfigCache.getInstance().initRef(metaData);
+ }
+
+ @Override
+ protected void updateReference(final MetaData metaData) {
+ AlibabaDubboConfigCache.getInstance().build(metaData);
+ }
+
+ @Override
+ protected void invalidateReference(final String path) {
+ AlibabaDubboConfigCache.getInstance().invalidate(path);
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/subscriber/AlibabaDubboMetaDataSubscriber.java
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/subscriber/AlibabaDubboMetaDataSubscriber.java
deleted file mode 100644
index 165e9d642..000000000
---
a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/plugin/alibaba/dubbo/subscriber/AlibabaDubboMetaDataSubscriber.java
+++ /dev/null
@@ -1,65 +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.shenyu.plugin.alibaba.dubbo.subscriber;
-
-import com.google.common.collect.Maps;
-
-import java.util.Objects;
-import java.util.concurrent.ConcurrentMap;
-
-import org.apache.shenyu.common.dto.MetaData;
-import org.apache.shenyu.common.enums.RpcTypeEnum;
-import org.apache.shenyu.plugin.alibaba.dubbo.cache.AlibabaDubboConfigCache;
-import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
-
-/**
- * The type Alibaba dubbo meta data subscriber.
- */
-public class AlibabaDubboMetaDataSubscriber implements MetaDataSubscriber {
-
- private static final ConcurrentMap<String, MetaData> META_DATA =
Maps.newConcurrentMap();
-
- @Override
- public void onSubscribe(final MetaData metaData) {
- if (RpcTypeEnum.DUBBO.getName().equals(metaData.getRpcType())) {
- MetaData exist = META_DATA.get(metaData.getPath());
- if (Objects.isNull(exist) ||
Objects.isNull(AlibabaDubboConfigCache.getInstance().get(metaData.getPath()))) {
- // The first initialization
- AlibabaDubboConfigCache.getInstance().initRef(metaData);
- } else {
- // There are updates, which only support the update of four
properties of serviceName rpcExt parameterTypes methodName,
- // because these four properties will affect the call of Dubbo;
- if (!Objects.equals(metaData.getServiceName(),
exist.getServiceName())
- || !Objects.equals(metaData.getRpcExt(),
exist.getRpcExt())
- || !Objects.equals(metaData.getParameterTypes(),
exist.getParameterTypes())
- || !Objects.equals(metaData.getMethodName(),
exist.getMethodName())) {
- AlibabaDubboConfigCache.getInstance().build(metaData);
- }
- }
- META_DATA.put(metaData.getPath(), metaData);
- }
- }
-
- @Override
- public void unSubscribe(final MetaData metaData) {
- if (RpcTypeEnum.DUBBO.getName().equals(metaData.getRpcType())) {
-
AlibabaDubboConfigCache.getInstance().invalidate(metaData.getPath());
- META_DATA.remove(metaData.getPath());
- }
- }
-}
diff --git
a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/test/java/org/apache/shenyu/plugin/alibaba/dubbo/subscriber/AlibabaDubboMetaDataSubscriberTest.java
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/test/java/org/apache/shenyu/plugin/alibaba/dubbo/handler/AlibabaDubboMetaDataHandlerTest.java
similarity index 76%
rename from
shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/test/java/org/apache/shenyu/plugin/alibaba/dubbo/subscriber/AlibabaDubboMetaDataSubscriberTest.java
rename to
shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/test/java/org/apache/shenyu/plugin/alibaba/dubbo/handler/AlibabaDubboMetaDataHandlerTest.java
index 29ea93676..7cd1c2c44 100644
---
a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/test/java/org/apache/shenyu/plugin/alibaba/dubbo/subscriber/AlibabaDubboMetaDataSubscriberTest.java
+++
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-alibaba-dubbo/src/test/java/org/apache/shenyu/plugin/alibaba/dubbo/handler/AlibabaDubboMetaDataHandlerTest.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shenyu.plugin.alibaba.dubbo.subscriber;
+package org.apache.shenyu.plugin.alibaba.dubbo.handler;
import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.enums.RpcTypeEnum;
@@ -29,21 +29,20 @@ import org.mockito.quality.Strictness;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
-
/**
- * The Test Case For AlibabaDubboMetaDataSubscriber.
+ * The Test Case For AlibabaDubboMetaDataHandler.
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
-public final class AlibabaDubboMetaDataSubscriberTest {
+public final class AlibabaDubboMetaDataHandlerTest {
- private AlibabaDubboMetaDataSubscriber alibabaDubboMetaDataSubscriber;
+ private AlibabaDubboMetaDataHandler alibabaDubboMetaDataHandler;
private MetaData metaData;
@BeforeEach
public void setUp() {
- alibabaDubboMetaDataSubscriber = new AlibabaDubboMetaDataSubscriber();
+ alibabaDubboMetaDataHandler = new AlibabaDubboMetaDataHandler();
metaData = new MetaData();
metaData.setId("1332017966661636096");
metaData.setAppName("dubbo");
@@ -57,7 +56,7 @@ public final class AlibabaDubboMetaDataSubscriberTest {
@Test
public void testOnSubscribe() {
- alibabaDubboMetaDataSubscriber.onSubscribe(metaData);
+ alibabaDubboMetaDataHandler.handle(metaData);
MetaData metaData = MetaData.builder()
.id("1332017966661636096")
.appName("dubbo")
@@ -67,11 +66,11 @@ public final class AlibabaDubboMetaDataSubscriberTest {
.rpcType(RpcTypeEnum.DUBBO.getName())
.rpcExt("{\"group\":\"Group\",\"version\":\"2.6.5\",\"url\":\"http://192.168.55.113/dubbo\",\"cluster\":\"failover\"}")
.parameterTypes("parameterTypes").build();
- AlibabaDubboMetaDataSubscriber alibabaDubboMetaDataSubscriberMock =
mock(AlibabaDubboMetaDataSubscriber.class);
-
doNothing().when(alibabaDubboMetaDataSubscriberMock).onSubscribe(metaData);
- alibabaDubboMetaDataSubscriberMock.onSubscribe(metaData);
+ AlibabaDubboMetaDataHandler alibabaDubboMetaDataHandlerMock =
mock(AlibabaDubboMetaDataHandler.class);
+ doNothing().when(alibabaDubboMetaDataHandlerMock).handle(metaData);
+ alibabaDubboMetaDataHandlerMock.handle(metaData);
// hit else
- alibabaDubboMetaDataSubscriber.onSubscribe(metaData);
- alibabaDubboMetaDataSubscriber.unSubscribe(metaData);
+ alibabaDubboMetaDataHandler.handle(metaData);
+ alibabaDubboMetaDataHandler.remove(metaData);
}
}
diff --git
a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/handler/ApacheDubboMetaDataHandler.java
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/handler/ApacheDubboMetaDataHandler.java
new file mode 100644
index 000000000..adb384e02
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/handler/ApacheDubboMetaDataHandler.java
@@ -0,0 +1,50 @@
+/*
+ * 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.shenyu.plugin.apache.dubbo.handler;
+
+import org.apache.shenyu.common.dto.MetaData;
+import org.apache.shenyu.plugin.apache.dubbo.cache.ApacheDubboConfigCache;
+import
org.apache.shenyu.plugin.dubbo.common.handler.AbstractDubboMetaDataHandler;
+
+import java.util.Objects;
+
+/**
+ * The type Apache dubbo meta data subscriber.
+ */
+public class ApacheDubboMetaDataHandler extends AbstractDubboMetaDataHandler {
+
+ @Override
+ protected boolean isInitialized(final MetaData metaData) {
+ return
Objects.nonNull(ApacheDubboConfigCache.getInstance().get(metaData.getPath()));
+ }
+
+ @Override
+ protected void initReference(final MetaData metaData) {
+ ApacheDubboConfigCache.getInstance().initRef(metaData);
+ }
+
+ @Override
+ protected void updateReference(final MetaData metaData) {
+ ApacheDubboConfigCache.getInstance().build(metaData);
+ }
+
+ @Override
+ protected void invalidateReference(final String path) {
+ ApacheDubboConfigCache.getInstance().invalidate(path);
+ }
+}
diff --git
a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/subscriber/ApacheDubboMetaDataSubscriber.java
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/subscriber/ApacheDubboMetaDataSubscriber.java
deleted file mode 100644
index ba007fe0c..000000000
---
a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/subscriber/ApacheDubboMetaDataSubscriber.java
+++ /dev/null
@@ -1,65 +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.shenyu.plugin.apache.dubbo.subscriber;
-
-import com.google.common.collect.Maps;
-
-import java.util.Objects;
-import java.util.concurrent.ConcurrentMap;
-
-import org.apache.shenyu.common.dto.MetaData;
-import org.apache.shenyu.common.enums.RpcTypeEnum;
-import org.apache.shenyu.plugin.apache.dubbo.cache.ApacheDubboConfigCache;
-import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
-
-/**
- * The type Apache dubbo meta data subscriber.
- */
-public class ApacheDubboMetaDataSubscriber implements MetaDataSubscriber {
-
- private static final ConcurrentMap<String, MetaData> META_DATA =
Maps.newConcurrentMap();
-
- @Override
- public void onSubscribe(final MetaData metaData) {
- if (RpcTypeEnum.DUBBO.getName().equals(metaData.getRpcType())) {
- MetaData exist = META_DATA.get(metaData.getPath());
- if (Objects.isNull(exist) ||
Objects.isNull(ApacheDubboConfigCache.getInstance().get(metaData.getPath()))) {
- // The first initialization
- ApacheDubboConfigCache.getInstance().initRef(metaData);
- } else {
- // There are updates, which only support the update of four
properties of serviceName rpcExt parameterTypes methodName,
- // because these four properties will affect the call of Dubbo;
- if (!Objects.equals(metaData.getServiceName(),
exist.getServiceName())
- || !Objects.equals(metaData.getRpcExt(),
exist.getRpcExt())
- || !Objects.equals(metaData.getParameterTypes(),
exist.getParameterTypes())
- || !Objects.equals(metaData.getMethodName(),
exist.getMethodName())) {
- ApacheDubboConfigCache.getInstance().build(metaData);
- }
- }
- META_DATA.put(metaData.getPath(), metaData);
- }
- }
-
- @Override
- public void unSubscribe(final MetaData metaData) {
- if (RpcTypeEnum.DUBBO.getName().equals(metaData.getRpcType())) {
-
ApacheDubboConfigCache.getInstance().invalidate(metaData.getPath());
- META_DATA.remove(metaData.getPath());
- }
- }
-}
diff --git
a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/subscriber/ApacheDubboMetaDataSubscriberTest.java
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/handler/ApacheDubboMetaDataHandlerTest.java
similarity index 78%
rename from
shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/subscriber/ApacheDubboMetaDataSubscriberTest.java
rename to
shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/handler/ApacheDubboMetaDataHandlerTest.java
index ba0090e91..9e741da04 100644
---
a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/subscriber/ApacheDubboMetaDataSubscriberTest.java
+++
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/test/java/org/apache/shenyu/plugin/apache/dubbo/handler/ApacheDubboMetaDataHandlerTest.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.shenyu.plugin.apache.dubbo.subscriber;
+package org.apache.shenyu.plugin.apache.dubbo.handler;
import org.apache.shenyu.common.dto.MetaData;
import org.apache.shenyu.common.enums.RpcTypeEnum;
@@ -30,19 +30,19 @@ import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
/**
- * The Test Case For ApacheDubboMetaDataSubscriber.
+ * The Test Case For ApacheDubboMetaDataHandler.
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
-public final class ApacheDubboMetaDataSubscriberTest {
+public final class ApacheDubboMetaDataHandlerTest {
- private ApacheDubboMetaDataSubscriber apacheDubboMetaDataSubscriber;
+ private ApacheDubboMetaDataHandler apacheDubboMetaDataHandler;
private MetaData metaData;
@BeforeEach
public void setUp() {
- apacheDubboMetaDataSubscriber = new ApacheDubboMetaDataSubscriber();
+ apacheDubboMetaDataHandler = new ApacheDubboMetaDataHandler();
metaData = new MetaData();
metaData.setId("1332017966661636096");
metaData.setAppName("dubbo");
@@ -56,7 +56,7 @@ public final class ApacheDubboMetaDataSubscriberTest {
@Test
public void testOnSubscribe() {
- apacheDubboMetaDataSubscriber.onSubscribe(metaData);
+ apacheDubboMetaDataHandler.handle(metaData);
MetaData metaData = MetaData.builder()
.id("1332017966661636096")
.appName("dubbo")
@@ -66,9 +66,9 @@ public final class ApacheDubboMetaDataSubscriberTest {
.rpcType(RpcTypeEnum.DUBBO.getName())
.rpcExt("{\"group\":\"Group\",\"version\":\"2.7.5\",\"loadbalance\":\"Balance\",\"url\":\"http://192.168.55.113/dubbo\"}")
.parameterTypes("parameterTypes").build();
- ApacheDubboMetaDataSubscriber apacheDubboMetaDataSubscriberMock =
mock(ApacheDubboMetaDataSubscriber.class);
-
doNothing().when(apacheDubboMetaDataSubscriberMock).onSubscribe(metaData);
- apacheDubboMetaDataSubscriberMock.onSubscribe(metaData);
- apacheDubboMetaDataSubscriber.unSubscribe(metaData);
+ ApacheDubboMetaDataHandler apacheDubboMetaDataHandlerMock =
mock(ApacheDubboMetaDataHandler.class);
+ doNothing().when(apacheDubboMetaDataHandlerMock).handle(metaData);
+ apacheDubboMetaDataHandlerMock.handle(metaData);
+ apacheDubboMetaDataHandler.remove(metaData);
}
}
diff --git
a/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/handler/AbstractDubboMetaDataHandler.java
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/handler/AbstractDubboMetaDataHandler.java
new file mode 100644
index 000000000..8bba4acd5
--- /dev/null
+++
b/shenyu-plugin/shenyu-plugin-dubbo/shenyu-plugin-dubbo-common/src/main/java/org/apache/shenyu/plugin/dubbo/common/handler/AbstractDubboMetaDataHandler.java
@@ -0,0 +1,72 @@
+/*
+ * 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.shenyu.plugin.dubbo.common.handler;
+
+import com.google.common.collect.Maps;
+import org.apache.shenyu.common.dto.MetaData;
+import org.apache.shenyu.common.enums.RpcTypeEnum;
+import org.apache.shenyu.plugin.base.handler.MetaDataHandler;
+
+import java.util.Objects;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * The common dubbo meta data handler.
+ */
+public abstract class AbstractDubboMetaDataHandler implements MetaDataHandler {
+
+ private static final ConcurrentMap<String, MetaData> META_DATA =
Maps.newConcurrentMap();
+
+ @Override
+ public void handle(final MetaData metaData) {
+ MetaData exist = META_DATA.get(metaData.getPath());
+ if (Objects.isNull(exist) || !isInitialized(metaData)) {
+ // The first initialization
+ initReference(metaData);
+ } else {
+ // There are updates, which only support the update of four
properties of serviceName rpcExt parameterTypes methodName,
+ // because these four properties will affect the call of Dubbo;
+ if (!Objects.equals(metaData.getServiceName(),
exist.getServiceName())
+ || !Objects.equals(metaData.getRpcExt(), exist.getRpcExt())
+ || !Objects.equals(metaData.getParameterTypes(),
exist.getParameterTypes())
+ || !Objects.equals(metaData.getMethodName(),
exist.getMethodName())) {
+ updateReference(metaData);
+ }
+ }
+ META_DATA.put(metaData.getPath(), metaData);
+ }
+
+ protected abstract boolean isInitialized(MetaData metaData);
+
+ protected abstract void initReference(MetaData metaData);
+
+ protected abstract void updateReference(MetaData metaData);
+
+ @Override
+ public void remove(final MetaData metaData) {
+ invalidateReference(metaData.getPath());
+ META_DATA.remove(metaData.getPath());
+ }
+
+ protected abstract void invalidateReference(String path);
+
+ @Override
+ public String rpcType() {
+ return RpcTypeEnum.DUBBO.getName();
+ }
+}
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfiguration.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfiguration.java
index 26f6b9fd4..3bdf05b77 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfiguration.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-gateway/src/main/java/org/apache/shenyu/springboot/starter/gateway/ShenyuConfiguration.java
@@ -21,8 +21,11 @@ import org.apache.shenyu.common.config.ShenyuConfig;
import org.apache.shenyu.plugin.api.RemoteAddressResolver;
import org.apache.shenyu.plugin.api.ShenyuPlugin;
import org.apache.shenyu.plugin.base.RpcParamTransformPlugin;
+import org.apache.shenyu.plugin.base.cache.CommonMetaDataSubscriber;
import org.apache.shenyu.plugin.base.cache.CommonPluginDataSubscriber;
+import org.apache.shenyu.plugin.base.handler.MetaDataHandler;
import org.apache.shenyu.plugin.base.handler.PluginDataHandler;
+import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
import org.apache.shenyu.sync.data.api.PluginDataSubscriber;
import org.apache.shenyu.web.configuration.ErrorHandlerConfiguration;
import org.apache.shenyu.web.configuration.ShenyuExtConfiguration;
@@ -110,7 +113,7 @@ public class ShenyuConfiguration {
}
/**
- * Plugin data subscriber.
+ * common plugin data subscriber.
*
* @param pluginDataHandlerList the plugin data handler list
* @param eventPublisher event publisher
@@ -121,6 +124,17 @@ public class ShenyuConfiguration {
final
ApplicationEventPublisher eventPublisher) {
return new
CommonPluginDataSubscriber(pluginDataHandlerList.getIfAvailable(Collections::emptyList),
eventPublisher);
}
+
+ /**
+ * common meta data subscriber.
+ *
+ * @param metaDataHandlerList the meta data handler list
+ * @return the meta data subscriber
+ */
+ @Bean
+ public MetaDataSubscriber commonMetaDataSubscriber(final
ObjectProvider<List<MetaDataHandler>> metaDataHandlerList) {
+ return new
CommonMetaDataSubscriber(metaDataHandlerList.getIfAvailable(Collections::emptyList));
+ }
/**
* Shenyu loader service.
@@ -133,8 +147,7 @@ public class ShenyuConfiguration {
@Bean
public ShenyuLoaderService shenyuLoaderService(final ShenyuWebHandler
shenyuWebHandler,
final PluginDataSubscriber
pluginDataSubscriber,
- final ShenyuConfig config
- ) {
+ final ShenyuConfig config) {
return new ShenyuLoaderService(shenyuWebHandler,
(CommonPluginDataSubscriber) pluginDataSubscriber, config);
}
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/springboot/starter/plugin/alibaba/dubbo/AlibabaDubboPluginConfiguration.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/springboot/starter/plugin/alibaba/dubbo
[...]
index 6dbaab99f..676aef006 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/springboot/starter/plugin/alibaba/dubbo/AlibabaDubboPluginConfiguration.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-alibaba-dubbo/src/main/java/org/apache/shenyu/springboot/starter/plugin/alibaba/dubbo/AlibabaDubboPluginConfiguration.java
@@ -20,12 +20,12 @@ package
org.apache.shenyu.springboot.starter.plugin.alibaba.dubbo;
import org.apache.shenyu.plugin.alibaba.dubbo.AlibabaDubboPlugin;
import
org.apache.shenyu.plugin.alibaba.dubbo.handler.AlibabaDubboPluginDataHandler;
import org.apache.shenyu.plugin.alibaba.dubbo.proxy.AlibabaDubboProxyService;
-import
org.apache.shenyu.plugin.alibaba.dubbo.subscriber.AlibabaDubboMetaDataSubscriber;
+import
org.apache.shenyu.plugin.alibaba.dubbo.handler.AlibabaDubboMetaDataHandler;
import org.apache.shenyu.plugin.api.ShenyuPlugin;
+import org.apache.shenyu.plugin.base.handler.MetaDataHandler;
import org.apache.shenyu.plugin.dubbo.common.param.DubboParamResolveService;
import org.apache.shenyu.plugin.base.handler.PluginDataHandler;
import
org.apache.shenyu.springboot.plugin.dubbo.common.DubboCommonConfiguration;
-import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -64,13 +64,13 @@ public class AlibabaDubboPluginConfiguration {
}
/**
- * Dubbo meta data subscriber.
+ * Dubbo meta data handler.
*
- * @return the meta data subscriber
+ * @return the meta data handler
*/
@Bean
- public MetaDataSubscriber dubboMetaDataSubscriber() {
- return new AlibabaDubboMetaDataSubscriber();
+ public MetaDataHandler alibabaDubboMetaDataHandler() {
+ return new AlibabaDubboMetaDataHandler();
}
}
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-alibaba-dubbo/src/test/java/org/apache/shenyu/springboot/starter/plugin/alibaba/dubbo/AlibabaDubboPluginConfigurationTest.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-alibaba-dubbo/src/test/java/org/apache/shenyu/springboot/starter/plugin/alibaba/d
[...]
index d362c0abf..1d45196b0 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-alibaba-dubbo/src/test/java/org/apache/shenyu/springboot/starter/plugin/alibaba/dubbo/AlibabaDubboPluginConfigurationTest.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-alibaba-dubbo/src/test/java/org/apache/shenyu/springboot/starter/plugin/alibaba/dubbo/AlibabaDubboPluginConfigurationTest.java
@@ -19,8 +19,8 @@ package
org.apache.shenyu.springboot.starter.plugin.alibaba.dubbo;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.plugin.api.ShenyuPlugin;
+import org.apache.shenyu.plugin.base.handler.MetaDataHandler;
import org.apache.shenyu.plugin.base.handler.PluginDataHandler;
-import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -71,10 +71,10 @@ public class AlibabaDubboPluginConfigurationTest {
}
@Test
- public void testAlibabaDubboMetaDataSubscriber() {
+ public void testAlibabaDubboMetaDataHandler() {
applicationContextRunner.run(context -> {
- MetaDataSubscriber subscriber =
context.getBean("dubboMetaDataSubscriber", MetaDataSubscriber.class);
- assertNotNull(subscriber);
+ MetaDataHandler handler =
context.getBean("alibabaDubboMetaDataHandler", MetaDataHandler.class);
+ assertNotNull(handler);
}
);
}
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-apache-dubbo/src/main/java/org/apache/shenyu/springboot/starter/plugin/apache/dubbo/ApacheDubboPluginConfiguration.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-apache-dubbo/src/main/java/org/apache/shenyu/springboot/starter/plugin/apache/dubbo/Apac
[...]
index a598ff80c..76a98e4dc 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-apache-dubbo/src/main/java/org/apache/shenyu/springboot/starter/plugin/apache/dubbo/ApacheDubboPluginConfiguration.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-apache-dubbo/src/main/java/org/apache/shenyu/springboot/starter/plugin/apache/dubbo/ApacheDubboPluginConfiguration.java
@@ -20,12 +20,12 @@ package
org.apache.shenyu.springboot.starter.plugin.apache.dubbo;
import org.apache.shenyu.plugin.apache.dubbo.ApacheDubboPlugin;
import
org.apache.shenyu.plugin.apache.dubbo.handler.ApacheDubboPluginDataHandler;
import org.apache.shenyu.plugin.apache.dubbo.proxy.ApacheDubboProxyService;
-import
org.apache.shenyu.plugin.apache.dubbo.subscriber.ApacheDubboMetaDataSubscriber;
+import
org.apache.shenyu.plugin.apache.dubbo.handler.ApacheDubboMetaDataHandler;
import org.apache.shenyu.plugin.api.ShenyuPlugin;
+import org.apache.shenyu.plugin.base.handler.MetaDataHandler;
import org.apache.shenyu.plugin.dubbo.common.param.DubboParamResolveService;
import org.apache.shenyu.plugin.base.handler.PluginDataHandler;
import
org.apache.shenyu.springboot.plugin.dubbo.common.DubboCommonConfiguration;
-import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -64,12 +64,12 @@ public class ApacheDubboPluginConfiguration {
}
/**
- * Apache dubbo meta data subscriber.
+ * Apache dubbo meta data handler.
*
- * @return the meta data subscriber
+ * @return the meta data handler
*/
@Bean
- public MetaDataSubscriber apacheDubboMetaDataSubscriber() {
- return new ApacheDubboMetaDataSubscriber();
+ public MetaDataHandler apacheDubboMetaDataHandler() {
+ return new ApacheDubboMetaDataHandler();
}
}
diff --git
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-apache-dubbo/src/test/java/org/apache/shenyu/springboot/starter/plugin/apache/dubbo/ApacheDubboPluginConfigurationTest.java
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-apache-dubbo/src/test/java/org/apache/shenyu/springboot/starter/plugin/apache/dubbo/
[...]
index aed8098c9..020d4c7ca 100644
---
a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-apache-dubbo/src/test/java/org/apache/shenyu/springboot/starter/plugin/apache/dubbo/ApacheDubboPluginConfigurationTest.java
+++
b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-dubbo/shenyu-spring-boot-starter-plugin-apache-dubbo/src/test/java/org/apache/shenyu/springboot/starter/plugin/apache/dubbo/ApacheDubboPluginConfigurationTest.java
@@ -19,8 +19,8 @@ package
org.apache.shenyu.springboot.starter.plugin.apache.dubbo;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.plugin.api.ShenyuPlugin;
+import org.apache.shenyu.plugin.base.handler.MetaDataHandler;
import org.apache.shenyu.plugin.base.handler.PluginDataHandler;
-import org.apache.shenyu.sync.data.api.MetaDataSubscriber;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -71,10 +71,10 @@ public class ApacheDubboPluginConfigurationTest {
}
@Test
- public void testApacheDubboMetaDataSubscriber() {
+ public void testApacheDubboMetaDataHandler() {
applicationContextRunner.run(context -> {
- MetaDataSubscriber subscriber =
context.getBean("apacheDubboMetaDataSubscriber", MetaDataSubscriber.class);
- assertNotNull(subscriber);
+ MetaDataHandler handler =
context.getBean("apacheDubboMetaDataHandler", MetaDataHandler.class);
+ assertNotNull(handler);
}
);
}