@Test public void testCreateRateLimiterRegistryWithSharedConfigs() { io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties defaultProperties = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties(); defaultProperties.setLimitForPeriod(3); defaultProperties.setLimitRefreshPeriod(Duration.ofNanos(5000000)); defaultProperties.setSubscribeForEvents(true); io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties sharedProperties = new io.github.resilience4j.common.ratelimiter.configuration.RateLimiterConfigurationProperties.InstanceProperties(); sharedProperties.setLimitForPeriod(2); sharedProperties.setLimitRefreshPeriod(Duration.ofNanos(6000000)); sharedProperties.setSubscribeForEvents(true); 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); 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); RateLimiterConfigurationProperties rateLimiterConfigurationProperties = new RateLimiterConfigurationProperties(); rateLimiterConfigurationProperties.getConfigs().put("default", defaultProperties); rateLimiterConfigurationProperties.getConfigs().put("sharedConfig", sharedProperties); rateLimiterConfigurationProperties.getInstances() .put("backendWithDefaultConfig", backendWithDefaultConfig); rateLimiterConfigurationProperties.getInstances() .put("backendWithSharedConfig", backendWithSharedConfig); RateLimiterConfiguration rateLimiterConfiguration = new RateLimiterConfiguration(); DefaultEventConsumerRegistry<RateLimiterEvent> eventConsumerRegistry = new DefaultEventConsumerRegistry<>(); RateLimiterRegistry rateLimiterRegistry = rateLimiterConfiguration .rateLimiterRegistry(rateLimiterConfigurationProperties, eventConsumerRegistry, new CompositeRegistryEventConsumer<>(emptyList()), compositeRateLimiterCustomizerTest()); assertThat(rateLimiterRegistry.getAllRateLimiters().size()).isEqualTo(2); RateLimiter rateLimiter1 = rateLimiterRegistry.rateLimiter("backendWithDefaultConfig"); assertThat(rateLimiter1).isNotNull(); assertThat(rateLimiter1.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(200); assertThat(rateLimiter1.getRateLimiterConfig().getLimitRefreshPeriod()) .isEqualTo(Duration.ofMillis(5)); RateLimiter rateLimiter2 = rateLimiterRegistry.rateLimiter("backendWithSharedConfig"); assertThat(rateLimiter2).isNotNull(); assertThat(rateLimiter2.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(300); assertThat(rateLimiter2.getRateLimiterConfig().getLimitRefreshPeriod()) .isEqualTo(Duration.ofMillis(6)); RateLimiter rerateLimiter3 = rateLimiterRegistry.rateLimiter("unknownBackend"); assertThat(rerateLimiter3).isNotNull(); assertThat(rerateLimiter3.getRateLimiterConfig().getLimitForPeriod()).isEqualTo(3); assertThat(eventConsumerRegistry.getAllEventConsumer()).hasSize(2); }
@Bean public RateLimiterRegistry rateLimiterRegistry( RateLimiterConfigurationProperties rateLimiterProperties, EventConsumerRegistry<RateLimiterEvent> rateLimiterEventsConsumerRegistry, RegistryEventConsumer<RateLimiter> rateLimiterRegistryEventConsumer, @Qualifier("compositeRateLimiterCustomizer") CompositeCustomizer<RateLimiterConfigCustomizer> compositeRateLimiterCustomizer) { RateLimiterRegistry rateLimiterRegistry = createRateLimiterRegistry(rateLimiterProperties, rateLimiterRegistryEventConsumer, compositeRateLimiterCustomizer); registerEventConsumer(rateLimiterRegistry, rateLimiterEventsConsumerRegistry, rateLimiterProperties); rateLimiterProperties.getInstances().forEach( (name, properties) -> rateLimiterRegistry .rateLimiter(name, rateLimiterProperties .createRateLimiterConfig(properties, compositeRateLimiterCustomizer, name)) ); return rateLimiterRegistry; }
RateLimiterConfiguration { @Bean public RateLimiterRegistry rateLimiterRegistry( RateLimiterConfigurationProperties rateLimiterProperties, EventConsumerRegistry<RateLimiterEvent> rateLimiterEventsConsumerRegistry, RegistryEventConsumer<RateLimiter> rateLimiterRegistryEventConsumer, @Qualifier("compositeRateLimiterCustomizer") CompositeCustomizer<RateLimiterConfigCustomizer> compositeRateLimiterCustomizer) { RateLimiterRegistry rateLimiterRegistry = createRateLimiterRegistry(rateLimiterProperties, rateLimiterRegistryEventConsumer, compositeRateLimiterCustomizer); registerEventConsumer(rateLimiterRegistry, rateLimiterEventsConsumerRegistry, rateLimiterProperties); rateLimiterProperties.getInstances().forEach( (name, properties) -> rateLimiterRegistry .rateLimiter(name, rateLimiterProperties .createRateLimiterConfig(properties, compositeRateLimiterCustomizer, name)) ); return rateLimiterRegistry; } }
RateLimiterConfiguration { @Bean public RateLimiterRegistry rateLimiterRegistry( RateLimiterConfigurationProperties rateLimiterProperties, EventConsumerRegistry<RateLimiterEvent> rateLimiterEventsConsumerRegistry, RegistryEventConsumer<RateLimiter> rateLimiterRegistryEventConsumer, @Qualifier("compositeRateLimiterCustomizer") CompositeCustomizer<RateLimiterConfigCustomizer> compositeRateLimiterCustomizer) { RateLimiterRegistry rateLimiterRegistry = createRateLimiterRegistry(rateLimiterProperties, rateLimiterRegistryEventConsumer, compositeRateLimiterCustomizer); registerEventConsumer(rateLimiterRegistry, rateLimiterEventsConsumerRegistry, rateLimiterProperties); rateLimiterProperties.getInstances().forEach( (name, properties) -> rateLimiterRegistry .rateLimiter(name, rateLimiterProperties .createRateLimiterConfig(properties, compositeRateLimiterCustomizer, name)) ); return rateLimiterRegistry; }  }
RateLimiterConfiguration { @Bean public RateLimiterRegistry rateLimiterRegistry( RateLimiterConfigurationProperties rateLimiterProperties, EventConsumerRegistry<RateLimiterEvent> rateLimiterEventsConsumerRegistry, RegistryEventConsumer<RateLimiter> rateLimiterRegistryEventConsumer, @Qualifier("compositeRateLimiterCustomizer") CompositeCustomizer<RateLimiterConfigCustomizer> compositeRateLimiterCustomizer) { RateLimiterRegistry rateLimiterRegistry = createRateLimiterRegistry(rateLimiterProperties, rateLimiterRegistryEventConsumer, compositeRateLimiterCustomizer); registerEventConsumer(rateLimiterRegistry, rateLimiterEventsConsumerRegistry, rateLimiterProperties); rateLimiterProperties.getInstances().forEach( (name, properties) -> rateLimiterRegistry .rateLimiter(name, rateLimiterProperties .createRateLimiterConfig(properties, compositeRateLimiterCustomizer, name)) ); return rateLimiterRegistry; }  @Bean @Qualifier("compositeRateLimiterCustomizer") CompositeCustomizer<RateLimiterConfigCustomizer> compositeRateLimiterCustomizer(
        @Nullable List<RateLimiterConfigCustomizer> configCustomizers); @Bean RateLimiterRegistry rateLimiterRegistry(
        RateLimiterConfigurationProperties rateLimiterProperties,
        EventConsumerRegistry<RateLimiterEvent> rateLimiterEventsConsumerRegistry,
        RegistryEventConsumer<RateLimiter> rateLimiterRegistryEventConsumer,
        @Qualifier("compositeRateLimiterCustomizer") CompositeCustomizer<RateLimiterConfigCustomizer> compositeRateLimiterCustomizer); @Bean @Primary RegistryEventConsumer<RateLimiter> rateLimiterRegistryEventConsumer(
        Optional<List<RegistryEventConsumer<RateLimiter>>> optionalRegistryEventConsumers); @Bean @Conditional(value = {AspectJOnClasspathCondition.class}) RateLimiterAspect rateLimiterAspect(
        RateLimiterConfigurationProperties rateLimiterProperties,
        RateLimiterRegistry rateLimiterRegistry,
        @Autowired(required = false) List<RateLimiterAspectExt> rateLimiterAspectExtList,
        FallbackDecorators fallbackDecorators,
        SpelResolver spelResolver
    ); @Bean @Conditional(value = {RxJava2OnClasspathCondition.class, AspectJOnClasspathCondition.class}) RxJava2RateLimiterAspectExt rxJava2RateLimiterAspectExt(); @Bean @Conditional(value = {ReactorOnClasspathCondition.class, AspectJOnClasspathCondition.class}) ReactorRateLimiterAspectExt reactorRateLimiterAspectExt(); @Bean EventConsumerRegistry<RateLimiterEvent> rateLimiterEventsConsumerRegistry(); }
RateLimiterConfiguration { @Bean public RateLimiterRegistry rateLimiterRegistry( RateLimiterConfigurationProperties rateLimiterProperties, EventConsumerRegistry<RateLimiterEvent> rateLimiterEventsConsumerRegistry, RegistryEventConsumer<RateLimiter> rateLimiterRegistryEventConsumer, @Qualifier("compositeRateLimiterCustomizer") CompositeCustomizer<RateLimiterConfigCustomizer> compositeRateLimiterCustomizer) { RateLimiterRegistry rateLimiterRegistry = createRateLimiterRegistry(rateLimiterProperties, rateLimiterRegistryEventConsumer, compositeRateLimiterCustomizer); registerEventConsumer(rateLimiterRegistry, rateLimiterEventsConsumerRegistry, rateLimiterProperties); rateLimiterProperties.getInstances().forEach( (name, properties) -> rateLimiterRegistry .rateLimiter(name, rateLimiterProperties .createRateLimiterConfig(properties, compositeRateLimiterCustomizer, name)) ); return rateLimiterRegistry; }  @Bean @Qualifier("compositeRateLimiterCustomizer") CompositeCustomizer<RateLimiterConfigCustomizer> compositeRateLimiterCustomizer(
        @Nullable List<RateLimiterConfigCustomizer> configCustomizers); @Bean RateLimiterRegistry rateLimiterRegistry(
        RateLimiterConfigurationProperties rateLimiterProperties,
        EventConsumerRegistry<RateLimiterEvent> rateLimiterEventsConsumerRegistry,
        RegistryEventConsumer<RateLimiter> rateLimiterRegistryEventConsumer,
        @Qualifier("compositeRateLimiterCustomizer") CompositeCustomizer<RateLimiterConfigCustomizer> compositeRateLimiterCustomizer); @Bean @Primary RegistryEventConsumer<RateLimiter> rateLimiterRegistryEventConsumer(
        Optional<List<RegistryEventConsumer<RateLimiter>>> optionalRegistryEventConsumers); @Bean @Conditional(value = {AspectJOnClasspathCondition.class}) RateLimiterAspect rateLimiterAspect(
        RateLimiterConfigurationProperties rateLimiterProperties,
        RateLimiterRegistry rateLimiterRegistry,
        @Autowired(required = false) List<RateLimiterAspectExt> rateLimiterAspectExtList,
        FallbackDecorators fallbackDecorators,
        SpelResolver spelResolver
    ); @Bean @Conditional(value = {RxJava2OnClasspathCondition.class, AspectJOnClasspathCondition.class}) RxJava2RateLimiterAspectExt rxJava2RateLimiterAspectExt(); @Bean @Conditional(value = {ReactorOnClasspathCondition.class, AspectJOnClasspathCondition.class}) ReactorRateLimiterAspectExt reactorRateLimiterAspectExt(); @Bean EventConsumerRegistry<RateLimiterEvent> rateLimiterEventsConsumerRegistry();  }