본문 바로가기
Backend

[Redis] 배포환경에서의 Redis관련 Error

by psihyeong 2023. 4. 26.

1. docker logs [spring backend 컨테이너]

2023-04-26 07:18:20.123 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/api/v1] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379] with root cause

 

-> Redis 구성 클래스를 작성안해줘서 커넥션이 안된것

 

2. RedisConfig.java 작성

package me.nogari.nogari.config;


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.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@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 RedisTemplate<String, Integer> redisTemplate() {
        RedisTemplate<String, Integer> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory());
        return redisTemplate;
    }
}

- Config파일을 적용하니 다음과 같은 READONLYEXCEPTION이 발생했다.

2023-04-26 08:09:24.547 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/api/v1] threw exception [Request processing failed; nested exception is org.springframework.data.redis.RedisSystemException: Error in execution; nested exception is io.lettuce.core.RedisReadOnlyException: READONLY You can't write against a read only replica.] with root cause

io.lettuce.core.RedisReadOnlyException: READONLY You can't write against a read only replica.

 

 

3. redis 접속

redis에 접속하기 전에 redis가 설치된 docker 컨테이너 내부로 접속

$ docker exec -it redis /bin/bashhtml

 

💡 참고

만약 아래와 같은 에러가 발생한다면 다음과 같이 접속

OCI runtime exec failed: exec failed: container_linux.go:380: starting container process caused: exec: "/bin/bash": stat /bin/bash: no such file or directory: unknown

$ docker exec -it redis /bin/shhtml

 

redis-cli 명령어를 이용하여 redis에 접속

/data# redis-cli

127.0.0.1:6379>

슬레이브에서 info 명령어를 내리면 slave_read_only 이다.

 

> info

slave_read_only:1

 

 

slave_read_only 값으로 상태를 변경한다.

 

> config set slave-read-only no

OK

 

해결 완