Skip to content

Instantly share code, notes, and snippets.

@reevik
Last active June 6, 2022 16:52
Show Gist options
  • Save reevik/2ef57d974ed80343a87977d249888cf1 to your computer and use it in GitHub Desktop.
Save reevik/2ef57d974ed80343a87977d249888cf1 to your computer and use it in GitHub Desktop.
@EnableWebMvc
@Configuration
@EnableCaching
public class AppConfig implements WebMvcConfigurer {
@Bean
public RateLimiter rateLimiter() throws IOException {
return new RateLimiter(proxyManager(), bucketConfiguration());
}
@Bean(destroyMethod = "shutdown")
public ConnectionManager redissonConnectionManager() throws IOException {
File resourceURL = ResourceUtils.getFile("classpath:redis.yml");
Config config = Config.fromYAML(resourceURL);
return ConfigSupport.createConnectionManager(config);
}
@Bean
public RedissonBasedProxyManager proxyManager() throws IOException {
CommandSyncService commandSyncService =
new CommandSyncService(redissonConnectionManager());
return new RedissonBasedProxyManager(commandSyncService,
ClientSideConfig.getDefault(),
Duration.ofMinutes(10));
}
@Bean
public BucketConfiguration bucketConfiguration() {
return BucketConfiguration
.builder()
.addLimit(Bandwidth.simple(2, Duration.ofSeconds(1)).withInitialTokens(2))
.build();
}
}
@jaksonlima
Copy link

RedissonBasedProxyManager ?

@reevik
Copy link
Author

reevik commented Jun 6, 2022

Hey @jaksonlima, please refer to the proxy manager implementation that is provided by Bucket4j.
You can also take a look at, https://medium.bitwise.blog/rate-limiting-with-token-buckets-7f912525819f

@jaksonlima
Copy link

Hello how are you ?
I already read your post I wanted to know if you have the repository on github to look at the dependencies because I can't find the class "RedissonBasedProxyManager"

@jaksonlima
Copy link

jaksonlima commented Jun 6, 2022

Using this dependency ('com.github.vladimir-bukhtoyarov:bucket4j-redis:7.5.0'), have I managed to follow these settings (https://medium.bitwise.blog/rate-limiting-with-token-buckets-7f912525819f) ?

@reevik
Copy link
Author

reevik commented Jun 6, 2022

Here is the repo of the example project: https://github.com/bagdemir/bucket4j-spring-redis-example
Enjoy!

@jaksonlima
Copy link

jaksonlima commented Jun 6, 2022

public class CommandSyncService extends CommandAsyncService implements CommandExecutor {

    public CommandSyncService(ConnectionManager connectionManager, RedissonObjectBuilder objectBuilder) {
        super(connectionManager, objectBuilder, RedissonObjectBuilder.ReferenceType.DEFAULT);
    }
}

CommandSyncService :
In the version (org.redisson:redisson-spring-boot-starter:3.17.0) it is receiving more than one parameter!
I will have to keep past version (org.redisson:redisson-spring-boot-starter:3.12.4)

@jaksonlima
Copy link

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisson' defined in class path resource [org/redisson/spring/starter/RedissonAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.redisson.api.RedissonClient]: Factory method 'redisson' threw exception; nested exception is java.lang.reflect.InaccessibleObjectException: Unable to make field private final byte[] java.lang.String.value accessible: module java.base does not "opens java.lang" to unnamed module @3e07d849

@jaksonlima
Copy link

import io.github.bucket4j.Bandwidth;
import io.github.bucket4j.BucketConfiguration;
import io.github.bucket4j.distributed.proxy.ClientSideConfig;
import io.github.bucket4j.redis.redisson.cas.RedissonBasedProxyManager;
import org.redisson.api.RedissonClient;
import org.redisson.command.CommandSyncService;
import org.redisson.config.Config;
import org.redisson.config.ConfigSupport;
import org.redisson.connection.ConnectionManager;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.io.File;
import java.io.IOException;
import java.time.Duration;

@EnableWebMvc
@Configuration
@EnableCaching
public class Bucket4JConfigurer {

    @Bean
    public RateLimiter rateLimiter() throws IOException {
        return new RateLimiter(proxyManager(), bucketConfiguration());
    }

    @Bean(destroyMethod = "shutdown")
    public ConnectionManager redissonConnectionManager() throws IOException {
        File resourceURL = ResourceUtils.getFile("classpath:redis.yml");
        Config config = Config.fromYAML(resourceURL);
        return ConfigSupport.createConnectionManager(config);
    }

    @Bean
    public RedissonBasedProxyManager proxyManager() throws IOException {
        CommandSyncService commandSyncService = new CommandSyncService(redissonConnectionManager());
        return new RedissonBasedProxyManager(commandSyncService,
                ClientSideConfig.getDefault(),
                Duration.ofMinutes(10));
    }

    @Bean
    public BucketConfiguration bucketConfiguration() {
        return BucketConfiguration.builder()
                .addLimit(Bandwidth.simple(2, Duration.ofSeconds(1)).withInitialTokens(2))
                .build();
    }

    @Bean
    public CacheManager cacheManager(RedissonClient redissonClient) throws IOException {
        String configFileName = "cache-config.yml";
        return new RedissonSpringCacheManager(redissonClient, "classpath:" + configFileName);
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment