-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: seungpang <[email protected]> Co-authored-by: yxxnghwan <[email protected]> Co-authored-by: kth990303 <[email protected]> Co-authored-by: asebn1 <[email protected]> Co-authored-by: prefer2 <[email protected]> Co-authored-by: soyi47 <[email protected]>
- Loading branch information
Showing
180 changed files
with
3,325 additions
and
1,337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
## local에서 간편하게 mysql 이중화 설정하기 | ||
|
||
### 도커 실행하기 | ||
|
||
```shell | ||
# docker-compose.yml있는 디렉토리로 이동 후 | ||
docker-compose up -d | ||
``` | ||
|
||
### source mysql 접속해서 복제 계정 생성 | ||
```shell | ||
docker ps | ||
docker exec -it {SOURCE_CONTAINER_ID} bash | ||
mysql -uroot -p | ||
#비밀번호는 password | ||
|
||
# 복제 계정 준비 | ||
CREATE USER 'repl_user'@'%' IDENTIFIED BY 'password'; | ||
GRANT REPLICATION SLAVE ON *.* To 'repl_user'@'%'; | ||
flush privileges; | ||
``` | ||
|
||
### source 서버 host 알기 | ||
```shell | ||
docker network ls | ||
docker inspect {CONTAINER ID} | ||
# docker_net-mysql | ||
``` | ||
|
||
### replica mysql 접속하기 | ||
```shell | ||
docker ps | ||
docker exec -it {REPLICA_CONTAINER_ID} bash | ||
mysql -uroot -p | ||
#비밀번호는 password | ||
``` | ||
|
||
### replica 도커 접속해서 복제 설정 입력하기 | ||
```shell | ||
stop replica; | ||
|
||
CHANGE REPLICATION SOURCE TO | ||
SOURCE_HOST='소스서버 host주소', | ||
SOURCE_PORT=3306, | ||
SOURCE_USER='repl_user', | ||
SOURCE_PASSWORD='password', | ||
SOURCE_AUTO_POSITION=1, | ||
GET_SOURCE_PUBLIC_KEY=1; | ||
|
||
start replica; | ||
``` | ||
|
||
### replica mysql 상태 확인하기 | ||
``` | ||
show replica status\G; | ||
``` | ||
|
||
```shell | ||
Replica_IO_Running: No 상태이고 | ||
Last_IO_Error: Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; | ||
these ids must be different for replication to work 이런 에러상태이면 | ||
|
||
해당 레플리카 서버의 server_id가 소스서버와 같은 상태 | ||
stop replica; | ||
|
||
SET GLOBAL server_id=2 | ||
|
||
start replica; | ||
``` | ||
|
||
### 이중화가 정상적으로 이루어 지는지 확인하는 법 | ||
```shell | ||
#mysql 접속 | ||
#log상태 확인 | ||
show variables like 'general%'; | ||
|
||
#log 활성화 | ||
set global general_log = ON; | ||
|
||
show variables like 'general%'; | ||
|
||
#해당 위치에 있는 로그 파일을 확인하면서 CUD, R로 잘 분기되는지 확인해볼 수 있음 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
version: "3" | ||
services: | ||
db-source: | ||
build: | ||
context: ./ | ||
dockerfile: source/Dockerfile | ||
restart: always | ||
environment: | ||
MYSQL_DATABASE: 'naepyeon' | ||
MYSQL_USER: 'user' | ||
MYSQL_PASSWORD: 'password' | ||
MYSQL_ROOT_PASSWORD: 'password' | ||
ports: | ||
- '3306:3306' | ||
# Where our data will be persisted | ||
volumes: | ||
- my-db-source:/var/lib/mysql | ||
- my-db-source:/var/lib/mysql-files | ||
networks: | ||
- net-mysql | ||
|
||
db-replica: | ||
build: | ||
context: ./ | ||
dockerfile: replica/Dockerfile | ||
restart: always | ||
environment: | ||
MYSQL_DATABASE: 'naepyeon' | ||
MYSQL_ROOT_PASSWORD: 'password' | ||
ports: | ||
- '3307:3306' | ||
# Where our data will be persisted | ||
volumes: | ||
- my-db-replica:/var/lib/mysql | ||
- my-db-replica:/var/lib/mysql-files | ||
networks: | ||
- net-mysql | ||
|
||
# Names our volume | ||
volumes: | ||
my-db-source: | ||
my-db-replica: | ||
|
||
networks: | ||
net-mysql: | ||
driver: bridge |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FROM --platform=linux/x86_64 mysql:8.0 | ||
ADD ./replica/my.cnf /etc/mysql/my.cnf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[mysqld] | ||
gtid_mode=ON | ||
enforce_gtid_consistency=ON | ||
server_id=2 | ||
relay_log =/var/lib/mysql/mysql-relay-bin | ||
relay_log_purge=ON | ||
read_only | ||
log_slave_updates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
FROM --platform=linux/x86_64 mysql:8.0 | ||
ADD ./source/my.cnf /etc/mysql/my.cnf |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[mysqld] | ||
gtid_mode=ON | ||
enforce_gtid_consistency=ON | ||
server_id=1 | ||
log_bin = mysql-bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
== 알림 | ||
|
||
=== 구독 | ||
operation::notification-controller-test/subscribe[snippets='http-request,http-response'] | ||
|
||
=== 안 읽은 알림 전체 조회 | ||
operation::notification-controller-test/notifications[snippets='http-request,http-response'] | ||
|
||
=== 읽지 않은 알림 단건 읽기 | ||
operation::notification-controller-test/read-notification[snippets='http-request,http-response'] | ||
|
||
=== 읽지 않은 알림 모두 읽기 | ||
operation::notification-controller-test/read-all-notifications[snippets='http-request,http-response'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
backend/src/main/java/com/woowacourse/naepyeon/config/DataSourceConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.woowacourse.naepyeon.config; | ||
|
||
import com.woowacourse.naepyeon.support.db.DatabaseType; | ||
import com.woowacourse.naepyeon.support.db.RoutingDataSource; | ||
import java.util.HashMap; | ||
import javax.sql.DataSource; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.jdbc.DataSourceBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.DependsOn; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class DataSourceConfig { | ||
|
||
@Bean | ||
@ConfigurationProperties(prefix = "spring.datasource.source.hikari") | ||
public DataSource sourceDataSource() { | ||
return DataSourceBuilder.create() | ||
.build(); | ||
} | ||
|
||
@Bean | ||
@ConfigurationProperties(prefix = "spring.datasource.replica.hikari") | ||
public DataSource replicaDataSource() { | ||
return DataSourceBuilder.create() | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public DataSource routingDataSource() { | ||
final RoutingDataSource routingDataSource = new RoutingDataSource(); | ||
final HashMap<Object, Object> dataSources = new HashMap<>(); | ||
|
||
dataSources.put(DatabaseType.SOURCE, sourceDataSource()); | ||
dataSources.put(DatabaseType.REPLICA, replicaDataSource()); | ||
|
||
routingDataSource.setDefaultTargetDataSource(sourceDataSource()); | ||
routingDataSource.setTargetDataSources(dataSources); | ||
|
||
return routingDataSource; | ||
} | ||
|
||
@Primary | ||
@Bean | ||
@DependsOn({"sourceDataSource", "replicaDataSource", "routingDataSource"}) | ||
public DataSource dataSource() { | ||
return new LazyConnectionDataSourceProxy(routingDataSource()); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
backend/src/main/java/com/woowacourse/naepyeon/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.woowacourse.naepyeon.config; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.woowacourse.naepyeon.service.dto.NotificationResponseDto; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.RedisOperations; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.data.redis.listener.RedisMessageListenerContainer; | ||
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializer; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Value("${spring.redis.host}") | ||
private String host; | ||
|
||
@Value("${spring.redis.port}") | ||
private int port; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
return new LettuceConnectionFactory(host, port); | ||
} | ||
|
||
@Bean | ||
public RedisOperations<String, NotificationResponseDto> eventRedisOperations( | ||
RedisConnectionFactory redisConnectionFactory, ObjectMapper objectMapper) { | ||
final Jackson2JsonRedisSerializer<NotificationResponseDto> jsonRedisSerializer = new Jackson2JsonRedisSerializer<>( | ||
NotificationResponseDto.class); | ||
jsonRedisSerializer.setObjectMapper(objectMapper); | ||
final RedisTemplate<String, NotificationResponseDto> eventRedisTemplate = new RedisTemplate<>(); | ||
eventRedisTemplate.setConnectionFactory(redisConnectionFactory); | ||
eventRedisTemplate.setKeySerializer(RedisSerializer.string()); | ||
eventRedisTemplate.setValueSerializer(jsonRedisSerializer); | ||
eventRedisTemplate.setHashKeySerializer(RedisSerializer.string()); | ||
eventRedisTemplate.setHashValueSerializer(jsonRedisSerializer); | ||
return eventRedisTemplate; | ||
} | ||
|
||
@Bean | ||
public RedisMessageListenerContainer redisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory) { | ||
final RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer(); | ||
redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory); | ||
return redisMessageListenerContainer; | ||
} | ||
} |
Oops, something went wrong.