Spring中resilience4j.circuitbreaker.configs与backends的区别及用法问询
Great question — let’s break down these three configuration properties clearly, since backends is a legacy concept that often causes confusion for newer Resilience4j users.
1. resilience4j.circuitbreaker.configs.<baseConfigName>: Reusable Base Configuration Templates
Think of this as a "blueprint" for circuit breaker settings. These are global, reusable configurations that you can apply to multiple circuit breaker instances to avoid duplicating code.
For example, you might define a base config with common defaults:
# Define a shared base config resilience4j.circuitbreaker.configs.default.sliding-window-size=100 resilience4j.circuitbreaker.configs.default.failure-rate-threshold=50 resilience4j.circuitbreaker.configs.default.wait-duration-in-open-state=10s
This default config isn’t a working circuit breaker on its own — it’s just a set of rules that other instances can reference and override as needed.
2. resilience4j.circuitbreaker.instances.<instanceName>: Actual Circuit Breaker Instances
This is the modern, recommended way to define working circuit breaker instances. Each instance maps to a specific service, method, or external dependency you want to protect.
You can either configure an instance from scratch, or reference a base config to reuse settings and override specific values:
# Create an instance that uses the default base config resilience4j.circuitbreaker.instances.paymentService.base-config=default # Override one setting for this specific instance resilience4j.circuitbreaker.instances.paymentService.wait-duration-in-open-state=15s
To use this instance in code, you’d reference its name in annotations or via the registry:
@CircuitBreaker(name = "paymentService") public void processPayment() { // Payment processing logic }
3. resilience4j.circuitbreaker.backends.<backendName>: Legacy Instance Naming
This is an older naming convention for circuit breaker instances, used in Resilience4j versions 0.17.x and earlier. It’s functionally identical to instances — the only difference is the property name. It’s kept for backward compatibility, so existing code doesn’t break when upgrading.
How backends relate to configs
Just like instances, you can link a backend to a base config using the base-config property:
# Legacy way to define a circuit breaker "backend" (same as an instance) resilience4j.circuitbreaker.backends.notificationService.base-config=default resilience4j.circuitbreaker.backends.notificationService.failure-rate-threshold=40
Using backends in code
You reference backends the same way you’d reference instances — by name in annotations or registry calls:
@CircuitBreaker(name = "notificationService") public void sendAlert() { // Notification logic }
Key Takeaways
configs= Reusable, global setting templates (no active circuit breaker on their own)instances= Modern, recommended way to define active circuit breaker instancesbackends= Legacy alias forinstances, maintained for backward compatibility- Both
instancesandbackendscan referenceconfigsviabase-configto reuse and customize settings
内容的提问来源于stack exchange,提问作者Dirk Hoffmann

