Skip to content
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

Add OkHttp integration #1456

Merged
merged 18 commits into from
May 15, 2020
1 change: 1 addition & 0 deletions sentinel-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<module>sentinel-spring-cloud-gateway-adapter</module>
<module>sentinel-spring-webmvc-adapter</module>
<module>sentinel-zuul2-adapter</module>
<module>sentinel-okhttp-adapter</module>
<module>sentinel-jax-rs-adapter</module>
</modules>

Expand Down
63 changes: 63 additions & 0 deletions sentinel-adapter/sentinel-okhttp-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Sentinel Spring OkHttp Adapter
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spring is used for only unit test, here whether it can be removed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy 别的 readme 忘记删了


## 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 |
|------|------------|------|-------|
| cleaner | aggregate URLs according to actual scenarios | `OkHttpUrlCleaner` | `DefaultOkHttpUrlCleaner` |
| fallback | handle request when it is blocked | `OkHttpFallback` | `DefaultOkHttpFallback` |

### cleaner (URL cleaner)

We can define `OkHttpUrlCleaner` to aggregate URLs according to actual scenarios, for example: /okhttp/back/1 ==> /okhttp/back/{id}

```java
OkHttpUrlCleaner cleaner = (request, connection) -> {
String url = request.url().toString();
String regex = "/okhttp/back/";
if (url.contains(regex)) {
url = url.substring(0, url.indexOf(regex) + regex.length()) + "{id}";
}
return url;
};
SentinelOkHttpConfig.setCleaner(cleaner);
```

### 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);
}
}
```
69 changes: 69 additions & 0 deletions sentinel-adapter/sentinel-okhttp-adapter/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?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>
<java.source.version>1.8</java.source.version>
zhaoyuguang marked this conversation as resolved.
Show resolved Hide resolved
<java.target.version>1.8</java.target.version>
<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>
</dependency>
sczyh30 marked this conversation as resolved.
Show resolved Hide resolved

<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>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to indent here, make the format good looking.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1999-2020 is better, since time goes by.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

恩 暂且吧本提交涉及的改了

*
* 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 okhttp3.Interceptor;
import okhttp3.Response;

import java.io.IOException;

/**
* @author zhaoyuguang
*/

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the redundant blank line?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

public class SentinelOkHttpInterceptor implements Interceptor {

@Override
public Response intercept(Chain chain) throws IOException {
Entry entry = null;
try {
String name = SentinelOkHttpConfig.getCleaner().clean(chain.request(), chain.connection());
entry = SphU.entry(name, ResourceTypeConstants.COMMON_WEB, EntryType.OUT);
return chain.proceed(chain.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();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.okhttp.cleaner;

import okhttp3.Connection;
import okhttp3.Request;

/**
* @author zhaoyuguang
*/

public class DefaultOkHttpUrlCleaner implements OkHttpUrlCleaner {

@Override
public String clean(Request request, Connection connection) {
return request.url().toString();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about carrying HTTP method prefix in the resource name? And it could be better if users could add customized prefix here (e.g. okhttp:xxx).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

恩 默认的DefaultOkHttpResourceExtractor 增加了HTTP METHOD+":"的记录,且增加了 config里面的prefix属性 默认""

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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.okhttp.cleaner;

import okhttp3.Connection;
import okhttp3.Request;

/**
* @author zhaoyuguang
*/

public interface OkHttpUrlCleaner {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about OkHttpResourceExtractor or other better?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,class name和method名称修改了


String clean(Request request, Connection connection);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.okhttp.config;

import com.alibaba.csp.sentinel.adapter.okhttp.cleaner.DefaultOkHttpUrlCleaner;
import com.alibaba.csp.sentinel.adapter.okhttp.cleaner.OkHttpUrlCleaner;
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 OkHttpUrlCleaner cleaner = new DefaultOkHttpUrlCleaner();
private static volatile OkHttpFallback fallback = new DefaultOkHttpFallback();

public static OkHttpUrlCleaner getCleaner() {
return cleaner;
}

public static void setCleaner(OkHttpUrlCleaner cleaner) {
AssertUtil.notNull(cleaner, "cleaner cannot be null");
SentinelOkHttpConfig.cleaner = cleaner;
}

public static OkHttpFallback getFallback() {
return fallback;
}

public static void setFallback(OkHttpFallback fallback) {
AssertUtil.notNull(fallback, "fallback cannot be null");
SentinelOkHttpConfig.fallback = fallback;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.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);
}
Loading