1
1
package com .sharetreats .chatbot .infra .config ;
2
2
3
+ import io .lettuce .core .ReadFrom ;
3
4
import org .springframework .beans .factory .annotation .Value ;
4
5
import org .springframework .context .annotation .Bean ;
5
6
import org .springframework .context .annotation .Configuration ;
6
7
import org .springframework .context .annotation .Primary ;
7
8
import org .springframework .data .redis .connection .RedisClusterConfiguration ;
9
+ import org .springframework .data .redis .connection .RedisConnectionFactory ;
8
10
import org .springframework .data .redis .connection .RedisNode ;
11
+ import org .springframework .data .redis .connection .lettuce .LettuceClientConfiguration ;
9
12
import org .springframework .data .redis .connection .lettuce .LettuceConnectionFactory ;
10
13
import org .springframework .data .redis .core .RedisTemplate ;
11
14
import org .springframework .data .redis .serializer .GenericJackson2JsonRedisSerializer ;
14
17
@ Configuration
15
18
public class RedisConfig {
16
19
17
- @ Value ("${spring.redis.gift-history.host}" )
18
- private String redisHost ;
19
-
20
- @ Value ("${spring.redis.gift-history.port}" )
21
- private int redisPort ;
22
-
23
- @ Value ("${spring.redis.token.host}" )
24
- private String tokenRedisHost ;
25
-
26
- @ Value ("${spring.redis.token.port}" )
27
- private int tokenRedisPort ;
28
-
29
20
@ Bean
30
- @ Primary
31
- public LettuceConnectionFactory connectionFactory () {
32
- return new LettuceConnectionFactory (redisHost , redisPort );
33
- }
34
-
35
- @ Bean
36
- public LettuceConnectionFactory tokenConnectionFactory () {
37
- RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration ();
38
- clusterConfig .addClusterNode (new RedisNode (tokenRedisHost , tokenRedisPort ));
39
-
40
- return new LettuceConnectionFactory (clusterConfig );
21
+ public RedisConnectionFactory redisConnectionFactory () {
22
+ LettuceClientConfiguration clientConfiguration = LettuceClientConfiguration .builder ()
23
+ .readFrom (ReadFrom .REPLICA_PREFERRED )
24
+ .build ();
25
+
26
+ RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration ()
27
+ .clusterNode ("localhost" , 8000 )
28
+ .clusterNode ("localhost" , 8001 )
29
+ .clusterNode ("localhost" , 8002 )
30
+ .clusterNode ("localhost" , 8003 )
31
+ .clusterNode ("localhost" , 8004 )
32
+ .clusterNode ("localhost" , 8005 );
33
+
34
+ LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory (redisClusterConfiguration , clientConfiguration );
35
+ return lettuceConnectionFactory ;
41
36
}
42
37
@ Bean
43
38
public RedisTemplate <String , Object > redisTemplate () {
44
39
RedisTemplate <String , Object > redisTemplate = new RedisTemplate <>();
45
40
redisTemplate .setDefaultSerializer (new GenericJackson2JsonRedisSerializer ());
46
- redisTemplate .setConnectionFactory (connectionFactory ());
41
+ redisTemplate .setConnectionFactory (redisConnectionFactory ());
47
42
return redisTemplate ;
48
43
}
49
44
@@ -52,7 +47,7 @@ public RedisTemplate<String, Object> redisTemplate() {
52
47
public RedisTemplate <String , String > tokenRedisTemplate () {
53
48
RedisTemplate <String , String > redisTemplate = new RedisTemplate <>();
54
49
redisTemplate .setDefaultSerializer (new StringRedisSerializer ());
55
- redisTemplate .setConnectionFactory (tokenConnectionFactory ());
50
+ redisTemplate .setConnectionFactory (redisConnectionFactory ());
56
51
return redisTemplate ;
57
52
}
58
53
}
0 commit comments