This is an automated email from the ASF dual-hosted git repository.
Aias00 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 550c0c3df1 fix(client): propagate namespaces through API registrars
(#7231)
550c0c3df1 is described below
commit 550c0c3df149b90aaf6c734387bfa6e0047771eb
Author: Liming Deng <[email protected]>
AuthorDate: Thu Oct 1 11:58:58 2026 +0800
fix(client): propagate namespaces through API registrars (#7231)
---
.../client/core/register/ClientRegisterConfig.java | 12 +++
.../core/register/ClientRegisterConfigImpl.java | 15 ++++
.../register/registrar/AbstractApiRegistrar.java | 13 +++-
.../registrar/MateDataApiRegistrarImpl.java | 10 +++
.../registrar/UriApiRegistrarImplImpl.java | 10 +++
.../register/registrar/RegistrarNamespaceTest.java | 89 ++++++++++++++++++++++
.../register/SpringMvcApiMetaRegister.java | 17 ++++-
.../register/SpringMvcApiMetaRegisterTest.java | 37 +++++++++
8 files changed, 199 insertions(+), 4 deletions(-)
diff --git
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientRegisterConfig.java
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientRegisterConfig.java
index f1b5e623d9..678158c76e 100644
---
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientRegisterConfig.java
+++
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientRegisterConfig.java
@@ -18,9 +18,21 @@
package org.apache.shenyu.client.core.register;
import org.apache.shenyu.common.enums.RpcTypeEnum;
+import org.apache.shenyu.common.constant.Constants;
+
+import java.util.Collections;
+import java.util.List;
public interface ClientRegisterConfig {
+ /**
+ * Gets the namespaces used for registration.
+ * @return namespace identifiers
+ */
+ default List<String> getNamespace() {
+ return Collections.singletonList(Constants.SYS_DEFAULT_NAMESPACE_ID);
+ }
+
/**
* Gets port.
* @return port
diff --git
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientRegisterConfigImpl.java
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientRegisterConfigImpl.java
index 6483e96860..1696e14ba6 100644
---
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientRegisterConfigImpl.java
+++
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientRegisterConfigImpl.java
@@ -17,6 +17,8 @@
package org.apache.shenyu.client.core.register;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
import org.apache.shenyu.client.core.utils.PortUtils;
import org.apache.shenyu.common.enums.RpcTypeEnum;
@@ -27,6 +29,9 @@ import org.springframework.context.ApplicationContext;
import org.springframework.core.env.Environment;
import java.util.Objects;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
import java.util.Optional;
import java.util.Properties;
@@ -34,6 +39,8 @@ public class ClientRegisterConfigImpl implements
ClientRegisterConfig {
private final Properties props;
+ private final List<String> namespace;
+
private Integer port;
private String host;
@@ -53,6 +60,9 @@ public class ClientRegisterConfigImpl implements
ClientRegisterConfig {
this.props =
shenyuClientConfig.getClient().get(rpcTypeEnum.getName()).getProps();
+ this.namespace =
Collections.unmodifiableList(Arrays.asList(StringUtils.split(
+ StringUtils.defaultIfBlank(shenyuClientConfig.getNamespace(),
Constants.SYS_DEFAULT_NAMESPACE_ID), Constants.SEPARATOR_CHARS)));
+
this.applicationContext = applicationContext;
this.rpcTypeEnum = rpcTypeEnum;
@@ -61,6 +71,11 @@ public class ClientRegisterConfigImpl implements
ClientRegisterConfig {
}
+ @Override
+ public List<String> getNamespace() {
+ return namespace;
+ }
+
@Override
public Integer getPort() {
diff --git
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/AbstractApiRegistrar.java
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/AbstractApiRegistrar.java
index e987049da8..23ff2f504c 100644
---
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/AbstractApiRegistrar.java
+++
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/AbstractApiRegistrar.java
@@ -22,6 +22,7 @@ import org.apache.shenyu.client.core.register.ApiBean;
import org.apache.shenyu.register.common.type.DataTypeParent;
import java.util.List;
+import java.util.Collections;
public abstract class AbstractApiRegistrar<D extends DataTypeParent>
implements ApiRegistrar {
@@ -51,6 +52,16 @@ public abstract class AbstractApiRegistrar<D extends
DataTypeParent> implements
throw new UnsupportedOperationException("If the preMatch method was
implemented ,the preParse method should be implemented.");
}
+ /**
+ * Parse all registrations for a bean while retaining the
single-registration extension point.
+ *
+ * @param apiBean bean to register
+ * @return registrations to publish
+ */
+ protected List<D> preParseList(final ApiBean apiBean) {
+ return Collections.singletonList(preParse(apiBean));
+ }
+
/**
* Determines whether apiDefinitions of apiBean can be filtered.
*
@@ -79,7 +90,7 @@ public abstract class AbstractApiRegistrar<D extends
DataTypeParent> implements
public void register(final ApiBean apiBean) {
if (preMatch(apiBean)) {
- publisher.publishEvent(preParse(apiBean));
+ preParseList(apiBean).forEach(publisher::publishEvent);
return;
}
diff --git
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/MateDataApiRegistrarImpl.java
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/MateDataApiRegistrarImpl.java
index e887d1a783..ac23a1436d 100644
---
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/MateDataApiRegistrarImpl.java
+++
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/MateDataApiRegistrarImpl.java
@@ -47,7 +47,12 @@ public class MateDataApiRegistrarImpl extends
BaseApiRegistrarImpl {
@Override
protected void doRegisterApi(final ApiBean.ApiDefinition api) {
+ clientRegisterConfig.getNamespace().forEach(namespaceId ->
registerApi(api, namespaceId));
+ }
+
+ private void registerApi(final ApiBean.ApiDefinition api, final String
namespaceId) {
final MetaDataRegisterDTO metaDataRegisterDTO =
MetaDataRegisterDTO.builder()
+ .namespaceId(namespaceId)
.contextPath(clientRegisterConfig.getContextPath())
.addPrefixed(clientRegisterConfig.getAddPrefixed())
.appName(clientRegisterConfig.getAppName())
@@ -69,7 +74,12 @@ public class MateDataApiRegistrarImpl extends
BaseApiRegistrarImpl {
@Override
protected void doRegisterBean(final ApiBean apiBean) {
+ clientRegisterConfig.getNamespace().forEach(namespaceId ->
registerBean(apiBean, namespaceId));
+ }
+
+ private void registerBean(final ApiBean apiBean, final String namespaceId)
{
final MetaDataRegisterDTO metaDataRegisterDTO =
MetaDataRegisterDTO.builder()
+ .namespaceId(namespaceId)
.contextPath(clientRegisterConfig.getContextPath())
.addPrefixed(clientRegisterConfig.getAddPrefixed())
.appName(clientRegisterConfig.getAppName())
diff --git
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/UriApiRegistrarImplImpl.java
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/UriApiRegistrarImplImpl.java
index c987428806..b785ee2bbf 100644
---
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/UriApiRegistrarImplImpl.java
+++
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/UriApiRegistrarImplImpl.java
@@ -41,7 +41,12 @@ public class UriApiRegistrarImplImpl extends
BaseApiRegistrarImpl {
@Override
protected void doRegisterApi(final ApiBean.ApiDefinition api) {
+ clientRegisterConfig.getNamespace().forEach(namespaceId ->
registerApi(api, namespaceId));
+ }
+
+ private void registerApi(final ApiBean.ApiDefinition api, final String
namespaceId) {
final URIRegisterDTO uriRegisterDTO = URIRegisterDTO.builder()
+ .namespaceId(namespaceId)
.contextPath(clientRegisterConfig.getContextPath())
.appName(clientRegisterConfig.getAppName())
.eventType(EventType.REGISTER)
@@ -54,7 +59,12 @@ public class UriApiRegistrarImplImpl extends
BaseApiRegistrarImpl {
@Override
protected void doRegisterBean(final ApiBean apiBean) {
+ clientRegisterConfig.getNamespace().forEach(namespaceId ->
registerBean(apiBean, namespaceId));
+ }
+
+ private void registerBean(final ApiBean apiBean, final String namespaceId)
{
final URIRegisterDTO uriRegisterDTO = URIRegisterDTO.builder()
+ .namespaceId(namespaceId)
.contextPath(clientRegisterConfig.getContextPath())
.appName(clientRegisterConfig.getAppName())
.eventType(EventType.REGISTER)
diff --git
a/shenyu-client/shenyu-client-core/src/test/java/org/apache/shenyu/client/core/register/registrar/RegistrarNamespaceTest.java
b/shenyu-client/shenyu-client-core/src/test/java/org/apache/shenyu/client/core/register/registrar/RegistrarNamespaceTest.java
new file mode 100644
index 0000000000..04191b727d
--- /dev/null
+++
b/shenyu-client/shenyu-client-core/src/test/java/org/apache/shenyu/client/core/register/registrar/RegistrarNamespaceTest.java
@@ -0,0 +1,89 @@
+/*
+ * 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.client.core.register.registrar;
+
+import
org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher;
+import org.apache.shenyu.client.core.register.ApiBean;
+import org.apache.shenyu.client.core.register.ClientRegisterConfig;
+import org.apache.shenyu.client.core.register.ClientRegisterConfigImpl;
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.enums.RpcTypeEnum;
+import org.apache.shenyu.register.common.config.ShenyuClientConfig;
+import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
+import org.apache.shenyu.register.common.dto.URIRegisterDTO;
+import org.apache.shenyu.register.common.type.DataTypeParent;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+import org.springframework.test.util.ReflectionTestUtils;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+class RegistrarNamespaceTest {
+
+ @Test
+ void testNamespaceConfiguration() {
+
assertEquals(Collections.singletonList(Constants.SYS_DEFAULT_NAMESPACE_ID),
config(null).getNamespace());
+
assertEquals(Collections.singletonList(Constants.SYS_DEFAULT_NAMESPACE_ID),
config(" ").getNamespace());
+ assertEquals(Arrays.asList("first", "second"), config("first" +
Constants.SEPARATOR_CHARS + "second").getNamespace());
+
assertEquals(Collections.singletonList(Constants.SYS_DEFAULT_NAMESPACE_ID), new
TestClientRegisterConfig().getNamespace());
+ }
+
+ @Test
+ void testUriAndMetadataUseIndependentNamespaceEvents() throws Exception {
+ ClientRegisterConfig config = config("first" +
Constants.SEPARATOR_CHARS + "second");
+ for (BaseApiRegistrarImpl registrar : Arrays.asList(new
UriApiRegistrarImplImpl(config), new MateDataApiRegistrarImpl(config))) {
+ ShenyuClientRegisterEventPublisher publisher =
mock(ShenyuClientRegisterEventPublisher.class);
+ ReflectionTestUtils.setField(registrar, "publisher", publisher);
+ ApiBean bean = new ApiBean(RpcTypeEnum.HTTP.getName(), "service",
this, "/service");
+ bean.addApiDefinition(Object.class.getMethod("toString"),
"/method");
+ registrar.doRegisterBean(bean);
+ registrar.doRegisterApi(bean.getApiDefinitions().get(0));
+ ArgumentCaptor<DataTypeParent> captor =
ArgumentCaptor.forClass(DataTypeParent.class);
+ verify(publisher, times(4)).publishEvent(captor.capture());
+ List<DataTypeParent> events = captor.getAllValues();
+ for (int index = 0; index < events.size(); index += 2) {
+ assertNotSame(events.get(index), events.get(index + 1));
+ assertEquals("first", namespace(events.get(index)));
+ assertEquals("second", namespace(events.get(index + 1)));
+ }
+ }
+ }
+
+ private ClientRegisterConfig config(final String namespace) {
+ ShenyuClientConfig config = new ShenyuClientConfig();
+ config.setNamespace(namespace);
+ ShenyuClientConfig.ClientPropertiesConfig properties = new
ShenyuClientConfig.ClientPropertiesConfig();
+ properties.getProps().setProperty("port", "8080");
+ properties.getProps().setProperty("host", "127.0.0.1");
+ properties.getProps().setProperty("appName", "service");
+ config.getClient().put(RpcTypeEnum.HTTP.getName(), properties);
+ return new ClientRegisterConfigImpl(config, RpcTypeEnum.HTTP, null,
null);
+ }
+
+ private String namespace(final DataTypeParent event) {
+ return event instanceof URIRegisterDTO ? ((URIRegisterDTO)
event).getNamespaceId() : ((MetaDataRegisterDTO) event).getNamespaceId();
+ }
+}
diff --git
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegister.java
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegister.java
index 2fbf8581c9..ec88f6e5be 100644
---
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegister.java
+++
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegister.java
@@ -17,7 +17,6 @@
package org.apache.shenyu.client.springmvc.register;
-import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import
org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher;
import org.apache.shenyu.client.core.register.ApiBean;
@@ -33,6 +32,7 @@ import org.springframework.core.annotation.AnnotationUtils;
import java.util.List;
import java.util.Objects;
+import java.util.stream.Collectors;
public class SpringMvcApiMetaRegister extends AbstractApiMetaRegistrar {
@@ -69,13 +69,23 @@ public class SpringMvcApiMetaRegister extends
AbstractApiMetaRegistrar {
return Objects.nonNull(annotation) &&
annotation.path()[0].endsWith("/**");
}
+ @Override
+ protected List<MetaDataRegisterDTO> preParseList(final ApiBean apiBean) {
+ return clientRegisterConfig.getNamespace().stream().map(namespaceId ->
preParse(apiBean, namespaceId)).collect(Collectors.toList());
+ }
+
@Override
protected MetaDataRegisterDTO preParse(final ApiBean apiBean) {
+ return preParse(apiBean, clientRegisterConfig.getNamespace().get(0));
+ }
+
+ private MetaDataRegisterDTO preParse(final ApiBean apiBean, final String
namespaceId) {
ShenyuSpringMvcClient annotation =
apiBean.getAnnotation(ShenyuSpringMvcClient.class);
String apiPath =
PathUtils.pathJoin(clientRegisterConfig.getContextPath(), annotation.path()[0]);
return MetaDataRegisterDTO.builder()
+ .namespaceId(namespaceId)
.contextPath(clientRegisterConfig.getContextPath())
.addPrefixed(addPrefixed)
.appName(appName)
@@ -137,7 +147,8 @@ public class SpringMvcApiMetaRegister extends
AbstractApiMetaRegistrar {
boolean registerMetaData = (Objects.isNull(classAnnotation) ||
classAnnotation.registerMetaData())
&& (Objects.isNull(methodAnnotation) ||
methodAnnotation.registerMetaData());
- return Lists.newArrayList(MetaDataRegisterDTO.builder()
+ return clientRegisterConfig.getNamespace().stream().map(namespaceId ->
MetaDataRegisterDTO.builder()
+ .namespaceId(namespaceId)
.contextPath(clientRegisterConfig.getContextPath())
.addPrefixed(addPrefixed)
.appName(appName)
@@ -152,6 +163,6 @@ public class SpringMvcApiMetaRegister extends
AbstractApiMetaRegistrar {
.enabled(enabled)
.ruleName(ruleName)
.registerMetaData(registerMetaData)
- .build());
+ .build()).collect(Collectors.toList());
}
}
diff --git
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegisterTest.java
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegisterTest.java
index ec8a7d5b3c..770564343a 100644
---
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegisterTest.java
+++
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/test/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiMetaRegisterTest.java
@@ -20,6 +20,9 @@ package org.apache.shenyu.client.springmvc.register;
import
org.apache.shenyu.client.core.disruptor.ShenyuClientRegisterEventPublisher;
import org.apache.shenyu.client.core.register.ApiBean;
import org.apache.shenyu.client.core.register.ClientRegisterConfig;
+import org.apache.shenyu.client.core.register.ClientRegisterConfigImpl;
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.register.common.config.ShenyuClientConfig;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.apache.shenyu.common.enums.RpcTypeEnum;
import org.apache.shenyu.register.client.api.ShenyuClientRegisterRepository;
@@ -27,11 +30,17 @@ import
org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
import org.apache.shenyu.register.common.type.DataTypeParent;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.NullSource;
+import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.stream.Collectors;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
@@ -129,6 +138,31 @@ public class SpringMvcApiMetaRegisterTest {
objectHashMap.put(beanClass.getName(),
beanClass.getDeclaredConstructor().newInstance());
return beansExtractor.extract(null, objectHashMap).get(0);
}
+
+ @ParameterizedTest
+ @NullSource
+ @ValueSource(strings = {"first;second", "", " "})
+ void publishesEachNamespaceForMethodsAndWildcardBeans(final String
namespace) throws Exception {
+ final ShenyuClientConfig config = new ShenyuClientConfig();
+ config.setNamespace(namespace);
+ ShenyuClientConfig.ClientPropertiesConfig properties = new
ShenyuClientConfig.ClientPropertiesConfig();
+ properties.getProps().setProperty("port", "8080");
+ properties.getProps().setProperty("host", "127.0.0.1");
+ properties.getProps().setProperty("appName", "test");
+ properties.getProps().setProperty("contextPath", "/testContext");
+ config.getClient().put(RpcTypeEnum.HTTP.getName(), properties);
+ for (Class<?> beanClass : List.of(TestPreApiBean.class,
TestApiBeanAnnotatedMethodAndClass.class)) {
+ TestShenyuClientRegisterEventPublisher publisher = new
TestShenyuClientRegisterEventPublisher();
+ SpringMvcApiMetaRegister registrar = new
SpringMvcApiMetaRegister(publisher, new ClientRegisterConfigImpl(config,
RpcTypeEnum.HTTP, null, null));
+ registrar.register(createSimpleApiBean(beanClass));
+ List<String> expected = "first;second".equals(namespace) ?
List.of("first", "second") : List.of(Constants.SYS_DEFAULT_NAMESPACE_ID);
+
assertThat(publisher.events.stream().map(MetaDataRegisterDTO::getNamespaceId).collect(Collectors.toList()),
equalTo(expected));
+ String path = beanClass == TestPreApiBean.class ?
"/testContext/testClass/**" : "/testContext/testClass/testMethod";
+ for (MetaDataRegisterDTO event : publisher.events) {
+ assertThat(event.getPath(), equalTo(path));
+ }
+ }
+ }
@ShenyuSpringMvcClient
@RestController
@@ -200,6 +234,8 @@ public class SpringMvcApiMetaRegisterTest {
static class TestShenyuClientRegisterEventPublisher extends
ShenyuClientRegisterEventPublisher {
private MetaDataRegisterDTO metaData;
+
+ private final List<MetaDataRegisterDTO> events = new ArrayList<>();
@Override
public void start(final ShenyuClientRegisterRepository
shenyuClientRegisterRepository) {
@@ -208,6 +244,7 @@ public class SpringMvcApiMetaRegisterTest {
@Override
public void publishEvent(final DataTypeParent data) {
this.metaData = (MetaDataRegisterDTO) data;
+ events.add(metaData);
}
}