Integrate an HTTP client

更新时间:
复制 MD 格式

AHAS traffic protection can be integrated with HTTP clients. To enable traffic protection for your HTTP client, you must first connect your application using the SDK. Then, you can connect the HTTP client calls in your application to the AHAS console. AHAS supports the OkHttp and Apache HttpClient (synchronous mode) frameworks. This topic describes how to integrate these HTTP clients.

Prerequisites

Integrate OkHttp

To integrate OkHttp with AHAS traffic protection, you can register the AHAS Sentinel interceptor when you create an OkHttpClient:

OkHttpClient client = new OkHttpClient.Builder()
        .addInterceptor(new SentinelOkHttpInterceptor(new SentinelOkHttpConfig()))
        .build();
Note

By default, AHAS extracts the HTTP method and URL to create a resource name. The resource name uses the okhttp:${httpMethod}:${url} pattern, such as okhttp:GET:http://localhost:8088/hello.

When you create a SentinelOkHttpInterceptor, you can also provide custom logic for resource name parsing by defining the OkHttpResourceExtractor in SentinelOkHttpConfig. The following code provides an example:

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

By default, a RuntimeException("SentinelBlockException") is thrown when access to a URL is throttled. You can also provide a custom fallback handler when you create a SentinelOkHttpConfig. The following code shows an example of a fallback handler:

public class SomeFallback implements OkHttpFallback {

    @Override
    public Response handle(Request request, Connection connection, BlockException e) {
        // Construct the response for a throttled request here.
        return new Response(myErrorBuilder);
    }
}

Integrate Apache HttpClient

Note

Only the synchronous `CloseableHttpClient` is supported.

To integrate Apache HttpClient with AHAS traffic protection, you can use SentinelApacheHttpClientBuilder to construct the CloseableHttpClient:

HttpClientBuilder builder = new SentinelApacheHttpClientBuilder(config); // Use this builder like a normal builder. You can pass in custom configurations.
CloseableHttpClient httpClient = builder.build();

You can use SentinelApacheHttpClientBuilder in the same way that you use a normal HttpClientBuilder and pass in custom HTTP client configurations. By default, AHAS extracts the HTTP method and URL to create a resource name. The resource name uses the httpclient:${httpMethod}:${url} pattern, such as httpclient:GET:http://localhost:8088/hello.

When you create a SentinelApacheHttpClientBuilder, you can also customize the resource name parsing logic (extractor) in SentinelApacheHttpClientConfig.

By default, a RuntimeException("SentinelBlockException") is thrown when access to a URL is throttled. You can also provide a custom fallback handler when you create a SentinelApacheHttpClientConfig. The following code shows an example of a fallback handler:

public class MyApacheHttpClientFallback implements ApacheHttpClientFallback {

    @Override
    public CloseableHttpResponse handle(HttpRequestWrapper request, BlockException e) {
        // Construct the response for a throttled request here.
    }
}

References

The following documents provide more information about connecting applications to AHAS Application Protection: