seninel部署
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
Eric Zhao fca70646ad Bump version to 1.8.2-SNAPSHOT 3 lat temu
..
src Refactor config mechanism for OkHttp adapter and polish related code 4 lat temu
README.md Refactor config mechanism for OkHttp adapter and polish related code 4 lat temu
pom.xml Bump version to 1.8.2-SNAPSHOT 3 lat temu

README.md

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):

<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:

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}

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:

public class DefaultOkHttpFallback implements OkHttpFallback {

    @Override
    public Response handle(Request request, Connection connection, BlockException e) {
        return new Response(myErrorBuilder);
    }
}