diff --git a/libraries.gradle b/libraries.gradle index e506a8b722..efff680a66 100755 --- a/libraries.gradle +++ b/libraries.gradle @@ -27,6 +27,7 @@ ext { micrometerVersion = '1.1.4' hibernateValidatorVersion = '6.0.16.Final' wiremockVersion = '2.22.0' + validationApiVersion = '2.0.1.Final' kotlinCoroutinesVersion = '1.2.0' libraries = [ @@ -114,6 +115,9 @@ ext { // Groovy groovy: "org.codehaus.groovy:groovy-all:2.5.6", + + // validation + validationApi: "javax.validation:validation-api:${validationApiVersion}", // Kotlin addon kotlin_stdlib: "org.jetbrains.kotlin:kotlin-stdlib-jdk8", diff --git a/resilience4j-core/src/main/java/io/github/resilience4j/core/ClassUtils.java b/resilience4j-core/src/main/java/io/github/resilience4j/core/ClassUtils.java new file mode 100644 index 0000000000..c4a6e40e45 --- /dev/null +++ b/resilience4j-core/src/main/java/io/github/resilience4j/core/ClassUtils.java @@ -0,0 +1,24 @@ +package io.github.resilience4j.core; + +import io.github.resilience4j.core.lang.Nullable; + +import java.lang.reflect.Constructor; +import java.util.function.Predicate; + +public class ClassUtils { + + @Nullable + @SuppressWarnings("unchecked") + public static Predicate instantiatePredicateClass(Class> clazz) { + try { + Constructor c = clazz.getConstructor(); + if (c != null) { + return c.newInstance(); + } else { + return null; + } + } catch (Exception e) { + throw new InstantiationException("Unable to create instance of class: " + clazz.getName(), e); + } + } +} diff --git a/resilience4j-core/src/main/java/io/github/resilience4j/core/InstantiationException.java b/resilience4j-core/src/main/java/io/github/resilience4j/core/InstantiationException.java new file mode 100644 index 0000000000..942e867b1e --- /dev/null +++ b/resilience4j-core/src/main/java/io/github/resilience4j/core/InstantiationException.java @@ -0,0 +1,23 @@ +package io.github.resilience4j.core; + +public class InstantiationException extends RuntimeException { + + public InstantiationException() { + } + + public InstantiationException(String message) { + super(message); + } + + public InstantiationException(String message, Throwable cause) { + super(message, cause); + } + + public InstantiationException(Throwable cause) { + super(cause); + } + + public InstantiationException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } +} diff --git a/resilience4j-core/src/main/java/io/github/resilience4j/core/StringUtils.java b/resilience4j-core/src/main/java/io/github/resilience4j/core/StringUtils.java new file mode 100644 index 0000000000..f8968fd92d --- /dev/null +++ b/resilience4j-core/src/main/java/io/github/resilience4j/core/StringUtils.java @@ -0,0 +1,11 @@ +package io.github.resilience4j.core; + +import io.github.resilience4j.core.lang.Nullable; + +public class StringUtils { + + public static boolean isNotEmpty(@Nullable String string) { + return string != null && !string.isEmpty(); + } + +} diff --git a/resilience4j-framework-common/build.gradle b/resilience4j-framework-common/build.gradle index fae14dec18..d1c860d43c 100644 --- a/resilience4j-framework-common/build.gradle +++ b/resilience4j-framework-common/build.gradle @@ -4,6 +4,9 @@ dependencies { compile project(':resilience4j-ratelimiter') compile project(':resilience4j-retry') compile project(':resilience4j-bulkhead') + + compile libraries.validationApi + compile libraries.hibernate_validator } ext.moduleName='io.github.resilience4j.framework-common' \ No newline at end of file diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/configuration/BulkheadConfigurationProperties.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/configuration/BulkheadConfigurationProperties.java new file mode 100644 index 0000000000..227fb5ce03 --- /dev/null +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/configuration/BulkheadConfigurationProperties.java @@ -0,0 +1,130 @@ +/* + * Copyright 2019 Dan Maas + * + * 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 io.github.resilience4j.common.bulkhead.configuration; + +import io.github.resilience4j.core.ConfigurationNotFoundException; +import io.github.resilience4j.core.StringUtils; +import io.github.resilience4j.core.lang.Nullable; + +import javax.validation.constraints.Min; +import java.util.HashMap; +import java.util.Map; + +public class BulkheadConfigurationProperties { + + private Map instances = new HashMap<>(); + private Map configs = new HashMap<>(); + + public io.github.resilience4j.bulkhead.BulkheadConfig createBulkheadConfig(InstanceProperties instanceProperties) { + if (StringUtils.isNotEmpty(instanceProperties.getBaseConfig())) { + InstanceProperties baseProperties = configs.get(instanceProperties.getBaseConfig()); + if (baseProperties == null) { + throw new ConfigurationNotFoundException(instanceProperties.getBaseConfig()); + } + return buildConfigFromBaseConfig(baseProperties, instanceProperties); + } + return buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.custom(), instanceProperties); + } + + private io.github.resilience4j.bulkhead.BulkheadConfig buildConfigFromBaseConfig(InstanceProperties baseProperties, InstanceProperties instanceProperties) { + io.github.resilience4j.bulkhead.BulkheadConfig baseConfig = buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.custom(), baseProperties); + return buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.from(baseConfig), instanceProperties); + } + + private io.github.resilience4j.bulkhead.BulkheadConfig buildBulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig.Builder builder, InstanceProperties instanceProperties) { + if (instanceProperties.getMaxConcurrentCalls() != null) { + builder.maxConcurrentCalls(instanceProperties.getMaxConcurrentCalls()); + } + if (instanceProperties.getMaxWaitTime() != null) { + builder.maxWaitTime(instanceProperties.getMaxWaitTime()); + } + return builder.build(); + } + + @Nullable + public InstanceProperties getBackendProperties(String backend) { + return instances.get(backend); + } + + public Map getInstances() { + return instances; + } + + /** + * For backwards compatibility when setting backends in configuration properties. + */ + public Map getBackends() { + return instances; + } + + public Map getConfigs() { + return configs; + } + + /** + * Bulkhead config adapter for integration with Ratpack. {@link #maxWaitTime} should + * almost always be set to 0, so the compute threads would not be blocked upon execution. + */ + public static class InstanceProperties { + + @Min(1) + private Integer maxConcurrentCalls; + @Min(0) + private Long maxWaitTime; + @Nullable + private String baseConfig; + @Min(1) + private Integer eventConsumerBufferSize = 100; + + public InstanceProperties setMaxConcurrentCalls(Integer maxConcurrentCalls) { + this.maxConcurrentCalls = maxConcurrentCalls; + return this; + } + + public InstanceProperties setMaxWaitTime(Long maxWaitTime) { + this.maxWaitTime = maxWaitTime; + return this; + } + + public InstanceProperties setBaseConfig(String baseConfig) { + this.baseConfig = baseConfig; + return this; + } + + public InstanceProperties eventConsumerBufferSize(Integer eventConsumerBufferSize) { + this.eventConsumerBufferSize = eventConsumerBufferSize; + return this; + } + + public Integer getMaxConcurrentCalls() { + return maxConcurrentCalls; + } + + public Long getMaxWaitTime() { + return maxWaitTime; + } + + public String getBaseConfig() { + return baseConfig; + } + + public Integer getEventConsumerBufferSize() { + return eventConsumerBufferSize; + } + + } + +} diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEndpointResponse.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEndpointResponse.java similarity index 93% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEndpointResponse.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEndpointResponse.java index aaace28d8a..fea24ac156 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEndpointResponse.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEndpointResponse.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.bulkhead.monitoring.endpoint; +package io.github.resilience4j.common.bulkhead.monitoring.endpoint; import java.util.List; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventDTO.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEventDTO.java similarity index 95% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventDTO.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEventDTO.java index c13ef2218e..874094c16f 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventDTO.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEventDTO.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.bulkhead.monitoring.endpoint; +package io.github.resilience4j.common.bulkhead.monitoring.endpoint; import io.github.resilience4j.bulkhead.event.BulkheadEvent; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventDTOFactory.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEventDTOFactory.java similarity index 94% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventDTOFactory.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEventDTOFactory.java index 12ae5efa96..20e67b9d33 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventDTOFactory.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEventDTOFactory.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.bulkhead.monitoring.endpoint; +package io.github.resilience4j.common.bulkhead.monitoring.endpoint; import io.github.resilience4j.bulkhead.event.BulkheadEvent; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventsEndpointResponse.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEventsEndpointResponse.java similarity index 94% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventsEndpointResponse.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEventsEndpointResponse.java index 6b99dc917b..97e301948c 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventsEndpointResponse.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/BulkheadEventsEndpointResponse.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.bulkhead.monitoring.endpoint; +package io.github.resilience4j.common.bulkhead.monitoring.endpoint; import java.util.List; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/package-info.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/package-info.java similarity index 91% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/package-info.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/package-info.java index 7b392591f2..78cdaf9c3e 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/package-info.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/bulkhead/monitoring/endpoint/package-info.java @@ -18,7 +18,7 @@ */ @NonNullApi @NonNullFields -package io.github.resilience4j.circuitbreaker.monitoring.endpoint; +package io.github.resilience4j.common.bulkhead.monitoring.endpoint; import io.github.resilience4j.core.lang.NonNullApi; import io.github.resilience4j.core.lang.NonNullFields; \ No newline at end of file diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/configuration/CircuitBreakerConfigurationProperties.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/configuration/CircuitBreakerConfigurationProperties.java new file mode 100644 index 0000000000..187fd5fbe6 --- /dev/null +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/configuration/CircuitBreakerConfigurationProperties.java @@ -0,0 +1,342 @@ +/* + * Copyright 2019 Dan Maas + * + * 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 io.github.resilience4j.common.circuitbreaker.configuration; + + +import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig; +import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.Builder; +import io.github.resilience4j.core.ClassUtils; +import io.github.resilience4j.core.ConfigurationNotFoundException; +import io.github.resilience4j.core.StringUtils; +import io.github.resilience4j.core.lang.Nullable; +import org.hibernate.validator.constraints.time.DurationMin; + +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.function.Predicate; + +public class CircuitBreakerConfigurationProperties { + + private Map instances = new HashMap<>(); + private Map configs = new HashMap<>(); + + public Optional findCircuitBreakerProperties(String name) { + return Optional.ofNullable(instances.get(name)); + } + + public CircuitBreakerConfig createCircuitBreakerConfig(InstanceProperties instanceProperties) { + if (StringUtils.isNotEmpty(instanceProperties.getBaseConfig())) { + InstanceProperties baseProperties = configs.get(instanceProperties.getBaseConfig()); + if(baseProperties == null){ + throw new ConfigurationNotFoundException(instanceProperties.getBaseConfig()); + } + return buildConfigFromBaseConfig(instanceProperties, baseProperties); + } + return buildConfig(CircuitBreakerConfig.custom(), instanceProperties); + } + + private CircuitBreakerConfig buildConfigFromBaseConfig(InstanceProperties instanceProperties, InstanceProperties baseProperties) { + CircuitBreakerConfig baseConfig = buildConfig(CircuitBreakerConfig.custom(), baseProperties); + return buildConfig(CircuitBreakerConfig.from(baseConfig), instanceProperties); + } + + private CircuitBreakerConfig buildConfig(Builder builder, InstanceProperties properties) { + + if (properties.getWaitDurationInOpenStateMillis() != null) { + builder.waitDurationInOpenState(Duration.ofMillis(properties.getWaitDurationInOpenStateMillis())); + } + + if (properties.getFailureRateThreshold() != null) { + builder.failureRateThreshold(properties.getFailureRateThreshold()); + } + + if (properties.getRingBufferSizeInClosedState() != null) { + builder.ringBufferSizeInClosedState(properties.getRingBufferSizeInClosedState()); + } + + if (properties.getRingBufferSizeInHalfOpenState() != null) { + builder.ringBufferSizeInHalfOpenState(properties.getRingBufferSizeInHalfOpenState()); + } + + if (properties.recordFailurePredicate != null) { + buildRecordFailurePredicate(properties, builder); + } + + if (properties.recordExceptions != null) { + builder.recordExceptions(properties.recordExceptions); + } + + if (properties.ignoreExceptions != null) { + builder.ignoreExceptions(properties.ignoreExceptions); + } + + if (properties.automaticTransitionFromOpenToHalfOpenEnabled != null) { + builder.automaticTransitionFromOpenToHalfOpenEnabled(properties.automaticTransitionFromOpenToHalfOpenEnabled); + } + + return builder.build(); + } + + private void buildRecordFailurePredicate(InstanceProperties properties, Builder builder) { + if (properties.getRecordFailurePredicate() != null) { + Predicate predicate = ClassUtils.instantiatePredicateClass(properties.getRecordFailurePredicate()); + if (predicate != null) { + builder.recordFailure(predicate); + } + } + } + + @Nullable + public InstanceProperties getBackendProperties(String backend) { + return instances.get(backend); + } + + public Map getInstances() { + return instances; + } + + /** + * For backwards compatibility when setting backends in configuration properties. + */ + public Map getBackends() { + return instances; + } + + public Map getConfigs() { + return configs; + } + + /** + * Class storing property values for configuring {@link io.github.resilience4j.circuitbreaker.CircuitBreaker} instances. + */ + public static class InstanceProperties { + + @DurationMin(seconds = 1) + @Nullable + private Integer waitDurationInOpenStateMillis; + + @Min(1) + @Max(100) + @Nullable + private Integer failureRateThreshold; + + @Min(1) + @Nullable + private Integer ringBufferSizeInClosedState; + + @Min(1) + @Nullable + private Integer ringBufferSizeInHalfOpenState; + + @Nullable + private Boolean automaticTransitionFromOpenToHalfOpenEnabled; + + @Min(1) + private Integer eventConsumerBufferSize = 100; + + @NotNull + private Boolean registerHealthIndicator = true; + + @Nullable + private Class> recordFailurePredicate; + + @Nullable + private Class[] recordExceptions; + + @Nullable + private Class[] ignoreExceptions; + + @Nullable + private String baseConfig; + + + /** + * Returns the failure rate threshold for the circuit breaker as percentage. + * + * @return the failure rate threshold + */ + @Nullable + public Integer getFailureRateThreshold() { + return failureRateThreshold; + } + + /** + * Sets the failure rate threshold for the circuit breaker as percentage. + * + * @param failureRateThreshold the failure rate threshold + */ + public InstanceProperties setFailureRateThreshold(Integer failureRateThreshold) { + this.failureRateThreshold = failureRateThreshold; + return this; + } + + /** + * Returns the wait duration the CircuitBreaker will stay open, before it switches to half closed. + * + * @return the wait duration + */ + @Nullable + public Integer getWaitDurationInOpenStateMillis() { + return waitDurationInOpenStateMillis; + } + + /** + * Sets the wait duration the CircuitBreaker should stay open, before it switches to half closed. + * + * @param waitDurationInOpenStateMillis the wait duration + */ + public InstanceProperties setWaitDurationInOpenStateMillis(Integer waitDurationInOpenStateMillis) { + this.waitDurationInOpenStateMillis = waitDurationInOpenStateMillis; + return this; + } + + /** + * Returns the ring buffer size for the circuit breaker while in closed state. + * + * @return the ring buffer size + */ + @Nullable + public Integer getRingBufferSizeInClosedState() { + return ringBufferSizeInClosedState; + } + + /** + * Sets the ring buffer size for the circuit breaker while in closed state. + * + * @param ringBufferSizeInClosedState the ring buffer size + */ + public InstanceProperties setRingBufferSizeInClosedState(Integer ringBufferSizeInClosedState) { + this.ringBufferSizeInClosedState = ringBufferSizeInClosedState; + return this; + } + + /** + * Returns the ring buffer size for the circuit breaker while in half open state. + * + * @return the ring buffer size + */ + @Nullable + public Integer getRingBufferSizeInHalfOpenState() { + return ringBufferSizeInHalfOpenState; + } + + /** + * Sets the ring buffer size for the circuit breaker while in half open state. + * + * @param ringBufferSizeInHalfOpenState the ring buffer size + */ + public InstanceProperties setRingBufferSizeInHalfOpenState(Integer ringBufferSizeInHalfOpenState) { + this.ringBufferSizeInHalfOpenState = ringBufferSizeInHalfOpenState; + return this; + } + + /** + * Returns if we should automaticly transition to half open after the timer has run out. + * + * @return setAutomaticTransitionFromOpenToHalfOpenEnabled if we should automaticly go to half open or not + */ + public Boolean getAutomaticTransitionFromOpenToHalfOpenEnabled() { + return this.automaticTransitionFromOpenToHalfOpenEnabled; + } + + /** + * Sets if we should automatically transition to half open after the timer has run out. + * + * @param automaticTransitionFromOpenToHalfOpenEnabled The flag for automatic transition to half open after the timer has run out. + */ + public InstanceProperties setAutomaticTransitionFromOpenToHalfOpenEnabled(Boolean automaticTransitionFromOpenToHalfOpenEnabled) { + this.automaticTransitionFromOpenToHalfOpenEnabled = automaticTransitionFromOpenToHalfOpenEnabled; + return this; + } + + public Integer getEventConsumerBufferSize() { + return eventConsumerBufferSize; + } + + public InstanceProperties setEventConsumerBufferSize(Integer eventConsumerBufferSize) { + this.eventConsumerBufferSize = eventConsumerBufferSize; + return this; + } + + public Boolean getRegisterHealthIndicator() { + return registerHealthIndicator; + } + + public InstanceProperties setRegisterHealthIndicator(Boolean registerHealthIndicator) { + this.registerHealthIndicator = registerHealthIndicator; + return this; + } + + @Nullable + public Class> getRecordFailurePredicate() { + return recordFailurePredicate; + } + + public InstanceProperties setRecordFailurePredicate(Class> recordFailurePredicate) { + this.recordFailurePredicate = recordFailurePredicate; + return this; + } + + @Nullable + public Class[] getRecordExceptions() { + return recordExceptions; + } + + public InstanceProperties setRecordExceptions(Class[] recordExceptions) { + this.recordExceptions = recordExceptions; + return this; + } + + @Nullable + public Class[] getIgnoreExceptions() { + return ignoreExceptions; + } + + public InstanceProperties setIgnoreExceptions(Class[] ignoreExceptions) { + this.ignoreExceptions = ignoreExceptions; + return this; + } + + /** + * Gets the shared configuration name. If this is set, the configuration builder will use the the shared + * configuration backend over this one. + * + * @return The shared configuration name. + */ + @Nullable + public String getBaseConfig() { + return baseConfig; + } + + /** + * Sets the shared configuration name. If this is set, the configuration builder will use the the shared + * configuration backend over this one. + * + * @param baseConfig The shared configuration name. + */ + public InstanceProperties setBaseConfig(String baseConfig) { + this.baseConfig = baseConfig; + return this; + } + + } + +} diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpointResponse.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpointResponse.java similarity index 88% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpointResponse.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpointResponse.java index 53fa2d69f2..5d08dae5d4 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpointResponse.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpointResponse.java @@ -1,4 +1,4 @@ -package io.github.resilience4j.circuitbreaker.monitoring.endpoint; +package io.github.resilience4j.common.circuitbreaker.monitoring.endpoint; import io.github.resilience4j.core.lang.Nullable; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTO.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTO.java similarity index 97% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTO.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTO.java index f593c3c8c6..9619ab86e8 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTO.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTO.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.circuitbreaker.monitoring.endpoint; +package io.github.resilience4j.common.circuitbreaker.monitoring.endpoint; import io.github.resilience4j.circuitbreaker.CircuitBreaker; import io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOBuilder.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOBuilder.java similarity index 96% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOBuilder.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOBuilder.java index 571ab0b10e..e6ae21f887 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOBuilder.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOBuilder.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.circuitbreaker.monitoring.endpoint; +package io.github.resilience4j.common.circuitbreaker.monitoring.endpoint; import io.github.resilience4j.circuitbreaker.CircuitBreaker; import io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOFactory.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOFactory.java similarity index 97% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOFactory.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOFactory.java index 6fb68bae8b..f5145a6995 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOFactory.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOFactory.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.circuitbreaker.monitoring.endpoint; +package io.github.resilience4j.common.circuitbreaker.monitoring.endpoint; import io.github.resilience4j.circuitbreaker.event.*; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpointResponse.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpointResponse.java similarity index 90% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpointResponse.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpointResponse.java index 7fbb3cd7fd..1dcb878204 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpointResponse.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpointResponse.java @@ -1,4 +1,4 @@ -package io.github.resilience4j.circuitbreaker.monitoring.endpoint; +package io.github.resilience4j.common.circuitbreaker.monitoring.endpoint; import io.github.resilience4j.core.lang.Nullable; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/package-info.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/package-info.java similarity index 91% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/package-info.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/package-info.java index a19c079e5b..9e0e6e6db3 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/package-info.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/circuitbreaker/monitoring/endpoint/package-info.java @@ -18,7 +18,7 @@ */ @NonNullApi @NonNullFields -package io.github.resilience4j.retry.monitoring.endpoint; +package io.github.resilience4j.common.circuitbreaker.monitoring.endpoint; import io.github.resilience4j.core.lang.NonNullApi; import io.github.resilience4j.core.lang.NonNullFields; \ No newline at end of file diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/configuration/RateLimiterConfigurationProperties.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/configuration/RateLimiterConfigurationProperties.java new file mode 100644 index 0000000000..69618429b7 --- /dev/null +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/configuration/RateLimiterConfigurationProperties.java @@ -0,0 +1,237 @@ +/* + * Copyright 2019 Dan Maas + * + * 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 io.github.resilience4j.common.ratelimiter.configuration; + +import io.github.resilience4j.core.ConfigurationNotFoundException; +import io.github.resilience4j.core.StringUtils; +import io.github.resilience4j.core.lang.Nullable; +import io.github.resilience4j.ratelimiter.RateLimiterConfig; + +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; + +public class RateLimiterConfigurationProperties { + + private Map instances = new HashMap<>(); + private Map configs = new HashMap<>(); + + public RateLimiterConfig createRateLimiterConfig(@Nullable InstanceProperties instanceProperties) { + if (instanceProperties == null) { + return RateLimiterConfig.ofDefaults(); + } + if (StringUtils.isNotEmpty(instanceProperties.getBaseConfig())) { + InstanceProperties baseProperties = configs.get(instanceProperties.baseConfig); + if (baseProperties == null) { + throw new ConfigurationNotFoundException(instanceProperties.getBaseConfig()); + } + return buildConfigFromBaseConfig(baseProperties, instanceProperties); + } + return buildRateLimiterConfig(RateLimiterConfig.custom(), instanceProperties); + } + + private RateLimiterConfig buildConfigFromBaseConfig(InstanceProperties baseProperties, InstanceProperties instanceProperties) { + RateLimiterConfig baseConfig = buildRateLimiterConfig(RateLimiterConfig.custom(), baseProperties); + return buildRateLimiterConfig(RateLimiterConfig.from(baseConfig), instanceProperties); + } + + private RateLimiterConfig buildRateLimiterConfig(RateLimiterConfig.Builder builder, @Nullable InstanceProperties instanceProperties) { + if (instanceProperties == null) { + return RateLimiterConfig.ofDefaults(); + } + + if (instanceProperties.getLimitForPeriod() != null) { + builder.limitForPeriod(instanceProperties.getLimitForPeriod()); + } + + if (instanceProperties.getLimitRefreshPeriodInNanos() != null) { + builder.limitRefreshPeriod(Duration.ofNanos(instanceProperties.getLimitRefreshPeriodInNanos())); + } + + if (instanceProperties.getTimeoutInMillis() != null) { + builder.timeoutDuration(Duration.ofMillis(instanceProperties.getTimeoutInMillis())); + } + + return builder.build(); + } + + private InstanceProperties getLimiterProperties(String limiter) { + return instances.get(limiter); + } + + public RateLimiterConfig createRateLimiterConfig(String limiter) { + return createRateLimiterConfig(getLimiterProperties(limiter)); + } + + @Nullable + public InstanceProperties getInstanceProperties(String instance) { + return instances.get(instance); + } + + public Map getInstances() { + return instances; + } + + /** + * For backwards compatibility when setting limiters in configuration properties. + */ + public Map getLimiters() { + return instances; + } + + public Map getConfigs() { + return configs; + } + + /** + * Class storing property values for configuring {@link RateLimiterConfig} instances. + */ + public static class InstanceProperties { + + private Integer limitForPeriod; + private Integer limitRefreshPeriodInNanos; + private Integer timeoutInMillis; + private Boolean subscribeForEvents = false; + private Boolean registerHealthIndicator = false; + private Integer eventConsumerBufferSize = 100; + @Nullable + private String baseConfig; + + /** + * Configures the permissions limit for refresh period. + * Count of permissions available during one rate limiter period + * specified by {@link RateLimiterConfig#getLimitRefreshPeriod()} value. + * Default value is 50. + * + * @return the permissions limit for refresh period + */ + @Nullable + public Integer getLimitForPeriod() { + return limitForPeriod; + } + + /** + * Configures the permissions limit for refresh period. + * Count of permissions available during one rate limiter period + * specified by {@link RateLimiterConfig#getLimitRefreshPeriod()} value. + * Default value is 50. + * + * @param limitForPeriod the permissions limit for refresh period + */ + public InstanceProperties setLimitForPeriod(Integer limitForPeriod) { + this.limitForPeriod = limitForPeriod; + return this; + } + + /** + * Configures the period of limit refresh. + * After each period rate limiter sets its permissions + * count to {@link RateLimiterConfig#getLimitForPeriod()} value. + * Default value is 500 nanoseconds. + * + * @return the period of limit refresh + */ + @Nullable + public Integer getLimitRefreshPeriodInNanos() { + return limitRefreshPeriodInNanos; + } + + /** + * Configures the period of limit refresh. + * After each period rate limiter sets its permissions + * count to {@link RateLimiterConfig#getLimitForPeriod()} value. + * Default value is 500 nanoseconds. + * + * @param limitRefreshPeriodInNanos the period of limit refresh + */ + public InstanceProperties setLimitRefreshPeriodInNanos(Integer limitRefreshPeriodInNanos) { + this.limitRefreshPeriodInNanos = limitRefreshPeriodInNanos; + return this; + } + + /** + * Configures the default wait for permission duration. + * Default value is 5 seconds. + * + * @return wait for permission duration + */ + @Nullable + public Integer getTimeoutInMillis() { + return timeoutInMillis; + } + + /** + * Configures the default wait for permission duration. + * Default value is 5 seconds. + * + * @param timeoutInMillis wait for permission duration + */ + public InstanceProperties setTimeoutInMillis(Integer timeoutInMillis) { + this.timeoutInMillis = timeoutInMillis; + return this; + } + + public Boolean getSubscribeForEvents() { + return subscribeForEvents; + } + + public InstanceProperties setSubscribeForEvents(Boolean subscribeForEvents) { + this.subscribeForEvents = subscribeForEvents; + return this; + } + + public Integer getEventConsumerBufferSize() { + return eventConsumerBufferSize; + } + + public InstanceProperties setEventConsumerBufferSize(Integer eventConsumerBufferSize) { + this.eventConsumerBufferSize = eventConsumerBufferSize; + return this; + } + + public Boolean getRegisterHealthIndicator() { + return registerHealthIndicator; + } + + public InstanceProperties setRegisterHealthIndicator(Boolean registerHealthIndicator) { + this.registerHealthIndicator = registerHealthIndicator; + return this; + } + + /** + * Gets the shared configuration name. If this is set, the configuration builder will use the the shared + * configuration instance over this one. + * + * @return The shared configuration name. + */ + @Nullable + public String getBaseConfig() { + return baseConfig; + } + + /** + * Sets the shared configuration name. If this is set, the configuration builder will use the the shared + * configuration instance over this one. + * + * @param baseConfig The shared configuration name. + */ + public InstanceProperties setBaseConfig(String baseConfig) { + this.baseConfig = baseConfig; + return this; + } + } + +} diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEndpointResponse.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/RateLimiterEndpointResponse.java similarity index 94% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEndpointResponse.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/RateLimiterEndpointResponse.java index 1d9ae0edc1..d073ac9ab8 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEndpointResponse.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/RateLimiterEndpointResponse.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.ratelimiter.monitoring.endpoint; +package io.github.resilience4j.common.ratelimiter.monitoring.endpoint; import io.github.resilience4j.core.lang.Nullable; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventDTO.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/RateLimiterEventDTO.java similarity index 96% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventDTO.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/RateLimiterEventDTO.java index e2cf2738f0..df63a876d9 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventDTO.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/RateLimiterEventDTO.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.ratelimiter.monitoring.endpoint; +package io.github.resilience4j.common.ratelimiter.monitoring.endpoint; import io.github.resilience4j.core.lang.Nullable; import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpointResponse.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpointResponse.java similarity index 94% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpointResponse.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpointResponse.java index 29f7fdcce8..b0475610c3 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpointResponse.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpointResponse.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.ratelimiter.monitoring.endpoint; +package io.github.resilience4j.common.ratelimiter.monitoring.endpoint; import io.github.resilience4j.core.lang.Nullable; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/package-info.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/package-info.java similarity index 91% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/package-info.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/package-info.java index 09727a6a67..c1806dacd6 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/package-info.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/ratelimiter/monitoring/endpoint/package-info.java @@ -18,7 +18,7 @@ */ @NonNullApi @NonNullFields -package io.github.resilience4j.ratelimiter.monitoring.endpoint; +package io.github.resilience4j.common.ratelimiter.monitoring.endpoint; import io.github.resilience4j.core.lang.NonNullApi; import io.github.resilience4j.core.lang.NonNullFields; \ No newline at end of file diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/configuration/RetryConfigurationProperties.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/configuration/RetryConfigurationProperties.java new file mode 100644 index 0000000000..9d8acd6e53 --- /dev/null +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/configuration/RetryConfigurationProperties.java @@ -0,0 +1,372 @@ +package io.github.resilience4j.common.retry.configuration; +/* + * Copyright 2019 Dan Maas + * + * 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. + */ + +import io.github.resilience4j.core.ClassUtils; +import io.github.resilience4j.core.ConfigurationNotFoundException; +import io.github.resilience4j.core.StringUtils; +import io.github.resilience4j.core.lang.Nullable; +import io.github.resilience4j.retry.IntervalFunction; +import io.github.resilience4j.retry.RetryConfig; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; +import java.time.Duration; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Predicate; + +/** + * Main spring properties for retry configuration + */ +public class RetryConfigurationProperties { + + private final Map instances = new HashMap<>(); + private Map configs = new HashMap<>(); + + /** + * @param backend backend name + * @return the retry configuration + */ + public RetryConfig createRetryConfig(String backend) { + return createRetryConfig(getBackendProperties(backend)); + } + + /** + * @param backend retry backend name + * @return the configured spring backend properties + */ + @Nullable + public InstanceProperties getBackendProperties(String backend) { + return instances.get(backend); + } + + /** + * @return the configured retry backend properties + */ + public Map getInstances() { + return instances; + } + + /** + * For backwards compatibility when setting backends in configuration properties. + */ + public Map getBackends() { + return instances; + } + + /** + * @return common configuration for retry backend + */ + public Map getConfigs() { + return configs; + } + + /** + * @param instanceProperties the retry backend spring properties + * @return the retry configuration + */ + public RetryConfig createRetryConfig(InstanceProperties instanceProperties) { + if (StringUtils.isNotEmpty(instanceProperties.getBaseConfig())) { + InstanceProperties baseProperties = configs.get(instanceProperties.getBaseConfig()); + if (baseProperties == null) { + throw new ConfigurationNotFoundException(instanceProperties.getBaseConfig()); + } + return buildConfigFromBaseConfig(baseProperties, instanceProperties); + } + return buildRetryConfig(RetryConfig.custom(), instanceProperties); + } + + private RetryConfig buildConfigFromBaseConfig(InstanceProperties baseProperties, InstanceProperties instanceProperties) { + RetryConfig baseConfig = buildRetryConfig(RetryConfig.custom(), baseProperties); + return buildRetryConfig(RetryConfig.from(baseConfig), instanceProperties); + } + + /** + * @param properties the configured spring backend properties + * @return retry config builder instance + */ + @SuppressWarnings("unchecked") + private RetryConfig buildRetryConfig(RetryConfig.Builder builder, InstanceProperties properties) { + if (properties == null) { + return builder.build(); + } + + if (properties.enableExponentialBackoff && properties.enableRandomizedWait) { + throw new IllegalStateException("you can not enable Exponential backoff policy and randomized delay at the same time , please enable only one of them"); + } + + configureRetryIntervalFunction(properties, builder); + + if (properties.getMaxRetryAttempts() != null && properties.getMaxRetryAttempts() != 0) { + builder.maxAttempts(properties.getMaxRetryAttempts()); + } + + if (properties.getRetryExceptionPredicate() != null) { + if (properties.getRetryExceptionPredicate() != null) { + Predicate predicate = ClassUtils.instantiatePredicateClass(properties.getRetryExceptionPredicate()); + if (predicate != null) { + builder.retryOnException(predicate); + } + } + } + + if (properties.getIgnoreExceptions() != null) { + builder.ignoreExceptions(properties.getIgnoreExceptions()); + } + + if (properties.getRetryExceptions() != null) { + builder.retryExceptions(properties.getRetryExceptions()); + } + + if (properties.getResultPredicate() != null) { + if (properties.getResultPredicate() != null) { + Predicate predicate = ClassUtils.instantiatePredicateClass(properties.getResultPredicate()); + if (predicate != null) { + builder.retryOnResult(predicate); + } + } + } + + return builder.build(); + } + + /** + * decide which retry delay policy will be configured based into the configured properties + * + * @param properties the backend retry properties + * @param builder the retry config builder + */ + private void configureRetryIntervalFunction(InstanceProperties properties, RetryConfig.Builder builder) { + if (properties.getWaitDurationMillis() != null && properties.getWaitDurationMillis() != 0) { + long waitDuration = properties.getWaitDurationMillis(); + if (properties.getEnableExponentialBackoff()) { + if (properties.getExponentialBackoffMultiplier() != 0) { + builder.intervalFunction(IntervalFunction.ofExponentialBackoff(waitDuration, properties.getExponentialBackoffMultiplier())); + } else { + builder.intervalFunction(IntervalFunction.ofExponentialBackoff(properties.getWaitDurationMillis())); + } + } else if (properties.getEnableRandomizedWait()) { + if (properties.getRandomizedWaitFactor() != 0) { + builder.intervalFunction(IntervalFunction.ofRandomized(waitDuration, properties.getRandomizedWaitFactor())); + } else { + builder.intervalFunction(IntervalFunction.ofRandomized(waitDuration)); + } + } else { + builder.waitDuration(Duration.ofMillis(properties.getWaitDurationMillis())); + } + } + } + + /** + * Class storing property values for configuring {@link io.github.resilience4j.retry.Retry} instances. + */ + public static class InstanceProperties { + /* + * wait long value for the next try + */ + @Min(100) + @Nullable + private Long waitDurationMillis; + /* + * max retry attempts value + */ + @Min(1) + @Nullable + private Integer maxRetryAttempts; + /* + * retry exception predicate class to be used to evaluate the exception to retry or not + */ + @Nullable + private Class> retryExceptionPredicate; + /* + * retry setResultPredicate predicate class to be used to evaluate the result to retry or not + */ + @Nullable + private Class> resultPredicate; + /* + * list of retry exception classes + */ + @SuppressWarnings("unchecked") + @Nullable + private Class[] retryExceptions; + /* + * list of retry ignored exception classes + */ + @SuppressWarnings("unchecked") + @Nullable + private Class[] ignoreExceptions; + /* + * event buffer size for generated retry events + */ + @Min(1) + private Integer eventConsumerBufferSize = 100; + /* + * flag to enable Exponential backoff policy or not for retry policy delay + */ + @NotNull + private Boolean enableExponentialBackoff = false; + /* + * exponential backoff multiplier value + */ + private double exponentialBackoffMultiplier; + @NotNull + /* + * flag to enable randomized delay policy or not for retry policy delay + */ + private Boolean enableRandomizedWait = false; + /* + * randomized delay factor value + */ + private double randomizedWaitFactor; + + @Nullable + private String baseConfig; + + @Nullable + public Long getWaitDurationMillis() { + return waitDurationMillis; + } + + public InstanceProperties setWaitDurationMillis(Long waitDurationMillis) { + this.waitDurationMillis = waitDurationMillis; + return this; + } + + @Nullable + public Integer getMaxRetryAttempts() { + return maxRetryAttempts; + } + + public InstanceProperties setMaxRetryAttempts(Integer maxRetryAttempts) { + this.maxRetryAttempts = maxRetryAttempts; + return this; + } + + @Nullable + public Class> getRetryExceptionPredicate() { + return retryExceptionPredicate; + } + + public InstanceProperties setRetryExceptionPredicate(Class> retryExceptionPredicate) { + this.retryExceptionPredicate = retryExceptionPredicate; + return this; + } + + @Nullable + public Class> getResultPredicate() { + return resultPredicate; + } + + public InstanceProperties setResultPredicate(Class> resultPredicate) { + this.resultPredicate = resultPredicate; + return this; + } + + @Nullable + public Class[] getRetryExceptions() { + return retryExceptions; + } + + public InstanceProperties setRetryExceptions(Class[] retryExceptions) { + this.retryExceptions = retryExceptions; + return this; + } + + @Nullable + public Class[] getIgnoreExceptions() { + return ignoreExceptions; + } + + public InstanceProperties setIgnoreExceptions(Class[] ignoreExceptions) { + this.ignoreExceptions = ignoreExceptions; + return this; + } + + @Nullable + public Integer getEventConsumerBufferSize() { + return eventConsumerBufferSize; + } + + public InstanceProperties setEventConsumerBufferSize(Integer eventConsumerBufferSize) { + this.eventConsumerBufferSize = eventConsumerBufferSize; + return this; + } + + public Boolean getEnableExponentialBackoff() { + return enableExponentialBackoff; + } + + public InstanceProperties setEnableExponentialBackoff(Boolean enableExponentialBackoff) { + this.enableExponentialBackoff = enableExponentialBackoff; + return this; + } + + @Nullable + public double getExponentialBackoffMultiplier() { + return exponentialBackoffMultiplier; + } + + public InstanceProperties setExponentialBackoffMultiplier(double exponentialBackoffMultiplier) { + this.exponentialBackoffMultiplier = exponentialBackoffMultiplier; + return this; + } + + @Nullable + public Boolean getEnableRandomizedWait() { + return enableRandomizedWait; + } + + public InstanceProperties setEnableRandomizedWait(Boolean enableRandomizedWait) { + this.enableRandomizedWait = enableRandomizedWait; + return this; + } + + @Nullable + public double getRandomizedWaitFactor() { + return randomizedWaitFactor; + } + + public InstanceProperties setRandomizedWaitFactor(double randomizedWaitFactor) { + this.randomizedWaitFactor = randomizedWaitFactor; + return this; + } + + /** + * Gets the shared configuration name. If this is set, the configuration builder will use the the shared + * configuration backend over this one. + * + * @return The shared configuration name. + */ + @Nullable + public String getBaseConfig() { + return baseConfig; + } + + /** + * Sets the shared configuration name. If this is set, the configuration builder will use the the shared + * configuration backend over this one. + * + * @param baseConfig The shared configuration name. + */ + public InstanceProperties setBaseConfig(String baseConfig) { + this.baseConfig = baseConfig; + return this; + } + + } + +} diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEndpointResponse.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEndpointResponse.java similarity index 85% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEndpointResponse.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEndpointResponse.java index 5f3e8999de..875b8a52f6 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEndpointResponse.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEndpointResponse.java @@ -1,4 +1,4 @@ -package io.github.resilience4j.retry.monitoring.endpoint; +package io.github.resilience4j.common.retry.monitoring.endpoint; import java.util.List; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTO.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventDTO.java similarity index 96% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTO.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventDTO.java index 9abd597e2b..99f875e60c 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTO.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventDTO.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.retry.monitoring.endpoint; +package io.github.resilience4j.common.retry.monitoring.endpoint; import io.github.resilience4j.retry.event.RetryEvent; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTOBuilder.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventDTOBuilder.java similarity index 95% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTOBuilder.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventDTOBuilder.java index a8efeef459..3f29779c9f 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTOBuilder.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventDTOBuilder.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.retry.monitoring.endpoint; +package io.github.resilience4j.common.retry.monitoring.endpoint; import io.github.resilience4j.retry.event.RetryEvent; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTOFactory.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventDTOFactory.java similarity index 97% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTOFactory.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventDTOFactory.java index e5c714fee6..bf7bdad6ce 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTOFactory.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventDTOFactory.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.retry.monitoring.endpoint; +package io.github.resilience4j.common.retry.monitoring.endpoint; import io.github.resilience4j.retry.event.*; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventsEndpointResponse.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventsEndpointResponse.java similarity index 94% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventsEndpointResponse.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventsEndpointResponse.java index 129113e652..9c7e28f89d 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventsEndpointResponse.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/RetryEventsEndpointResponse.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.github.resilience4j.retry.monitoring.endpoint; +package io.github.resilience4j.common.retry.monitoring.endpoint; import io.github.resilience4j.core.lang.Nullable; diff --git a/resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/package-info.java b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/package-info.java similarity index 92% rename from resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/package-info.java rename to resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/package-info.java index 4e7a8a6caa..a41598eeea 100644 --- a/resilience4j-framework-common/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/package-info.java +++ b/resilience4j-framework-common/src/main/java/io/github/resilience4j/common/retry/monitoring/endpoint/package-info.java @@ -18,7 +18,7 @@ */ @NonNullApi @NonNullFields -package io.github.resilience4j.bulkhead.monitoring.endpoint; +package io.github.resilience4j.common.retry.monitoring.endpoint; import io.github.resilience4j.core.lang.NonNullApi; import io.github.resilience4j.core.lang.NonNullFields; \ No newline at end of file diff --git a/resilience4j-framework-common/src/test/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventDTOFactoryTest.java b/resilience4j-framework-common/src/test/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventDTOFactoryTest.java index 15bdbf7dad..0cc3a477a5 100644 --- a/resilience4j-framework-common/src/test/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventDTOFactoryTest.java +++ b/resilience4j-framework-common/src/test/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventDTOFactoryTest.java @@ -3,6 +3,8 @@ import io.github.resilience4j.bulkhead.event.BulkheadOnCallFinishedEvent; import io.github.resilience4j.bulkhead.event.BulkheadOnCallPermittedEvent; import io.github.resilience4j.bulkhead.event.BulkheadOnCallRejectedEvent; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventDTO; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventDTOFactory; import org.junit.Test; import static io.github.resilience4j.bulkhead.event.BulkheadEvent.Type.*; diff --git a/resilience4j-framework-common/src/test/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOFactoryTest.java b/resilience4j-framework-common/src/test/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOFactoryTest.java index 0a37d1fa45..c697be8d9f 100644 --- a/resilience4j-framework-common/src/test/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOFactoryTest.java +++ b/resilience4j-framework-common/src/test/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventDTOFactoryTest.java @@ -1,6 +1,8 @@ package io.github.resilience4j.circuitbreaker.monitoring.endpoint; import io.github.resilience4j.circuitbreaker.event.*; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTO; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTOFactory; import org.junit.Test; import java.io.IOException; diff --git a/resilience4j-framework-common/src/test/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventDTOTest.java b/resilience4j-framework-common/src/test/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventDTOTest.java index 0640818740..5d2f329698 100644 --- a/resilience4j-framework-common/src/test/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventDTOTest.java +++ b/resilience4j-framework-common/src/test/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventDTOTest.java @@ -1,5 +1,6 @@ package io.github.resilience4j.ratelimiter.monitoring.endpoint; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventDTO; import io.github.resilience4j.ratelimiter.event.RateLimiterOnFailureEvent; import io.github.resilience4j.ratelimiter.event.RateLimiterOnSuccessEvent; import org.junit.Test; diff --git a/resilience4j-framework-common/src/test/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTOFactoryTest.java b/resilience4j-framework-common/src/test/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTOFactoryTest.java index 70476f3959..920a90eab0 100644 --- a/resilience4j-framework-common/src/test/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTOFactoryTest.java +++ b/resilience4j-framework-common/src/test/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventDTOFactoryTest.java @@ -1,5 +1,7 @@ package io.github.resilience4j.retry.monitoring.endpoint; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventDTO; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventDTOFactory; import io.github.resilience4j.retry.event.*; import org.junit.Test; diff --git a/resilience4j-ratpack/build.gradle b/resilience4j-ratpack/build.gradle index 2b237b337a..8c65da1092 100644 --- a/resilience4j-ratpack/build.gradle +++ b/resilience4j-ratpack/build.gradle @@ -15,6 +15,7 @@ dependencies { compileOnly ( libraries.ratpack_metrics ) compileOnly project(':resilience4j-prometheus') compileOnly project(':resilience4j-metrics') + compileOnly ( libraries.hibernate_validator ) testCompile ( libraries.ratpack_test ) testCompile ( libraries.spock ) testCompile ( libraries.ratpack_metrics ) diff --git a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/Resilience4jConfig.java b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/Resilience4jConfig.java index 5ef882bb3c..8d16f70e70 100644 --- a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/Resilience4jConfig.java +++ b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/Resilience4jConfig.java @@ -16,60 +16,73 @@ package io.github.resilience4j.ratpack; -import io.github.resilience4j.ratpack.bulkhead.BulkheadConfig; -import io.github.resilience4j.ratpack.circuitbreaker.CircuitBreakerConfig; -import io.github.resilience4j.ratpack.ratelimiter.RateLimiterConfig; -import io.github.resilience4j.ratpack.retry.RetryConfig; +import io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties; +import io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties; +import io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties; +import io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties; import ratpack.func.Function; -import java.util.HashMap; -import java.util.Map; - import static ratpack.util.Exceptions.uncheck; public class Resilience4jConfig { - private Map circuitBreakers = new HashMap<>(); - private Map rateLimiters = new HashMap<>(); - private Map retries = new HashMap<>(); - private Map bulkheads = new HashMap<>(); + private BulkheadConfigurationProperties bulkhead = new BulkheadConfigurationProperties(); + private CircuitBreakerConfigurationProperties circuitBreaker = new CircuitBreakerConfigurationProperties(); + private RateLimiterConfigurationProperties rateLimiter = new RateLimiterConfigurationProperties(); + private RetryConfigurationProperties retry = new RetryConfigurationProperties(); private boolean metrics = false; private boolean prometheus = false; private EndpointsConfig endpoints = new EndpointsConfig(); - public Resilience4jConfig circuitBreaker(String name, Function configure) { + public Resilience4jConfig circuitBreaker(String name) { + return circuitBreaker(name, config -> config); + } + + public Resilience4jConfig circuitBreaker(String name, Function configure) { try { - CircuitBreakerConfig finalConfig = configure.apply(new CircuitBreakerConfig()); - circuitBreakers.put(name, finalConfig); + CircuitBreakerConfigurationProperties.InstanceProperties finalConfig = configure.apply(new CircuitBreakerConfigurationProperties.InstanceProperties()); + circuitBreaker.getInstances().put(name, finalConfig); return this; } catch (Exception e) { throw uncheck(e); } } - public Resilience4jConfig rateLimiter(String name, Function configure) { + public Resilience4jConfig rateLimiter(String name) { + return rateLimiter(name, config -> config); + } + + public Resilience4jConfig rateLimiter(String name, Function configure) { try { - RateLimiterConfig finalConfig = configure.apply(new RateLimiterConfig()); - rateLimiters.put(name, finalConfig); + RateLimiterConfigurationProperties.InstanceProperties finalConfig = configure.apply(new RateLimiterConfigurationProperties.InstanceProperties()); + rateLimiter.getInstances().put(name, finalConfig); return this; } catch (Exception e) { throw uncheck(e); } } - public Resilience4jConfig retry(String name, Function configure) { + public Resilience4jConfig retry(String name) { + return retry(name, config -> config); + } + + public Resilience4jConfig retry(String name, Function configure) { try { - RetryConfig finalConfig = configure.apply(new RetryConfig()); - retries.put(name, finalConfig); + RetryConfigurationProperties.InstanceProperties finalConfig = configure.apply(new RetryConfigurationProperties.InstanceProperties()); + retry.getInstances().put(name, finalConfig); return this; } catch (Exception e) { throw uncheck(e); } } - public Resilience4jConfig bulkhead(String name, Function configure) { + public Resilience4jConfig bulkhead(String name) { + return bulkhead(name, config -> config); + } + + public Resilience4jConfig bulkhead(String name, Function configure) { try { - BulkheadConfig finalConfig = configure.apply(new BulkheadConfig()); - bulkheads.put(name, finalConfig); + BulkheadConfigurationProperties.InstanceProperties finalConfig = configure.apply(new BulkheadConfigurationProperties.InstanceProperties()); + bulkhead.getInstances().put(name, finalConfig); return this; } catch (Exception e) { throw uncheck(e); @@ -95,20 +108,20 @@ public Resilience4jConfig endpoints(Function getCircuitBreakers() { - return circuitBreakers; + public CircuitBreakerConfigurationProperties getCircuitBreaker() { + return circuitBreaker; } - public Map getRateLimiters() { - return rateLimiters; + public RateLimiterConfigurationProperties getRateLimiter() { + return rateLimiter; } - public Map getRetries() { - return retries; + public RetryConfigurationProperties getRetry() { + return retry; } - public Map getBulkheads() { - return bulkheads; + public BulkheadConfigurationProperties getBulkhead() { + return bulkhead; } public boolean isMetrics() { diff --git a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/Resilience4jModule.java b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/Resilience4jModule.java index 3ddc60ae59..81ec3d8ca8 100644 --- a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/Resilience4jModule.java +++ b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/Resilience4jModule.java @@ -23,14 +23,16 @@ import com.google.inject.matcher.Matchers; import com.google.inject.multibindings.Multibinder; import com.google.inject.multibindings.OptionalBinder; -import io.github.resilience4j.bulkhead.BulkheadConfig; import io.github.resilience4j.bulkhead.BulkheadRegistry; import io.github.resilience4j.bulkhead.annotation.Bulkhead; import io.github.resilience4j.bulkhead.event.BulkheadEvent; -import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig; import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker; import io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent; +import io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties; +import io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties; +import io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties; +import io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties; import io.github.resilience4j.consumer.DefaultEventConsumerRegistry; import io.github.resilience4j.consumer.EventConsumerRegistry; import io.github.resilience4j.metrics.BulkheadMetrics; @@ -40,7 +42,6 @@ import io.github.resilience4j.prometheus.collectors.BulkheadMetricsCollector; import io.github.resilience4j.prometheus.collectors.CircuitBreakerMetricsCollector; import io.github.resilience4j.prometheus.collectors.RateLimiterMetricsCollector; -import io.github.resilience4j.ratelimiter.RateLimiterConfig; import io.github.resilience4j.ratelimiter.RateLimiterRegistry; import io.github.resilience4j.ratelimiter.annotation.RateLimiter; import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; @@ -52,7 +53,6 @@ import io.github.resilience4j.ratpack.ratelimiter.monitoring.endpoint.RateLimiterChain; import io.github.resilience4j.ratpack.retry.RetryMethodInterceptor; import io.github.resilience4j.ratpack.retry.monitoring.endpoint.RetryChain; -import io.github.resilience4j.retry.RetryConfig; import io.github.resilience4j.retry.RetryRegistry; import io.github.resilience4j.retry.annotation.Retry; import io.github.resilience4j.retry.event.RetryEvent; @@ -65,7 +65,6 @@ import ratpack.service.StartEvent; import javax.inject.Inject; -import java.time.Duration; /** * This module registers class and method interceptors for bulkheads, circuit breakers, rate limiters, and retries. @@ -173,23 +172,10 @@ public void onStart(StartEvent event) { CircuitBreakerRegistry circuitBreakerRegistry = injector.getInstance(CircuitBreakerRegistry.class); EventConsumerRegistry cbConsumerRegistry = injector.getInstance(Key.get(new TypeLiteral>() { })); - config.getCircuitBreakers().forEach((name, circuitBreakerConfig) -> { - io.github.resilience4j.circuitbreaker.CircuitBreaker circuitBreaker; - if (circuitBreakerConfig.getDefaults()) { - circuitBreaker = circuitBreakerRegistry.circuitBreaker(name); - } else { - CircuitBreakerConfig.Builder builder = CircuitBreakerConfig.custom() - .failureRateThreshold(circuitBreakerConfig.getFailureRateThreshold()) - .ringBufferSizeInClosedState(circuitBreakerConfig.getRingBufferSizeInClosedState()) - .ringBufferSizeInHalfOpenState(circuitBreakerConfig.getRingBufferSizeInHalfOpenState()) - .waitDurationInOpenState(Duration.ofMillis(circuitBreakerConfig.getWaitIntervalInMillis())) - .recordExceptions(circuitBreakerConfig.getRecordExceptionClasses()) - .ignoreExceptions(circuitBreakerConfig.getIgnoreExceptionClasses()); - if (circuitBreakerConfig.isAutomaticTransitionFromOpenToHalfOpen()) { - builder.enableAutomaticTransitionFromOpenToHalfOpen(); - } - circuitBreaker = circuitBreakerRegistry.circuitBreaker(name, builder.build()); - } + CircuitBreakerConfigurationProperties circuitBreakerConfigurations = config.getCircuitBreaker(); + circuitBreakerConfigurations.getInstances().forEach((name, circuitBreakerConfig) -> { + io.github.resilience4j.circuitbreaker.CircuitBreaker circuitBreaker = + circuitBreakerRegistry.circuitBreaker(name, circuitBreakerConfigurations.createCircuitBreakerConfig(circuitBreakerConfig)); if (endpointsConfig.getCircuitBreakers().isEnabled()) { circuitBreaker.getEventPublisher().onEvent(cbConsumerRegistry.createEventConsumer(name, endpointsConfig.getCircuitBreakers().getEventConsumerBufferSize())); } @@ -199,17 +185,10 @@ public void onStart(StartEvent event) { RateLimiterRegistry rateLimiterRegistry = injector.getInstance(RateLimiterRegistry.class); EventConsumerRegistry rlConsumerRegistry = injector.getInstance(Key.get(new TypeLiteral>() { })); - config.getRateLimiters().forEach((name, rateLimiterConfig) -> { - io.github.resilience4j.ratelimiter.RateLimiter rateLimiter; - if (rateLimiterConfig.getDefaults()) { - rateLimiter = rateLimiterRegistry.rateLimiter(name); - } else { - rateLimiter = rateLimiterRegistry.rateLimiter(name, RateLimiterConfig.custom() - .limitForPeriod(rateLimiterConfig.getLimitForPeriod()) - .limitRefreshPeriod(Duration.ofNanos(rateLimiterConfig.getLimitRefreshPeriodInNanos())) - .timeoutDuration(Duration.ofMillis(rateLimiterConfig.getTimeoutInMillis())) - .build()); - } + RateLimiterConfigurationProperties rateLimiterConfigurations = config.getRateLimiter(); + rateLimiterConfigurations.getInstances().forEach((name, rateLimiterConfig) -> { + io.github.resilience4j.ratelimiter.RateLimiter rateLimiter = + rateLimiterRegistry.rateLimiter(name, rateLimiterConfigurations.createRateLimiterConfig(rateLimiterConfig)); if (endpointsConfig.getRateLimiters().isEnabled()) { rateLimiter.getEventPublisher().onEvent(rlConsumerRegistry.createEventConsumer(name, endpointsConfig.getRateLimiters().getEventConsumerBufferSize())); } @@ -219,16 +198,10 @@ public void onStart(StartEvent event) { RetryRegistry retryRegistry = injector.getInstance(RetryRegistry.class); EventConsumerRegistry rConsumerRegistry = injector.getInstance(Key.get(new TypeLiteral>() { })); - config.getRetries().forEach((name, retryConfig) -> { - io.github.resilience4j.retry.Retry retry; - if (retryConfig.getDefaults()) { - retry = retryRegistry.retry(name); - } else { - retry = retryRegistry.retry(name, RetryConfig.custom() - .maxAttempts(retryConfig.getMaxAttempts()) - .waitDuration(Duration.ofMillis(retryConfig.getWaitDurationInMillis())) - .build()); - } + RetryConfigurationProperties retryConfigurations = config.getRetry(); + retryConfigurations.getInstances().forEach((name, retryConfig) -> { + io.github.resilience4j.retry.Retry retry = + retryRegistry.retry(name, retryConfigurations.createRetryConfig(retryConfig)); if (endpointsConfig.getRetries().isEnabled()) { retry.getEventPublisher().onEvent(rConsumerRegistry.createEventConsumer(name, endpointsConfig.getRetries().getEventConsumerBufferSize())); } @@ -238,16 +211,10 @@ public void onStart(StartEvent event) { BulkheadRegistry bulkheadRegistry = injector.getInstance(BulkheadRegistry.class); EventConsumerRegistry bConsumerRegistry = injector.getInstance(Key.get(new TypeLiteral>() { })); - config.getBulkheads().forEach((name, bulkheadConfig) -> { - io.github.resilience4j.bulkhead.Bulkhead bulkhead; - if (bulkheadConfig.getDefaults()) { - bulkhead = bulkheadRegistry.bulkhead(name); - } else { - bulkhead = bulkheadRegistry.bulkhead(name, BulkheadConfig.custom() - .maxConcurrentCalls(bulkheadConfig.getMaxConcurrentCalls()) - .maxWaitTime(bulkheadConfig.getMaxWaitTime()) - .build()); - } + BulkheadConfigurationProperties bulkheadConfigurations= config.getBulkhead(); + bulkheadConfigurations.getInstances().forEach((name, bulkheadConfig) -> { + io.github.resilience4j.bulkhead.Bulkhead bulkhead = + bulkheadRegistry.bulkhead(name, bulkheadConfigurations.createBulkheadConfig(bulkheadConfig)); if (endpointsConfig.getBulkheads().isEnabled()) { bulkhead.getEventPublisher().onEvent(bConsumerRegistry.createEventConsumer(name, endpointsConfig.getBulkheads().getEventConsumerBufferSize())); } diff --git a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/bulkhead/BulkheadConfig.java b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/bulkhead/BulkheadConfig.java deleted file mode 100644 index a37e5b169c..0000000000 --- a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/bulkhead/BulkheadConfig.java +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2017 Jan Sykora - * - * 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 io.github.resilience4j.ratpack.bulkhead; - -/** - * Bulkhead config adapter for integration with Ratpack. {@link #maxWaitTime} should - * almost always be set to 0, so the compute threads would not be blocked upon execution. - */ -public class BulkheadConfig { - - private boolean defaults = false; - private int maxConcurrentCalls; - private long maxWaitTime; - - public BulkheadConfig defaults(boolean defaults) { - this.defaults = defaults; - return this; - } - - public BulkheadConfig maxConcurrentCalls(int maxConcurrentCalls) { - this.maxConcurrentCalls = maxConcurrentCalls; - return this; - } - - public BulkheadConfig maxWaitTime(int maxWaitTime) { - this.maxWaitTime = maxWaitTime; - return this; - } - - public boolean getDefaults() { - return defaults; - } - - public int getMaxConcurrentCalls() { - return maxConcurrentCalls; - } - - public long getMaxWaitTime() { - return maxWaitTime; - } -} diff --git a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/bulkhead/monitoring/endpoint/BulkheadChain.java b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/bulkhead/monitoring/endpoint/BulkheadChain.java index 7c937a1a58..a1a2af77c4 100644 --- a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/bulkhead/monitoring/endpoint/BulkheadChain.java +++ b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/bulkhead/monitoring/endpoint/BulkheadChain.java @@ -18,8 +18,8 @@ import io.github.resilience4j.bulkhead.Bulkhead; import io.github.resilience4j.bulkhead.BulkheadRegistry; import io.github.resilience4j.bulkhead.event.BulkheadEvent; -import io.github.resilience4j.bulkhead.monitoring.endpoint.BulkheadEventDTOFactory; -import io.github.resilience4j.bulkhead.monitoring.endpoint.BulkheadEventsEndpointResponse; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventDTOFactory; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventsEndpointResponse; import io.github.resilience4j.consumer.CircularEventConsumer; import io.github.resilience4j.consumer.EventConsumerRegistry; import io.github.resilience4j.ratpack.Resilience4jConfig; diff --git a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/circuitbreaker/CircuitBreakerConfig.java b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/circuitbreaker/CircuitBreakerConfig.java deleted file mode 100644 index 12de6f0cbf..0000000000 --- a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/circuitbreaker/CircuitBreakerConfig.java +++ /dev/null @@ -1,158 +0,0 @@ -/* - * Copyright 2017 Dan Maas - * - * 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 io.github.resilience4j.ratpack.circuitbreaker; - -import java.util.Arrays; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -import static io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.*; - -public class CircuitBreakerConfig { - private boolean defaults = false; - private Integer waitIntervalInMillis = DEFAULT_WAIT_DURATION_IN_OPEN_STATE * 1000; - private Integer failureRateThreshold = DEFAULT_MAX_FAILURE_THRESHOLD; - private Integer ringBufferSizeInClosedState = DEFAULT_RING_BUFFER_SIZE_IN_CLOSED_STATE; - private Integer ringBufferSizeInHalfOpenState = DEFAULT_RING_BUFFER_SIZE_IN_HALF_OPEN_STATE; - // default is that all exceptions are recorded, and none are ignored - private List recordExceptions = Arrays.asList(Throwable.class.getName()); - private List ignoreExceptions = Arrays.asList(); - private boolean automaticTransitionFromOpenToHalfOpen = false; - - /** - * Use config provided by circuitbreaker registry instead of these config values. - * - * @param defaults - * @return - */ - public CircuitBreakerConfig defaults(boolean defaults) { - this.defaults = defaults; - return this; - } - - public CircuitBreakerConfig waitIntervalInMillis(Integer waitInterval) { - this.waitIntervalInMillis = waitInterval; - return this; - } - - public CircuitBreakerConfig failureRateThreshold(Integer failureRateThreshold) { - this.failureRateThreshold = failureRateThreshold; - return this; - } - - public CircuitBreakerConfig ringBufferSizeInClosedState(Integer ringBufferSizeInClosedState) { - this.ringBufferSizeInClosedState = ringBufferSizeInClosedState; - return this; - } - - public CircuitBreakerConfig ringBufferSizeInHalfOpenState(Integer ringBufferSizeInHalfOpenState) { - this.ringBufferSizeInHalfOpenState = ringBufferSizeInHalfOpenState; - return this; - } - - /** - * Each element must be the fully qualified string class name of an exception class. - * - * @param recordExceptions - * @return - */ - public CircuitBreakerConfig recordExceptions(List recordExceptions) { - this.recordExceptions = recordExceptions; - return this; - } - - /** - * Each element must be the fully qualified string class name of an exception class. - * - * @param ignoreExceptions - * @return - */ - public CircuitBreakerConfig ignoreExceptions(List ignoreExceptions) { - this.ignoreExceptions = ignoreExceptions; - return this; - } - - public CircuitBreakerConfig automaticTransitionFromOpenToHalfOpen(boolean automaticTransitionFromOpenToHalfOpen) { - this.automaticTransitionFromOpenToHalfOpen = automaticTransitionFromOpenToHalfOpen; - return this; - } - - public boolean getDefaults() { - return defaults; - } - - public Integer getWaitIntervalInMillis() { - return waitIntervalInMillis; - } - - public Integer getFailureRateThreshold() { - return failureRateThreshold; - } - - public Integer getRingBufferSizeInClosedState() { - return ringBufferSizeInClosedState; - } - - public Integer getRingBufferSizeInHalfOpenState() { - return ringBufferSizeInHalfOpenState; - } - - public List getRecordExceptions() { - return recordExceptions; - } - - @SuppressWarnings("unchecked") - public Class[] getRecordExceptionClasses() { - List> classList = buildArray(recordExceptions); - Class[] classes = new Class[classList.size() < 1 ? 1 : classList.size()]; - if (classes.length == 1 && classes[0] == null) { - classes[0] = Throwable.class; - } - return classList.toArray(classes); - } - - public List getIgnoreExceptions() { - return ignoreExceptions; - } - - @SuppressWarnings("unchecked") - public Class[] getIgnoreExceptionClasses() { - List> classList = buildArray(ignoreExceptions); - Class[] classes = new Class[classList.size()]; - return classList.toArray(classes); - } - - public boolean isAutomaticTransitionFromOpenToHalfOpen() { - return automaticTransitionFromOpenToHalfOpen; - } - - @SuppressWarnings("unchecked") - private List> buildArray(List list) { - return list.stream() - .map(t -> { - try { - return (Class)Class.forName(t); - } catch (ClassNotFoundException e) { - return null; - } - }) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - } - -} diff --git a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/circuitbreaker/monitoring/endpoint/CircuitBreakerChain.java b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/circuitbreaker/monitoring/endpoint/CircuitBreakerChain.java index 2f5d29c407..2d21bee237 100644 --- a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/circuitbreaker/monitoring/endpoint/CircuitBreakerChain.java +++ b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/circuitbreaker/monitoring/endpoint/CircuitBreakerChain.java @@ -19,8 +19,8 @@ import io.github.resilience4j.circuitbreaker.CircuitBreaker; import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; import io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent; -import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTOFactory; -import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTOFactory; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse; import io.github.resilience4j.consumer.CircularEventConsumer; import io.github.resilience4j.consumer.EventConsumerRegistry; import io.github.resilience4j.ratpack.Resilience4jConfig; diff --git a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/ratelimiter/RateLimiterConfig.java b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/ratelimiter/RateLimiterConfig.java deleted file mode 100644 index 03d3022efb..0000000000 --- a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/ratelimiter/RateLimiterConfig.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2017 Dan Maas - * - * 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 io.github.resilience4j.ratpack.ratelimiter; - -public class RateLimiterConfig { - - private boolean defaults = false; - private Integer limitForPeriod = 50; - private Integer limitRefreshPeriodInNanos = 500; - private Integer timeoutInMillis = 5000; - - public RateLimiterConfig defaults(boolean defaults) { - this.defaults = defaults; - return this; - } - - public RateLimiterConfig limitForPeriod(Integer limitForPeriod) { - this.limitForPeriod = limitForPeriod; - return this; - } - - public RateLimiterConfig limitRefreshPeriodInNanos(Integer limitRefreshPeriodInNanos) { - this.limitRefreshPeriodInNanos = limitRefreshPeriodInNanos; - return this; - } - - public RateLimiterConfig timeoutInMillis(Integer timeoutInMillis) { - this.timeoutInMillis = timeoutInMillis; - return this; - } - - public Boolean getDefaults() { - return defaults; - } - - public Integer getLimitForPeriod() { - return limitForPeriod; - } - - public Integer getLimitRefreshPeriodInNanos() { - return limitRefreshPeriodInNanos; - } - - public Integer getTimeoutInMillis() { - return timeoutInMillis; - } - -} diff --git a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/ratelimiter/monitoring/endpoint/RateLimiterChain.java b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/ratelimiter/monitoring/endpoint/RateLimiterChain.java index 1870228994..5d13a00bb1 100644 --- a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/ratelimiter/monitoring/endpoint/RateLimiterChain.java +++ b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/ratelimiter/monitoring/endpoint/RateLimiterChain.java @@ -21,8 +21,8 @@ import io.github.resilience4j.ratelimiter.RateLimiter; import io.github.resilience4j.ratelimiter.RateLimiterRegistry; import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEventDTO; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpointResponse; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventDTO; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpointResponse; import io.github.resilience4j.ratpack.Resilience4jConfig; import io.github.resilience4j.reactor.adapter.ReactorAdapter; import io.vavr.collection.Seq; diff --git a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/retry/RetryConfig.java b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/retry/RetryConfig.java deleted file mode 100644 index 687418a26d..0000000000 --- a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/retry/RetryConfig.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2017 Dan Maas - * - * 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 io.github.resilience4j.ratpack.retry; - -public class RetryConfig { - - private boolean defaults = false; - private int maxAttempts; - private int waitDurationInMillis; - - public RetryConfig defaults(boolean defaults) { - this.defaults = defaults; - return this; - } - - public RetryConfig maxAttempts(int maxAttempts) { - this.maxAttempts = maxAttempts; - return this; - } - - public RetryConfig waitDurationInMillis(int waitDurationInMillis) { - this.waitDurationInMillis = waitDurationInMillis; - return this; - } - - public boolean getDefaults() { - return defaults; - } - - public int getMaxAttempts() { - return maxAttempts; - } - - public int getWaitDurationInMillis() { - return waitDurationInMillis; - } -} diff --git a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/retry/monitoring/endpoint/RetryChain.java b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/retry/monitoring/endpoint/RetryChain.java index 5f5310712a..559f8967f3 100644 --- a/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/retry/monitoring/endpoint/RetryChain.java +++ b/resilience4j-ratpack/src/main/java/io/github/resilience4j/ratpack/retry/monitoring/endpoint/RetryChain.java @@ -23,9 +23,9 @@ import io.github.resilience4j.retry.Retry; import io.github.resilience4j.retry.RetryRegistry; import io.github.resilience4j.retry.event.RetryEvent; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEventDTO; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEventDTOFactory; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEventsEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventDTO; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventDTOFactory; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventsEndpointResponse; import io.vavr.collection.Seq; import ratpack.exec.Promise; import ratpack.func.Action; diff --git a/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/Resilience4jModuleSpec.groovy b/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/Resilience4jModuleSpec.groovy index fd5607bdaf..96ca935961 100644 --- a/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/Resilience4jModuleSpec.groovy +++ b/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/Resilience4jModuleSpec.groovy @@ -52,15 +52,14 @@ class Resilience4jModuleSpec extends Specification { bindings { bindInstance(CircuitBreakerRegistry, circuitBreakerRegistry) module(Resilience4jModule) { - it.circuitBreaker('test') { - it.defaults(true) - }.circuitBreaker('test2') { - it.failureRateThreshold(50) - .waitIntervalInMillis(5000) - .ringBufferSizeInClosedState(200) - .ringBufferSizeInHalfOpenState(20) - .failureRateThreshold(60) - .automaticTransitionFromOpenToHalfOpen(true) + it.circuitBreaker('test') + .circuitBreaker('test2') { + it.setFailureRateThreshold(50) + .setWaitDurationInOpenStateMillis(5000) + .setRingBufferSizeInClosedState(200) + .setRingBufferSizeInHalfOpenState(20) + .setFailureRateThreshold(60) + .setAutomaticTransitionFromOpenToHalfOpenEnabled(true) } } } @@ -168,10 +167,10 @@ class Resilience4jModuleSpec extends Specification { test1.name == 'test1' test1.circuitBreakerConfig.with { assert ringBufferSizeInClosedState == 100 - assert ringBufferSizeInHalfOpenState == 10 - assert waitDurationInOpenState == Duration.ofMinutes(1) - assert failureRateThreshold == 50 - assert !automaticTransitionFromOpenToHalfOpenEnabled + assert ringBufferSizeInHalfOpenState == 20 + assert waitDurationInOpenState == Duration.ofMillis(1000) + assert failureRateThreshold == 60 + assert automaticTransitionFromOpenToHalfOpenEnabled assert recordFailurePredicate.test(new DummyException1("test")) assert recordFailurePredicate.test(new DummyException2("test")) it @@ -200,12 +199,11 @@ class Resilience4jModuleSpec extends Specification { bindings { bindInstance(RateLimiterRegistry, rateLimiterRegistry) module(Resilience4jModule) { - it.rateLimiter('test') { - it.defaults(true) - }.rateLimiter('test2') { - it.limitForPeriod(100) - .limitRefreshPeriodInNanos(900) - .timeoutInMillis(10) + it.rateLimiter('test') + .rateLimiter('test2') { + it.setLimitForPeriod(100) + .setLimitRefreshPeriodInNanos(900) + .setTimeoutInMillis(10) } } } @@ -307,9 +305,9 @@ class Resilience4jModuleSpec extends Specification { def test1 = rateLimiterRegistry.rateLimiter('test1') test1.name == 'test1' test1.rateLimiterConfig.with { - assert limitForPeriod == 50 - assert limitRefreshPeriod == Duration.ofNanos(500) - assert timeoutDuration == Duration.ofSeconds(5) + assert limitForPeriod == 150 + assert limitRefreshPeriod == Duration.ofNanos(900) + assert timeoutDuration == Duration.ofMillis(10) it } def test2 = rateLimiterRegistry.rateLimiter('test2') @@ -332,11 +330,10 @@ class Resilience4jModuleSpec extends Specification { bindings { bindInstance(RetryRegistry, retryRegistry) module(Resilience4jModule) { - it.retry('test') { - it.defaults(true) - }.retry('test2') { - it.maxAttempts(3) - .waitDurationInMillis(1000) + it.retry('test') + .retry('test2') { + it.setMaxRetryAttempts(3) + .setWaitDurationMillis(1000) } } } @@ -434,7 +431,7 @@ class Resilience4jModuleSpec extends Specification { def test1 = retryRegistry.retry('test1') test1.name == 'test1' test1.retryConfig.with { - assert maxAttempts == 3 + assert maxAttempts == 4 it } def test2 = retryRegistry.retry('test2') @@ -455,11 +452,10 @@ class Resilience4jModuleSpec extends Specification { bindings { bindInstance(BulkheadRegistry, bulkheadRegistry) module(Resilience4jModule) { - it.bulkhead('test') { - it.defaults(true) - }.bulkhead('test2') { - it.maxConcurrentCalls(100) - .maxWaitTime(1000) + it.bulkhead('test') + .bulkhead('test2') { + it.setMaxConcurrentCalls(100) + .setMaxWaitTime(1000) } } } @@ -559,8 +555,8 @@ class Resilience4jModuleSpec extends Specification { def test1 = bulkheadRegistry.bulkhead('test1') test1.name == 'test1' test1.bulkheadConfig.with { - assert maxConcurrentCalls == 25 - assert maxWaitTime == 0 + assert maxConcurrentCalls == 50 + assert maxWaitTime == 750 it } def test2 = bulkheadRegistry.bulkhead('test2') diff --git a/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/bulkhead/monitoring/endpoint/BulkheadChainSpec.groovy b/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/bulkhead/monitoring/endpoint/BulkheadChainSpec.groovy index c2c79a1af6..977325b8b6 100644 --- a/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/bulkhead/monitoring/endpoint/BulkheadChainSpec.groovy +++ b/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/bulkhead/monitoring/endpoint/BulkheadChainSpec.groovy @@ -17,7 +17,7 @@ package io.github.resilience4j.ratpack.bulkhead.monitoring.endpoint import com.fasterxml.jackson.databind.ObjectMapper import io.github.resilience4j.bulkhead.BulkheadRegistry -import io.github.resilience4j.bulkhead.monitoring.endpoint.BulkheadEventsEndpointResponse +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventsEndpointResponse import io.github.resilience4j.ratpack.Resilience4jModule import ratpack.http.client.HttpClient import ratpack.test.embed.EmbeddedApp @@ -50,9 +50,9 @@ class BulkheadChainSpec extends Specification { bindInstance(BulkheadRegistry, bulkheadRegistry) module(Resilience4jModule) { it.bulkhead('test1') { - it.maxConcurrentCalls(10).maxWaitTime(0) + it.setMaxConcurrentCalls(10).setMaxWaitTime(0) }.bulkhead('test2') { - it.maxConcurrentCalls(20).maxWaitTime(0) + it.setMaxConcurrentCalls(20).setMaxWaitTime(0) } } } @@ -130,9 +130,9 @@ class BulkheadChainSpec extends Specification { bindInstance(BulkheadRegistry, bulkheadRegistry) module(Resilience4jModule) { it.bulkhead('test1') { - it.maxConcurrentCalls(10).maxWaitTime(9) + it.setMaxConcurrentCalls(10).setMaxWaitTime(9) }.bulkhead('test2') { - it.maxConcurrentCalls(20).maxWaitTime(0) + it.setMaxConcurrentCalls(20).setMaxWaitTime(0) } } } @@ -203,9 +203,9 @@ class BulkheadChainSpec extends Specification { bindInstance(BulkheadRegistry, bulkheadRegistry) module(Resilience4jModule) { it.bulkhead('test1') { - it.maxConcurrentCalls(10).maxWaitTime(0) + it.setMaxConcurrentCalls(10).setMaxWaitTime(0) }.bulkhead('test2') { - it.maxConcurrentCalls(20).maxWaitTime(0) + it.setMaxConcurrentCalls(20).setMaxWaitTime(0) }.endpoints { it.bulkheads { it.enabled(false) diff --git a/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/circuitbreaker/monitoring/endpoint/CircuitBreakerChainSpec.groovy b/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/circuitbreaker/monitoring/endpoint/CircuitBreakerChainSpec.groovy index 8f002cb9b5..2481ae8fd9 100644 --- a/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/circuitbreaker/monitoring/endpoint/CircuitBreakerChainSpec.groovy +++ b/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/circuitbreaker/monitoring/endpoint/CircuitBreakerChainSpec.groovy @@ -18,7 +18,7 @@ package io.github.resilience4j.ratpack.circuitbreaker.monitoring.endpoint import com.fasterxml.jackson.databind.ObjectMapper import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry -import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse import io.github.resilience4j.ratpack.Resilience4jModule import io.github.resilience4j.ratpack.circuitbreaker.monitoring.endpoint.states.CircuitBreakerStatesEndpointResponse import ratpack.http.client.HttpClient @@ -54,9 +54,9 @@ class CircuitBreakerChainSpec extends Specification { bindInstance(CircuitBreakerRegistry, circuitBreakerRegistry) module(Resilience4jModule) { it.circuitBreaker('test1') { - it.failureRateThreshold(75).waitIntervalInMillis(5000) + it.setFailureRateThreshold(75).setWaitDurationInOpenStateMillis(5000) }.circuitBreaker('test2') { - it.failureRateThreshold(25).waitIntervalInMillis(5000) + it.setFailureRateThreshold(25).setWaitDurationInOpenStateMillis(5000) } } } @@ -115,9 +115,9 @@ class CircuitBreakerChainSpec extends Specification { bindInstance(CircuitBreakerRegistry, circuitBreakerRegistry) module(Resilience4jModule) { it.circuitBreaker('test1') { - it.failureRateThreshold(75).waitIntervalInMillis(5000) + it.setFailureRateThreshold(75).setWaitDurationInOpenStateMillis(5000) }.circuitBreaker('test2') { - it.failureRateThreshold(25).waitIntervalInMillis(5000) + it.setFailureRateThreshold(25).setWaitDurationInOpenStateMillis(5000) } } } @@ -186,9 +186,9 @@ class CircuitBreakerChainSpec extends Specification { bindInstance(CircuitBreakerRegistry, circuitBreakerRegistry) module(Resilience4jModule) { it.circuitBreaker('test1') { - it.failureRateThreshold(75).waitIntervalInMillis(5000) + it.setFailureRateThreshold(75).setWaitDurationInOpenStateMillis(5000) }.circuitBreaker('test2') { - it.failureRateThreshold(25).waitIntervalInMillis(5000) + it.setFailureRateThreshold(25).setWaitDurationInOpenStateMillis(5000) } } } @@ -257,9 +257,9 @@ class CircuitBreakerChainSpec extends Specification { bindInstance(CircuitBreakerRegistry, circuitBreakerRegistry) module(Resilience4jModule) { it.circuitBreaker('test1') { - it.failureRateThreshold(75).waitIntervalInMillis(5000) + it.setFailureRateThreshold(75).setWaitDurationInOpenStateMillis(5000) }.circuitBreaker('test2') { - it.failureRateThreshold(25).waitIntervalInMillis(5000) + it.setFailureRateThreshold(25).setWaitDurationInOpenStateMillis(5000) }.endpoints { it.circuitBreakers { it.enabled(false) diff --git a/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/ratelimiter/monitoring/endpoint/RateLimiterChainSpec.groovy b/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/ratelimiter/monitoring/endpoint/RateLimiterChainSpec.groovy index 239a22dbe7..db1c46b1fa 100644 --- a/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/ratelimiter/monitoring/endpoint/RateLimiterChainSpec.groovy +++ b/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/ratelimiter/monitoring/endpoint/RateLimiterChainSpec.groovy @@ -19,7 +19,7 @@ package io.github.resilience4j.ratpack.ratelimiter.monitoring.endpoint import com.fasterxml.jackson.databind.ObjectMapper import io.github.resilience4j.ratelimiter.RateLimiterRegistry import io.github.resilience4j.ratelimiter.event.RateLimiterEvent -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpointResponse +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpointResponse import io.github.resilience4j.ratpack.Resilience4jModule import ratpack.http.client.HttpClient import ratpack.test.embed.EmbeddedApp @@ -57,9 +57,9 @@ class RateLimiterChainSpec extends Specification { bindInstance(RateLimiterRegistry, rateLimiterRegistry) module(Resilience4jModule) { it.rateLimiter('test1') { - it.limitForPeriod(5).limitRefreshPeriodInNanos(1000000000).timeoutInMillis(0) + it.setLimitForPeriod(5).setLimitRefreshPeriodInNanos(1000000000).setTimeoutInMillis(0) }.rateLimiter('test2') { - it.limitForPeriod(5).limitRefreshPeriodInNanos(1000000000).timeoutInMillis(0) + it.setLimitForPeriod(5).setLimitRefreshPeriodInNanos(1000000000).setTimeoutInMillis(0) } } } @@ -141,9 +141,9 @@ class RateLimiterChainSpec extends Specification { bindInstance(RateLimiterRegistry, rateLimiterRegistry) module(Resilience4jModule) { it.rateLimiter('test1') { - it.limitForPeriod(5).limitRefreshPeriodInNanos(1000000000).timeoutInMillis(0) + it.setLimitForPeriod(5).setLimitRefreshPeriodInNanos(1000000000).setTimeoutInMillis(0) }.rateLimiter('test2') { - it.limitForPeriod(5).limitRefreshPeriodInNanos(1000000000).timeoutInMillis(0) + it.setLimitForPeriod(5).setLimitRefreshPeriodInNanos(1000000000).setTimeoutInMillis(0) } } } @@ -211,9 +211,9 @@ class RateLimiterChainSpec extends Specification { bindInstance(RateLimiterRegistry, rateLimiterRegistry) module(Resilience4jModule) { it.rateLimiter('test1') { - it.limitForPeriod(5).limitRefreshPeriodInNanos(1000000000).timeoutInMillis(0) + it.setLimitForPeriod(5).setLimitRefreshPeriodInNanos(1000000000).setTimeoutInMillis(0) }.rateLimiter('test2') { - it.limitForPeriod(5).limitRefreshPeriodInNanos(1000000000).timeoutInMillis(0) + it.setLimitForPeriod(5).setLimitRefreshPeriodInNanos(1000000000).setTimeoutInMillis(0) }.endpoints { it.rateLimiters { it.enabled(false) diff --git a/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/retry/monitoring/endpoint/RetryChainSpec.groovy b/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/retry/monitoring/endpoint/RetryChainSpec.groovy index d4a6606495..9c31fc79d4 100644 --- a/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/retry/monitoring/endpoint/RetryChainSpec.groovy +++ b/resilience4j-ratpack/src/test/groovy/io/github/resilience4j/ratpack/retry/monitoring/endpoint/RetryChainSpec.groovy @@ -21,7 +21,7 @@ import io.github.resilience4j.ratpack.Resilience4jModule import io.github.resilience4j.retry.Retry import io.github.resilience4j.retry.RetryRegistry import io.github.resilience4j.retry.event.RetryEvent -import io.github.resilience4j.retry.monitoring.endpoint.RetryEventsEndpointResponse +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventsEndpointResponse import io.vavr.CheckedFunction0 import io.vavr.control.Try import ratpack.http.client.HttpClient @@ -56,9 +56,9 @@ class RetryChainSpec extends Specification { bindInstance(RetryRegistry, retryRegistry) module(Resilience4jModule) { it.retry('test1') { - it.maxAttempts(3).waitDurationInMillis(100) + it.setMaxRetryAttempts(3).setWaitDurationMillis(100) }.retry('test2') { - it.maxAttempts(3).waitDurationInMillis(100) + it.setMaxRetryAttempts(3).setWaitDurationMillis(100) } } } @@ -138,9 +138,9 @@ class RetryChainSpec extends Specification { bindInstance(RetryRegistry, retryRegistry) module(Resilience4jModule) { it.retry('test1') { - it.maxAttempts(3).waitDurationInMillis(100) + it.setMaxRetryAttempts(3).setWaitDurationMillis(100) }.retry('test2') { - it.maxAttempts(3).waitDurationInMillis(100) + it.setMaxRetryAttempts(3).setWaitDurationMillis(100) } } } @@ -209,9 +209,9 @@ class RetryChainSpec extends Specification { bindInstance(RetryRegistry, retryRegistry) module(Resilience4jModule) { it.retry('test1') { - it.maxAttempts(3).waitDurationInMillis(100) + it.setMaxRetryAttempts(3).setWaitDurationMillis(100) }.retry('test2') { - it.maxAttempts(3).waitDurationInMillis(100) + it.setMaxRetryAttempts(3).setWaitDurationMillis(100) }.endpoints { it.retries { it.enabled(false) diff --git a/resilience4j-ratpack/src/test/resources/application.yml b/resilience4j-ratpack/src/test/resources/application.yml index 9046c4fc8e..5965a9a6e3 100644 --- a/resilience4j-ratpack/src/test/resources/application.yml +++ b/resilience4j-ratpack/src/test/resources/application.yml @@ -16,33 +16,64 @@ resilience4j: enabled: true path: bulkhead eventConsumerBufferSize: 100 - circuitBreakers: - test1: - defaults: true - test2: - ringBufferSizeInClosedState: 200 - ringBufferSizeInHalfOpenState: 20 - waitIntervalInMillis: 5000 - failureRateThreshold: 60 - automaticTransitionFromOpenToHalfOpen: true - recordExceptions: - - io.github.resilience4j.ratpack.Resilience4jModuleSpec$DummyException1 - rateLimiters: - test1: - defaults: true - test2: - limitForPeriod: 100 - limitRefreshPeriodInNanos: 900 - timeoutInMillis: 10 - retries: - test1: - defaults: true - test2: - maxAttempts: 3 - waitDurationInMillis: 1000 - bulkheads: - test1: - defaults: true - test2: - maxConcurrentCalls: 100 - maxWaitTime: 1000 + circuitBreaker: + configs: + default: + ringBufferSizeInClosedState: 200 + ringBufferSizeInHalfOpenState: 20 + waitDurationInOpenStateMillis: 1000 + failureRateThreshold: 60 + automaticTransitionFromOpenToHalfOpenEnabled: true + recordExceptions: + - io.github.resilience4j.ratpack.Resilience4jModuleSpec$DummyException1 + - io.github.resilience4j.ratpack.Resilience4jModuleSpec$DummyException2 + backends: + test1: + baseConfig: default + ringBufferSizeInClosedState: 100 + test2: + ringBufferSizeInClosedState: 200 + ringBufferSizeInHalfOpenState: 20 + waitDurationInOpenStateMillis: 5000 + failureRateThreshold: 60 + automaticTransitionFromOpenToHalfOpenEnabled: true + recordExceptions: + - io.github.resilience4j.ratpack.Resilience4jModuleSpec$DummyException1 + rateLimiter: + configs: + default: + limitForPeriod: 100 + limitRefreshPeriodInNanos: 900 + timeoutInMillis: 10 + limiters: + test1: + baseConfig: default + limitForPeriod: 150 + test2: + limitForPeriod: 100 + limitRefreshPeriodInNanos: 900 + timeoutInMillis: 10 + retry: + configs: + default: + maxRetryAttempts: 3 + waitDurationInMillis: 1000 + backends: + test1: + baseConfig: default + maxRetryAttempts: 4 + test2: + maxRetryAttempts: 3 + waitDurationInMillis: 1000 + bulkhead: + configs: + default: + maxConcurrentCalls: 50 + maxWaitTime: 500 + backends: + test1: + baseConfig: default + maxWaitTime: 750 + test2: + maxConcurrentCalls: 100 + maxWaitTime: 1000 diff --git a/resilience4j-spring-boot-common/build.gradle b/resilience4j-spring-boot-common/build.gradle index 7cd2046edf..76ea07590f 100644 --- a/resilience4j-spring-boot-common/build.gradle +++ b/resilience4j-spring-boot-common/build.gradle @@ -1,6 +1,5 @@ dependencies { compile project(':resilience4j-spring') - compile project(':resilience4j-framework-common') compileOnly(libraries.spring_boot2_aop) compileOnly(libraries.spring_boot2_config_processor) diff --git a/resilience4j-spring-boot-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/health/CircuitBreakerHealthIndicator.java b/resilience4j-spring-boot-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/health/CircuitBreakerHealthIndicator.java index 7bb2cb2b4e..4e11126b6a 100644 --- a/resilience4j-spring-boot-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/health/CircuitBreakerHealthIndicator.java +++ b/resilience4j-spring-boot-common/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/health/CircuitBreakerHealthIndicator.java @@ -29,7 +29,7 @@ public class CircuitBreakerHealthIndicator implements HealthIndicator { private static final String FAILURE_RATE = "failureRate"; - private static final String FAILURE_RATE_THRESHOLD = "failureRateThreshold"; + private static final String FAILURE_RATE_THRESHOLD = "setFailureRateThreshold"; private static final String BUFFERED_CALLS = "bufferedCalls"; private static final String FAILED_CALLS = "failedCalls"; private static final String NOT_PERMITTED = "notPermittedCalls"; diff --git a/resilience4j-spring-boot-common/src/test/java/io/github/resilience4j/circuitbreaker/monitoring/health/CircuitBreakerHealthIndicatorTest.java b/resilience4j-spring-boot-common/src/test/java/io/github/resilience4j/circuitbreaker/monitoring/health/CircuitBreakerHealthIndicatorTest.java index 4f27dba27b..6dd2fc34cf 100644 --- a/resilience4j-spring-boot-common/src/test/java/io/github/resilience4j/circuitbreaker/monitoring/health/CircuitBreakerHealthIndicatorTest.java +++ b/resilience4j-spring-boot-common/src/test/java/io/github/resilience4j/circuitbreaker/monitoring/health/CircuitBreakerHealthIndicatorTest.java @@ -47,7 +47,7 @@ public void healthMetricsAndConfig() { then(health.getDetails()) .contains( entry("failureRate", "0.2%"), - entry("failureRateThreshold", "0.3%"), + entry("setFailureRateThreshold", "0.3%"), entry("bufferedCalls", 100), entry("failedCalls", 20), entry("notPermittedCalls", 0L), diff --git a/resilience4j-spring-boot/build.gradle b/resilience4j-spring-boot/build.gradle index 65c6d1f74c..04ae30b92b 100644 --- a/resilience4j-spring-boot/build.gradle +++ b/resilience4j-spring-boot/build.gradle @@ -25,6 +25,9 @@ dependencies { testCompile project(':resilience4j-prometheus') testCompile project(':resilience4j-metrics') testCompile(libraries.prometheus_spring_boot) + testCompile project(':resilience4j-reactor') + testCompile project(':resilience4j-rxjava2') + testCompile project(':resilience4j-micrometer') } compileJava.dependsOn(processResources) diff --git a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEndpoint.java b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEndpoint.java index cc47b9c6c0..9f5104fd91 100644 --- a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEndpoint.java +++ b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEndpoint.java @@ -17,6 +17,7 @@ import java.util.List; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEndpointResponse; import org.springframework.boot.actuate.endpoint.AbstractEndpoint; import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventsEndpoint.java b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventsEndpoint.java index 25bd2dbe0c..e7472fc5e9 100644 --- a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventsEndpoint.java +++ b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventsEndpoint.java @@ -16,6 +16,9 @@ package io.github.resilience4j.bulkhead.monitoring.endpoint; import io.github.resilience4j.bulkhead.event.BulkheadEvent; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventDTO; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventDTOFactory; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventsEndpointResponse; import io.github.resilience4j.consumer.CircularEventConsumer; import io.github.resilience4j.consumer.EventConsumerRegistry; import io.vavr.collection.List; diff --git a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/autoconfigure/CircuitBreakerConfigurationOnMissingBean.java b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/autoconfigure/CircuitBreakerConfigurationOnMissingBean.java index dd9e6dfdc4..dde147bdb8 100644 --- a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/autoconfigure/CircuitBreakerConfigurationOnMissingBean.java +++ b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/autoconfigure/CircuitBreakerConfigurationOnMissingBean.java @@ -42,7 +42,7 @@ public EventConsumerRegistry eventConsumerRegistry() { protected void createHealthIndicatorForCircuitBreaker(CircuitBreaker circuitBreaker, CircuitBreakerConfigurationProperties circuitBreakerProperties) { boolean registerHealthIndicator = circuitBreakerProperties.findCircuitBreakerProperties(circuitBreaker.getName()) - .map(CircuitBreakerConfigurationProperties.BackendProperties::getRegisterHealthIndicator) + .map(io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties::getRegisterHealthIndicator) .orElse(true); if(registerHealthIndicator){ diff --git a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpoint.java b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpoint.java index 7163666750..f9e2c5c322 100644 --- a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpoint.java +++ b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpoint.java @@ -15,6 +15,7 @@ */ package io.github.resilience4j.circuitbreaker.monitoring.endpoint; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEndpointResponse; import org.springframework.boot.actuate.endpoint.AbstractEndpoint; import org.springframework.boot.actuate.endpoint.Endpoint; import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpoint.java b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpoint.java index a3932476cd..962ed29a11 100644 --- a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpoint.java +++ b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpoint.java @@ -17,6 +17,8 @@ import io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTOFactory; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse; import io.github.resilience4j.consumer.CircularEventConsumer; import io.github.resilience4j.consumer.EventConsumerRegistry; import io.vavr.collection.List; diff --git a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/autoconfigure/RateLimiterAutoConfiguration.java b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/autoconfigure/RateLimiterAutoConfiguration.java index 525130d0e9..1560afac96 100644 --- a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/autoconfigure/RateLimiterAutoConfiguration.java +++ b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/autoconfigure/RateLimiterAutoConfiguration.java @@ -15,8 +15,13 @@ */ package io.github.resilience4j.ratelimiter.autoconfigure; -import javax.annotation.PostConstruct; - +import io.github.resilience4j.consumer.EventConsumerRegistry; +import io.github.resilience4j.ratelimiter.RateLimiter; +import io.github.resilience4j.ratelimiter.RateLimiterRegistry; +import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; +import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEndpoint; +import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpoint; +import io.github.resilience4j.ratelimiter.monitoring.health.RateLimiterHealthIndicator; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigureBefore; @@ -26,13 +31,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import io.github.resilience4j.consumer.EventConsumerRegistry; -import io.github.resilience4j.ratelimiter.RateLimiter; -import io.github.resilience4j.ratelimiter.RateLimiterRegistry; -import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEndpoint; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpoint; -import io.github.resilience4j.ratelimiter.monitoring.health.RateLimiterHealthIndicator; +import javax.annotation.PostConstruct; /** * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration @@ -66,7 +65,7 @@ public RateLimiterEventsEndpoint rateLimiterEventsEndpoint(EventConsumerRegistry @PostConstruct public void configureHealthIndicators() { - rateLimiterProperties.getLimiters().forEach( + rateLimiterProperties.getInstances().forEach( (name, properties) -> { RateLimiter rateLimiter = rateLimiterRegistry.rateLimiter(name); if (properties.getRegisterHealthIndicator()) { diff --git a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEndpoint.java b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEndpoint.java index 0a824e269f..d46ad4d2e3 100644 --- a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEndpoint.java +++ b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEndpoint.java @@ -15,6 +15,7 @@ */ package io.github.resilience4j.ratelimiter.monitoring.endpoint; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEndpointResponse; import io.github.resilience4j.ratelimiter.RateLimiter; import io.github.resilience4j.ratelimiter.RateLimiterRegistry; diff --git a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpoint.java b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpoint.java index e5487d3f67..f34509c6ef 100644 --- a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpoint.java +++ b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpoint.java @@ -15,6 +15,8 @@ */ package io.github.resilience4j.ratelimiter.monitoring.endpoint; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventDTO; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpointResponse; import io.github.resilience4j.consumer.CircularEventConsumer; import io.github.resilience4j.consumer.EventConsumerRegistry; import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; diff --git a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEndpoint.java b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEndpoint.java index b49d9f0575..a64775e240 100644 --- a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEndpoint.java +++ b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEndpoint.java @@ -18,6 +18,7 @@ import java.util.List; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEndpointResponse; import org.springframework.boot.actuate.endpoint.AbstractEndpoint; import org.springframework.boot.context.properties.ConfigurationProperties; diff --git a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventsEndpoint.java b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventsEndpoint.java index c64d7edf6b..b88742118d 100644 --- a/resilience4j-spring-boot/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventsEndpoint.java +++ b/resilience4j-spring-boot/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventsEndpoint.java @@ -16,6 +16,8 @@ package io.github.resilience4j.retry.monitoring.endpoint; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventDTOFactory; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventsEndpointResponse; import io.github.resilience4j.consumer.CircularEventConsumer; import io.github.resilience4j.consumer.EventConsumerRegistry; import io.github.resilience4j.retry.event.RetryEvent; diff --git a/resilience4j-spring-boot/src/test/java/io/github/resilience4j/bulkhead/BulkheadAutoConfigurationTest.java b/resilience4j-spring-boot/src/test/java/io/github/resilience4j/bulkhead/BulkheadAutoConfigurationTest.java index 992e17c118..733ebee03f 100644 --- a/resilience4j-spring-boot/src/test/java/io/github/resilience4j/bulkhead/BulkheadAutoConfigurationTest.java +++ b/resilience4j-spring-boot/src/test/java/io/github/resilience4j/bulkhead/BulkheadAutoConfigurationTest.java @@ -18,9 +18,9 @@ import io.github.resilience4j.bulkhead.autoconfigure.BulkheadProperties; import io.github.resilience4j.bulkhead.configure.BulkheadAspect; import io.github.resilience4j.bulkhead.event.BulkheadEvent; -import io.github.resilience4j.bulkhead.monitoring.endpoint.BulkheadEndpointResponse; -import io.github.resilience4j.bulkhead.monitoring.endpoint.BulkheadEventDTO; -import io.github.resilience4j.bulkhead.monitoring.endpoint.BulkheadEventsEndpointResponse; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEndpointResponse; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventDTO; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventsEndpointResponse; import io.github.resilience4j.service.test.BulkheadDummyService; import io.github.resilience4j.service.test.TestApplication; import org.junit.Test; diff --git a/resilience4j-spring-boot/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationTest.java b/resilience4j-spring-boot/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationTest.java index 638999dec0..da3e6bcae1 100644 --- a/resilience4j-spring-boot/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationTest.java +++ b/resilience4j-spring-boot/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationTest.java @@ -17,8 +17,8 @@ import io.github.resilience4j.circuitbreaker.autoconfigure.CircuitBreakerProperties; import io.github.resilience4j.circuitbreaker.configure.CircuitBreakerAspect; -import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEndpointResponse; -import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEndpointResponse; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse; import io.github.resilience4j.service.test.DummyService; import io.github.resilience4j.service.test.TestApplication; import io.prometheus.client.CollectorRegistry; @@ -76,7 +76,7 @@ public void testCircuitBreakerAutoConfiguration() throws IOException { try { dummyService.doSomething(true); } catch (IOException ex) { - // Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure. + // Do nothing. The IOException is recorded by the CircuitBreaker as part of the setRecordFailurePredicate as a failure. } // The invocation is recorded by the CircuitBreaker as a success. dummyService.doSomething(false); @@ -113,8 +113,8 @@ public void testCircuitBreakerAutoConfiguration() throws IOException { assertThat(healthResponse.getBody()).doesNotContain("backendBCircuitBreaker"); assertThat(healthResponse.getBody()).doesNotContain("dynamicBackend"); - // Verify that an exception for which recordFailurePredicate returns false and it is not included in - // recordExceptions evaluates to false. + // Verify that an exception for which setRecordFailurePredicate returns false and it is not included in + // setRecordExceptions evaluates to false. assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new Exception())).isFalse(); assertThat(circuitBreakerAspect.getOrder()).isEqualTo(400); @@ -123,7 +123,7 @@ public void testCircuitBreakerAutoConfiguration() throws IOException { CircuitBreaker sharedA = circuitBreakerRegistry.circuitBreaker("backendSharedA"); CircuitBreaker sharedB = circuitBreakerRegistry.circuitBreaker("backendSharedB"); - Duration defaultWaitDuration = Duration.ofSeconds(10L); + Duration defaultWaitDuration = Duration.ofSeconds(60L); float defaultFailureRate = 60f; int defaultRingBufferSizeInHalfOpenState = 10; int defaultRingBufferSizeInClosedState = 100; diff --git a/resilience4j-spring-boot/src/test/java/io/github/resilience4j/ratelimiter/RateLimiterAutoConfigurationTest.java b/resilience4j-spring-boot/src/test/java/io/github/resilience4j/ratelimiter/RateLimiterAutoConfigurationTest.java index 2e10a587f5..04a45d4d9c 100644 --- a/resilience4j-spring-boot/src/test/java/io/github/resilience4j/ratelimiter/RateLimiterAutoConfigurationTest.java +++ b/resilience4j-spring-boot/src/test/java/io/github/resilience4j/ratelimiter/RateLimiterAutoConfigurationTest.java @@ -18,9 +18,9 @@ import io.github.resilience4j.ratelimiter.autoconfigure.RateLimiterProperties; import io.github.resilience4j.ratelimiter.configure.RateLimiterAspect; import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEndpointResponse; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEventDTO; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpointResponse; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEndpointResponse; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventDTO; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpointResponse; import io.github.resilience4j.service.test.DummyService; import io.github.resilience4j.service.test.TestApplication; import io.prometheus.client.CollectorRegistry; diff --git a/resilience4j-spring-boot/src/test/java/io/github/resilience4j/retry/AsyncRetryAutoConfigurationTest.java b/resilience4j-spring-boot/src/test/java/io/github/resilience4j/retry/AsyncRetryAutoConfigurationTest.java index aadb0794d6..b60897ced3 100644 --- a/resilience4j-spring-boot/src/test/java/io/github/resilience4j/retry/AsyncRetryAutoConfigurationTest.java +++ b/resilience4j-spring-boot/src/test/java/io/github/resilience4j/retry/AsyncRetryAutoConfigurationTest.java @@ -17,8 +17,8 @@ import io.github.resilience4j.circuitbreaker.IgnoredException; import io.github.resilience4j.retry.autoconfigure.RetryProperties; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEndpointResponse; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEventsEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventsEndpointResponse; import io.github.resilience4j.service.test.RetryDummyService; import io.github.resilience4j.service.test.TestApplication; import org.junit.Test; diff --git a/resilience4j-spring-boot/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationTest.java b/resilience4j-spring-boot/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationTest.java index 4cff17e759..15429d2905 100644 --- a/resilience4j-spring-boot/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationTest.java +++ b/resilience4j-spring-boot/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationTest.java @@ -18,8 +18,8 @@ import io.github.resilience4j.circuitbreaker.IgnoredException; import io.github.resilience4j.retry.autoconfigure.RetryProperties; import io.github.resilience4j.retry.configure.RetryAspect; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEndpointResponse; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEventsEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventsEndpointResponse; import io.github.resilience4j.service.test.RetryDummyService; import io.github.resilience4j.service.test.TestApplication; import org.junit.Test; diff --git a/resilience4j-spring-boot/src/test/resources/application.yaml b/resilience4j-spring-boot/src/test/resources/application.yaml index f4c6f81571..68980c243b 100644 --- a/resilience4j-spring-boot/src/test/resources/application.yaml +++ b/resilience4j-spring-boot/src/test/resources/application.yaml @@ -3,7 +3,7 @@ resilience4j.retry: backends: retryBackendA: maxRetryAttempts: 3 - waitDuration: 600 + waitDurationMillis: 600 retryExceptionPredicate: io.github.resilience4j.circuitbreaker.RecordFailurePredicate retryExceptions: - java.io.IOException @@ -11,7 +11,7 @@ resilience4j.retry: - io.github.resilience4j.circuitbreaker.IgnoredException retryBackendB: maxRetryAttempts: 3 - waitDuration: 600 + waitDurationMillis: 600 retryExceptionPredicate: io.github.resilience4j.circuitbreaker.RecordFailurePredicate retryExceptions: - java.io.IOException @@ -58,23 +58,23 @@ resilience4j.ratelimiter: limiters: backendA: limitForPeriod: 10 - limitRefreshPeriodInMillis: 1000 + limitRefreshPeriodInNanos: 1000000000 timeoutInMillis: 0 subscribeForEvents: true registerHealthIndicator: true backendB: limitForPeriod: 100 - limitRefreshPeriodInMillis: 500 + limitRefreshPeriodInNanos: 500000000 timeoutInMillis: 3000 resilience4j.bulkhead: bulkheadAspectOrder: 398 backends: backendA: - maxConcurrentCall: 1 + maxConcurrentCalls: 1 backendB: maxWaitTime: 10 - maxConcurrentCall: 2 + maxConcurrentCalls: 2 management.security.enabled: false management.endpoint.health.show-details: always \ No newline at end of file diff --git a/resilience4j-spring-boot2/build.gradle b/resilience4j-spring-boot2/build.gradle index ccaee24ef9..430616f3f7 100644 --- a/resilience4j-spring-boot2/build.gradle +++ b/resilience4j-spring-boot2/build.gradle @@ -19,6 +19,7 @@ dependencies { testCompile(libraries.reactor) testCompile(libraries.metrics) testCompile(libraries.spring_boot2_actuator) + testCompile project(':resilience4j-micrometer') } compileJava.dependsOn(processResources) diff --git a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEndpoint.java b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEndpoint.java index 0bb954dcee..3082b8584f 100644 --- a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEndpoint.java +++ b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEndpoint.java @@ -15,15 +15,15 @@ */ package io.github.resilience4j.bulkhead.monitoring.endpoint; -import java.util.List; - -import org.springframework.boot.actuate.endpoint.annotation.Endpoint; -import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; - import io.github.resilience4j.bulkhead.Bulkhead; import io.github.resilience4j.bulkhead.BulkheadRegistry; import io.github.resilience4j.bulkhead.ThreadPoolBulkhead; import io.github.resilience4j.bulkhead.ThreadPoolBulkheadRegistry; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEndpointResponse; +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; +import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; + +import java.util.List; /** diff --git a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventsEndpoint.java b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventsEndpoint.java index 269d61765e..7fbfb1ed6e 100644 --- a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventsEndpoint.java +++ b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/bulkhead/monitoring/endpoint/BulkheadEventsEndpoint.java @@ -15,17 +15,19 @@ */ package io.github.resilience4j.bulkhead.monitoring.endpoint; -import java.util.Comparator; - -import org.springframework.boot.actuate.endpoint.annotation.Endpoint; -import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; -import org.springframework.boot.actuate.endpoint.annotation.Selector; - import io.github.resilience4j.bulkhead.ThreadPoolBulkhead; import io.github.resilience4j.bulkhead.event.BulkheadEvent; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventDTO; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventDTOFactory; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventsEndpointResponse; import io.github.resilience4j.consumer.CircularEventConsumer; import io.github.resilience4j.consumer.EventConsumerRegistry; import io.vavr.collection.List; +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; +import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; +import org.springframework.boot.actuate.endpoint.annotation.Selector; + +import java.util.Comparator; @Endpoint(id = "bulkheadevents") public class BulkheadEventsEndpoint { diff --git a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/autoconfigure/CircuitBreakerConfigurationOnMissingBean.java b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/autoconfigure/CircuitBreakerConfigurationOnMissingBean.java index c133bff090..66cf007939 100644 --- a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/autoconfigure/CircuitBreakerConfigurationOnMissingBean.java +++ b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/autoconfigure/CircuitBreakerConfigurationOnMissingBean.java @@ -57,7 +57,7 @@ public EventConsumerRegistry eventConsumerRegistry() { @Override protected void createHealthIndicatorForCircuitBreaker(CircuitBreaker circuitBreaker, CircuitBreakerConfigurationProperties circuitBreakerProperties) { boolean registerHealthIndicator = circuitBreakerProperties.findCircuitBreakerProperties(circuitBreaker.getName()) - .map(CircuitBreakerConfigurationProperties.BackendProperties::getRegisterHealthIndicator) + .map(io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties::getRegisterHealthIndicator) .orElse(true); if (registerHealthIndicator) { diff --git a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpoint.java b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpoint.java index 611dd1ce6d..f8233b0393 100644 --- a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpoint.java +++ b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEndpoint.java @@ -18,6 +18,7 @@ import java.util.List; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEndpointResponse; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; diff --git a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpoint.java b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpoint.java index ebffdddade..820247212f 100644 --- a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpoint.java +++ b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/circuitbreaker/monitoring/endpoint/CircuitBreakerEventsEndpoint.java @@ -17,6 +17,8 @@ import io.github.resilience4j.circuitbreaker.event.CircuitBreakerEvent; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventDTOFactory; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse; import io.github.resilience4j.consumer.CircularEventConsumer; import io.github.resilience4j.consumer.EventConsumerRegistry; import io.vavr.collection.List; diff --git a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/autoconfigure/RateLimiterAutoConfiguration.java b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/autoconfigure/RateLimiterAutoConfiguration.java index d21908057d..0101ee9f56 100644 --- a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/autoconfigure/RateLimiterAutoConfiguration.java +++ b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/autoconfigure/RateLimiterAutoConfiguration.java @@ -15,8 +15,14 @@ */ package io.github.resilience4j.ratelimiter.autoconfigure; -import javax.annotation.PostConstruct; - +import io.github.resilience4j.consumer.EventConsumerRegistry; +import io.github.resilience4j.fallback.autoconfigure.FallbackConfigurationOnMissingBean; +import io.github.resilience4j.ratelimiter.RateLimiter; +import io.github.resilience4j.ratelimiter.RateLimiterRegistry; +import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; +import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEndpoint; +import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpoint; +import io.github.resilience4j.ratelimiter.monitoring.health.RateLimiterHealthIndicator; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; @@ -27,14 +33,7 @@ import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import io.github.resilience4j.consumer.EventConsumerRegistry; -import io.github.resilience4j.ratelimiter.RateLimiter; -import io.github.resilience4j.ratelimiter.RateLimiterRegistry; -import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEndpoint; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpoint; -import io.github.resilience4j.ratelimiter.monitoring.health.RateLimiterHealthIndicator; -import io.github.resilience4j.fallback.autoconfigure.FallbackConfigurationOnMissingBean; +import javax.annotation.PostConstruct; /** * {@link org.springframework.boot.autoconfigure.EnableAutoConfiguration @@ -70,7 +69,7 @@ public RateLimiterEventsEndpoint rateLimiterEventsEndpoint(EventConsumerRegistry @PostConstruct public void configureHealthIndicators() { - rateLimiterProperties.getLimiters().forEach( + rateLimiterProperties.getInstances().forEach( (name, properties) -> { if (properties.getRegisterHealthIndicator()) { createHealthIndicatorForLimiter(name); diff --git a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEndpoint.java b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEndpoint.java index c93b754871..891171b70c 100644 --- a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEndpoint.java +++ b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEndpoint.java @@ -15,6 +15,7 @@ */ package io.github.resilience4j.ratelimiter.monitoring.endpoint; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEndpointResponse; import io.github.resilience4j.ratelimiter.RateLimiter; import io.github.resilience4j.ratelimiter.RateLimiterRegistry; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; diff --git a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpoint.java b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpoint.java index 5081edef87..cf78bc4c25 100644 --- a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpoint.java +++ b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/ratelimiter/monitoring/endpoint/RateLimiterEventsEndpoint.java @@ -15,6 +15,8 @@ */ package io.github.resilience4j.ratelimiter.monitoring.endpoint; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventDTO; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpointResponse; import io.github.resilience4j.consumer.CircularEventConsumer; import io.github.resilience4j.consumer.EventConsumerRegistry; import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; diff --git a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEndpoint.java b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEndpoint.java index 48159e7a2a..3b51de2444 100644 --- a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEndpoint.java +++ b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEndpoint.java @@ -18,6 +18,7 @@ import java.util.List; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEndpointResponse; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; diff --git a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventsEndpoint.java b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventsEndpoint.java index df78abf300..f2cc2785d7 100644 --- a/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventsEndpoint.java +++ b/resilience4j-spring-boot2/src/main/java/io/github/resilience4j/retry/monitoring/endpoint/RetryEventsEndpoint.java @@ -16,6 +16,8 @@ package io.github.resilience4j.retry.monitoring.endpoint; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventDTOFactory; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventsEndpointResponse; import io.github.resilience4j.consumer.CircularEventConsumer; import io.github.resilience4j.consumer.EventConsumerRegistry; import io.github.resilience4j.retry.event.RetryEvent; diff --git a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/bulkhead/BulkheadAutoConfigurationTest.java b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/bulkhead/BulkheadAutoConfigurationTest.java index 242e1c98a7..f0048cd262 100644 --- a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/bulkhead/BulkheadAutoConfigurationTest.java +++ b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/bulkhead/BulkheadAutoConfigurationTest.java @@ -36,9 +36,9 @@ import io.github.resilience4j.bulkhead.autoconfigure.ThreadPoolBulkheadProperties; import io.github.resilience4j.bulkhead.configure.BulkheadAspect; import io.github.resilience4j.bulkhead.event.BulkheadEvent; -import io.github.resilience4j.bulkhead.monitoring.endpoint.BulkheadEndpointResponse; -import io.github.resilience4j.bulkhead.monitoring.endpoint.BulkheadEventDTO; -import io.github.resilience4j.bulkhead.monitoring.endpoint.BulkheadEventsEndpointResponse; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEndpointResponse; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventDTO; +import io.github.resilience4j.common.bulkhead.monitoring.endpoint.BulkheadEventsEndpointResponse; import io.github.resilience4j.service.test.TestApplication; import io.github.resilience4j.service.test.bulkhead.BulkheadDummyService; import io.github.resilience4j.service.test.bulkhead.BulkheadReactiveDummyService; diff --git a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationRxJava2Test.java b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationRxJava2Test.java index c202a8bded..f2cd181efd 100644 --- a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationRxJava2Test.java +++ b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationRxJava2Test.java @@ -17,8 +17,8 @@ import io.github.resilience4j.circuitbreaker.autoconfigure.CircuitBreakerProperties; import io.github.resilience4j.circuitbreaker.configure.CircuitBreakerAspect; -import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEndpointResponse; -import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEndpointResponse; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse; import io.github.resilience4j.service.test.DummyService; import io.github.resilience4j.service.test.ReactiveDummyService; import io.github.resilience4j.service.test.TestApplication; @@ -73,7 +73,7 @@ public void testCircuitBreakerAutoConfigurationReactiveRxJava2() throws IOExcept try { reactiveDummyService.doSomethingFlowable(true).blockingSubscribe(String::toUpperCase, throwable -> System.out.println("Exception received:" + throwable.getMessage())); } catch (Exception ex) { - // Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure. + // Do nothing. The IOException is recorded by the CircuitBreaker as part of the setRecordFailurePredicate as a failure. } // The invocation is recorded by the CircuitBreaker as a success. reactiveDummyService.doSomethingFlowable(false).blockingSubscribe(String::toUpperCase, throwable -> System.out.println("Exception received:" + throwable.getMessage())); @@ -106,7 +106,7 @@ public void testCircuitBreakerAutoConfigurationReactiveRxJava2() throws IOExcept try { reactiveDummyService.doSomethingObservable(true).blockingSubscribe(String::toUpperCase, Throwable::getCause); } catch (IOException ex) { - // Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure. + // Do nothing. The IOException is recorded by the CircuitBreaker as part of the setRecordFailurePredicate as a failure. } // The invocation is recorded by the CircuitBreaker as a success. reactiveDummyService.doSomethingObservable(false).blockingSubscribe(String::toUpperCase, Throwable::getCause); @@ -119,7 +119,7 @@ public void testCircuitBreakerAutoConfigurationReactiveRxJava2() throws IOExcept try { reactiveDummyService.doSomethingMaybe(true).blockingGet("goo"); } catch (Exception ex) { - // Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure. + // Do nothing. The IOException is recorded by the CircuitBreaker as part of the setRecordFailurePredicate as a failure. } // The invocation is recorded by the CircuitBreaker as a success. reactiveDummyService.doSomethingMaybe(false).blockingGet(); @@ -132,7 +132,7 @@ public void testCircuitBreakerAutoConfigurationReactiveRxJava2() throws IOExcept try { reactiveDummyService.doSomethingSingle(true).blockingGet(); } catch (Exception ex) { - // Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure. + // Do nothing. The IOException is recorded by the CircuitBreaker as part of the setRecordFailurePredicate as a failure. } // The invocation is recorded by the CircuitBreaker as a success. reactiveDummyService.doSomethingSingle(false).blockingGet(); @@ -146,7 +146,7 @@ public void testCircuitBreakerAutoConfigurationReactiveRxJava2() throws IOExcept try { reactiveDummyService.doSomethingCompletable(true).blockingAwait(); } catch (Exception ex) { - // Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure. + // Do nothing. The IOException is recorded by the CircuitBreaker as part of the setRecordFailurePredicate as a failure. } // The invocation is recorded by the CircuitBreaker as a success. reactiveDummyService.doSomethingCompletable(false).blockingAwait(); diff --git a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationTest.java b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationTest.java index 312632ad5c..94dff35d96 100644 --- a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationTest.java +++ b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/circuitbreaker/CircuitBreakerAutoConfigurationTest.java @@ -17,8 +17,8 @@ import io.github.resilience4j.circuitbreaker.autoconfigure.CircuitBreakerProperties; import io.github.resilience4j.circuitbreaker.configure.CircuitBreakerAspect; -import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEndpointResponse; -import io.github.resilience4j.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEndpointResponse; +import io.github.resilience4j.common.circuitbreaker.monitoring.endpoint.CircuitBreakerEventsEndpointResponse; import io.github.resilience4j.service.test.DummyService; import io.github.resilience4j.service.test.ReactiveDummyService; import io.github.resilience4j.service.test.TestApplication; @@ -75,7 +75,7 @@ public void testCircuitBreakerAutoConfiguration() throws IOException { try { dummyService.doSomething(true); } catch (IOException ex) { - // Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure. + // Do nothing. The IOException is recorded by the CircuitBreaker as part of the setRecordFailurePredicate as a failure. } // The invocation is recorded by the CircuitBreaker as a success. dummyService.doSomething(false); @@ -91,7 +91,7 @@ public void testCircuitBreakerAutoConfiguration() throws IOException { assertThat(circuitBreaker.getCircuitBreakerConfig().getRingBufferSizeInClosedState()).isEqualTo(6); assertThat(circuitBreaker.getCircuitBreakerConfig().getRingBufferSizeInHalfOpenState()).isEqualTo(2); assertThat(circuitBreaker.getCircuitBreakerConfig().getFailureRateThreshold()).isEqualTo(70f); - assertThat(circuitBreaker.getCircuitBreakerConfig().getWaitDurationInOpenState()).isEqualByComparingTo(Duration.ofSeconds(5L)); + assertThat(circuitBreaker.getCircuitBreakerConfig().getWaitDurationInOpenState()).isEqualByComparingTo(Duration.ofSeconds(60L)); // Create CircuitBreaker dynamically with default config CircuitBreaker dynamicCircuitBreaker = circuitBreakerRegistry.circuitBreaker("dynamicBackend"); @@ -119,8 +119,8 @@ public void testCircuitBreakerAutoConfiguration() throws IOException { assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new RecordedException())).isTrue(); assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new IgnoredException())).isFalse(); - // Verify that an exception for which recordFailurePredicate returns false and it is not included in - // recordExceptions evaluates to false. + // Verify that an exception for which setRecordFailurePredicate returns false and it is not included in + // setRecordExceptions evaluates to false. assertThat(circuitBreaker.getCircuitBreakerConfig().getRecordFailurePredicate().test(new Exception())).isFalse(); // expect aspect configured as defined in application.yml @@ -130,7 +130,7 @@ public void testCircuitBreakerAutoConfiguration() throws IOException { CircuitBreaker sharedA = circuitBreakerRegistry.circuitBreaker("backendSharedA"); CircuitBreaker sharedB = circuitBreakerRegistry.circuitBreaker("backendSharedB"); - Duration defaultWaitDuration = Duration.ofSeconds(10L); + Duration defaultWaitDuration = Duration.ofSeconds(60L); float defaultFailureRate = 60f; int defaultRingBufferSizeInHalfOpenState = 10; int defaultRingBufferSizeInClosedState = 100; @@ -165,7 +165,7 @@ public void testCircuitBreakerAutoConfigurationAsync() throws IOException, Execu try { dummyService.doSomethingAsync(true); } catch (IOException ex) { - // Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure. + // Do nothing. The IOException is recorded by the CircuitBreaker as part of the setRecordFailurePredicate as a failure. } // The invocation is recorded by the CircuitBreaker as a success. final CompletableFuture stringCompletionStage = dummyService.doSomethingAsync(false); @@ -213,7 +213,7 @@ public void testCircuitBreakerAutoConfigurationReactive() throws IOException { try { reactiveDummyService.doSomethingFlux(true).subscribe(String::toUpperCase, throwable -> System.out.println("Exception received:" + throwable.getMessage())); } catch (IOException ex) { - // Do nothing. The IOException is recorded by the CircuitBreaker as part of the recordFailurePredicate as a failure. + // Do nothing. The IOException is recorded by the CircuitBreaker as part of the setRecordFailurePredicate as a failure. } // The invocation is recorded by the CircuitBreaker as a success. reactiveDummyService.doSomethingFlux(false).subscribe(String::toUpperCase, throwable -> System.out.println("Exception received:" + throwable.getMessage())); @@ -226,7 +226,7 @@ public void testCircuitBreakerAutoConfigurationReactive() throws IOException { assertThat(circuitBreaker.getCircuitBreakerConfig().getRingBufferSizeInClosedState()).isEqualTo(10); assertThat(circuitBreaker.getCircuitBreakerConfig().getRingBufferSizeInHalfOpenState()).isEqualTo(5); assertThat(circuitBreaker.getCircuitBreakerConfig().getFailureRateThreshold()).isEqualTo(50f); - assertThat(circuitBreaker.getCircuitBreakerConfig().getWaitDurationInOpenState()).isEqualByComparingTo(Duration.ofSeconds(5L)); + assertThat(circuitBreaker.getCircuitBreakerConfig().getWaitDurationInOpenState()).isEqualByComparingTo(Duration.ofSeconds(60L)); // expect circuitbreakers actuator endpoint contains all circuitbreakers ResponseEntity circuitBreakerList = restTemplate.getForEntity("/actuator/circuitbreakers", CircuitBreakerEndpointResponse.class); diff --git a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/ratelimiter/RateLimiterAutoConfigurationTest.java b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/ratelimiter/RateLimiterAutoConfigurationTest.java index 4ded6c2612..720a3909c7 100644 --- a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/ratelimiter/RateLimiterAutoConfigurationTest.java +++ b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/ratelimiter/RateLimiterAutoConfigurationTest.java @@ -18,9 +18,9 @@ import io.github.resilience4j.ratelimiter.autoconfigure.RateLimiterProperties; import io.github.resilience4j.ratelimiter.configure.RateLimiterAspect; import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEndpointResponse; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEventDTO; -import io.github.resilience4j.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpointResponse; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEndpointResponse; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventDTO; +import io.github.resilience4j.common.ratelimiter.monitoring.endpoint.RateLimiterEventsEndpointResponse; import io.github.resilience4j.service.test.DummyService; import io.github.resilience4j.service.test.TestApplication; import org.junit.Test; diff --git a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationAsyncTest.java b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationAsyncTest.java index b96f409b2e..7076cf09ce 100644 --- a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationAsyncTest.java +++ b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationAsyncTest.java @@ -18,8 +18,8 @@ import io.github.resilience4j.circuitbreaker.IgnoredException; import io.github.resilience4j.retry.autoconfigure.RetryProperties; import io.github.resilience4j.retry.configure.RetryAspect; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEndpointResponse; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEventsEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventsEndpointResponse; import io.github.resilience4j.service.test.TestApplication; import io.github.resilience4j.service.test.retry.RetryDummyService; import org.junit.Test; diff --git a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationReactorTest.java b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationReactorTest.java index f241cab38b..2077433134 100644 --- a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationReactorTest.java +++ b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationReactorTest.java @@ -19,8 +19,8 @@ import io.github.resilience4j.reactor.retry.RetryExceptionWrapper; import io.github.resilience4j.retry.autoconfigure.RetryProperties; import io.github.resilience4j.retry.configure.RetryAspect; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEndpointResponse; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEventsEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventsEndpointResponse; import io.github.resilience4j.service.test.TestApplication; import io.github.resilience4j.service.test.retry.ReactiveRetryDummyService; import org.junit.Test; diff --git a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationRxJavaTest.java b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationRxJavaTest.java index 9982428aec..bb248fb2f3 100644 --- a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationRxJavaTest.java +++ b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationRxJavaTest.java @@ -19,8 +19,8 @@ import io.github.resilience4j.reactor.retry.RetryExceptionWrapper; import io.github.resilience4j.retry.autoconfigure.RetryProperties; import io.github.resilience4j.retry.configure.RetryAspect; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEndpointResponse; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEventsEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventsEndpointResponse; import io.github.resilience4j.service.test.TestApplication; import io.github.resilience4j.service.test.retry.ReactiveRetryDummyService; import org.junit.Test; diff --git a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationTest.java b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationTest.java index 17afa97fd2..69b09d04df 100644 --- a/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationTest.java +++ b/resilience4j-spring-boot2/src/test/java/io/github/resilience4j/retry/RetryAutoConfigurationTest.java @@ -18,8 +18,8 @@ import io.github.resilience4j.circuitbreaker.IgnoredException; import io.github.resilience4j.retry.autoconfigure.RetryProperties; import io.github.resilience4j.retry.configure.RetryAspect; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEndpointResponse; -import io.github.resilience4j.retry.monitoring.endpoint.RetryEventsEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEndpointResponse; +import io.github.resilience4j.common.retry.monitoring.endpoint.RetryEventsEndpointResponse; import io.github.resilience4j.service.test.TestApplication; import io.github.resilience4j.service.test.retry.RetryDummyService; import org.junit.Test; diff --git a/resilience4j-spring-boot2/src/test/resources/application.yaml b/resilience4j-spring-boot2/src/test/resources/application.yaml index f9e4255a7f..14c252f3bb 100644 --- a/resilience4j-spring-boot2/src/test/resources/application.yaml +++ b/resilience4j-spring-boot2/src/test/resources/application.yaml @@ -3,7 +3,7 @@ resilience4j.retry: configs: default: maxRetryAttempts: 3 - waitDuration: 100 + waitDurationMillis: 100 retryExceptionPredicate: io.github.resilience4j.circuitbreaker.RecordFailurePredicate retryExceptions: - java.io.IOException @@ -16,7 +16,7 @@ resilience4j.retry: baseConfig: default retryBackendC: maxRetryAttempts: 3 - waitDuration: 100 + waitDurationMillis: 100 retryExceptionPredicate: io.github.resilience4j.circuitbreaker.RecordFailurePredicate retryExceptions: - java.lang.IllegalArgumentException @@ -63,7 +63,7 @@ resilience4j.ratelimiter: configs: default: limitForPeriod: 10 - limitRefreshPeriodInMillis: 1000 + limitRefreshPeriodInNanos: 1000000000 timeoutInMillis: 0 subscribeForEvents: true registerHealthIndicator: true @@ -74,7 +74,7 @@ resilience4j.ratelimiter: registerHealthIndicator: true backendB: limitForPeriod: 100 - limitRefreshPeriodInMillis: 500 + limitRefreshPeriodInNanos: 500000000 timeoutInMillis: 3000 resilience4j.bulkhead: @@ -82,10 +82,10 @@ resilience4j.bulkhead: configs: default: maxWaitTime: 10 - maxConcurrentCall: 2 + maxConcurrentCalls: 2 backends: backendA: - maxConcurrentCall: 1 + maxConcurrentCalls: 1 backendB: baseConfig: default diff --git a/resilience4j-spring/build.gradle b/resilience4j-spring/build.gradle index bcc76267a4..c7a3af52fc 100644 --- a/resilience4j-spring/build.gradle +++ b/resilience4j-spring/build.gradle @@ -1,10 +1,7 @@ dependencies { compile project(':resilience4j-annotations') compile project(':resilience4j-consumer') - compile project(':resilience4j-circuitbreaker') - compile project(':resilience4j-ratelimiter') - compile project(':resilience4j-retry') - compile project(':resilience4j-bulkhead') + compile project(':resilience4j-framework-common') compileOnly(libraries.aspectj) compileOnly(libraries.hibernate_validator) diff --git a/resilience4j-spring/src/main/java/io/github/resilience4j/bulkhead/configure/BulkheadConfiguration.java b/resilience4j-spring/src/main/java/io/github/resilience4j/bulkhead/configure/BulkheadConfiguration.java index 075ca83b7d..e651620672 100644 --- a/resilience4j-spring/src/main/java/io/github/resilience4j/bulkhead/configure/BulkheadConfiguration.java +++ b/resilience4j-spring/src/main/java/io/github/resilience4j/bulkhead/configure/BulkheadConfiguration.java @@ -57,7 +57,7 @@ public BulkheadRegistry bulkheadRegistry(BulkheadConfigurationProperties bulkhea EventConsumerRegistry bulkheadEventConsumerRegistry) { BulkheadRegistry bulkheadRegistry = createBulkheadRegistry(bulkheadConfigurationProperties); registerEventConsumer(bulkheadRegistry, bulkheadEventConsumerRegistry, bulkheadConfigurationProperties); - bulkheadConfigurationProperties.getBackends().forEach((name, properties) -> bulkheadRegistry.bulkhead(name, bulkheadConfigurationProperties.createBulkheadConfig(name))); + bulkheadConfigurationProperties.getInstances().forEach((name, properties) -> bulkheadRegistry.bulkhead(name, bulkheadConfigurationProperties.createBulkheadConfig(properties))); return bulkheadRegistry; } @@ -88,7 +88,7 @@ private void registerEventConsumer(BulkheadRegistry bulkheadRegistry, private void registerEventConsumer(EventConsumerRegistry eventConsumerRegistry, Bulkhead bulkHead, BulkheadConfigurationProperties bulkheadConfigurationProperties) { int eventConsumerBufferSize = Optional.ofNullable(bulkheadConfigurationProperties.getBackendProperties(bulkHead.getName())) - .map(BulkheadConfigurationProperties.BackendProperties::getEventConsumerBufferSize) + .map(io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties::getEventConsumerBufferSize) .orElse(100); bulkHead.getEventPublisher().onEvent(eventConsumerRegistry.createEventConsumer(bulkHead.getName(), eventConsumerBufferSize)); } diff --git a/resilience4j-spring/src/main/java/io/github/resilience4j/bulkhead/configure/BulkheadConfigurationProperties.java b/resilience4j-spring/src/main/java/io/github/resilience4j/bulkhead/configure/BulkheadConfigurationProperties.java index 392da8ee93..b6a80dd970 100644 --- a/resilience4j-spring/src/main/java/io/github/resilience4j/bulkhead/configure/BulkheadConfigurationProperties.java +++ b/resilience4j-spring/src/main/java/io/github/resilience4j/bulkhead/configure/BulkheadConfigurationProperties.java @@ -15,21 +15,7 @@ */ package io.github.resilience4j.bulkhead.configure; -import static io.github.resilience4j.bulkhead.BulkheadConfig.Builder; - -import java.util.HashMap; -import java.util.Map; - -import javax.validation.constraints.Min; - -import org.springframework.util.StringUtils; - -import io.github.resilience4j.bulkhead.Bulkhead; -import io.github.resilience4j.bulkhead.BulkheadConfig; -import io.github.resilience4j.core.ConfigurationNotFoundException; -import io.github.resilience4j.core.lang.Nullable; - -public class BulkheadConfigurationProperties { +public class BulkheadConfigurationProperties extends io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties { /* This property gives you control over Bulkhead aspect application order. By default Bulkhead will be executed BEFORE CircuitBreaker and RateLimiter. @@ -37,8 +23,6 @@ public class BulkheadConfigurationProperties { you explicitly define aspects execution sequence. */ private int bulkheadAspectOrder = Integer.MAX_VALUE - 2; - private Map backends = new HashMap<>(); - private Map configs = new HashMap<>(); public int getBulkheadAspectOrder() { return bulkheadAspectOrder; @@ -48,134 +32,4 @@ public void setBulkheadAspectOrder(int bulkheadAspectOrder) { this.bulkheadAspectOrder = bulkheadAspectOrder; } - @Nullable - public BackendProperties getBackendProperties(String backend) { - return backends.get(backend); - } - - public BulkheadConfig createBulkheadConfig(String backend) { - return createBulkheadConfig(getBackendProperties(backend)); - } - - public BulkheadConfig createBulkheadConfig(BackendProperties backendProperties) { - if (!StringUtils.isEmpty(backendProperties.getBaseConfig())) { - BackendProperties baseProperties = configs.get(backendProperties.getBaseConfig()); - if (baseProperties == null) { - throw new ConfigurationNotFoundException(backendProperties.getBaseConfig()); - } - return buildConfigFromBaseConfig(baseProperties, backendProperties); - } - return buildBulkheadConfig(BulkheadConfig.custom(), backendProperties); - } - - private BulkheadConfig buildConfigFromBaseConfig(BackendProperties baseProperties, BackendProperties backendProperties) { - BulkheadConfig baseConfig = buildBulkheadConfig(BulkheadConfig.custom(), baseProperties); - return buildBulkheadConfig(BulkheadConfig.from(baseConfig), backendProperties); - } - - public BulkheadConfig buildBulkheadConfig(Builder builder, BackendProperties properties) { - if (properties == null) { - return BulkheadConfig.custom().build(); - } - - if (properties.getMaxConcurrentCall() != null) { - builder.maxConcurrentCalls(properties.getMaxConcurrentCall()); - } - - if (properties.getMaxWaitTime() != null) { - builder.maxWaitTime(properties.getMaxWaitTime()); - } - - return builder.build(); - } - - public Map getBackends() { - return backends; - } - - public Map getConfigs() { - return configs; - } - - /** - * Class storing property values for configuring {@link Bulkhead} instances. - */ - public static class BackendProperties { - @Min(1) - private Integer maxConcurrentCall; - - @Min(0) - private Long maxWaitTime; - - @Min(1) - private Integer eventConsumerBufferSize = 100; - - @Nullable - private String baseConfig; - - /** - * Returns the max concurrent call of the bulkhead. - * - * @return the max concurrent call - */ - public Integer getMaxConcurrentCall() { - return maxConcurrentCall; - } - - /** - * Sets the max concurrent call of the bulkhead. - * - * @param maxConcurrentCall the max concurrent call - */ - public void setMaxConcurrentCall(Integer maxConcurrentCall) { - this.maxConcurrentCall = maxConcurrentCall; - } - - /** - * Returns the max wait time for the bulkhead in milliseconds. - * - * @return the failure rate threshold - */ - public Long getMaxWaitTime() { - return maxWaitTime; - } - - /** - * Sets the max wait time for the bulkhead in milliseconds. - * - * @param maxWaitTime the max wait time - */ - public void setMaxWaitTime(Long maxWaitTime) { - this.maxWaitTime = maxWaitTime; - } - - public Integer getEventConsumerBufferSize() { - return eventConsumerBufferSize; - } - - public void setEventConsumerBufferSize(Integer eventConsumerBufferSize) { - this.eventConsumerBufferSize = eventConsumerBufferSize; - } - - /** - * Gets the shared configuration name. If this is set, the configuration builder will use the the shared - * configuration backend over this one. - * - * @return The shared configuration name. - */ - @Nullable - public String getBaseConfig() { - return baseConfig; - } - - /** - * Sets the shared configuration name. If this is set, the configuration builder will use the the shared - * configuration backend over this one. - * - * @param baseConfig The shared configuration name. - */ - public void setBaseConfig(String baseConfig) { - this.baseConfig = baseConfig; - } - } } diff --git a/resilience4j-spring/src/main/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfiguration.java b/resilience4j-spring/src/main/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfiguration.java index b36d3929d0..b12295c385 100644 --- a/resilience4j-spring/src/main/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfiguration.java +++ b/resilience4j-spring/src/main/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfiguration.java @@ -108,7 +108,7 @@ public CircuitBreakerRegistry createCircuitBreakerRegistry(CircuitBreakerConfigu * @param circuitBreakerRegistry The circuit breaker registry. */ public void initCircuitBreakerRegistry(CircuitBreakerRegistry circuitBreakerRegistry) { - circuitBreakerProperties.getBackends().forEach( + circuitBreakerProperties.getInstances().forEach( (name, properties) -> circuitBreakerRegistry.circuitBreaker(name, circuitBreakerProperties.createCircuitBreakerConfig(properties)) ); } @@ -126,7 +126,7 @@ public void registerEventConsumer(CircuitBreakerRegistry circuitBreakerRegistry, private void registerEventConsumer(EventConsumerRegistry eventConsumerRegistry, CircuitBreaker circuitBreaker) { int eventConsumerBufferSize = circuitBreakerProperties.findCircuitBreakerProperties(circuitBreaker.getName()) - .map(CircuitBreakerConfigurationProperties.BackendProperties::getEventConsumerBufferSize) + .map(io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties::getEventConsumerBufferSize) .orElse(100); circuitBreaker.getEventPublisher().onEvent(eventConsumerRegistry.createEventConsumer(circuitBreaker.getName(), eventConsumerBufferSize)); } diff --git a/resilience4j-spring/src/main/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfigurationProperties.java b/resilience4j-spring/src/main/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfigurationProperties.java index 74af39d614..c02aa0b941 100644 --- a/resilience4j-spring/src/main/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfigurationProperties.java +++ b/resilience4j-spring/src/main/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfigurationProperties.java @@ -16,32 +16,15 @@ package io.github.resilience4j.circuitbreaker.configure; -import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig; -import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.Builder; -import io.github.resilience4j.core.ConfigurationNotFoundException; -import io.github.resilience4j.core.lang.Nullable; -import org.hibernate.validator.constraints.time.DurationMin; -import org.springframework.beans.BeanUtils; import org.springframework.context.annotation.Configuration; -import javax.validation.constraints.Max; -import javax.validation.constraints.Min; -import javax.validation.constraints.NotNull; -import java.time.Duration; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.function.Predicate; - @Configuration -public class CircuitBreakerConfigurationProperties { +public class CircuitBreakerConfigurationProperties extends io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties { // This property gives you control over CircuitBreaker aspect application order. // By default CircuitBreaker will be executed BEFORE RateLimiter. // By adjusting RateLimiterProperties.rateLimiterAspectOrder and CircuitBreakerProperties.circuitBreakerAspectOrder // you explicitly define aspects CircuitBreaker and RateLimiter execution sequence. private int circuitBreakerAspectOrder = Integer.MAX_VALUE - 1; - private Map backends = new HashMap<>(); - private Map configs = new HashMap<>(); public int getCircuitBreakerAspectOrder() { return circuitBreakerAspectOrder; @@ -51,302 +34,6 @@ public void setCircuitBreakerAspectOrder(int circuitBreakerAspectOrder) { this.circuitBreakerAspectOrder = circuitBreakerAspectOrder; } - public Optional findCircuitBreakerProperties(String name) { - return Optional.ofNullable(backends.get(name)); - } - - public CircuitBreakerConfig createCircuitBreakerConfig(BackendProperties backendProperties) { - if(backendProperties.getBaseConfig() != null){ - BackendProperties baseProperties = configs.get(backendProperties.getBaseConfig()); - if(baseProperties == null){ - throw new ConfigurationNotFoundException(backendProperties.getBaseConfig()); - } - return buildConfigFromBaseConfig(backendProperties, baseProperties); - } - return buildConfig(CircuitBreakerConfig.custom(), backendProperties); - } - - private CircuitBreakerConfig buildConfigFromBaseConfig(BackendProperties backendProperties, BackendProperties baseProperties) { - CircuitBreakerConfig baseConfig = buildConfig(CircuitBreakerConfig.custom(), baseProperties); - return buildConfig(CircuitBreakerConfig.from(baseConfig), backendProperties); - } - - private CircuitBreakerConfig buildConfig(Builder builder, BackendProperties properties) { - - if (properties.getWaitDurationInOpenState() != null) { - builder.waitDurationInOpenState(properties.getWaitDurationInOpenState()); - } - - if (properties.getFailureRateThreshold() != null) { - builder.failureRateThreshold(properties.getFailureRateThreshold()); - } - - if (properties.getRingBufferSizeInClosedState() != null) { - builder.ringBufferSizeInClosedState(properties.getRingBufferSizeInClosedState()); - } - - if (properties.getRingBufferSizeInHalfOpenState() != null) { - builder.ringBufferSizeInHalfOpenState(properties.getRingBufferSizeInHalfOpenState()); - } - - if (properties.recordFailurePredicate != null) { - buildRecordFailurePredicate(properties, builder); - } - - if (properties.recordExceptions != null) { - builder.recordExceptions(properties.recordExceptions); - } - - if (properties.ignoreExceptions != null) { - builder.ignoreExceptions(properties.ignoreExceptions); - } - - if (properties.automaticTransitionFromOpenToHalfOpenEnabled != null) { - builder.automaticTransitionFromOpenToHalfOpenEnabled(properties.automaticTransitionFromOpenToHalfOpenEnabled); - } - - return builder.build(); - } - - protected void buildRecordFailurePredicate(BackendProperties properties, Builder builder) { - builder.recordFailure(BeanUtils.instantiateClass(properties.getRecordFailurePredicate())); - } - - public Map getBackends() { - return backends; - } - - public Map getConfigs() { - return configs; - } - - /** - * Class storing property values for configuring {@link io.github.resilience4j.circuitbreaker.CircuitBreaker} instances. - */ - public static class BackendProperties { - - @DurationMin(seconds = 1) - @Nullable - private Duration waitDurationInOpenState; - - @Min(1) - @Max(100) - @Nullable - private Integer failureRateThreshold; - - @Min(1) - @Nullable - private Integer ringBufferSizeInClosedState; - - @Min(1) - @Nullable - private Integer ringBufferSizeInHalfOpenState; - - @Nullable - private Boolean automaticTransitionFromOpenToHalfOpenEnabled; - - @Min(1) - private Integer eventConsumerBufferSize = 100; - - @NotNull - private Boolean registerHealthIndicator = true; - @Nullable - private Class> recordFailurePredicate; - - @Nullable - private Class[] recordExceptions; - - @Nullable - private Class[] ignoreExceptions; - - @Nullable - private String baseConfig; - - - /** - * @deprecated - * @since (0.0.14) use instead {@link #setWaitDurationInOpenState(Duration)} ()} - * Sets the wait duration in seconds the CircuitBreaker should stay open, before it switches to half closed. - * - * @param waitInterval the wait duration - */ - @Deprecated - public void setWaitInterval(Integer waitInterval) { - this.waitDurationInOpenState = Duration.ofMillis(waitInterval); - } - - /** - * @deprecated - * @since (0.0.14) use instead {@link #getWaitDurationInOpenState()} - * - * get the wait duration in seconds the CircuitBreaker should stay open, before it switches to half closed. - * - * @return waitInterval the wait duration - */ - @Deprecated - public Duration getWaitInterval() { - return getWaitDurationInOpenState(); - } - - /** - * Returns the failure rate threshold for the circuit breaker as percentage. - * - * @return the failure rate threshold - */ - @Nullable - public Integer getFailureRateThreshold() { - return failureRateThreshold; - } - - /** - * Sets the failure rate threshold for the circuit breaker as percentage. - * - * @param failureRateThreshold the failure rate threshold - */ - public void setFailureRateThreshold(Integer failureRateThreshold) { - this.failureRateThreshold = failureRateThreshold; - } - - /** - * Returns the wait duration the CircuitBreaker will stay open, before it switches to half closed. - * - * @return the wait duration - */ - @Nullable - public Duration getWaitDurationInOpenState() { - return waitDurationInOpenState; - } - - /** - * Sets the wait duration the CircuitBreaker should stay open, before it switches to half closed. - * - * @param waitDurationInOpenState the wait duration - */ - public void setWaitDurationInOpenState(Duration waitDurationInOpenState) { - this.waitDurationInOpenState = waitDurationInOpenState; - } - - /** - * Returns the ring buffer size for the circuit breaker while in closed state. - * - * @return the ring buffer size - */ - @Nullable - public Integer getRingBufferSizeInClosedState() { - return ringBufferSizeInClosedState; - } - - /** - * Sets the ring buffer size for the circuit breaker while in closed state. - * - * @param ringBufferSizeInClosedState the ring buffer size - */ - public void setRingBufferSizeInClosedState(Integer ringBufferSizeInClosedState) { - this.ringBufferSizeInClosedState = ringBufferSizeInClosedState; - } - - /** - * Returns the ring buffer size for the circuit breaker while in half open state. - * - * @return the ring buffer size - */ - @Nullable - public Integer getRingBufferSizeInHalfOpenState() { - return ringBufferSizeInHalfOpenState; - } - - /** - * Sets the ring buffer size for the circuit breaker while in half open state. - * - * @param ringBufferSizeInHalfOpenState the ring buffer size - */ - public void setRingBufferSizeInHalfOpenState(Integer ringBufferSizeInHalfOpenState) { - this.ringBufferSizeInHalfOpenState = ringBufferSizeInHalfOpenState; - } - - /** - * Returns if we should automaticly transition to half open after the timer has run out. - * - * @return automaticTransitionFromOpenToHalfOpenEnabled if we should automaticly go to half open or not - */ - public Boolean getAutomaticTransitionFromOpenToHalfOpenEnabled() { - return this.automaticTransitionFromOpenToHalfOpenEnabled; - } - - /** - * Sets if we should automatically transition to half open after the timer has run out. - * - * @param automaticTransitionFromOpenToHalfOpenEnabled The flag for automatic transition to half open after the timer has run out. - */ - public void setAutomaticTransitionFromOpenToHalfOpenEnabled(Boolean automaticTransitionFromOpenToHalfOpenEnabled) { - this.automaticTransitionFromOpenToHalfOpenEnabled = automaticTransitionFromOpenToHalfOpenEnabled; - } - - public Integer getEventConsumerBufferSize() { - return eventConsumerBufferSize; - } - - public void setEventConsumerBufferSize(Integer eventConsumerBufferSize) { - this.eventConsumerBufferSize = eventConsumerBufferSize; - } - - public Boolean getRegisterHealthIndicator() { - return registerHealthIndicator; - } - - public void setRegisterHealthIndicator(Boolean registerHealthIndicator) { - this.registerHealthIndicator = registerHealthIndicator; - } - - @Nullable - public Class> getRecordFailurePredicate() { - return recordFailurePredicate; - } - - public void setRecordFailurePredicate(Class> recordFailurePredicate) { - this.recordFailurePredicate = recordFailurePredicate; - } - - @Nullable - public Class[] getRecordExceptions() { - return recordExceptions; - } - - public void setRecordExceptions(Class[] recordExceptions) { - this.recordExceptions = recordExceptions; - } - - @Nullable - public Class[] getIgnoreExceptions() { - return ignoreExceptions; - } - - public void setIgnoreExceptions(Class[] ignoreExceptions) { - this.ignoreExceptions = ignoreExceptions; - } - - /** - * Gets the shared configuration name. If this is set, the configuration builder will use the the shared - * configuration backend over this one. - * - * @return The shared configuration name. - */ - @Nullable - public String getBaseConfig() { - return baseConfig; - } - - /** - * Sets the shared configuration name. If this is set, the configuration builder will use the the shared - * configuration backend over this one. - * - * @param baseConfig The shared configuration name. - */ - public void setBaseConfig(String baseConfig) { - this.baseConfig = baseConfig; - } - - } } diff --git a/resilience4j-spring/src/main/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfiguration.java b/resilience4j-spring/src/main/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfiguration.java index 9cd230e4e8..2f1c4122c4 100644 --- a/resilience4j-spring/src/main/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfiguration.java +++ b/resilience4j-spring/src/main/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfiguration.java @@ -16,24 +16,23 @@ package io.github.resilience4j.ratelimiter.configure; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Conditional; -import org.springframework.context.annotation.Configuration; - import io.github.resilience4j.consumer.DefaultEventConsumerRegistry; import io.github.resilience4j.consumer.EventConsumerRegistry; +import io.github.resilience4j.fallback.FallbackDecorators; import io.github.resilience4j.ratelimiter.RateLimiter; import io.github.resilience4j.ratelimiter.RateLimiterConfig; import io.github.resilience4j.ratelimiter.RateLimiterRegistry; import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; -import io.github.resilience4j.fallback.FallbackDecorators; import io.github.resilience4j.utils.ReactorOnClasspathCondition; import io.github.resilience4j.utils.RxJava2OnClasspathCondition; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Conditional; +import org.springframework.context.annotation.Configuration; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** * {@link org.springframework.context.annotation.Configuration @@ -47,7 +46,7 @@ public RateLimiterRegistry rateLimiterRegistry(RateLimiterConfigurationPropertie EventConsumerRegistry rateLimiterEventsConsumerRegistry) { RateLimiterRegistry rateLimiterRegistry = createRateLimiterRegistry(rateLimiterProperties); registerEventConsumer(rateLimiterRegistry, rateLimiterEventsConsumerRegistry, rateLimiterProperties); - rateLimiterProperties.getLimiters().forEach( + rateLimiterProperties.getInstances().forEach( (name, properties) -> rateLimiterRegistry.rateLimiter(name, rateLimiterProperties.createRateLimiterConfig(properties)) ); return rateLimiterRegistry; @@ -79,7 +78,7 @@ private void registerEventConsumer(RateLimiterRegistry rateLimiterRegistry, } private void registerEventConsumer(EventConsumerRegistry eventConsumerRegistry, RateLimiter rateLimiter, RateLimiterConfigurationProperties rateLimiterConfigurationProperties) { - final RateLimiterConfigurationProperties.LimiterProperties limiterProperties = rateLimiterConfigurationProperties.getLimiters().get(rateLimiter.getName()); + final io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties limiterProperties = rateLimiterConfigurationProperties.getInstances().get(rateLimiter.getName()); if (limiterProperties != null && limiterProperties.getSubscribeForEvents()) { rateLimiter.getEventPublisher().onEvent(eventConsumerRegistry.createEventConsumer(rateLimiter.getName(), limiterProperties.getEventConsumerBufferSize() != 0 ? limiterProperties.getEventConsumerBufferSize() : 100)); } diff --git a/resilience4j-spring/src/main/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationProperties.java b/resilience4j-spring/src/main/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationProperties.java index 0368dd8a50..ee41648aab 100644 --- a/resilience4j-spring/src/main/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationProperties.java +++ b/resilience4j-spring/src/main/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationProperties.java @@ -15,72 +15,13 @@ */ package io.github.resilience4j.ratelimiter.configure; -import java.time.Duration; -import java.util.HashMap; -import java.util.Map; - -import org.springframework.util.StringUtils; - -import io.github.resilience4j.core.ConfigurationNotFoundException; -import io.github.resilience4j.core.lang.Nullable; -import io.github.resilience4j.ratelimiter.RateLimiterConfig; - -public class RateLimiterConfigurationProperties { +public class RateLimiterConfigurationProperties extends io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties { // This property gives you control over RateLimiter aspect application order. // Integer.MAX_VALUE means that RateLimiter aspect will be last one applied to any decorated bean. // It also means that by default RateLimiter will be executed AFTER CircuitBreaker. // Be adjusting RateLimiterProperties.rateLimiterAspectOrder and CircuitBreakerProperties.circuitBreakerAspectOrder // you explicitly define aspects CircuitBreaker and RateLimiter execution sequence. private int rateLimiterAspectOrder = Integer.MAX_VALUE; - private Map limiters = new HashMap<>(); - private Map configs = new HashMap<>(); - - public RateLimiterConfig createRateLimiterConfig(@Nullable LimiterProperties limiterProperties) { - if (limiterProperties == null) { - return RateLimiterConfig.ofDefaults(); - } - if (!StringUtils.isEmpty(limiterProperties.getBaseConfig())) { - LimiterProperties baseProperties = configs.get(limiterProperties.baseConfig); - if (baseProperties == null) { - throw new ConfigurationNotFoundException(limiterProperties.getBaseConfig()); - } - return buildConfigFromBaseConfig(baseProperties, limiterProperties); - } - return buildRateLimiterConfig(RateLimiterConfig.custom(), limiterProperties); - } - - private RateLimiterConfig buildConfigFromBaseConfig(LimiterProperties baseProperties, LimiterProperties backendProperties) { - RateLimiterConfig baseConfig = buildRateLimiterConfig(RateLimiterConfig.custom(), baseProperties); - return buildRateLimiterConfig(RateLimiterConfig.from(baseConfig), backendProperties); - } - - private RateLimiterConfig buildRateLimiterConfig(RateLimiterConfig.Builder builder, @Nullable LimiterProperties limiterProperties) { - if (limiterProperties == null) { - return RateLimiterConfig.ofDefaults(); - } - - if (limiterProperties.getLimitForPeriod() != null) { - builder.limitForPeriod(limiterProperties.getLimitForPeriod()); - } - - if (limiterProperties.getLimitRefreshPeriodInMillis() != null) { - builder.limitRefreshPeriod(Duration.ofMillis(limiterProperties.getLimitRefreshPeriodInMillis())); - } - - if (limiterProperties.getTimeoutInMillis() != null) { - builder.timeoutDuration(Duration.ofMillis(limiterProperties.getTimeoutInMillis())); - } - - return builder.build(); - } - - private LimiterProperties getLimiterProperties(String limiter) { - return limiters.get(limiter); - } - - public RateLimiterConfig createRateLimiterConfig(String limiter) { - return createRateLimiterConfig(getLimiterProperties(limiter)); - } public int getRateLimiterAspectOrder() { return rateLimiterAspectOrder; @@ -89,145 +30,4 @@ public int getRateLimiterAspectOrder() { public void setRateLimiterAspectOrder(int rateLimiterAspectOrder) { this.rateLimiterAspectOrder = rateLimiterAspectOrder; } - - public Map getLimiters() { - return limiters; - } - - public Map getConfigs() { - return configs; - } - - /** - * Class storing property values for configuring {@link RateLimiterConfig} instances. - */ - public static class LimiterProperties { - - private Integer limitForPeriod; - private Integer limitRefreshPeriodInMillis; - private Integer timeoutInMillis; - private Boolean subscribeForEvents = false; - private Boolean registerHealthIndicator = false; - private Integer eventConsumerBufferSize = 100; - @Nullable - private String baseConfig; - - /** - * Configures the permissions limit for refresh period. - * Count of permissions available during one rate limiter period - * specified by {@link RateLimiterConfig#limitRefreshPeriod} value. - * Default value is 50. - * - * @return the permissions limit for refresh period - */ - @Nullable - public Integer getLimitForPeriod() { - return limitForPeriod; - } - - /** - * Configures the permissions limit for refresh period. - * Count of permissions available during one rate limiter period - * specified by {@link RateLimiterConfig#limitRefreshPeriod} value. - * Default value is 50. - * - * @param limitForPeriod the permissions limit for refresh period - */ - public void setLimitForPeriod(Integer limitForPeriod) { - this.limitForPeriod = limitForPeriod; - } - - /** - * Configures the period of limit refresh. - * After each period rate limiter sets its permissions - * count to {@link RateLimiterConfig#limitForPeriod} value. - * Default value is 500 nanoseconds. - * - * @return the period of limit refresh - */ - @Nullable - public Integer getLimitRefreshPeriodInMillis() { - return limitRefreshPeriodInMillis; - } - - /** - * Configures the period of limit refresh. - * After each period rate limiter sets its permissions - * count to {@link RateLimiterConfig#limitForPeriod} value. - * Default value is 500 nanoseconds. - * - * @param limitRefreshPeriodInMillis the period of limit refresh - */ - public void setLimitRefreshPeriodInMillis(Integer limitRefreshPeriodInMillis) { - this.limitRefreshPeriodInMillis = limitRefreshPeriodInMillis; - } - - /** - * Configures the default wait for permission duration. - * Default value is 5 seconds. - * - * @return wait for permission duration - */ - @Nullable - public Integer getTimeoutInMillis() { - return timeoutInMillis; - } - - /** - * Configures the default wait for permission duration. - * Default value is 5 seconds. - * - * @param timeoutInMillis wait for permission duration - */ - public void setTimeoutInMillis(Integer timeoutInMillis) { - this.timeoutInMillis = timeoutInMillis; - } - - public Boolean getSubscribeForEvents() { - return subscribeForEvents; - } - - public void setSubscribeForEvents(Boolean subscribeForEvents) { - this.subscribeForEvents = subscribeForEvents; - } - - public Integer getEventConsumerBufferSize() { - return eventConsumerBufferSize; - } - - public void setEventConsumerBufferSize(Integer eventConsumerBufferSize) { - this.eventConsumerBufferSize = eventConsumerBufferSize; - } - - public Boolean getRegisterHealthIndicator() { - return registerHealthIndicator; - } - - public void setRegisterHealthIndicator(Boolean registerHealthIndicator) { - this.registerHealthIndicator = registerHealthIndicator; - } - - - /** - * Gets the shared configuration name. If this is set, the configuration builder will use the the shared - * configuration backend over this one. - * - * @return The shared configuration name. - */ - @Nullable - public String getBaseConfig() { - return baseConfig; - } - - /** - * Sets the shared configuration name. If this is set, the configuration builder will use the the shared - * configuration backend over this one. - * - * @param baseConfig The shared configuration name. - */ - public void setBaseConfig(String baseConfig) { - this.baseConfig = baseConfig; - } - } - } diff --git a/resilience4j-spring/src/main/java/io/github/resilience4j/retry/configure/RetryConfiguration.java b/resilience4j-spring/src/main/java/io/github/resilience4j/retry/configure/RetryConfiguration.java index a5ec21032f..746c39622b 100644 --- a/resilience4j-spring/src/main/java/io/github/resilience4j/retry/configure/RetryConfiguration.java +++ b/resilience4j-spring/src/main/java/io/github/resilience4j/retry/configure/RetryConfiguration.java @@ -52,7 +52,7 @@ public RetryRegistry retryRegistry(RetryConfigurationProperties retryConfigurati RetryRegistry retryRegistry = createRetryRegistry(retryConfigurationProperties); registerEventConsumer(retryRegistry, retryEventConsumerRegistry, retryConfigurationProperties); - retryConfigurationProperties.getBackends().forEach((name, properties) -> retryRegistry.retry(name, retryConfigurationProperties.createRetryConfig(name))); + retryConfigurationProperties.getInstances().forEach((name, properties) -> retryRegistry.retry(name, retryConfigurationProperties.createRetryConfig(name))); return retryRegistry; } @@ -84,7 +84,7 @@ private void registerEventConsumer(RetryRegistry retryRegistry, private void registerEventConsumer(EventConsumerRegistry eventConsumerRegistry, Retry retry, RetryConfigurationProperties retryConfigurationProperties) { int eventConsumerBufferSize = Optional.ofNullable(retryConfigurationProperties.getBackendProperties(retry.getName())) - .map(RetryConfigurationProperties.BackendProperties::getEventConsumerBufferSize) + .map(io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties::getEventConsumerBufferSize) .orElse(100); retry.getEventPublisher().onEvent(eventConsumerRegistry.createEventConsumer(retry.getName(), eventConsumerBufferSize)); } diff --git a/resilience4j-spring/src/main/java/io/github/resilience4j/retry/configure/RetryConfigurationProperties.java b/resilience4j-spring/src/main/java/io/github/resilience4j/retry/configure/RetryConfigurationProperties.java index a1e941656d..ee3b1b614e 100644 --- a/resilience4j-spring/src/main/java/io/github/resilience4j/retry/configure/RetryConfigurationProperties.java +++ b/resilience4j-spring/src/main/java/io/github/resilience4j/retry/configure/RetryConfigurationProperties.java @@ -15,26 +15,10 @@ * limitations under the License. */ -import io.github.resilience4j.core.ConfigurationNotFoundException; -import io.github.resilience4j.core.lang.Nullable; -import io.github.resilience4j.retry.IntervalFunction; -import io.github.resilience4j.retry.RetryConfig; -import org.springframework.beans.BeanUtils; -import org.springframework.util.StringUtils; - -import javax.validation.constraints.Min; -import javax.validation.constraints.NotNull; -import java.time.Duration; -import java.util.HashMap; -import java.util.Map; -import java.util.function.Predicate; - /** * Main spring properties for retry configuration */ -public class RetryConfigurationProperties { - private final Map backends = new HashMap<>(); - private Map configs = new HashMap<>(); +public class RetryConfigurationProperties extends io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties { /* This property gives you control over Retry aspect application order. By default Retry will be executed BEFORE Circuit breaker, rateLimiter and bulkhead. By adjusting each aspect order from ConfigurationProperties @@ -42,14 +26,6 @@ public class RetryConfigurationProperties { */ private int retryAspectOrder = Integer.MAX_VALUE - 3; - /** - * @param backend backend name - * @return the retry configuration - */ - public RetryConfig createRetryConfig(String backend) { - return createRetryConfig(getBackendProperties(backend)); - } - /** * @return spring aspect apply order */ @@ -64,299 +40,4 @@ public void setRetryAspectOrder(int retryAspectOrder) { this.retryAspectOrder = retryAspectOrder; } - /** - * @return the configured retry backend properties - */ - public Map getBackends() { - return backends; - } - - /** - * @return common configuration for retry backend - */ - public Map getConfigs() { - return configs; - } - - /** - * @param backendProperties the retry backend spring properties - * @return the retry configuration - */ - public RetryConfig createRetryConfig(BackendProperties backendProperties) { - if (!StringUtils.isEmpty(backendProperties.getBaseConfig())) { - BackendProperties baseProperties = configs.get(backendProperties.getBaseConfig()); - if (baseProperties == null) { - throw new ConfigurationNotFoundException(backendProperties.getBaseConfig()); - } - return buildConfigFromBaseConfig(baseProperties, backendProperties); - } - return buildRetryConfig(RetryConfig.custom(), backendProperties); - } - - private RetryConfig buildConfigFromBaseConfig(BackendProperties baseProperties, BackendProperties backendProperties) { - RetryConfig baseConfig = buildRetryConfig(RetryConfig.custom(), baseProperties); - return buildRetryConfig(RetryConfig.from(baseConfig), backendProperties); - } - - /** - * @param backend retry backend name - * @return the configured spring backend properties - */ - @Nullable - public BackendProperties getBackendProperties(String backend) { - return backends.get(backend); - } - - /** - * @param properties the configured spring backend properties - * @return retry config builder instance - */ - @SuppressWarnings("unchecked") - private RetryConfig buildRetryConfig(RetryConfig.Builder builder, BackendProperties properties) { - if (properties == null) { - return builder.build(); - } - - if (properties.enableExponentialBackoff && properties.enableRandomizedWait) { - throw new IllegalStateException("you can not enable Exponential backoff policy and randomized delay at the same time , please enable only one of them"); - } - - configureRetryIntervalFunction(properties, builder); - - if (properties.getMaxRetryAttempts() != null && properties.getMaxRetryAttempts() != 0) { - builder.maxAttempts(properties.getMaxRetryAttempts()); - } - - if (properties.getRetryExceptionPredicate() != null) { - builder.retryOnException(BeanUtils.instantiateClass(properties.getRetryExceptionPredicate())); - } - - if (properties.getIgnoreExceptions() != null) { - builder.ignoreExceptions(properties.getIgnoreExceptions()); - } - - if (properties.getRetryExceptions() != null) { - builder.retryExceptions(properties.getRetryExceptions()); - } - - if (properties.getResultPredicate() != null) { - builder.retryOnResult(BeanUtils.instantiateClass(properties.getResultPredicate())); - } - - return builder.build(); - } - - /** - * decide which retry delay polciy will be configured based into the configured properties - * - * @param properties the backend retry properties - * @param builder the retry config builder - */ - private void configureRetryIntervalFunction(BackendProperties properties, RetryConfig.Builder builder) { - if (properties.getWaitDuration() != null && properties.getWaitDuration() != 0) { - long waitDuration = properties.getWaitDuration(); - if (properties.getEnableExponentialBackoff()) { - if (properties.getExponentialBackoffMultiplier() != 0) { - builder.intervalFunction(IntervalFunction.ofExponentialBackoff(waitDuration, properties.getExponentialBackoffMultiplier())); - } else { - builder.intervalFunction(IntervalFunction.ofExponentialBackoff(properties.getWaitDuration())); - } - } else if (properties.getEnableRandomizedWait()) { - if (properties.getRandomizedWaitFactor() != 0) { - builder.intervalFunction(IntervalFunction.ofRandomized(waitDuration, properties.getRandomizedWaitFactor())); - } else { - builder.intervalFunction(IntervalFunction.ofRandomized(waitDuration)); - } - } else { - builder.waitDuration(Duration.ofMillis(properties.getWaitDuration())); - } - } - } - - /** - * Class storing property values for configuring {@link io.github.resilience4j.retry.Retry} instances. - */ - public static class BackendProperties { - /* - * wait long value for the next try - */ - @Min(100) - @Nullable - private Long waitDuration; - /* - * max retry attempts value - */ - @Min(1) - @Nullable - private Integer maxRetryAttempts; - /* - * retry exception predicate class to be used to evaluate the exception to retry or not - */ - @Nullable - private Class> retryExceptionPredicate; - /* - * retry resultPredicate predicate class to be used to evaluate the result to retry or not - */ - @Nullable - private Class resultPredicate; - /* - * list of retry exception classes - */ - @SuppressWarnings("unchecked") - @Nullable - private Class[] retryExceptions; - /* - * list of retry ignored exception classes - */ - @SuppressWarnings("unchecked") - @Nullable - private Class[] ignoreExceptions; - /* - * event buffer size for generated retry events - */ - @Min(1) - private Integer eventConsumerBufferSize = 100; - /* - * flag to enable Exponential backoff policy or not for retry policy delay - */ - @NotNull - private Boolean enableExponentialBackoff = false; - /* - * exponential backoff multiplier value - */ - private double exponentialBackoffMultiplier; - @NotNull - /* - * flag to enable randomized delay policy or not for retry policy delay - */ - private Boolean enableRandomizedWait = false; - /* - * randomized delay factor value - */ - private double randomizedWaitFactor; - - @Nullable - private String baseConfig; - - @Nullable - public Long getWaitDuration() { - return waitDuration; - } - - public void setWaitDuration(Long waitDuration) { - this.waitDuration = waitDuration; - } - - @Nullable - public Integer getMaxRetryAttempts() { - return maxRetryAttempts; - } - - public void setMaxRetryAttempts(Integer maxRetryAttempts) { - this.maxRetryAttempts = maxRetryAttempts; - } - - @Nullable - public Class> getRetryExceptionPredicate() { - return retryExceptionPredicate; - } - - public void setRetryExceptionPredicate(Class> retryExceptionPredicate) { - this.retryExceptionPredicate = retryExceptionPredicate; - } - - @Nullable - public Class getResultPredicate() { - return resultPredicate; - } - - public void setResultPredicate(Class resultPredicate) { - this.resultPredicate = resultPredicate; - } - - @Nullable - public Class[] getRetryExceptions() { - return retryExceptions; - } - - public void setRetryExceptions(Class[] retryExceptions) { - this.retryExceptions = retryExceptions; - } - - @Nullable - public Class[] getIgnoreExceptions() { - return ignoreExceptions; - } - - public void setIgnoreExceptions(Class[] ignoreExceptions) { - this.ignoreExceptions = ignoreExceptions; - } - - @Nullable - public Integer getEventConsumerBufferSize() { - return eventConsumerBufferSize; - } - - public void setEventConsumerBufferSize(Integer eventConsumerBufferSize) { - this.eventConsumerBufferSize = eventConsumerBufferSize; - } - - public Boolean getEnableExponentialBackoff() { - return enableExponentialBackoff; - } - - public void setEnableExponentialBackoff(Boolean enableExponentialBackoff) { - this.enableExponentialBackoff = enableExponentialBackoff; - } - - @Nullable - public double getExponentialBackoffMultiplier() { - return exponentialBackoffMultiplier; - } - - public void setExponentialBackoffMultiplier(double exponentialBackoffMultiplier) { - this.exponentialBackoffMultiplier = exponentialBackoffMultiplier; - } - - @Nullable - public Boolean getEnableRandomizedWait() { - return enableRandomizedWait; - } - - public void setEnableRandomizedWait(Boolean enableRandomizedWait) { - this.enableRandomizedWait = enableRandomizedWait; - } - - @Nullable - public double getRandomizedWaitFactor() { - return randomizedWaitFactor; - } - - public void setRandomizedWaitFactor(double randomizedWaitFactor) { - this.randomizedWaitFactor = randomizedWaitFactor; - } - - /** - * Gets the shared configuration name. If this is set, the configuration builder will use the the shared - * configuration backend over this one. - * - * @return The shared configuration name. - */ - @Nullable - public String getBaseConfig() { - return baseConfig; - } - - /** - * Sets the shared configuration name. If this is set, the configuration builder will use the the shared - * configuration backend over this one. - * - * @param baseConfig The shared configuration name. - */ - public void setBaseConfig(String baseConfig) { - this.baseConfig = baseConfig; - } - - } - } diff --git a/resilience4j-spring/src/test/java/io/github/resilience4j/bulkhead/configure/BulkHeadConfigurationSpringTest.java b/resilience4j-spring/src/test/java/io/github/resilience4j/bulkhead/configure/BulkHeadConfigurationSpringTest.java index 942fd66586..1bd39161b5 100644 --- a/resilience4j-spring/src/test/java/io/github/resilience4j/bulkhead/configure/BulkHeadConfigurationSpringTest.java +++ b/resilience4j-spring/src/test/java/io/github/resilience4j/bulkhead/configure/BulkHeadConfigurationSpringTest.java @@ -104,10 +104,10 @@ public BulkheadConfigurationProperties bulkheadConfigurationProperties() { private class BulkheadConfigurationPropertiesTest extends BulkheadConfigurationProperties { BulkheadConfigurationPropertiesTest() { - BackendProperties backendProperties = new BackendProperties(); - backendProperties.setBaseConfig("sharedConfig"); - backendProperties.setMaxConcurrentCall(3); - getConfigs().put("sharedBackend", backendProperties); + InstanceProperties instanceProperties = new InstanceProperties(); + instanceProperties.setBaseConfig("sharedConfig"); + instanceProperties.setMaxConcurrentCalls(3); + getConfigs().put("sharedBackend", instanceProperties); } } diff --git a/resilience4j-spring/src/test/java/io/github/resilience4j/bulkhead/configure/BulkHeadConfigurationTest.java b/resilience4j-spring/src/test/java/io/github/resilience4j/bulkhead/configure/BulkHeadConfigurationTest.java index 4a30a1a1dd..af8877ca35 100644 --- a/resilience4j-spring/src/test/java/io/github/resilience4j/bulkhead/configure/BulkHeadConfigurationTest.java +++ b/resilience4j-spring/src/test/java/io/github/resilience4j/bulkhead/configure/BulkHeadConfigurationTest.java @@ -125,15 +125,15 @@ public void testCreateThreadPoolBulkHeadRegistryWithSharedConfigs() { @Test public void testBulkHeadRegistry() { //Given - BulkheadConfigurationProperties.BackendProperties backendProperties1 = new BulkheadConfigurationProperties.BackendProperties(); - backendProperties1.setMaxConcurrentCall(3); + io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties instanceProperties1 = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties(); + instanceProperties1.setMaxConcurrentCalls(3); - BulkheadConfigurationProperties.BackendProperties backendProperties2 = new BulkheadConfigurationProperties.BackendProperties(); - backendProperties2.setMaxConcurrentCall(2); + io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties instanceProperties2 = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties(); + instanceProperties2.setMaxConcurrentCalls(2); BulkheadConfigurationProperties bulkheadConfigurationProperties = new BulkheadConfigurationProperties(); - bulkheadConfigurationProperties.getBackends().put("backend1", backendProperties1); - bulkheadConfigurationProperties.getBackends().put("backend2", backendProperties2); + bulkheadConfigurationProperties.getInstances().put("backend1", instanceProperties1); + bulkheadConfigurationProperties.getInstances().put("backend2", instanceProperties2); BulkheadConfiguration bulkheadConfiguration = new BulkheadConfiguration(); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); @@ -157,19 +157,19 @@ public void testBulkHeadRegistry() { @Test public void testCreateBulkHeadRegistryWithSharedConfigs() { //Given - BulkheadConfigurationProperties.BackendProperties defaultProperties = new BulkheadConfigurationProperties.BackendProperties(); - defaultProperties.setMaxConcurrentCall(3); + io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties defaultProperties = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties(); + defaultProperties.setMaxConcurrentCalls(3); defaultProperties.setMaxWaitTime(50L); - BulkheadConfigurationProperties.BackendProperties sharedProperties = new BulkheadConfigurationProperties.BackendProperties(); - sharedProperties.setMaxConcurrentCall(2); + io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties sharedProperties = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties(); + sharedProperties.setMaxConcurrentCalls(2); sharedProperties.setMaxWaitTime(100L); - BulkheadConfigurationProperties.BackendProperties backendWithDefaultConfig = new BulkheadConfigurationProperties.BackendProperties(); + io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties backendWithDefaultConfig = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties(); backendWithDefaultConfig.setBaseConfig("default"); backendWithDefaultConfig.setMaxWaitTime(200L); - BulkheadConfigurationProperties.BackendProperties backendWithSharedConfig = new BulkheadConfigurationProperties.BackendProperties(); + io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties backendWithSharedConfig = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties(); backendWithSharedConfig.setBaseConfig("sharedConfig"); backendWithSharedConfig.setMaxWaitTime(300L); @@ -177,8 +177,8 @@ public void testCreateBulkHeadRegistryWithSharedConfigs() { bulkheadConfigurationProperties.getConfigs().put("default", defaultProperties); bulkheadConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties); - bulkheadConfigurationProperties.getBackends().put("backendWithDefaultConfig", backendWithDefaultConfig); - bulkheadConfigurationProperties.getBackends().put("backendWithSharedConfig", backendWithSharedConfig); + bulkheadConfigurationProperties.getInstances().put("backendWithDefaultConfig", backendWithDefaultConfig); + bulkheadConfigurationProperties.getInstances().put("backendWithSharedConfig", backendWithSharedConfig); BulkheadConfiguration bulkheadConfiguration = new BulkheadConfiguration(); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); @@ -213,9 +213,9 @@ public void testCreateBulkHeadRegistryWithSharedConfigs() { public void testCreateBulkHeadRegistryWithUnknownConfig() { BulkheadConfigurationProperties bulkheadConfigurationProperties = new BulkheadConfigurationProperties(); - BulkheadConfigurationProperties.BackendProperties backendProperties = new BulkheadConfigurationProperties.BackendProperties(); - backendProperties.setBaseConfig("unknownConfig"); - bulkheadConfigurationProperties.getBackends().put("backend", backendProperties); + io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties instanceProperties = new io.github.resilience4j.common.bulkhead.configuration.BulkheadConfigurationProperties.InstanceProperties(); + instanceProperties.setBaseConfig("unknownConfig"); + bulkheadConfigurationProperties.getInstances().put("backend", instanceProperties); BulkheadConfiguration bulkheadConfiguration = new BulkheadConfiguration(); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); diff --git a/resilience4j-spring/src/test/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfigurationTest.java b/resilience4j-spring/src/test/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfigurationTest.java index 16de30e913..d6b5e49869 100644 --- a/resilience4j-spring/src/test/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfigurationTest.java +++ b/resilience4j-spring/src/test/java/io/github/resilience4j/circuitbreaker/configure/CircuitBreakerConfigurationTest.java @@ -21,15 +21,15 @@ public class CircuitBreakerConfigurationTest { @Test public void testCreateCircuitBreakerRegistry() { //Given - CircuitBreakerConfigurationProperties.BackendProperties backendProperties1 = new CircuitBreakerConfigurationProperties.BackendProperties(); - backendProperties1.setRingBufferSizeInClosedState(1000); + io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties instanceProperties1 = new io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties(); + instanceProperties1.setRingBufferSizeInClosedState(1000); - CircuitBreakerConfigurationProperties.BackendProperties backendProperties2 = new CircuitBreakerConfigurationProperties.BackendProperties(); - backendProperties2.setRingBufferSizeInClosedState(1337); + io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties instanceProperties2 = new io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties(); + instanceProperties2.setRingBufferSizeInClosedState(1337); CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties = new CircuitBreakerConfigurationProperties(); - circuitBreakerConfigurationProperties.getBackends().put("backend1", backendProperties1); - circuitBreakerConfigurationProperties.getBackends().put("backend2", backendProperties2); + circuitBreakerConfigurationProperties.getInstances().put("backend1", instanceProperties1); + circuitBreakerConfigurationProperties.getInstances().put("backend2", instanceProperties2); CircuitBreakerConfiguration circuitBreakerConfiguration = new CircuitBreakerConfiguration(circuitBreakerConfigurationProperties); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); @@ -53,19 +53,19 @@ public void testCreateCircuitBreakerRegistry() { @Test public void testCreateCircuitBreakerRegistryWithSharedConfigs() { //Given - CircuitBreakerConfigurationProperties.BackendProperties defaultProperties = new CircuitBreakerConfigurationProperties.BackendProperties(); + io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties defaultProperties = new io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties(); defaultProperties.setRingBufferSizeInClosedState(1000); defaultProperties.setRingBufferSizeInHalfOpenState(100); - CircuitBreakerConfigurationProperties.BackendProperties sharedProperties = new CircuitBreakerConfigurationProperties.BackendProperties(); + io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties sharedProperties = new io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties(); sharedProperties.setRingBufferSizeInClosedState(1337); sharedProperties.setRingBufferSizeInHalfOpenState(1000); - CircuitBreakerConfigurationProperties.BackendProperties backendWithDefaultConfig = new CircuitBreakerConfigurationProperties.BackendProperties(); + io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties backendWithDefaultConfig = new io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties(); backendWithDefaultConfig.setBaseConfig("default"); backendWithDefaultConfig.setRingBufferSizeInHalfOpenState(99); - CircuitBreakerConfigurationProperties.BackendProperties backendWithSharedConfig = new CircuitBreakerConfigurationProperties.BackendProperties(); + io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties backendWithSharedConfig = new io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties(); backendWithSharedConfig.setBaseConfig("sharedConfig"); backendWithSharedConfig.setRingBufferSizeInHalfOpenState(999); @@ -73,8 +73,8 @@ public void testCreateCircuitBreakerRegistryWithSharedConfigs() { circuitBreakerConfigurationProperties.getConfigs().put("default", defaultProperties); circuitBreakerConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties); - circuitBreakerConfigurationProperties.getBackends().put("backendWithDefaultConfig", backendWithDefaultConfig); - circuitBreakerConfigurationProperties.getBackends().put("backendWithSharedConfig", backendWithSharedConfig); + circuitBreakerConfigurationProperties.getInstances().put("backendWithDefaultConfig", backendWithDefaultConfig); + circuitBreakerConfigurationProperties.getInstances().put("backendWithSharedConfig", backendWithSharedConfig); CircuitBreakerConfiguration circuitBreakerConfiguration = new CircuitBreakerConfiguration(circuitBreakerConfigurationProperties); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); @@ -109,9 +109,9 @@ public void testCreateCircuitBreakerRegistryWithSharedConfigs() { public void testCreateCircuitBreakerRegistryWithUnknownConfig() { CircuitBreakerConfigurationProperties circuitBreakerConfigurationProperties = new CircuitBreakerConfigurationProperties(); - CircuitBreakerConfigurationProperties.BackendProperties backendProperties = new CircuitBreakerConfigurationProperties.BackendProperties(); - backendProperties.setBaseConfig("unknownConfig"); - circuitBreakerConfigurationProperties.getBackends().put("backend", backendProperties); + io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties instanceProperties = new io.github.resilience4j.common.circuitbreaker.configuration.CircuitBreakerConfigurationProperties.InstanceProperties(); + instanceProperties.setBaseConfig("unknownConfig"); + circuitBreakerConfigurationProperties.getInstances().put("backend", instanceProperties); CircuitBreakerConfiguration circuitBreakerConfiguration = new CircuitBreakerConfiguration(circuitBreakerConfigurationProperties); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); diff --git a/resilience4j-spring/src/test/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationSpringTest.java b/resilience4j-spring/src/test/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationSpringTest.java index e5b16c71ed..9b6dfae3fa 100644 --- a/resilience4j-spring/src/test/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationSpringTest.java +++ b/resilience4j-spring/src/test/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationSpringTest.java @@ -94,14 +94,13 @@ public RateLimiterConfigurationProperties rateLimiterConfigurationProperties() { private class RateLimiterConfigurationPropertiesTest extends RateLimiterConfigurationProperties { RateLimiterConfigurationPropertiesTest() { - RateLimiterConfigurationProperties.LimiterProperties backendProperties = new RateLimiterConfigurationProperties.LimiterProperties(); - backendProperties.setBaseConfig("sharedConfig"); - backendProperties.setLimitForPeriod(3); - getConfigs().put("sharedBackend", backendProperties); + InstanceProperties instanceProperties = new InstanceProperties(); + instanceProperties.setBaseConfig("sharedConfig"); + instanceProperties.setLimitForPeriod(3); + getConfigs().put("sharedBackend", instanceProperties); } } } - } \ No newline at end of file diff --git a/resilience4j-spring/src/test/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationTest.java b/resilience4j-spring/src/test/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationTest.java index d99108c013..a910b37420 100644 --- a/resilience4j-spring/src/test/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationTest.java +++ b/resilience4j-spring/src/test/java/io/github/resilience4j/ratelimiter/configure/RateLimiterConfigurationTest.java @@ -1,19 +1,18 @@ package io.github.resilience4j.ratelimiter.configure; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import java.time.Duration; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; - import io.github.resilience4j.consumer.DefaultEventConsumerRegistry; import io.github.resilience4j.core.ConfigurationNotFoundException; import io.github.resilience4j.ratelimiter.RateLimiter; import io.github.resilience4j.ratelimiter.RateLimiterRegistry; import io.github.resilience4j.ratelimiter.event.RateLimiterEvent; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +import java.time.Duration; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * test custom init of rate limiter configuration @@ -24,17 +23,17 @@ public class RateLimiterConfigurationTest { @Test public void testRateLimiterRegistry() { //Given - RateLimiterConfigurationProperties.LimiterProperties backendProperties1 = new RateLimiterConfigurationProperties.LimiterProperties(); - backendProperties1.setLimitForPeriod(2); - backendProperties1.setSubscribeForEvents(true); + io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties instanceProperties1 = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties(); + instanceProperties1.setLimitForPeriod(2); + instanceProperties1.setSubscribeForEvents(true); - RateLimiterConfigurationProperties.LimiterProperties backendProperties2 = new RateLimiterConfigurationProperties.LimiterProperties(); - backendProperties2.setLimitForPeriod(4); - backendProperties2.setSubscribeForEvents(true); + io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties instanceProperties2 = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties(); + instanceProperties2.setLimitForPeriod(4); + instanceProperties2.setSubscribeForEvents(true); RateLimiterConfigurationProperties rateLimiterConfigurationProperties = new RateLimiterConfigurationProperties(); - rateLimiterConfigurationProperties.getLimiters().put("backend1", backendProperties1); - rateLimiterConfigurationProperties.getLimiters().put("backend2", backendProperties2); + rateLimiterConfigurationProperties.getInstances().put("backend1", instanceProperties1); + rateLimiterConfigurationProperties.getInstances().put("backend2", instanceProperties2); RateLimiterConfiguration rateLimiterConfiguration = new RateLimiterConfiguration(); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); @@ -58,22 +57,22 @@ public void testRateLimiterRegistry() { @Test public void testCreateRateLimiterRegistryWithSharedConfigs() { //Given - RateLimiterConfigurationProperties.LimiterProperties defaultProperties = new RateLimiterConfigurationProperties.LimiterProperties(); + io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties defaultProperties = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties(); defaultProperties.setLimitForPeriod(3); - defaultProperties.setLimitRefreshPeriodInMillis(5); + defaultProperties.setLimitRefreshPeriodInNanos(5000000); defaultProperties.setSubscribeForEvents(true); - RateLimiterConfigurationProperties.LimiterProperties sharedProperties = new RateLimiterConfigurationProperties.LimiterProperties(); + io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties sharedProperties = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties(); sharedProperties.setLimitForPeriod(2); - sharedProperties.setLimitRefreshPeriodInMillis(6); + sharedProperties.setLimitRefreshPeriodInNanos(6000000); sharedProperties.setSubscribeForEvents(true); - RateLimiterConfigurationProperties.LimiterProperties backendWithDefaultConfig = new RateLimiterConfigurationProperties.LimiterProperties(); + io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties backendWithDefaultConfig = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties(); backendWithDefaultConfig.setBaseConfig("default"); backendWithDefaultConfig.setLimitForPeriod(200); backendWithDefaultConfig.setSubscribeForEvents(true); - RateLimiterConfigurationProperties.LimiterProperties backendWithSharedConfig = new RateLimiterConfigurationProperties.LimiterProperties(); + io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties backendWithSharedConfig = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties(); backendWithSharedConfig.setBaseConfig("sharedConfig"); backendWithSharedConfig.setLimitForPeriod(300); backendWithSharedConfig.setSubscribeForEvents(true); @@ -82,8 +81,8 @@ public void testCreateRateLimiterRegistryWithSharedConfigs() { rateLimiterConfigurationProperties.getConfigs().put("default", defaultProperties); rateLimiterConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties); - rateLimiterConfigurationProperties.getLimiters().put("backendWithDefaultConfig", backendWithDefaultConfig); - rateLimiterConfigurationProperties.getLimiters().put("backendWithSharedConfig", backendWithSharedConfig); + rateLimiterConfigurationProperties.getInstances().put("backendWithDefaultConfig", backendWithDefaultConfig); + rateLimiterConfigurationProperties.getInstances().put("backendWithSharedConfig", backendWithSharedConfig); RateLimiterConfiguration rateLimiterConfiguration = new RateLimiterConfiguration(); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); @@ -118,9 +117,9 @@ public void testCreateRateLimiterRegistryWithSharedConfigs() { public void testCreateRateLimiterRegistryWithUnknownConfig() { RateLimiterConfigurationProperties rateLimiterConfigurationProperties = new RateLimiterConfigurationProperties(); - RateLimiterConfigurationProperties.LimiterProperties backendProperties = new RateLimiterConfigurationProperties.LimiterProperties(); - backendProperties.setBaseConfig("unknownConfig"); - rateLimiterConfigurationProperties.getLimiters().put("backend", backendProperties); + io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties instanceProperties = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties(); + instanceProperties.setBaseConfig("unknownConfig"); + rateLimiterConfigurationProperties.getInstances().put("backend", instanceProperties); RateLimiterConfiguration rateLimiterConfiguration = new RateLimiterConfiguration(); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); diff --git a/resilience4j-spring/src/test/java/io/github/resilience4j/retry/configure/RetryConfigurationSpringTest.java b/resilience4j-spring/src/test/java/io/github/resilience4j/retry/configure/RetryConfigurationSpringTest.java index e19b8d5f94..9e9a16f209 100644 --- a/resilience4j-spring/src/test/java/io/github/resilience4j/retry/configure/RetryConfigurationSpringTest.java +++ b/resilience4j-spring/src/test/java/io/github/resilience4j/retry/configure/RetryConfigurationSpringTest.java @@ -95,10 +95,10 @@ public RetryConfigurationProperties retryConfigurationProperties() { private class RetryConfigurationPropertiesTest extends RetryConfigurationProperties { RetryConfigurationPropertiesTest() { - BackendProperties backendProperties = new BackendProperties(); - backendProperties.setBaseConfig("sharedConfig"); - backendProperties.setMaxRetryAttempts(3); - getConfigs().put("sharedBackend", backendProperties); + InstanceProperties instanceProperties = new InstanceProperties(); + instanceProperties.setBaseConfig("sharedConfig"); + instanceProperties.setMaxRetryAttempts(3); + getConfigs().put("sharedBackend", instanceProperties); } } diff --git a/resilience4j-spring/src/test/java/io/github/resilience4j/retry/configure/RetryConfigurationTest.java b/resilience4j-spring/src/test/java/io/github/resilience4j/retry/configure/RetryConfigurationTest.java index 7fc9cf75ee..753009c0bd 100644 --- a/resilience4j-spring/src/test/java/io/github/resilience4j/retry/configure/RetryConfigurationTest.java +++ b/resilience4j-spring/src/test/java/io/github/resilience4j/retry/configure/RetryConfigurationTest.java @@ -1,17 +1,16 @@ package io.github.resilience4j.retry.configure; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.junit.MockitoJUnitRunner; - import io.github.resilience4j.consumer.DefaultEventConsumerRegistry; import io.github.resilience4j.core.ConfigurationNotFoundException; import io.github.resilience4j.retry.Retry; import io.github.resilience4j.retry.RetryRegistry; import io.github.resilience4j.retry.event.RetryEvent; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.junit.MockitoJUnitRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; /** * test custom init of retry configuration @@ -22,15 +21,15 @@ public class RetryConfigurationTest { @Test public void testRetryRegistry() { //Given - RetryConfigurationProperties.BackendProperties backendProperties1 = new RetryConfigurationProperties.BackendProperties(); - backendProperties1.setMaxRetryAttempts(3); + io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties instanceProperties1 = new io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties(); + instanceProperties1.setMaxRetryAttempts(3); - RetryConfigurationProperties.BackendProperties backendProperties2 = new RetryConfigurationProperties.BackendProperties(); - backendProperties2.setMaxRetryAttempts(2); + io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties instanceProperties2 = new io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties(); + instanceProperties2.setMaxRetryAttempts(2); RetryConfigurationProperties retryConfigurationProperties = new RetryConfigurationProperties(); - retryConfigurationProperties.getBackends().put("backend1", backendProperties1); - retryConfigurationProperties.getBackends().put("backend2", backendProperties2); + retryConfigurationProperties.getInstances().put("backend1", instanceProperties1); + retryConfigurationProperties.getInstances().put("backend2", instanceProperties2); RetryConfiguration retryConfiguration = new RetryConfiguration(); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); @@ -54,28 +53,28 @@ public void testRetryRegistry() { @Test public void testCreateRetryRegistryWithSharedConfigs() { //Given - RetryConfigurationProperties.BackendProperties defaultProperties = new RetryConfigurationProperties.BackendProperties(); + io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties defaultProperties = new io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties(); defaultProperties.setMaxRetryAttempts(3); - defaultProperties.setWaitDuration(50L); + defaultProperties.setWaitDurationMillis(50L); - RetryConfigurationProperties.BackendProperties sharedProperties = new RetryConfigurationProperties.BackendProperties(); + io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties sharedProperties = new io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties(); sharedProperties.setMaxRetryAttempts(2); - sharedProperties.setWaitDuration(100L); + sharedProperties.setWaitDurationMillis(100L); - RetryConfigurationProperties.BackendProperties backendWithDefaultConfig = new RetryConfigurationProperties.BackendProperties(); + io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties backendWithDefaultConfig = new io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties(); backendWithDefaultConfig.setBaseConfig("default"); - backendWithDefaultConfig.setWaitDuration(200L); + backendWithDefaultConfig.setWaitDurationMillis(200L); - RetryConfigurationProperties.BackendProperties backendWithSharedConfig = new RetryConfigurationProperties.BackendProperties(); + io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties backendWithSharedConfig = new io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties(); backendWithSharedConfig.setBaseConfig("sharedConfig"); - backendWithSharedConfig.setWaitDuration(300L); + backendWithSharedConfig.setWaitDurationMillis(300L); RetryConfigurationProperties retryConfigurationProperties = new RetryConfigurationProperties(); retryConfigurationProperties.getConfigs().put("default", defaultProperties); retryConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties); - retryConfigurationProperties.getBackends().put("backendWithDefaultConfig", backendWithDefaultConfig); - retryConfigurationProperties.getBackends().put("backendWithSharedConfig", backendWithSharedConfig); + retryConfigurationProperties.getInstances().put("backendWithDefaultConfig", backendWithDefaultConfig); + retryConfigurationProperties.getInstances().put("backendWithSharedConfig", backendWithSharedConfig); RetryConfiguration retryConfiguration = new RetryConfiguration(); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); @@ -110,9 +109,9 @@ public void testCreateRetryRegistryWithSharedConfigs() { public void testCreateRetryRegistryWithUnknownConfig() { RetryConfigurationProperties retryConfigurationProperties = new RetryConfigurationProperties(); - RetryConfigurationProperties.BackendProperties backendProperties = new RetryConfigurationProperties.BackendProperties(); - backendProperties.setBaseConfig("unknownConfig"); - retryConfigurationProperties.getBackends().put("backend", backendProperties); + io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties instanceProperties = new io.github.resilience4j.common.retry.configuration.RetryConfigurationProperties.InstanceProperties(); + instanceProperties.setBaseConfig("unknownConfig"); + retryConfigurationProperties.getInstances().put("backend", instanceProperties); RetryConfiguration retryConfiguration = new RetryConfiguration(); DefaultEventConsumerRegistry eventConsumerRegistry = new DefaultEventConsumerRegistry<>();