today'work
spring boot app과 redis cluster를 docker로 사용할 때 Redis connection failed 발생
msna
2023. 7. 19. 01:59
- 발생문제
- redis cluster를 docker로 기동하고 있는데, spring boot app을 docker로 기동해야하는 필요가 생김.
- redis cluster는 localhost로 연결하고 있었는데, spring boot app이 docker가 되면서 redis cluster의 컨테이너명("redis-cluster")으로 변경함.
- 로그를 보면 redis에 연결에 성공하는 로그도 보이나, 연결 실패도 발생함.
- 연결 성공 로그
{"level":"DEBUG","message":"Connecting to Redis at redis-cluster:6379: Success","stackTrace":""}
-
-
- 연결 실패 로그
-
{"level":"ERROR", "message":"Application run failed", "stackTrace":"org.springframework.context.ApplicationContextException: Failed to start bean 'springSessionRedisMessageListenerContainer'; nested exception is org.springframework.data.redis.listener.adapter.RedisListenerExecutionFailedException: org.springframework.data.redis.RedisConnectionFailureException: Redis connection failed; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to [redis://*********@redis-cluster, redis://*********@redis-cluster:6380, redis://*********@redis-cluster:6381]; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Redis connection failed; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to [redis://*********@redis-cluster, redis://*********@redis-cluster:6380, redis://*********@redis-cluster:6381]\n \tat org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181)\n Caused by: org.springframework.data.redis.listener.adapter.RedisListenerExecutionFailedException: org.springframework.data.redis.RedisConnectionFailureException: Redis connection failed; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to [redis://*********@redis-cluster, redis://*********@redis-cluster:6380, redis://*********@redis-cluster:6381]; nested exception is org.springframework.data.redis.RedisConnectionFailureException: Redis connection failed; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to [redis://*********@redis-cluster, redis://*********@redis-cluster:6380, redis://*********@redis-cluster:6381]\n \tat org.springframework.data.redis.listener.RedisMessageListenerContainer.lazyListen(RedisMessageListenerContainer.java:275)\nCaused by: org.springframework.data.redis.RedisConnectionFailureException: Redis connection failed; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to [redis://*********@redis-cluster, redis://*********@redis-cluster:6380, redis://*********@redis-cluster:6381]\n \tat org.springframework.data.redis.connection.lettuce.LettuceExceptionConverter.convert(LettuceExceptionConverter.java:66)\nCaused by: io.lettuce.core.RedisConnectionException: Unable to connect to [redis://*********@redis-cluster, redis://*********@redis-cluster:6380, redis://*********@redis-cluster:6381]\n \tat io.lettuce.core.RedisConnectionException.create(RedisConnectionException.java:78)\nCaused by: io.netty.channel.AbstractChannel$AnnotatedConnectException: finishConnect(..) failed: Connection refused: /127.0.0.1:6381\n Caused by: java.net.ConnectException: finishConnect(..) failed: Connection refused\n \tat io.netty.channel.unix.Errors.newConnectException0(Errors.java:155)\n"}
- 해결방법
- 기존에 localhost가 동작했으니 "host.internal.docker"를 이용 => 정확한 이유는 모르겠지만 실패.
- spring boot app이 redis cluster의 네트워크를 이용하도록 설정
- 기존 docker-compose:
services:
spring-boot-app:
ports:
- 8888:8888
redis-cluster:
container_name: redis-cluster
command: redis-server /conf/redis.conf
image: redis:alpine
ports:
- 6379:6379
- 6380:6380
- 6381:6381
-
- 변경 docker-compose:
services:
spring-boot-app:
network_mode: service:redis-cluster
redis-cluster:
container_name: redis-cluster
command: redis-server /conf/redis.conf
image: redis:alpine
ports:
- 6379:6379
- 6380:6380
- 6381:6381
- 8888:8888
- 마무리
- 좋은 해결방법인지는 모르겠지만 어차피 검증서버는 redis가 잘 움직이고 있으므로 더 찾아보지 않고 이걸로 종결.