|
| 1 | +package com.loopers.config.redis; |
| 2 | + |
| 3 | + |
| 4 | +import io.lettuce.core.ReadFrom; |
| 5 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 6 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 7 | +import org.springframework.context.annotation.Bean; |
| 8 | +import org.springframework.context.annotation.Configuration; |
| 9 | +import org.springframework.context.annotation.Primary; |
| 10 | +import org.springframework.data.redis.connection.RedisStaticMasterReplicaConfiguration; |
| 11 | +import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration; |
| 12 | +import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; |
| 13 | +import org.springframework.data.redis.core.RedisTemplate; |
| 14 | +import org.springframework.data.redis.serializer.StringRedisSerializer; |
| 15 | + |
| 16 | +import java.util.List; |
| 17 | +import java.util.function.Consumer; |
| 18 | + |
| 19 | +@Configuration |
| 20 | +@EnableConfigurationProperties(RedisProperties.class) |
| 21 | +public class RedisConfig{ |
| 22 | + private static final String CONNECTION_MASTER = "redisConnectionMaster"; |
| 23 | + public static final String REDIS_TEMPLATE_MASTER = "redisTemplateMaster"; |
| 24 | + |
| 25 | + private final RedisProperties redisProperties; |
| 26 | + |
| 27 | + public RedisConfig(RedisProperties redisProperties){ |
| 28 | + this.redisProperties = redisProperties; |
| 29 | + } |
| 30 | + |
| 31 | + @Primary |
| 32 | + @Bean |
| 33 | + public LettuceConnectionFactory defaultRedisConnectionFactory() { |
| 34 | + int database = redisProperties.database(); |
| 35 | + RedisNodeInfo master = redisProperties.master(); |
| 36 | + List<RedisNodeInfo> replicas = redisProperties.replicas(); |
| 37 | + return lettuceConnectionFactory( |
| 38 | + database, master, replicas, |
| 39 | + b -> b.readFrom(ReadFrom.REPLICA_PREFERRED) |
| 40 | + ); |
| 41 | + } |
| 42 | + |
| 43 | + @Qualifier(CONNECTION_MASTER) |
| 44 | + @Bean |
| 45 | + public LettuceConnectionFactory masterRedisConnectionFactory() { |
| 46 | + int database = redisProperties.database(); |
| 47 | + RedisNodeInfo master = redisProperties.master(); |
| 48 | + List<RedisNodeInfo> replicas = redisProperties.replicas(); |
| 49 | + return lettuceConnectionFactory( |
| 50 | + database, master, replicas, |
| 51 | + b -> b.readFrom(ReadFrom.MASTER) |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + @Primary |
| 56 | + @Bean |
| 57 | + public RedisTemplate<String, String> defaultRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { |
| 58 | + RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); |
| 59 | + return defaultRedisTemplate(redisTemplate, lettuceConnectionFactory); |
| 60 | + } |
| 61 | + |
| 62 | + @Qualifier(REDIS_TEMPLATE_MASTER) |
| 63 | + @Bean |
| 64 | + public RedisTemplate<String, String> masterRedisTemplate( |
| 65 | + @Qualifier(CONNECTION_MASTER) LettuceConnectionFactory lettuceConnectionFactory |
| 66 | + ) { |
| 67 | + RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); |
| 68 | + return defaultRedisTemplate(redisTemplate, lettuceConnectionFactory); |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | + private LettuceConnectionFactory lettuceConnectionFactory( |
| 73 | + int database, |
| 74 | + RedisNodeInfo master, |
| 75 | + List<RedisNodeInfo> replicas, |
| 76 | + Consumer<LettuceClientConfiguration.LettuceClientConfigurationBuilder> customizer |
| 77 | + ){ |
| 78 | + LettuceClientConfiguration.LettuceClientConfigurationBuilder builder = LettuceClientConfiguration.builder(); |
| 79 | + if(customizer != null) customizer.accept(builder); |
| 80 | + LettuceClientConfiguration clientConfig = builder.build(); |
| 81 | + RedisStaticMasterReplicaConfiguration masterReplicaConfig = new RedisStaticMasterReplicaConfiguration(master.host(), master.port()); |
| 82 | + masterReplicaConfig.setDatabase(database); |
| 83 | + for(RedisNodeInfo r : replicas){ |
| 84 | + masterReplicaConfig.addNode(r.host(), r.port()); |
| 85 | + } |
| 86 | + return new LettuceConnectionFactory(masterReplicaConfig, clientConfig); |
| 87 | + } |
| 88 | + |
| 89 | + private <K,V> RedisTemplate<K,V> defaultRedisTemplate( |
| 90 | + RedisTemplate<K,V> template, |
| 91 | + LettuceConnectionFactory connectionFactory |
| 92 | + ){ |
| 93 | + StringRedisSerializer s = new StringRedisSerializer(); |
| 94 | + template.setKeySerializer(s); |
| 95 | + template.setValueSerializer(s); |
| 96 | + template.setHashKeySerializer(s); |
| 97 | + template.setHashValueSerializer(s); |
| 98 | + template.setConnectionFactory(connectionFactory); |
| 99 | + return template; |
| 100 | + } |
| 101 | +} |
0 commit comments