-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Zuul2 integration #1138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Zuul2 integration #1138
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ff4e7d5
Merge pull request #1 from alibaba/master
wavesZh 1a3510f
Merge branch 'master' of github.com:wavesZh/Sentinel
wavesZh 380fd93
Merge remote-tracking branch 'upstream/master'
wavesZh f451f4f
Merge remote-tracking branch 'upstream/master'
wavesZh 41201d0
Merge remote-tracking branch 'upstream/master'
wavesZh fd2983e
init zuul2-Integration
wavesZh 77fb922
Merge remote-tracking branch 'upstream/master'
wavesZh 0673e37
Merge branch 'master' into zuul2-Integration
wavesZh 6846ec9
init
wavesZh 1cd936a
improve code
wavesZh edebd11
Merge remote-tracking branch 'upstream/master'
wavesZh 7c4ff8a
Merge branch 'master' into zuul2-Integration
wavesZh 78cc475
integration of zuul2
wavesZh 5b63fed
Merge remote-tracking branch 'upstream/master' into zuul2-Integration
wavesZh a276d0f
integration of zuul2
wavesZh e0a4ecd
clear code
wavesZh a6502bf
scope 调整
wavesZh 6c3857d
scope 调整
wavesZh 61487a4
improve code
wavesZh bf529be
Merge remote-tracking branch 'upstream/master' into zuul2-Integration
wavesZh 2bf159f
improve code
wavesZh 10d275c
merge code
wavesZh 162331d
integration of zuul2
wavesZh e239dcf
Merge branch 'master' into zuul2-Integration
sczyh30 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| # Sentinel Zuul2 Adapter | ||
|
|
||
| Sentinel Zuul2 Adapter provides **route level** and **customized API level** | ||
| flow control for Zuul API Gateway. | ||
|
|
||
| > *Note*: this adapter only support Zuul 2.x. | ||
|
|
||
| ## How to use | ||
|
|
||
| > You can refer to demo `sentinel-demo-zuul2-gateway` | ||
|
|
||
| 1. Add Maven dependency to your `pom.xml`: | ||
|
|
||
| ```xml | ||
| <dependency> | ||
| <groupId>com.alibaba.csp</groupId> | ||
| <artifactId>sentinel-zuul2-adapter</artifactId> | ||
| <version>x.y.z</version> | ||
| </dependency> | ||
| ``` | ||
|
|
||
| 2. Register filters | ||
|
|
||
| ```java | ||
| filterMultibinder.addBinding().toInstance(new SentinelZuulInboundFilter(500)); | ||
| filterMultibinder.addBinding().toInstance(new SentinelZuulOutboundFilter(500)); | ||
| filterMultibinder.addBinding().toInstance(new SentinelZuulEndpoint()); | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| As Zuul 2.x is based on netty, a event-drive model, so we use `AsyncEntry` to do flow control. | ||
|
|
||
| - `SentinelZuulInboundFilter`: This inbound filter will regard all proxy ID (`proxy` in `SessionContext`) and all customized API as resources. When a `BlockException` caught, the filter will set endpoint to find a fallback to execute. | ||
| - `SentinelZuulOutboundFilter`: When the response has no exception caught, the post filter will trace the exception and complete the entries. | ||
| - `SentinelZuulEndpoint`: When an exception is caught, the filter will find a fallback to execute. | ||
|
|
||
| ## Integration with Sentinel Dashboard | ||
|
|
||
| 1. Start [Sentinel Dashboard](https://github.com/alibaba/Sentinel/wiki/Dashboard). | ||
| 2. You can configure the rules in Sentinel dashboard or via dynamic rule configuration. | ||
|
|
||
| ## Fallbacks | ||
|
|
||
| You can implement `ZuulBlockFallbackProvider` to define your own fallback provider when Sentinel `BlockException` is thrown. | ||
| The default fallback provider is `DefaultBlockFallbackProvider`. | ||
|
|
||
| By default fallback route is proxy ID (or customized API name). | ||
|
|
||
| Here is an example: | ||
|
|
||
| ```java | ||
|
|
||
| // custom provider | ||
| public class MyBlockFallbackProvider implements ZuulBlockFallbackProvider { | ||
|
|
||
| private Logger logger = LoggerFactory.getLogger(DefaultBlockFallbackProvider.class); | ||
|
|
||
| // you can define root as service level | ||
| @Override | ||
| public String getRoute() { | ||
| return "my-route"; | ||
| } | ||
|
|
||
| @Override | ||
| public BlockResponse fallbackResponse(String route, Throwable cause) { | ||
| RecordLog.info(String.format("[Sentinel DefaultBlockFallbackProvider] Run fallback route: %s", route)); | ||
| if (cause instanceof BlockException) { | ||
| return new BlockResponse(429, "Sentinel block exception", route); | ||
| } else { | ||
| return new BlockResponse(500, "System Error", route); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // register fallback | ||
| ZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider()); | ||
| ``` | ||
|
|
||
| Default block response: | ||
|
|
||
| ```json | ||
| { | ||
| "code":429, | ||
| "message":"Sentinel block exception", | ||
| "route":"/" | ||
| } | ||
| ``` | ||
|
|
||
| ## Request origin parser | ||
|
|
||
| You can register customized request origin parser like this: | ||
|
|
||
| ```java | ||
| public class MyRequestOriginParser implements RequestOriginParser { | ||
| @Override | ||
| public String parseOrigin(HttpRequestMessage request) { | ||
| return request.getInboundRequest().getOriginalHost() + ":" + request.getInboundRequest().getOriginalPort(); | ||
| } | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <parent> | ||
| <artifactId>sentinel-adapter</artifactId> | ||
| <groupId>com.alibaba.csp</groupId> | ||
| <version>1.7.1-SNAPSHOT</version> | ||
| </parent> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <artifactId>sentinel-zuul2-adapter</artifactId> | ||
| <packaging>jar</packaging> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-compiler-plugin</artifactId> | ||
| <configuration> | ||
| <source>8</source> | ||
| <target>8</target> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| <properties> | ||
| <zuul.version>2.1.5</zuul.version> | ||
| </properties> | ||
|
|
||
| <dependencies> | ||
| <dependency> | ||
| <groupId>com.alibaba.csp</groupId> | ||
| <artifactId>sentinel-core</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>com.alibaba.csp</groupId> | ||
| <artifactId>sentinel-api-gateway-adapter-common</artifactId> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>com.netflix.zuul</groupId> | ||
| <artifactId>zuul-core</artifactId> | ||
| <version>${zuul.version}</version> | ||
| <scope>provided</scope> | ||
| <exclusions> | ||
| <exclusion> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-core</artifactId> | ||
| </exclusion> | ||
| </exclusions> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.springframework</groupId> | ||
| <artifactId>spring-core</artifactId> | ||
| <version>5.1.9.RELEASE</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>junit</groupId> | ||
| <artifactId>junit</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>org.mockito</groupId> | ||
| <artifactId>mockito-core</artifactId> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| </project> | ||
32 changes: 32 additions & 0 deletions
32
...ain/java/com/alibaba/csp/sentinel/adapter/gateway/zuul2/HttpRequestMessageItemParser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package com.alibaba.csp.sentinel.adapter.gateway.zuul2; | ||
|
|
||
| import com.alibaba.csp.sentinel.adapter.gateway.common.param.RequestItemParser; | ||
| import com.netflix.zuul.message.http.HttpRequestMessage; | ||
|
|
||
| public class HttpRequestMessageItemParser implements RequestItemParser<HttpRequestMessage> { | ||
|
|
||
| @Override | ||
| public String getPath(HttpRequestMessage request) { | ||
| return request.getInboundRequest().getPath(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getRemoteAddress(HttpRequestMessage request) { | ||
| return request.getOriginalHost(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getHeader(HttpRequestMessage request, String key) { | ||
| return String.valueOf(request.getInboundRequest().getHeaders().get(key)); | ||
| } | ||
|
|
||
| @Override | ||
| public String getUrlParam(HttpRequestMessage request, String paramName) { | ||
| return String.valueOf(request.getInboundRequest().getQueryParams().get(paramName)); | ||
| } | ||
|
|
||
| @Override | ||
| public String getCookieValue(HttpRequestMessage request, String cookieName) { | ||
| return String.valueOf(request.getInboundRequest().parseCookies().get(cookieName)); | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
...a/com/alibaba/csp/sentinel/adapter/gateway/zuul2/api/ZuulApiDefinitionChangeObserver.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * Copyright 1999-2019 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 | ||
| * | ||
| * https://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.zuul2.api; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition; | ||
| import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinitionChangeObserver; | ||
|
|
||
| /** | ||
| * @author Eric Zhao | ||
| * @since 1.6.0 | ||
| */ | ||
| public class ZuulApiDefinitionChangeObserver implements ApiDefinitionChangeObserver { | ||
|
|
||
| @Override | ||
| public void onChange(Set<ApiDefinition> apiDefinitions) { | ||
| ZuulGatewayApiMatcherManager.loadApiDefinitions(apiDefinitions); | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
...java/com/alibaba/csp/sentinel/adapter/gateway/zuul2/api/ZuulGatewayApiMatcherManager.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /* | ||
| * Copyright 1999-2019 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 | ||
| * | ||
| * https://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.zuul2.api; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition; | ||
| import com.alibaba.csp.sentinel.adapter.gateway.zuul2.api.matcher.HttpRequestMessageApiMatcher; | ||
|
|
||
| /** | ||
| * @author wavesZh | ||
| */ | ||
| public final class ZuulGatewayApiMatcherManager { | ||
|
|
||
| private static final Map<String, HttpRequestMessageApiMatcher> API_MATCHER_MAP = new ConcurrentHashMap<>(); | ||
|
|
||
| public static Map<String, HttpRequestMessageApiMatcher> getApiMatcherMap() { | ||
| return Collections.unmodifiableMap(API_MATCHER_MAP); | ||
| } | ||
|
|
||
| public static HttpRequestMessageApiMatcher getMatcher(final String apiName) { | ||
| if (apiName == null) { | ||
| return null; | ||
| } | ||
| return API_MATCHER_MAP.get(apiName); | ||
| } | ||
|
|
||
| public static Set<ApiDefinition> getApiDefinitionSet() { | ||
| Set<ApiDefinition> set = new HashSet<>(); | ||
| for (HttpRequestMessageApiMatcher matcher : API_MATCHER_MAP.values()) { | ||
| set.add(matcher.getApiDefinition()); | ||
| } | ||
| return set; | ||
| } | ||
|
|
||
| static synchronized void loadApiDefinitions(/*@Valid*/ Set<ApiDefinition> definitions) { | ||
| if (definitions == null || definitions.isEmpty()) { | ||
| API_MATCHER_MAP.clear(); | ||
| return; | ||
| } | ||
| for (ApiDefinition definition : definitions) { | ||
| addApiDefinition(definition); | ||
| } | ||
| } | ||
|
|
||
| static void addApiDefinition(ApiDefinition definition) { | ||
| API_MATCHER_MAP.put(definition.getApiName(), new HttpRequestMessageApiMatcher(definition)); | ||
| } | ||
|
|
||
| private ZuulGatewayApiMatcherManager() {} | ||
| } |
70 changes: 70 additions & 0 deletions
70
.../alibaba/csp/sentinel/adapter/gateway/zuul2/api/matcher/HttpRequestMessageApiMatcher.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| /* | ||
| * Copyright 1999-2019 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 | ||
| * | ||
| * https://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.zuul2.api.matcher; | ||
|
|
||
| import com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants; | ||
| import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition; | ||
| import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPathPredicateItem; | ||
| import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiPredicateItem; | ||
| import com.alibaba.csp.sentinel.adapter.gateway.common.api.matcher.AbstractApiMatcher; | ||
| import com.alibaba.csp.sentinel.adapter.gateway.zuul2.api.route.ZuulRouteMatchers; | ||
| import com.alibaba.csp.sentinel.util.StringUtil; | ||
| import com.alibaba.csp.sentinel.util.function.Predicate; | ||
| import com.netflix.zuul.message.http.HttpRequestMessage; | ||
|
|
||
| /** | ||
| * @author wavesZh | ||
| */ | ||
| public class HttpRequestMessageApiMatcher extends AbstractApiMatcher<HttpRequestMessage> { | ||
|
|
||
| public HttpRequestMessageApiMatcher(ApiDefinition apiDefinition) { | ||
| super(apiDefinition); | ||
| } | ||
|
|
||
| @Override | ||
| protected void initializeMatchers() { | ||
| if (apiDefinition.getPredicateItems() != null) { | ||
| for (ApiPredicateItem item : apiDefinition.getPredicateItems()) { | ||
| Predicate<HttpRequestMessage> predicate = fromApiPredicate(item); | ||
| if (predicate != null) { | ||
| matchers.add(predicate); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Predicate<HttpRequestMessage> fromApiPredicate(/*@NonNull*/ ApiPredicateItem item) { | ||
| if (item instanceof ApiPathPredicateItem) { | ||
| return fromApiPathPredicate((ApiPathPredicateItem)item); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private Predicate<HttpRequestMessage> fromApiPathPredicate(/*@Valid*/ ApiPathPredicateItem item) { | ||
| String pattern = item.getPattern(); | ||
| if (StringUtil.isBlank(pattern)) { | ||
| return null; | ||
| } | ||
| switch (item.getMatchStrategy()) { | ||
| case SentinelGatewayConstants.URL_MATCH_STRATEGY_REGEX: | ||
| return ZuulRouteMatchers.regexPath(pattern); | ||
| case SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX: | ||
| return ZuulRouteMatchers.antPath(pattern); | ||
| default: | ||
| return ZuulRouteMatchers.exactPath(pattern); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.