Skip to content

Commit

Permalink
Polish code and demo of Sentinel Zuul 2.x adapter
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Zhao <sczyh16@gmail.com>
  • Loading branch information
sczyh30 committed Mar 14, 2020
1 parent 5b9865d commit 0536fb6
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 273 deletions.
23 changes: 6 additions & 17 deletions sentinel-adapter/sentinel-zuul2-adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
This adapter provides **route level** and **customized API level**
flow control for Zuul 2.x API Gateway.

> *Note*: this adapter only support Zuul 2.x.
> *Note*: this adapter only supports Zuul 2.x.
## How to use

> You can refer to demo `sentinel-demo-zuul2-gateway`
> You can refer to demo [`sentinel-demo-zuul2-gateway`](https://github.com/alibaba/Sentinel/tree/master/sentinel-demo/sentinel-demo-zuul2-gateway).
1. Add Maven dependency to your `pom.xml`:

Expand All @@ -29,9 +29,9 @@ 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.
As Zuul 2.x is based on Netty, an event-driven asynchronous model, so we use `AsyncEntry`.

- `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.
- `SentinelZuulInboundFilter`: This inbound filter will regard all routes (`routeVIP` in `SessionContext` by default) 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.

Expand All @@ -40,6 +40,8 @@ As Zuul 2.x is based on netty, a event-drive model, so we use `AsyncEntry` to do
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.

> You may need to add `-Dcsp.sentinel.app.type=1` property to mark this application as API gateway.
## Fallbacks

You can implement `ZuulBlockFallbackProvider` to define your own fallback provider when Sentinel `BlockException` is thrown.
Expand Down Expand Up @@ -86,16 +88,3 @@ Default block response:
"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();
}
}
```
1 change: 1 addition & 0 deletions sentinel-adapter/sentinel-zuul2-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
</exclusions>
</dependency>

<!-- The Spring library is introduced for AntMatcher -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
/*
* 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;

import com.alibaba.csp.sentinel.adapter.gateway.common.param.RequestItemParser;
import com.netflix.zuul.message.http.HttpRequestMessage;

/**
* @author wavesZh
* @since 1.7.2
*/
public class HttpRequestMessageItemParser implements RequestItemParser<HttpRequestMessage> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/**
* @author Eric Zhao
* @since 1.6.0
* @since 1.7.2
*/
public class ZuulApiDefinitionChangeObserver implements ApiDefinitionChangeObserver {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

/**
* @author wavesZh
* @since 1.7.2
*/
public final class ZuulGatewayApiMatcherManager {

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

package com.alibaba.csp.sentinel.adapter.gateway.zuul2.constants;


/**
* @author wavesZh
*/
public class ZuulConstant {
public class SentinelZuul2Constants {
/**
* Zuul use Sentinel as default context when serviceId is empty.
* The default entrance (context) name when the routeId is empty.
*/
public static final String ZUUL_DEFAULT_CONTEXT = "zuul_default_context";
public static final String ZUUL_DEFAULT_CONTEXT = "zuul2_default_context";
/**
* Zuul context key for keeping Sentinel entries.
*/
Expand All @@ -36,5 +35,5 @@ public class ZuulConstant {
*/
public static final String ZUUL_CTX_SENTINEL_BLOCKED_FLAG = "_sentinel_blocked_flag";

private ZuulConstant(){}
private SentinelZuul2Constants() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.alibaba.csp.sentinel.slots.block.BlockException;

/**
* Default Fallback provider for sentinel {@link BlockException}, {@literal *} meant for all routes.
* Default fallback provider for Sentinel {@link BlockException}, {@literal *} meant for all routes.
*
* @author tiger
*/
Expand All @@ -33,7 +33,7 @@ public String getRoute() {
@Override
public BlockResponse fallbackResponse(String route, Throwable cause) {
if (cause instanceof BlockException) {
return new BlockResponse(429, "Sentinel block exception", route);
return new BlockResponse(429, "SentinelBlockException", route);
} else {
return new BlockResponse(500, "System Error", route);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package com.alibaba.csp.sentinel.adapter.gateway.zuul2.filters.endpoint;

import com.alibaba.csp.sentinel.adapter.gateway.zuul2.constants.ZuulConstant;
import com.alibaba.csp.sentinel.adapter.gateway.zuul2.constants.SentinelZuul2Constants;
import com.alibaba.csp.sentinel.adapter.gateway.zuul2.fallback.BlockResponse;
import com.alibaba.csp.sentinel.adapter.gateway.zuul2.fallback.ZuulBlockFallbackManager;
import com.alibaba.csp.sentinel.adapter.gateway.zuul2.fallback.ZuulBlockFallbackProvider;

import com.netflix.zuul.context.SessionContext;
import com.netflix.zuul.filters.http.HttpSyncEndpoint;
import com.netflix.zuul.message.http.HttpRequestMessage;
Expand All @@ -32,13 +33,14 @@
* @author wavesZh
*/
public class SentinelZuulEndpoint extends HttpSyncEndpoint {

@Override
public HttpResponseMessage apply(HttpRequestMessage request) {
SessionContext context = request.getContext();
Throwable throwable = context.getError();
String fallBackRoute = (String) context.get(ZuulConstant.ZUUL_CTX_SENTINEL_FALLBACK_ROUTE);
ZuulBlockFallbackProvider zuulBlockFallbackProvider = ZuulBlockFallbackManager.getFallbackProvider(
fallBackRoute);
String fallBackRoute = (String) context.get(SentinelZuul2Constants.ZUUL_CTX_SENTINEL_FALLBACK_ROUTE);
ZuulBlockFallbackProvider zuulBlockFallbackProvider = ZuulBlockFallbackManager
.getFallbackProvider(fallBackRoute);
BlockResponse response = zuulBlockFallbackProvider.fallbackResponse(fallBackRoute, throwable);
HttpResponseMessage resp = new HttpResponseMessageImpl(context, request, response.getCode());
resp.setBodyAsText(response.toString());
Expand Down
Loading

0 comments on commit 0536fb6

Please sign in to comment.