-
Notifications
You must be signed in to change notification settings - Fork 8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
99d4355
commit 2b68a6c
Showing
20 changed files
with
829 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,63 @@ | ||
# Sentinel OkHttp Adapter | ||
|
||
## Introduction | ||
|
||
Sentinel provides integration for OkHttp client to enable flow control for web requests. | ||
|
||
Add the following dependency in `pom.xml` (if you are using Maven): | ||
|
||
```xml | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-okhttp-adapter</artifactId> | ||
<version>x.y.z</version> | ||
</dependency> | ||
``` | ||
|
||
We can add the `SentinelOkHttpInterceptor` interceptor when `OkHttpClient` at initialization, for example: | ||
|
||
```java | ||
OkHttpClient client = new OkHttpClient.Builder() | ||
.addInterceptor(new SentinelOkHttpInterceptor()) | ||
.build(); | ||
``` | ||
|
||
## Configuration | ||
|
||
- `SentinelOkHttpConfig` configuration: | ||
|
||
| name | description | type | default value | | ||
|------|------------|------|-------| | ||
| extractor | custom resource extractor | `OkHttpResourceExtractor` | `DefaultOkHttpResourceExtractor` | | ||
| fallback | handle request when it is blocked | `OkHttpFallback` | `DefaultOkHttpFallback` | | ||
|
||
### extractor (resource extractor) | ||
|
||
We can define `OkHttpResourceExtractor` to custom resource extractor replace `DefaultOkHttpResourceExtractor`, for example: okhttp:GET:ip:port/okhttp/back/1 ==> /okhttp/back/{id} | ||
|
||
```java | ||
OkHttpResourceExtractor extractor = (request, connection) -> { | ||
String resource = request.url().toString(); | ||
String regex = "/okhttp/back/"; | ||
if (resource.contains(regex)) { | ||
resource = resource.substring(0, resource.indexOf(regex) + regex.length()) + "{id}"; | ||
} | ||
return resource; | ||
}; | ||
SentinelOkHttpConfig.setExtractor(extractor); | ||
``` | ||
|
||
### fallback (Block handling) | ||
|
||
We can define `OkHttpFallback` to handle request is blocked according to the actual scenario, for example: | ||
|
||
```java | ||
public class DefaultOkHttpFallback implements OkHttpFallback { | ||
|
||
@Override | ||
public Response handle(Request request, Connection connection, BlockException e) { | ||
// Just wrap and throw the exception. | ||
throw new SentinelRpcException(e); | ||
} | ||
} | ||
``` |
This file contains 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 @@ | ||
<?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.8.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>sentinel-okhttp-adapter</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<properties> | ||
<okhttp.version>3.6.0</okhttp.version> | ||
<spring.boot.version>2.1.3.RELEASE</spring.boot.version> | ||
<spring-test.version>5.1.5.RELEASE</spring-test.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.alibaba.csp</groupId> | ||
<artifactId>sentinel-core</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.squareup.okhttp3</groupId> | ||
<artifactId>okhttp</artifactId> | ||
<version>${okhttp.version}</version> | ||
<scope>provided</scope> | ||
</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> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-test</artifactId> | ||
<version>${spring.boot.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<version>${spring-test.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
55 changes: 55 additions & 0 deletions
55
...pter/src/main/java/com/alibaba/csp/sentinel/adapter/okhttp/SentinelOkHttpInterceptor.java
This file contains 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,55 @@ | ||
/* | ||
* Copyright 1999-2020 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.okhttp; | ||
|
||
import com.alibaba.csp.sentinel.*; | ||
import com.alibaba.csp.sentinel.adapter.okhttp.config.SentinelOkHttpConfig; | ||
import com.alibaba.csp.sentinel.slots.block.BlockException; | ||
import com.alibaba.csp.sentinel.util.StringUtil; | ||
import okhttp3.Interceptor; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* @author zhaoyuguang | ||
*/ | ||
public class SentinelOkHttpInterceptor implements Interceptor { | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Entry entry = null; | ||
try { | ||
Request request = chain.request(); | ||
String name = SentinelOkHttpConfig.getExtractor().extract(request.url().toString(), request, chain.connection()); | ||
if (!StringUtil.isEmpty(SentinelOkHttpConfig.getPrefix())) { | ||
name = SentinelOkHttpConfig.getPrefix() + name; | ||
} | ||
entry = SphU.entry(name, ResourceTypeConstants.COMMON_WEB, EntryType.OUT); | ||
return chain.proceed(request); | ||
} catch (BlockException e) { | ||
return SentinelOkHttpConfig.getFallback().handle(chain.request(), chain.connection(), e); | ||
} catch (Throwable t) { | ||
Tracer.traceEntry(t, entry); | ||
throw t; | ||
} finally { | ||
if (entry != null) { | ||
entry.exit(); | ||
} | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...er/src/main/java/com/alibaba/csp/sentinel/adapter/okhttp/config/SentinelOkHttpConfig.java
This file contains 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,59 @@ | ||
/* | ||
* Copyright 1999-2020 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.okhttp.config; | ||
|
||
import com.alibaba.csp.sentinel.adapter.okhttp.extractor.DefaultOkHttpResourceExtractor; | ||
import com.alibaba.csp.sentinel.adapter.okhttp.extractor.OkHttpResourceExtractor; | ||
import com.alibaba.csp.sentinel.adapter.okhttp.fallback.DefaultOkHttpFallback; | ||
import com.alibaba.csp.sentinel.adapter.okhttp.fallback.OkHttpFallback; | ||
import com.alibaba.csp.sentinel.util.AssertUtil; | ||
|
||
/** | ||
* @author zhaoyuguang | ||
*/ | ||
public final class SentinelOkHttpConfig { | ||
|
||
private static volatile String prefix = "okhttp:"; | ||
private static volatile OkHttpResourceExtractor extractor = new DefaultOkHttpResourceExtractor(); | ||
private static volatile OkHttpFallback fallback = new DefaultOkHttpFallback(); | ||
|
||
public static String getPrefix() { | ||
return prefix; | ||
} | ||
|
||
public static void setPrefix(String prefix) { | ||
AssertUtil.notNull(prefix, "prefix cannot be null"); | ||
SentinelOkHttpConfig.prefix = prefix; | ||
} | ||
|
||
public static OkHttpResourceExtractor getExtractor() { | ||
return extractor; | ||
} | ||
|
||
public static void setExtractor(OkHttpResourceExtractor extractor) { | ||
AssertUtil.notNull(extractor, "extractor cannot be null"); | ||
SentinelOkHttpConfig.extractor = extractor; | ||
} | ||
|
||
public static OkHttpFallback getFallback() { | ||
return fallback; | ||
} | ||
|
||
public static void setFallback(OkHttpFallback fallback) { | ||
AssertUtil.notNull(fallback, "fallback cannot be null"); | ||
SentinelOkHttpConfig.fallback = fallback; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ava/com/alibaba/csp/sentinel/adapter/okhttp/extractor/DefaultOkHttpResourceExtractor.java
This file contains 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,30 @@ | ||
/* | ||
* Copyright 1999-2020 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.okhttp.extractor; | ||
|
||
import okhttp3.Connection; | ||
import okhttp3.Request; | ||
|
||
/** | ||
* @author zhaoyuguang | ||
*/ | ||
public class DefaultOkHttpResourceExtractor implements OkHttpResourceExtractor { | ||
|
||
@Override | ||
public String extract(String url, Request request, Connection connection) { | ||
return request.method() + ":" + request.url().toString(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
.../main/java/com/alibaba/csp/sentinel/adapter/okhttp/extractor/OkHttpResourceExtractor.java
This file contains 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,27 @@ | ||
/* | ||
* Copyright 1999-2020 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.okhttp.extractor; | ||
|
||
import okhttp3.Connection; | ||
import okhttp3.Request; | ||
|
||
/** | ||
* @author zhaoyuguang | ||
*/ | ||
public interface OkHttpResourceExtractor { | ||
|
||
String extract(String url, Request request, Connection connection); | ||
} |
34 changes: 34 additions & 0 deletions
34
...src/main/java/com/alibaba/csp/sentinel/adapter/okhttp/fallback/DefaultOkHttpFallback.java
This file contains 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,34 @@ | ||
/* | ||
* Copyright 1999-2020 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.okhttp.fallback; | ||
|
||
import com.alibaba.csp.sentinel.slots.block.BlockException; | ||
import com.alibaba.csp.sentinel.slots.block.SentinelRpcException; | ||
import okhttp3.Connection; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
/** | ||
* @author zhaoyuguang | ||
*/ | ||
public class DefaultOkHttpFallback implements OkHttpFallback { | ||
|
||
@Override | ||
public Response handle(Request request, Connection connection, BlockException e) { | ||
// Just wrap and throw the exception. | ||
throw new SentinelRpcException(e); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...dapter/src/main/java/com/alibaba/csp/sentinel/adapter/okhttp/fallback/OkHttpFallback.java
This file contains 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,29 @@ | ||
/* | ||
* Copyright 1999-2020 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.okhttp.fallback; | ||
|
||
import com.alibaba.csp.sentinel.slots.block.BlockException; | ||
import okhttp3.Connection; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
|
||
/** | ||
* @author zhaoyuguang | ||
*/ | ||
public interface OkHttpFallback { | ||
|
||
Response handle(Request request, Connection connection, BlockException e); | ||
} |
Oops, something went wrong.