sentinel-adapter/sentinel-spring-restclient-adapter/README.md
Sentinel Spring RestClient Adapter provides Sentinel integration for Spring Framework 6.0+ RestClient. With this adapter, you can easily add flow control, circuit breaking, and degradation features to HTTP requests made via RestClient.
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-restclient-adapter</artifactId>
<version>${sentinel.version}</version>
</dependency>
import com.alibaba.csp.sentinel.adapter.spring.restclient.SentinelRestClientInterceptor;
import org.springframework.web.client.RestClient;
// Create RestClient with Sentinel interceptor
RestClient restClient = RestClient.builder()
.requestInterceptor(new SentinelRestClientInterceptor())
.build();
// Use RestClient to send requests (protected by Sentinel)
String result = restClient.get()
.uri("https://httpbin.org/get")
.retrieve()
.body(String.class);
import com.alibaba.csp.sentinel.adapter.spring.restclient.SentinelRestClientConfig;
import com.alibaba.csp.sentinel.adapter.spring.restclient.SentinelRestClientInterceptor;
import com.alibaba.csp.sentinel.adapter.spring.restclient.extractor.RestClientResourceExtractor;
import com.alibaba.csp.sentinel.adapter.spring.restclient.fallback.RestClientFallback;
// Custom resource name extractor
RestClientResourceExtractor customExtractor = request -> {
// Example: normalize RESTful path parameters
String path = request.getURI().getPath();
if (path.matches("/users/\\d+")) {
path = "/users/{id}";
}
return request.getMethod() + ":" + request.getURI().getHost() + path;
};
// Custom fallback: throw a custom exception when blocked
RestClientFallback customFallback = (request, body, execution, ex) -> {
throw new RuntimeException("Service temporarily unavailable, please retry later", ex);
};
// Create configuration
SentinelRestClientConfig config = new SentinelRestClientConfig(
"my-restclient:", // Resource name prefix
customExtractor,
customFallback
);
// Create interceptor with custom configuration
RestClient restClient = RestClient.builder()
.requestInterceptor(new SentinelRestClientInterceptor(config))
.build();
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import java.util.Collections;
// Configure flow control rule
FlowRule rule = new FlowRule("restclient:GET:https://httpbin.org/get");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setCount(10); // Max 10 requests per second
rule.setLimitApp("default");
FlowRuleManager.loadRules(Collections.singletonList(rule));
The main interceptor implementation responsible for:
Configuration class containing:
resourcePrefix: Resource name prefix (default: restclient:)resourceExtractor: Resource name extractorfallback: Fallback handlerInterface for resource name extraction, allowing customization of resource name generation logic.
Interface for fallback handling, invoked when requests are blocked by flow control or circuit breaking.
The default resource name format: {prefix}{METHOD}:{URL}
Examples:
restclient:GET:https://httpbin.org/getrestclient:POST:http://localhost:8080/api/usersThis adapter only supports RestClient from Spring Framework 6.0+, not RestTemplate.
This adapter provides basic Sentinel integration. For Spring Cloud Alibaba projects:
spring-cloud-starter-alibaba-sentinel@SentinelRestClient annotation for simplified configurationApache License 2.0