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

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