-The order of filters can be changed in property.
+The order of filters can be changed via the constructor.
The invocation chain resembles this:
```bash
-EntranceNode: machine-root(t:3 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
--EntranceNode: coke(t:2 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
---coke(t:2 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
----/coke/coke(t:0 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
--EntranceNode: sentinel_default_context(t:0 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
--EntranceNode: book(t:1 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
---book(t:1 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
----/book/coke(t:0 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
+-EntranceNode: sentinel_gateway_context$$route$$another-route-b(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:8 1mb:1 1mt:9)
+--another-route-b(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:4 1mb:1 1mt:5)
+--another_customized_api(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:4 1mb:0 1mt:4)
+-EntranceNode: sentinel_gateway_context$$route$$my-route-1(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:6 1mb:0 1mt:6)
+--my-route-1(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:2 1mb:0 1mt:2)
+--some_customized_api(t:0 pq:0.0 bq:0.0 tq:0.0 rt:0.0 prq:0.0 1mp:2 1mb:0 1mt:2)
```
-- `book` and `coke` are serviceId.
-- `/book/coke` is api path, the real API path is `/coke`.
-
## Integration with Sentinel Dashboard
1. Start [Sentinel Dashboard](https://github.com/alibaba/Sentinel/wiki/Dashboard).
@@ -76,7 +91,7 @@ EntranceNode: machine-root(t:3 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
You can implement `SentinelFallbackProvider` to define your own fallback provider when Sentinel `BlockException` is thrown.
The default fallback provider is `DefaultBlockFallbackProvider`.
-By default fallback route is `ServiveId + URI PATH`, example `/book/coke`, first `book` is serviceId, `/coke` is URI PATH, so that both can be needed.
+By default fallback route is proxy ID (or customized API name).
Here is an example:
@@ -90,7 +105,7 @@ public class MyBlockFallbackProvider implements ZuulBlockFallbackProvider {
// you can define root as service level
@Override
public String getRoute() {
- return "/coke/coke";
+ return "my-route";
}
@Override
diff --git a/sentinel-adapter/sentinel-zuul-adapter/pom.xml b/sentinel-adapter/sentinel-zuul-adapter/pom.xml
index 41228d52..68f67b8d 100755
--- a/sentinel-adapter/sentinel-zuul-adapter/pom.xml
+++ b/sentinel-adapter/sentinel-zuul-adapter/pom.xml
@@ -21,6 +21,11 @@
Process the url. Some path variables should be handled and unified.
- *e.g. collect_item_relation--10200012121-.html will be converted to collect_item_relation.html
- * - * @param originUrl original url - * @return processed url - */ - String clean(String originUrl); -} diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/AbstractSentinelFilter.java b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/AbstractSentinelFilter.java deleted file mode 100644 index 0becdf53..00000000 --- a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/AbstractSentinelFilter.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.csp.sentinel.adapter.zuul.filters; - -import com.alibaba.csp.sentinel.adapter.zuul.properties.SentinelZuulProperties; -import com.alibaba.csp.sentinel.util.AssertUtil; -import com.netflix.zuul.ZuulFilter; - -/** - * Abstract class for sentinel filters. - * - * @author tiger - */ -public abstract class AbstractSentinelFilter extends ZuulFilter { - - private final SentinelZuulProperties sentinelZuulProperties; - - public SentinelZuulProperties getSentinelZuulProperties() { - return sentinelZuulProperties; - } - - public AbstractSentinelFilter(SentinelZuulProperties sentinelZuulProperties) { - AssertUtil.notNull(sentinelZuulProperties,"SentinelZuulProperties can not be null"); - this.sentinelZuulProperties = sentinelZuulProperties; - } - - @Override - public boolean shouldFilter() { - return sentinelZuulProperties.isEnabled(); - } - -} diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelErrorFilter.java b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelErrorFilter.java deleted file mode 100644 index dc192dba..00000000 --- a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelErrorFilter.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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.csp.sentinel.adapter.zuul.filters; - -import com.alibaba.csp.sentinel.Tracer; -import com.alibaba.csp.sentinel.adapter.zuul.properties.SentinelZuulProperties; -import com.alibaba.csp.sentinel.context.ContextUtil; -import com.alibaba.csp.sentinel.log.RecordLog; -import com.alibaba.csp.sentinel.slots.block.BlockException; -import com.netflix.zuul.context.RequestContext; -import com.netflix.zuul.exception.ZuulException; - -import static com.alibaba.csp.sentinel.adapter.zuul.constants.ZuulConstant.ERROR_TYPE; - -/** - * This filter track routing exception and exit entry; - * - * @author tiger - */ -public class SentinelErrorFilter extends AbstractSentinelFilter { - - public SentinelErrorFilter(SentinelZuulProperties sentinelZuulProperties) { - super(sentinelZuulProperties); - } - - @Override - public String filterType() { - return ERROR_TYPE; - } - - @Override - public boolean shouldFilter() { - RequestContext ctx = RequestContext.getCurrentContext(); - return getSentinelZuulProperties().isEnabled() && ctx.getThrowable() != null; - } - - @Override - public int filterOrder() { - return getSentinelZuulProperties().getOrder().getError(); - } - - /** - * Trace not {@link BlockException} ex. - * While loop will exit all entries, - * Case serviceId and URL entry twice in {@link SentinelPreFilter}. - * The ContextUtil.getContext().getCurEntry() will exit from inner to outer. - */ - @Override - public Object run() throws ZuulException { - try { - RequestContext ctx = RequestContext.getCurrentContext(); - Throwable throwable = ctx.getThrowable(); - if (throwable != null) { - if (!BlockException.isBlockException(throwable)) { - Tracer.trace(throwable.getCause()); - RecordLog.info("[Sentinel Error Filter] Trace cause", throwable.getCause()); - } - } - } finally { - while (ContextUtil.getContext() != null && ContextUtil.getContext().getCurEntry() != null) { - ContextUtil.getContext().getCurEntry().exit(); - } - ContextUtil.exit(); - } - return null; - } -} diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPostFilter.java b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPostFilter.java deleted file mode 100644 index 9159489c..00000000 --- a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPostFilter.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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.csp.sentinel.adapter.zuul.filters; - -import com.alibaba.csp.sentinel.adapter.zuul.properties.SentinelZuulProperties; -import com.alibaba.csp.sentinel.context.ContextUtil; -import com.alibaba.csp.sentinel.log.RecordLog; -import com.netflix.zuul.exception.ZuulException; - -import static com.alibaba.csp.sentinel.adapter.zuul.constants.ZuulConstant.POST_TYPE; - -/** - * This filter do success routing RT statistic and exit {@link com.alibaba.csp.sentinel.Entry} - * - * @author tiger - */ -public class SentinelPostFilter extends AbstractSentinelFilter { - - public SentinelPostFilter(SentinelZuulProperties sentinelZuulProperties) { - super(sentinelZuulProperties); - } - - @Override - public String filterType() { - return POST_TYPE; - } - - @Override - public int filterOrder() { - return getSentinelZuulProperties().getOrder().getPost(); - } - - /** - * While loop will exit all entries, - * Case serviceId and URL entry twice in {@link SentinelPreFilter}. - * The ContextUtil.getContext().getCurEntry() will exit from inner to outer. - */ - @Override - public Object run() throws ZuulException { - while (ContextUtil.getContext() != null && ContextUtil.getContext().getCurEntry() != null) { - ContextUtil.getContext().getCurEntry().exit(); - } - ContextUtil.exit(); - return null; - } -} diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPreFilter.java b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPreFilter.java deleted file mode 100644 index 7ef2c787..00000000 --- a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPreFilter.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * 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.csp.sentinel.adapter.zuul.filters; - -import com.alibaba.csp.sentinel.EntryType; -import com.alibaba.csp.sentinel.SphU; -import com.alibaba.csp.sentinel.adapter.zuul.fallback.*; -import com.alibaba.csp.sentinel.adapter.zuul.properties.SentinelZuulProperties; -import com.alibaba.csp.sentinel.adapter.zuul.util.FilterUtil; -import com.alibaba.csp.sentinel.context.ContextUtil; -import com.alibaba.csp.sentinel.log.RecordLog; -import com.alibaba.csp.sentinel.slots.block.BlockException; -import com.alibaba.csp.sentinel.util.AssertUtil; -import com.alibaba.csp.sentinel.util.StringUtil; -import com.netflix.zuul.context.RequestContext; -import com.netflix.zuul.exception.ZuulException; - -import javax.servlet.http.HttpServletRequest; - -import static com.alibaba.csp.sentinel.adapter.zuul.constants.ZuulConstant.PRE_TYPE; -import static com.alibaba.csp.sentinel.adapter.zuul.constants.ZuulConstant.SERVICE_ID_KEY; -import static com.alibaba.csp.sentinel.adapter.zuul.constants.ZuulConstant.ZUUL_DEFAULT_CONTEXT; - -/** - * This pre filter get an entry of resource,the first order is ServiceId, then API Path. - * When get a BlockException run fallback logic. - * - * @author tiger - */ -public class SentinelPreFilter extends AbstractSentinelFilter { - - private final UrlCleaner urlCleaner; - - private final RequestOriginParser requestOriginParser; - - public SentinelPreFilter(SentinelZuulProperties sentinelZuulProperties, - UrlCleaner urlCleaner, - RequestOriginParser requestOriginParser) { - super(sentinelZuulProperties); - AssertUtil.notNull(urlCleaner, "UrlCleaner can not be null"); - AssertUtil.notNull(requestOriginParser, "RequestOriginParser can not be null"); - this.urlCleaner = urlCleaner; - this.requestOriginParser = requestOriginParser; - } - - @Override - public String filterType() { - return PRE_TYPE; - } - - /** - * This run before route filter so we can get more accurate RT time. - */ - @Override - public int filterOrder() { - return getSentinelZuulProperties().getOrder().getPre(); - } - - @Override - public Object run() throws ZuulException { - RequestContext ctx = RequestContext.getCurrentContext(); - String origin = parseOrigin(ctx.getRequest()); - String serviceTarget = (String) ctx.get(SERVICE_ID_KEY); - // When serviceId blocked first get the service level fallback provider. - String fallBackRoute = serviceTarget; - try { - if (StringUtil.isNotEmpty(serviceTarget)) { - RecordLog.info(String.format("[Sentinel Pre Filter] Origin: %s enter ServiceId: %s", origin, serviceTarget)); - ContextUtil.enter(serviceTarget, origin); - SphU.entry(serviceTarget, EntryType.IN); - } else { - RecordLog.info("[Sentinel Pre Filter] ServiceId is empty"); - ContextUtil.enter(ZUUL_DEFAULT_CONTEXT, origin); - } - String uriTarget = FilterUtil.filterTarget(ctx.getRequest()); - // Clean and unify the URL. - // For REST APIs, you have to clean the URL (e.g. `/foo/1` and `/foo/2` -> `/foo/:id`), or - // the amount of context and resources will exceed the threshold. - uriTarget = urlCleaner.clean(uriTarget); - fallBackRoute = uriTarget; - RecordLog.info(String.format("[Sentinel Pre Filter] Origin: %s enter Uri Path: %s", origin, uriTarget)); - SphU.entry(uriTarget, EntryType.IN); - } catch (BlockException ex) { - RecordLog.warn(String.format("[Sentinel Pre Filter] Block Exception when Origin: %s enter fall back route: %s", origin, fallBackRoute), ex); - ZuulBlockFallbackProvider zuulBlockFallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(fallBackRoute); - BlockResponse blockResponse = zuulBlockFallbackProvider.fallbackResponse(fallBackRoute, ex); - // prevent routing from running - ctx.setRouteHost(null); - ctx.set(SERVICE_ID_KEY, null); - ctx.setResponseBody(blockResponse.toString()); - } - return null; - } - - private String parseOrigin(HttpServletRequest request) { - return requestOriginParser.parseOrigin(request); - } -} diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/properties/SentinelZuulProperties.java b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/properties/SentinelZuulProperties.java deleted file mode 100644 index 8e7681ed..00000000 --- a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/properties/SentinelZuulProperties.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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.csp.sentinel.adapter.zuul.properties; - -import static com.alibaba.csp.sentinel.adapter.zuul.constants.ZuulConstant.SEND_RESPONSE_FILTER_ORDER; - -/** - * Sentinel Spring Cloud Zuul AutoConfiguration property. - * - * @author tiger - */ -public class SentinelZuulProperties { - - private boolean enabled = false; - - private Order order = new Order(); - - public static class Order { - - private int post = SEND_RESPONSE_FILTER_ORDER - 10; - - private int pre = 10000; - - private int error = -1; - - public int getPost() { - return post; - } - - public void setPost(int post) { - this.post = post; - } - - public int getPre() { - return pre; - } - - public void setPre(int pre) { - this.pre = pre; - } - - public int getError() { - return error; - } - - public void setError(int error) { - this.error = error; - } - } - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public Order getOrder() { - return order; - } - - public void setOrder(Order order) { - this.order = order; - } -} diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/util/FilterUtil.java b/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/util/FilterUtil.java deleted file mode 100755 index cc00ccee..00000000 --- a/sentinel-adapter/sentinel-zuul-adapter/src/main/java/com/alibaba/csp/sentinel/adapter/zuul/util/FilterUtil.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * 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.csp.sentinel.adapter.zuul.util; - -import com.alibaba.csp.sentinel.util.StringUtil; - -import javax.servlet.http.HttpServletRequest; - -/** - * Util class for web servlet filter. - * This is same as servlet adapter util class - * - * @author tiger - */ -public final class FilterUtil { - - public static String filterTarget(HttpServletRequest request) { - String pathInfo = getResourcePath(request); - if (!pathInfo.startsWith("/")) { - pathInfo = "/" + pathInfo; - } - - if ("/".equals(pathInfo)) { - return pathInfo; - } - - // Note: pathInfo should be converted to camelCase style. - int lastSlashIndex = pathInfo.lastIndexOf("/"); - - if (lastSlashIndex >= 0) { - pathInfo = pathInfo.substring(0, lastSlashIndex) + "/" - + StringUtil.trim(pathInfo.substring(lastSlashIndex + 1)); - } else { - pathInfo = "/" + StringUtil.trim(pathInfo); - } - - return pathInfo; - } - - private static String getResourcePath(HttpServletRequest request) { - String pathInfo = normalizeAbsolutePath(request.getPathInfo(), false); - String servletPath = normalizeAbsolutePath(request.getServletPath(), pathInfo.length() != 0); - - return servletPath + pathInfo; - } - - private static String normalizeAbsolutePath(String path, boolean removeTrailingSlash) throws IllegalStateException { - return normalizePath(path, true, false, removeTrailingSlash); - } - - private static String normalizePath(String path, boolean forceAbsolute, boolean forceRelative, - boolean removeTrailingSlash) throws IllegalStateException { - char[] pathChars = StringUtil.trimToEmpty(path).toCharArray(); - int length = pathChars.length; - - // Check path and slash. - boolean startsWithSlash = false; - boolean endsWithSlash = false; - - if (length > 0) { - char firstChar = pathChars[0]; - char lastChar = pathChars[length - 1]; - - startsWithSlash = firstChar == '/' || firstChar == '\\'; - endsWithSlash = lastChar == '/' || lastChar == '\\'; - } - - StringBuilder buf = new StringBuilder(length); - boolean isAbsolutePath = forceAbsolute || !forceRelative && startsWithSlash; - int index = startsWithSlash ? 0 : -1; - int level = 0; - - if (isAbsolutePath) { - buf.append("/"); - } - - while (index < length) { - index = indexOfSlash(pathChars, index + 1, false); - - if (index == length) { - break; - } - - int nextSlashIndex = indexOfSlash(pathChars, index, true); - - String element = new String(pathChars, index, nextSlashIndex - index); - index = nextSlashIndex; - - // Ignore "." - if (".".equals(element)) { - continue; - } - - // Backtrack ".." - if ("..".equals(element)) { - if (level == 0) { - if (isAbsolutePath) { - throw new IllegalStateException(path); - } else { - buf.append("../"); - } - } else { - buf.setLength(pathChars[--level]); - } - - continue; - } - - pathChars[level++] = (char)buf.length(); - buf.append(element).append('/'); - } - - // remove the last "/" - if (buf.length() > 0) { - if (!endsWithSlash || removeTrailingSlash) { - buf.setLength(buf.length() - 1); - } - } - - return buf.toString(); - } - - private static int indexOfSlash(char[] chars, int beginIndex, boolean slash) { - int i = beginIndex; - - for (; i < chars.length; i++) { - char ch = chars[i]; - - if (slash) { - if (ch == '/' || ch == '\\') { - break; // if a slash - } - } else { - if (ch != '/' && ch != '\\') { - break; // if not a slash - } - } - } - - return i; - } - - private FilterUtil() {} -} diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinitionChangeObserver b/sentinel-adapter/sentinel-zuul-adapter/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinitionChangeObserver new file mode 100644 index 00000000..a924cf5b --- /dev/null +++ b/sentinel-adapter/sentinel-zuul-adapter/src/main/resources/META-INF/services/com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinitionChangeObserver @@ -0,0 +1 @@ +com.alibaba.csp.sentinel.adapter.gateway.zuul.api.ZuulApiDefinitionChangeObserver \ No newline at end of file diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/fallback/ZuulBlockFallbackManagerTest.java b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/fallback/ZuulBlockFallbackManagerTest.java similarity index 94% rename from sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/fallback/ZuulBlockFallbackManagerTest.java rename to sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/fallback/ZuulBlockFallbackManagerTest.java index 81425be2..f3c0b3e2 100644 --- a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/fallback/ZuulBlockFallbackManagerTest.java +++ b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/fallback/ZuulBlockFallbackManagerTest.java @@ -14,13 +14,13 @@ * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.zuul.fallback; +package com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback; import com.alibaba.csp.sentinel.slots.block.flow.FlowException; + import org.junit.Assert; import org.junit.Test; - /** * @author tiger */ @@ -56,7 +56,7 @@ public class ZuulBlockFallbackManagerTest { ZuulBlockFallbackManager.registerProvider(myNullResponseFallBackProvider); Assert.assertEquals(myNullResponseFallBackProvider.getRoute(), ROUTE); ZuulBlockFallbackManager.clear(); - Assert.assertEquals(ZuulBlockFallbackManager.getFallbackProvider(ROUTE).getRoute(),DEFAULT_ROUTE); + Assert.assertEquals(ZuulBlockFallbackManager.getFallbackProvider(ROUTE).getRoute(), DEFAULT_ROUTE); } } \ No newline at end of file diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/fallback/ZuulBlockFallbackProviderTest.java b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/fallback/ZuulBlockFallbackProviderTest.java similarity index 94% rename from sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/fallback/ZuulBlockFallbackProviderTest.java rename to sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/fallback/ZuulBlockFallbackProviderTest.java index f701d4a9..f7ec49bc 100644 --- a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/fallback/ZuulBlockFallbackProviderTest.java +++ b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/fallback/ZuulBlockFallbackProviderTest.java @@ -14,9 +14,10 @@ * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.zuul.fallback; +package com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback; import com.alibaba.csp.sentinel.slots.block.flow.FlowException; + import org.junit.Assert; import org.junit.Test; @@ -48,7 +49,8 @@ public class ZuulBlockFallbackProviderTest { @Test public void testFlowControlFallbackResponse() throws Exception { ZuulBlockFallbackProvider fallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(ALL_ROUTE); - BlockResponse clientHttpResponse = fallbackProvider.fallbackResponse(ALL_ROUTE, new FlowException("flow exception")); + BlockResponse clientHttpResponse = fallbackProvider.fallbackResponse(ALL_ROUTE, + new FlowException("flow exception")); Assert.assertEquals(clientHttpResponse.getCode(), 429); } diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelErrorFilterTest.java b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/filters/SentinelZuulErrorFilterTest.java similarity index 51% rename from sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelErrorFilterTest.java rename to sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/filters/SentinelZuulErrorFilterTest.java index 8f2bca89..4c9096d5 100644 --- a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelErrorFilterTest.java +++ b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/filters/SentinelZuulErrorFilterTest.java @@ -14,42 +14,37 @@ * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.zuul.filters; +package com.alibaba.csp.sentinel.adapter.gateway.zuul.filters; -import com.alibaba.csp.sentinel.adapter.zuul.properties.SentinelZuulProperties; import com.netflix.zuul.context.RequestContext; import org.junit.Assert; import org.junit.Test; -import static com.alibaba.csp.sentinel.adapter.zuul.constants.ZuulConstant.ERROR_TYPE; +import static com.alibaba.csp.sentinel.adapter.gateway.zuul.constants.ZuulConstant.ERROR_TYPE; /** * @author tiger */ -public class SentinelErrorFilterTest { +public class SentinelZuulErrorFilterTest { + @Test public void testFilterType() throws Exception { - SentinelZuulProperties properties = new SentinelZuulProperties(); - SentinelErrorFilter sentinelErrorFilter = new SentinelErrorFilter(properties); - Assert.assertEquals(sentinelErrorFilter.filterType(), ERROR_TYPE); + SentinelZuulErrorFilter sentinelZuulErrorFilter = new SentinelZuulErrorFilter(); + Assert.assertEquals(sentinelZuulErrorFilter.filterType(), ERROR_TYPE); } @Test public void testShouldFilter() { - SentinelZuulProperties properties = new SentinelZuulProperties(); - Assert.assertFalse(properties.isEnabled()); - properties.setEnabled(true); - SentinelErrorFilter sentinelErrorFilter = new SentinelErrorFilter(properties); + SentinelZuulErrorFilter sentinelZuulErrorFilter = new SentinelZuulErrorFilter(); RequestContext ctx = RequestContext.getCurrentContext(); ctx.setThrowable(new RuntimeException()); - Assert.assertTrue(sentinelErrorFilter.shouldFilter()); + Assert.assertTrue(sentinelZuulErrorFilter.shouldFilter()); } @Test public void testRun() throws Exception { - SentinelZuulProperties properties = new SentinelZuulProperties(); - SentinelErrorFilter sentinelErrorFilter = new SentinelErrorFilter(properties); - Object result = sentinelErrorFilter.run(); + SentinelZuulErrorFilter sentinelZuulErrorFilter = new SentinelZuulErrorFilter(); + Object result = sentinelZuulErrorFilter.run(); Assert.assertNull(result); } diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPostFilterTest.java b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/filters/SentinelZuulPostFilterTest.java similarity index 55% rename from sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPostFilterTest.java rename to sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/filters/SentinelZuulPostFilterTest.java index 6ccd6cae..8d804619 100644 --- a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPostFilterTest.java +++ b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/filters/SentinelZuulPostFilterTest.java @@ -14,31 +14,28 @@ * limitations under the License. */ -package com.alibaba.csp.sentinel.adapter.zuul.filters; +package com.alibaba.csp.sentinel.adapter.gateway.zuul.filters; -import com.alibaba.csp.sentinel.adapter.zuul.properties.SentinelZuulProperties; import org.junit.Assert; import org.junit.Test; -import static com.alibaba.csp.sentinel.adapter.zuul.constants.ZuulConstant.POST_TYPE; +import static com.alibaba.csp.sentinel.adapter.gateway.zuul.constants.ZuulConstant.POST_TYPE; /** * @author tiger */ -public class SentinelPostFilterTest { +public class SentinelZuulPostFilterTest { @Test public void testFilterType() throws Exception { - SentinelZuulProperties properties = new SentinelZuulProperties(); - SentinelPostFilter sentinelPostFilter = new SentinelPostFilter(properties); - Assert.assertEquals(sentinelPostFilter.filterType(), POST_TYPE); + SentinelZuulPostFilter sentinelZuulPostFilter = new SentinelZuulPostFilter(); + Assert.assertEquals(sentinelZuulPostFilter.filterType(), POST_TYPE); } @Test public void testRun() throws Exception { - SentinelZuulProperties properties = new SentinelZuulProperties(); - SentinelPostFilter sentinelPostFilter = new SentinelPostFilter(properties); - Object result = sentinelPostFilter.run(); + SentinelZuulPostFilter sentinelZuulPostFilter = new SentinelZuulPostFilter(); + Object result = sentinelZuulPostFilter.run(); Assert.assertNull(result); } diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/filters/SentinelZuulPreFilterTest.java b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/filters/SentinelZuulPreFilterTest.java new file mode 100644 index 00000000..289a9b92 --- /dev/null +++ b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/gateway/zuul/filters/SentinelZuulPreFilterTest.java @@ -0,0 +1,61 @@ +/* + * 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.csp.sentinel.adapter.gateway.zuul.filters; + +import com.netflix.zuul.context.RequestContext; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import javax.servlet.http.HttpServletRequest; + +import static com.alibaba.csp.sentinel.adapter.gateway.zuul.constants.ZuulConstant.PRE_TYPE; +import static com.alibaba.csp.sentinel.adapter.gateway.zuul.constants.ZuulConstant.SERVICE_ID_KEY; +import static org.mockito.Mockito.when; + +/** + * @author tiger + */ +public class SentinelZuulPreFilterTest { + + private String SERVICE_ID = "servicea"; + + private String URI = "/servicea/test"; + + @Mock + private HttpServletRequest httpServletRequest; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + when(httpServletRequest.getContextPath()).thenReturn(""); + when(httpServletRequest.getPathInfo()).thenReturn(URI); + RequestContext requestContext = new RequestContext(); + requestContext.set(SERVICE_ID_KEY, SERVICE_ID); + requestContext.setRequest(httpServletRequest); + RequestContext.testSetCurrentContext(requestContext); + } + + @Test + public void testFilterType() throws Exception { + SentinelZuulPreFilter sentinelZuulPreFilter = new SentinelZuulPreFilter(); + Assert.assertEquals(sentinelZuulPreFilter.filterType(), PRE_TYPE); + } + +} \ No newline at end of file diff --git a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPreFilterTest.java b/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPreFilterTest.java deleted file mode 100644 index ab71e5cf..00000000 --- a/sentinel-adapter/sentinel-zuul-adapter/src/test/java/com/alibaba/csp/sentinel/adapter/zuul/filters/SentinelPreFilterTest.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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.csp.sentinel.adapter.zuul.filters; - -import com.alibaba.csp.sentinel.adapter.zuul.fallback.DefaultRequestOriginParser; -import com.alibaba.csp.sentinel.adapter.zuul.fallback.RequestOriginParser; -import com.alibaba.csp.sentinel.adapter.zuul.fallback.UrlCleaner; -import com.alibaba.csp.sentinel.adapter.zuul.properties.SentinelZuulProperties; -import com.alibaba.csp.sentinel.slots.block.flow.FlowException; -import com.netflix.zuul.context.RequestContext; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; - -import javax.servlet.http.HttpServletRequest; - -import static com.alibaba.csp.sentinel.adapter.zuul.constants.ZuulConstant.PRE_TYPE; -import static com.alibaba.csp.sentinel.adapter.zuul.constants.ZuulConstant.SERVICE_ID_KEY; -import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.when; - -/** - * @author tiger - */ -public class SentinelPreFilterTest { - - private String SERVICE_ID = "servicea"; - - private String URI = "/servicea/test"; - - @Mock - private HttpServletRequest httpServletRequest; - - @Mock - private UrlCleaner urlCleaner; - - private final RequestOriginParser requestOriginParser = new DefaultRequestOriginParser(); - - @Before - public void setUp() { - MockitoAnnotations.initMocks(this); - when(httpServletRequest.getContextPath()).thenReturn(""); - when(httpServletRequest.getPathInfo()).thenReturn(URI); - RequestContext requestContext = new RequestContext(); - requestContext.set(SERVICE_ID_KEY, SERVICE_ID); - requestContext.setRequest(httpServletRequest); - RequestContext.testSetCurrentContext(requestContext); - } - - @Test - public void testFilterType() throws Exception { - SentinelZuulProperties properties = new SentinelZuulProperties(); - SentinelPreFilter sentinelPreFilter = new SentinelPreFilter(properties, urlCleaner, requestOriginParser); - Assert.assertEquals(sentinelPreFilter.filterType(), PRE_TYPE); - } - - @Test - public void testRun() throws Exception { - RequestContext ctx = RequestContext.getCurrentContext(); - SentinelZuulProperties properties = new SentinelZuulProperties(); - SentinelPreFilter sentinelPreFilter = new SentinelPreFilter(properties, urlCleaner, requestOriginParser); - given(urlCleaner.clean(URI)).willReturn(URI); - sentinelPreFilter.run(); - Assert.assertNull(ctx.getRouteHost()); - Assert.assertEquals(ctx.get(SERVICE_ID_KEY), SERVICE_ID); - } - - @Test - public void testServiceFallBackRun() throws Exception { - RequestContext ctx = RequestContext.getCurrentContext(); - SentinelZuulProperties properties = new SentinelZuulProperties(); - properties.setEnabled(true); - SentinelPreFilter sentinelPreFilter = new SentinelPreFilter(properties, urlCleaner, requestOriginParser); - - given(urlCleaner.clean(URI)).willAnswer( - new Answer