Skip to content

Commit

Permalink
Merge pull request #97 from 2023-Team-Joon-CheckIt/BE/feat/#96
Browse files Browse the repository at this point in the history
[feat/#96] redis 클러스터를 사용한 좋아요 기능 구현
  • Loading branch information
fnzl54 authored Oct 6, 2023
2 parents e5bf327 + 532bc94 commit 4e7144a
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 20 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor"
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}
Expand Down
90 changes: 78 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,86 @@ services:
networks:
default_bridge:
ipv4_address: 172.16.1.5
redis:
container_name: redis
image: redis:latest

# redis:
# container_name: redis
# image: redis:latest
# ports:
# - 6379:6379
# volumes:
# - ./redis/data:/data
# labels:
# - "name=redis"
# - "mode=standalone"
# restart: always
# command: redis-server

redis-master-1:
container_name: redis-master-1
image: redis
volumes:
- ./redis/config/redis-master-1.conf:/etc/redis.conf
command: redis-server /etc/redis.conf
ports:
- 6379:6379
- 7001:7001
- 7002:7002
- 7003:7003
- 7101:7101
- 7102:7102
- 7103:7103

redis-master-2:
network_mode: "service:redis-master-1"
container_name: redis-master-2
image: redis
volumes:
- ./redis/data:/data
labels:
- "name=redis"
- "mode=standalone"
restart: always
command: redis-server
networks:
- default_bridge
- ./redis/config/redis-master-2.conf:/etc/redis.conf
command: redis-server /etc/redis.conf

redis-master-3:
network_mode: "service:redis-master-1"
container_name: redis-master-3
image: redis
volumes:
- ./redis/config/redis-master-3.conf:/etc/redis.conf
command: redis-server /etc/redis.conf

redis-slave-1:
network_mode: "service:redis-master-1"
container_name: redis-slave-1
image: redis
volumes:
- ./redis/config/redis-slave-1.conf:/etc/redis.conf
command: redis-server /etc/redis.conf

redis-slave-2:
network_mode: "service:redis-master-1"
container_name: redis-slave-2
image: redis
volumes:
- ./redis/config/redis-slave-2.conf:/etc/redis.conf
command: redis-server /etc/redis.conf

redis-slave-3:
network_mode: "service:redis-master-1"
container_name: redis-slave-3
image: redis
volumes:
- ./redis/config/redis-slave-3.conf:/etc/redis.conf
command: redis-server /etc/redis.conf

redis-cluster-entry:
network_mode: "service:redis-master-1"
image: redis
container_name: redis-cluster
command: redis-cli --cluster create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7101 127.0.0.1:7102 127.0.0.1:7103 --cluster-replicas 1 --cluster-yes
depends_on:
- redis-master-1
- redis-master-2
- redis-master-3
- redis-slave-1
- redis-slave-2
- redis-slave-3

elasticsearch:
restart: always
Expand Down
5 changes: 5 additions & 0 deletions redis/config/redis-master-1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
port 7001
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 3000
appendonly yes
5 changes: 5 additions & 0 deletions redis/config/redis-master-2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
port 7002
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 3000
appendonly yes
5 changes: 5 additions & 0 deletions redis/config/redis-master-3.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
port 7003
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 3000
appendonly yes
5 changes: 5 additions & 0 deletions redis/config/redis-slave-1.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
port 7101
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 3000
appendonly yes
5 changes: 5 additions & 0 deletions redis/config/redis-slave-2.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
port 7102
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 3000
appendonly yes
5 changes: 5 additions & 0 deletions redis/config/redis-slave-3.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
port 7103
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 3000
appendonly yes
20 changes: 20 additions & 0 deletions src/main/java/com/techeer/checkIt/global/config/RedisInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.techeer.checkIt.global.config;

import java.util.List;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

@Getter
@Setter
@NoArgsConstructor
@ConfigurationProperties(prefix = "redis") // 설정 값을 불러올 때 prefix 값을 지정할 수 있다.
@Configuration
public class RedisInfo {
private String host;
private int port;
private RedisInfo master;
private List<RedisInfo> slaves;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.techeer.checkIt.global.config;

import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
Expand All @@ -15,24 +15,30 @@
@Configuration
@EnableRedisRepositories
public class RedisRepositoryConfig {
private final RedisProperties redisProperties;

// lettuce
// RedisConnectionFactory 인터페이스를 통해 LettuceConnectionFactory를 생성하여 반환한다.
// RedisProperties로 yaml에 저장한 host, post를 가지고 와서 연결한다.

@Bean
public RedisConnectionFactory redisConnectionFactory(){
return new LettuceConnectionFactory(redisProperties.getHost(), redisProperties.getPort());
public RedisConnectionFactory redisConnectionFactory() {
RedisClusterConfiguration clusterConfig = new RedisClusterConfiguration();
clusterConfig.addClusterNode(new RedisNode("127.0.0.1", 7001));
clusterConfig.addClusterNode(new RedisNode("127.0.0.1", 7002));
clusterConfig.addClusterNode(new RedisNode("127.0.0.1", 7003));
clusterConfig.addClusterNode(new RedisNode("127.0.0.1", 7101));
clusterConfig.addClusterNode(new RedisNode("127.0.0.1", 7102));
clusterConfig.addClusterNode(new RedisNode("127.0.0.1", 7103));

return new LettuceConnectionFactory(clusterConfig);
}

// setKeySerializer, setValueSerializer 설정으로 redis-cli를 통해 직접 데이터를 보는게 가능하다.
@Bean
public RedisTemplate<String, Object> redisTemplate() {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}

Expand All @@ -42,7 +48,6 @@ public RedisTemplate<String, String> redisLikeTemplate() {
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(redisConnectionFactory());
redisTemplate.setEnableTransactionSupport(true);
return redisTemplate;
}
}

0 comments on commit 4e7144a

Please sign in to comment.