getLocales() {
- return null;
- }
-
- @Override
- public boolean isSecure() {
- return false;
- }
-
- @Override
- public RequestDispatcher getRequestDispatcher(String s) {
- return null;
- }
-
- @Override
- public String getRealPath(String s) {
- return null;
- }
-
- @Override
- public int getRemotePort() {
- return 0;
- }
-
- @Override
- public String getLocalName() {
- return null;
- }
-
- @Override
- public String getLocalAddr() {
- return null;
- }
-
- @Override
- public int getLocalPort() {
- return 0;
- }
-
- @Override
- public ServletContext getServletContext() {
- return null;
- }
-
- @Override
- public AsyncContext startAsync() throws IllegalStateException {
- return null;
- }
-
- @Override
- public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) throws IllegalStateException {
- return null;
- }
-
- @Override
- public boolean isAsyncStarted() {
- return false;
- }
-
- @Override
- public boolean isAsyncSupported() {
- return false;
- }
-
- @Override
- public AsyncContext getAsyncContext() {
- return null;
- }
-
- @Override
- public DispatcherType getDispatcherType() {
- return null;
- }
-}
diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/NamingConfig.java b/naming/src/main/java/com/alibaba/nacos/naming/web/NamingConfig.java
index 858558711..f037a953c 100644
--- a/naming/src/main/java/com/alibaba/nacos/naming/web/NamingConfig.java
+++ b/naming/src/main/java/com/alibaba/nacos/naming/web/NamingConfig.java
@@ -51,6 +51,18 @@ public class NamingConfig {
return registration;
}
+ @Bean
+ public FilterRegistrationBean tenantFilterRegistration() {
+ FilterRegistrationBean registration = new FilterRegistrationBean();
+
+ registration.setFilter(tenantFilter());
+ registration.addUrlPatterns("/v1/ns/instance/*", "/v1/ns/service/*", "/v1/ns/cluster/*", "/v1/ns/health/*");
+ registration.setName("tenantFilter");
+ registration.setOrder(4);
+
+ return registration;
+ }
+
@Bean
public Filter distroFilter() {
return new DistroFilter();
@@ -60,4 +72,9 @@ public class NamingConfig {
public Filter authFilter() {
return new AuthFilter();
}
+
+ @Bean
+ public Filter tenantFilter() {
+ return new TenantFilter();
+ }
}
diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/OverrideParameterRequestWrapper.java b/naming/src/main/java/com/alibaba/nacos/naming/web/OverrideParameterRequestWrapper.java
new file mode 100644
index 000000000..755a45472
--- /dev/null
+++ b/naming/src/main/java/com/alibaba/nacos/naming/web/OverrideParameterRequestWrapper.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed 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 com.alibaba.nacos.naming.web;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * A request wrapper to override the parameters.
+ *
+ * Referenced article is https://blog.csdn.net/xieyuooo/article/details/8447301
+ *
+ * @author nkorange
+ * @since 0.8.0
+ */
+public class OverrideParameterRequestWrapper extends HttpServletRequestWrapper {
+
+ private Map params = new HashMap();
+
+ /**
+ * Constructs a request object wrapping the given request.
+ *
+ * @param request The request to wrap
+ * @throws IllegalArgumentException if the request is null
+ */
+ public OverrideParameterRequestWrapper(HttpServletRequest request) {
+ super(request);
+ this.params.putAll(request.getParameterMap());
+ }
+
+ public static OverrideParameterRequestWrapper buildRequest(HttpServletRequest request) {
+ return new OverrideParameterRequestWrapper(request);
+ }
+
+ public static OverrideParameterRequestWrapper buildRequest(HttpServletRequest request, String name, String value) {
+ OverrideParameterRequestWrapper requestWrapper = new OverrideParameterRequestWrapper(request);
+ requestWrapper.addParameter(name, value);
+ return requestWrapper;
+ }
+
+ public static OverrideParameterRequestWrapper buildRequest(HttpServletRequest request, Map appendParameters) {
+ OverrideParameterRequestWrapper requestWrapper = new OverrideParameterRequestWrapper(request);
+ requestWrapper.params.putAll(appendParameters);
+ return requestWrapper;
+ }
+
+ @Override
+ public String getParameter(String name) {
+ String[] values = params.get(name);
+ if (values == null || values.length == 0) {
+ return null;
+ }
+ return values[0];
+ }
+
+ @Override
+ public Map getParameterMap() {
+ return params;
+ }
+
+ public String[] getParameterValues(String name) {
+ return params.get(name);
+ }
+
+ public void addParameter(String name, String value) {
+ if (value != null) {
+ params.put(name, new String[]{value});
+ }
+ }
+
+}
diff --git a/naming/src/main/java/com/alibaba/nacos/naming/web/TenantFilter.java b/naming/src/main/java/com/alibaba/nacos/naming/web/TenantFilter.java
new file mode 100644
index 000000000..254bdc4c9
--- /dev/null
+++ b/naming/src/main/java/com/alibaba/nacos/naming/web/TenantFilter.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed 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 com.alibaba.nacos.naming.web;
+
+import com.alibaba.nacos.api.common.Constants;
+import com.alibaba.nacos.core.utils.WebUtils;
+import com.alibaba.nacos.naming.misc.UtilsAndCommons;
+import org.apache.commons.lang3.StringUtils;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+/**
+ * A filter to intercept tenant parameter.
+ *
+ * @author nkorange
+ * @since 0.8.0
+ */
+public class TenantFilter implements Filter {
+
+ @Override
+ public void init(FilterConfig filterConfig) throws ServletException {
+
+ }
+
+ @Override
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+
+ HttpServletRequest req = (HttpServletRequest) request;
+ HttpServletResponse resp = (HttpServletResponse) response;
+
+ String tenantId = WebUtils.optional(req, Constants.REQUEST_PARAM_TENANT_ID, StringUtils.EMPTY);
+ String serviceName = WebUtils.optional(req, Constants.REQUEST_PARAM_SERVICE_NAME, StringUtils.EMPTY);
+
+ if (StringUtils.isBlank(tenantId) || StringUtils.isBlank(serviceName)) {
+ chain.doFilter(req, resp);
+ return;
+ }
+
+ OverrideParameterRequestWrapper requestWrapper = new OverrideParameterRequestWrapper(req);
+ requestWrapper.addParameter(Constants.REQUEST_PARAM_SERVICE_NAME,
+ serviceName + UtilsAndCommons.SERVICE_TENANT_CONNECTOR + tenantId);
+
+
+ chain.doFilter(requestWrapper, resp);
+ }
+
+ @Override
+ public void destroy() {
+
+ }
+}
diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java
index 4db794aa3..38f9d5b76 100644
--- a/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java
+++ b/test/src/test/java/com/alibaba/nacos/test/naming/AutoDeregisterInstance_ITCase.java
@@ -163,7 +163,7 @@ public class AutoDeregisterInstance_ITCase {
Assert.assertEquals(instances.size(), 1);
BeatInfo beatInfo = new BeatInfo();
- beatInfo.setDom(serviceName);
+ beatInfo.setServiceName(serviceName);
beatInfo.setIp("127.0.0.1");
beatInfo.setPort(TEST_PORT);
@@ -207,7 +207,7 @@ public class AutoDeregisterInstance_ITCase {
Assert.assertEquals(instances.size(), 1);
BeatInfo beatInfo = new BeatInfo();
- beatInfo.setDom(serviceName);
+ beatInfo.setServiceName(serviceName);
beatInfo.setIp("127.0.0.1");
beatInfo.setPort(TEST_PORT);
beatInfo.setCluster("c1");
diff --git a/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java b/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java
index 8512b5294..201245181 100644
--- a/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java
+++ b/test/src/test/java/com/alibaba/nacos/test/naming/RegisterInstance_ITCase.java
@@ -15,6 +15,8 @@
*/
package com.alibaba.nacos.test.naming;
+import com.alibaba.nacos.api.NacosFactory;
+import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
@@ -29,10 +31,7 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.test.context.junit4.SpringRunner;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
import java.util.concurrent.TimeUnit;
import static com.alibaba.nacos.test.naming.NamingBase.*;
@@ -64,7 +63,14 @@ public class RegisterInstance_ITCase {
@Test
@Ignore
public void regService() throws NacosException, InterruptedException {
- String serviceName = "dungu.test.99";
+
+ Properties properties = new Properties();
+ properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
+ properties.put(PropertyKeyConst.NAMESPACE, "t3");
+
+ naming = NacosFactory.createNamingService(properties);
+
+ String serviceName = "dungu.test.8";
naming.registerInstance(serviceName, "127.0.0.1", 80, "c1");
naming.registerInstance(serviceName, "127.0.0.2", 80, "c2");
Thread.sleep(100000000L);