micronaut-projects / micronaut-redis

Integration between Micronaut and Redis
Apache License 2.0
30 stars 34 forks source link

DefaultRedisClusterClientFactory uses default StringCodec #536

Open herisn23 opened 4 weeks ago

herisn23 commented 4 weeks ago

There is a issue with RedisCodec when using different environment configuration (cluster, non-cluster) when using custom codec.

When developing on localhost i use standalone non-cluster redis. This configuration uses codec created in my factory class.

@Singleton
@Replaces(RedisCodec::class)
@Primary
fun serializedObjectCodec(): RedisCodec<String, Any> =
    createJdkSerializerCodec()

But DefaultRedisClusterClientFactory bypassing this codec because not provide codec when call client.connect() like in non-cluster configuration.

This behaviour causes errors on kubernetes where redis-cluster running, because connection uses default lettuce StringCodec instead of user-defined codec.

It is possible to inherit AbstractRedisClientFactory<K, V> like in DefaultRedisClientFactory and create StatefulRedisClusterConnection with user defined codec?

For now i have work around for this issue with custom configuration but i wont have this code in my app.

@Bean(preDestroy = "close")
@Replaces(StatefulRedisClusterConnection::class)
@Singleton
@Primary
fun redisConnection(
    @Primary codec: RedisCodec<String, Any>,
    @Primary redisClient: RedisClusterClient
): StatefulRedisClusterConnection<String, Any> {
    return redisClient.connect(codec)
}

This is the class which, i think, need improvements.

https://github.com/micronaut-projects/micronaut-redis/blob/80009dc2bec103e499e992e5ddfdfc6d8513adb7/redis-lettuce/src/main/java/io/micronaut/configuration/lettuce/DefaultRedisClusterClientFactory.java#L45

graemerocher commented 4 weeks ago

seems like you diagnosed the problem, would you send a PR?