This commit is contained in:
shuhongfan
2023-09-04 16:40:17 +08:00
commit cf5ac25c14
8267 changed files with 1305066 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
# 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(new SentinelOkHttpConfig()))
.build();
```
## Configuration
`SentinelOkHttpConfig` configuration:
| name | description | type | default value |
|------|------------|------|-------|
| resourcePrefix | customized resource name prefix | `String` | `okhttp:` |
| resourceExtractor | customized resource extractor | `OkHttpResourceExtractor` | `DefaultOkHttpResourceExtractor` |
| fallback | handle request when it is blocked | `OkHttpFallback` | `DefaultOkHttpFallback` |
### Resource Extractor
We can define `OkHttpResourceExtractor` to customize the logic of extracting resource name from the HTTP request.
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;
};
```
The pattern of default resource name extractor is `${HTTP_METHOD}:${URL}` (e.g. `GET:/foo`).
### Fallback (Block handling)
We can define `OkHttpFallback` to handle blocked request. For example:
```java
public class DefaultOkHttpFallback implements OkHttpFallback {
@Override
public Response handle(Request request, Connection connection, BlockException e) {
return new Response(myErrorBuilder);
}
}
```

View File

@@ -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.3</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>

View File

@@ -0,0 +1,78 @@
/*
* 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.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
* @author Eric Zhao
*/
public class SentinelOkHttpConfig {
public static final String DEFAULT_RESOURCE_PREFIX = "okhttp:";
private final String resourcePrefix;
private final OkHttpResourceExtractor resourceExtractor;
private final OkHttpFallback fallback;
public SentinelOkHttpConfig() {
this(DEFAULT_RESOURCE_PREFIX);
}
public SentinelOkHttpConfig(String resourcePrefix) {
this(resourcePrefix, new DefaultOkHttpResourceExtractor(), new DefaultOkHttpFallback());
}
public SentinelOkHttpConfig(OkHttpResourceExtractor resourceExtractor, OkHttpFallback fallback) {
this(DEFAULT_RESOURCE_PREFIX, resourceExtractor, fallback);
}
public SentinelOkHttpConfig(String resourcePrefix,
OkHttpResourceExtractor resourceExtractor,
OkHttpFallback fallback) {
AssertUtil.notNull(resourceExtractor, "resourceExtractor cannot be null");
AssertUtil.notNull(fallback, "fallback cannot be null");
this.resourcePrefix = resourcePrefix;
this.resourceExtractor = resourceExtractor;
this.fallback = fallback;
}
public String getResourcePrefix() {
return resourcePrefix;
}
public OkHttpResourceExtractor getResourceExtractor() {
return resourceExtractor;
}
public OkHttpFallback getFallback() {
return fallback;
}
@Override
public String toString() {
return "SentinelOkHttpConfig{" +
"resourcePrefix='" + resourcePrefix + '\'' +
", resourceExtractor=" + resourceExtractor +
", fallback=" + fallback +
'}';
}
}

View File

@@ -0,0 +1,67 @@
/*
* 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.slots.block.BlockException;
import com.alibaba.csp.sentinel.util.AssertUtil;
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 {
private final SentinelOkHttpConfig config;
public SentinelOkHttpInterceptor() {
this.config = new SentinelOkHttpConfig();
}
public SentinelOkHttpInterceptor(SentinelOkHttpConfig config) {
AssertUtil.notNull(config, "config cannot be null");
this.config = config;
}
@Override
public Response intercept(Chain chain) throws IOException {
Entry entry = null;
try {
Request request = chain.request();
String name = config.getResourceExtractor().extract(request, chain.connection());
if (StringUtil.isNotBlank(config.getResourcePrefix())) {
name = config.getResourcePrefix() + name;
}
entry = SphU.entry(name, ResourceTypeConstants.COMMON_WEB, EntryType.OUT);
return chain.proceed(request);
} catch (BlockException e) {
return config.getFallback().handle(chain.request(), chain.connection(), e);
} catch (IOException ex) {
Tracer.traceEntry(ex, entry);
throw ex;
} finally {
if (entry != null) {
entry.exit();
}
}
}
}

View File

@@ -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(Request request, Connection connection) {
return request.method() + ":" + request.url().toString();
}
}

View File

@@ -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.extractor;
import okhttp3.Connection;
import okhttp3.Request;
/**
* @author zhaoyuguang
*/
public interface OkHttpResourceExtractor {
/**
* Extracts the resource name from the HTTP request.
*
* @param request HTTP request entity
* @param connection HTTP connection
* @return the resource name of current request
*/
String extract(Request request, Connection connection);
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -0,0 +1,98 @@
/*
* 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.Constants;
import com.alibaba.csp.sentinel.adapter.okhttp.app.TestApplication;
import com.alibaba.csp.sentinel.adapter.okhttp.extractor.OkHttpResourceExtractor;
import com.alibaba.csp.sentinel.adapter.okhttp.fallback.DefaultOkHttpFallback;
import com.alibaba.csp.sentinel.node.ClusterNode;
import com.alibaba.csp.sentinel.slots.clusterbuilder.ClusterBuilderSlot;
import okhttp3.Connection;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertNotNull;
/**
* @author zhaoyuguang
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
properties = {
"server.port=8086"
})
public class SentinelOkHttpInterceptorTest {
@Value("${server.port}")
private Integer port;
@Test
public void testSentinelOkHttpInterceptor0() throws Exception {
// With prefix
SentinelOkHttpConfig config = new SentinelOkHttpConfig("okhttp:");
String url0 = "http://localhost:" + port + "/okhttp/back";
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new SentinelOkHttpInterceptor(config))
.build();
Request request = new Request.Builder()
.url(url0)
.build();
System.out.println(client.newCall(request).execute().body().string());
ClusterNode cn = ClusterBuilderSlot.getClusterNode(config.getResourcePrefix() + "GET:" + url0);
assertNotNull(cn);
Constants.ROOT.removeChildList();
ClusterBuilderSlot.getClusterNodeMap().clear();
}
@Test
public void testSentinelOkHttpInterceptor1() throws Exception {
String url0 = "http://localhost:" + port + "/okhttp/back/1";
SentinelOkHttpConfig config = new SentinelOkHttpConfig(new OkHttpResourceExtractor() {
@Override
public String extract(Request request, Connection connection) {
String regex = "/okhttp/back/";
String url = request.url().toString();
if (url.contains(regex)) {
url = url.substring(0, url.indexOf(regex) + regex.length()) + "{id}";
}
return request.method() + ":" + url;
}
}, new DefaultOkHttpFallback());
OkHttpClient client = new OkHttpClient.Builder()
.addInterceptor(new SentinelOkHttpInterceptor(config))
.build();
Request request = new Request.Builder()
.url(url0)
.build();
System.out.println(client.newCall(request).execute().body().string());
String url1 = config.getResourcePrefix() + "GET:http://localhost:" + port + "/okhttp/back/{id}";
ClusterNode cn = ClusterBuilderSlot.getClusterNode(url1);
assertNotNull(cn);
Constants.ROOT.removeChildList();
ClusterBuilderSlot.getClusterNodeMap().clear();
}
}

View File

@@ -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.app;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author zhaoyuguang
*/
@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
SpringApplication.run(TestApplication.class);
}
}

View File

@@ -0,0 +1,37 @@
/*
* 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.app.controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zhaoyuguang
*/
@RestController
public class TestController {
@RequestMapping("/okhttp/back")
public String back() {
return "Welcome Back!";
}
@RequestMapping("/okhttp/back/{id}")
public String back(@PathVariable String id) {
return "Welcome Back! " + id;
}
}

View File

@@ -0,0 +1,38 @@
/*
* 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.SentinelOkHttpConfig;
import com.alibaba.csp.sentinel.adapter.okhttp.extractor.DefaultOkHttpResourceExtractor;
import com.alibaba.csp.sentinel.adapter.okhttp.fallback.DefaultOkHttpFallback;
import org.junit.Test;
/**
* @author zhaoyuguang
*/
public class SentinelOkHttpConfigTest {
@Test(expected = IllegalArgumentException.class)
public void testConfigSetCleaner() {
SentinelOkHttpConfig config = new SentinelOkHttpConfig(null, new DefaultOkHttpFallback());
}
@Test(expected = IllegalArgumentException.class)
public void testConfigSetFallback() {
SentinelOkHttpConfig config = new SentinelOkHttpConfig(new DefaultOkHttpResourceExtractor(), null);
}
}

View File

@@ -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.extractor;
import okhttp3.Connection;
import okhttp3.Request;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* @author zhaoyuguang
*/
public class OkHttpResourceExtractorTest {
@Test
public void testDefaultOkHttpResourceExtractor() {
OkHttpResourceExtractor extractor = new DefaultOkHttpResourceExtractor();
String url = "http://localhost:8083/okhttp/back";
Request request = new Request.Builder()
.url(url)
.build();
String resource = extractor.extract(request, null);
assertEquals("GET:" + url, resource);
}
@Test
public void testCustomizeOkHttpUrlCleaner() {
OkHttpResourceExtractor extractor = new OkHttpResourceExtractor() {
@Override
public String extract(Request request, Connection connection) {
String regex = "/okhttp/back/";
String url = request.url().toString();
if (url.contains(regex)) {
url = url.substring(0, url.indexOf(regex) + regex.length()) + "{id}";
}
return request.method() + ":" + url;
}
};
String url = "http://localhost:8083/okhttp/back/abc";
Request request = new Request.Builder()
.url(url)
.build();
assertEquals("GET:http://localhost:8083/okhttp/back/{id}", extractor.extract(request, null));
}
}

View File

@@ -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 com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import org.junit.Test;
/**
* @author zhaoyuguang
*/
public class OkHttpFallbackTest {
@Test(expected = SentinelRpcException.class)
public void testDefaultOkHttpFallback() {
BlockException e = new FlowException("xxx");
OkHttpFallback fallback = new DefaultOkHttpFallback();
fallback.handle(null, null, e);
}
}