Update document and pom for sentinel-zuul-adapter

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
Eric Zhao 2019-01-14 14:08:42 +08:00
parent 3a1eb56338
commit a06a7cf86e
2 changed files with 43 additions and 58 deletions

View File

@ -1,15 +1,12 @@
# Sentinel Zuul Adapter # Sentinel Zuul Adapter
Zuul does not provide rateLimit function, If use default `SentinelRibbonFilter` route filter. it wrapped by Hystrix Command. so only provide Service level Sentinel Zuul Adapter provides **ServiceId level** and **API Path level** flow control for Zuul gateway service.
circuit protect.
Sentinel can provide `ServiceId` level and `API Path` level flow control for zuul gateway service. > *Note*: this adapter only support Zuul 1.x.
*Note*: this project is for zuul 1.
## How to use ## How to use
1. Add maven dependency 1. Add Maven dependency to your `pom.xml`:
```xml ```xml
<dependency> <dependency>
@ -17,7 +14,6 @@ Sentinel can provide `ServiceId` level and `API Path` level flow control for zuu
<artifactId>sentinel-zuul-adapter</artifactId> <artifactId>sentinel-zuul-adapter</artifactId>
<version>x.y.z</version> <version>x.y.z</version>
</dependency> </dependency>
``` ```
2. Register filters 2. Register filters
@ -25,7 +21,7 @@ Sentinel can provide `ServiceId` level and `API Path` level flow control for zuu
```java ```java
// get registry // get registry
final FilterRegistry r = FilterRegistry.instance(); final FilterRegistry r = FilterRegistry.instance();
// this is property config. set filter ennable // this is property config. set filter enable
SentinelZuulProperties properties = new SentinelZuulProperties(); SentinelZuulProperties properties = new SentinelZuulProperties();
properties.setEnabled(true); properties.setEnabled(true);
// set url cleaner, here use default // set url cleaner, here use default
@ -48,18 +44,15 @@ As Zuul run as per thread per connection block model, we add filters around `rou
- `SentinelPreFilter`: Get an entry of resource, the first order is **ServiceId** (the key in RequestContext is `serviceId`, this can set in own custom filter), then **API Path**. - `SentinelPreFilter`: Get an entry of resource, the first order is **ServiceId** (the key in RequestContext is `serviceId`, this can set in own custom filter), then **API Path**.
- `SentinelPostFilter`: When success response, exit entry. - `SentinelPostFilter`: When success response, exit entry.
- `SentinelPreFilter`: When get an `Exception`, trace the exception and exit context. - `SentinelPreFilter`: When an `Exception` caught, trace the exception and exit context.
<img width="792" src="https://user-images.githubusercontent.com/9305625/47277113-6b5da780-d5ef-11e8-8a0a-93a6b09b0887.png">
the order of Filter can be changed in property: The order of filters can be changed in property.
Filters create structure like:
The invocation chain resembles this:
```bash ```bash
EntranceNode: machine-root(t:3 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0) 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) -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(t:2 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
@ -68,27 +61,22 @@ EntranceNode: machine-root(t:3 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) -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(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) ---/book/coke(t:0 pq:0 bq:0 tq:0 rt:0 prq:0 1mp:0 1mb:0 1mt:0)
``` ```
`book` and `coke` are serviceId. - `book` and `coke` are serviceId.
- `/book/coke` is api path, the real API path is `/coke`.
`---/book/coke` is api path, the real uri is `/coke`. ## Integration with Sentinel Dashboard
1. Start [Sentinel Dashboard](https://github.com/alibaba/Sentinel/wiki/Dashboard).
## Integration with Sentinel DashBord 2. You can configure the rules in Sentinel dashboard or via dynamic rule configuration.
1. Start [Sentinel DashBord](https://github.com/alibaba/Sentinel/wiki/%E6%8E%A7%E5%88%B6%E5%8F%B0).
2. Sentinel has full rule config features. see [Dynamic-Rule-Configuration](https://github.com/alibaba/Sentinel/wiki/Dynamic-Rule-Configuration)
## Fallbacks ## Fallbacks
Implements `SentinelFallbackProvider` to define your own Fallback Provider when Sentinel Block Exception throwing. the default You can implement `SentinelFallbackProvider` to define your own fallback provider when Sentinel `BlockException` is thrown.
Fallback Provider is `DefaultBlockFallbackProvider`. 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 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.
can be needed.
Here is an example: Here is an example:
@ -118,13 +106,11 @@ public class MyBlockFallbackProvider implements ZuulBlockFallbackProvider {
// register fallback // register fallback
ZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider()); ZuulBlockFallbackManager.registerProvider(new MyBlockFallbackProvider());
``` ```
Default block response Default block response:
```json ```json
{ {
"code":429, "code":429,
"message":"Sentinel block exception", "message":"Sentinel block exception",
@ -132,17 +118,15 @@ Default block response
} }
``` ```
## Origin parser ## Request origin parser
自定义解析URL You can register customized request origin parser like this:
```java ```java
public class MyRequestOriginParser implements RequestOriginParser {
public class DefaultRequestOriginParser implements RequestOriginParser {
@Override @Override
public String parseOrigin(HttpServletRequest request) { public String parseOrigin(HttpServletRequest request) {
return ""; return request.getRemoteAddr();
} }
} }
``` ```

View File

@ -21,12 +21,19 @@
<groupId>com.alibaba.csp</groupId> <groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId> <artifactId>sentinel-core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.api.version}</version>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>com.netflix.zuul</groupId> <groupId>com.netflix.zuul</groupId>
<artifactId>zuul-core</artifactId> <artifactId>zuul-core</artifactId>
<version>${zuul.version}</version> <version>${zuul.version}</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
@ -37,12 +44,6 @@
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.api.version}</version>
<scope>provided</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>