Не удается подключиться к серверу Docker Redis после интеграционных тестов

#java #spring-boot #docker #redis

Вопрос:

Я наконец-то не знаю, где искать. Я использую общий образ докера для redis в своем docker-compose.yml (тогда есть Кафка и Zookeeper, но это не должно мешать):

 version: '3'
services:
  redis:
    image: redis
    ports:
      - "6369:6379"
    hostname: localhost
    container_name: redis-integration-test
 

Когда я запускаю докер (docker-compose up), все в порядке — я могу подключиться к redis через redis-cli (с измененным портом).

Но после того, как я запустил свои интеграционные тесты, которые действительно используют redis, я больше не могу подключиться к redis. Используя redis-cli -p 6369 после прохождения тестов, дайте мне Could not connect to Redis at 127.0.0.1:6369: Connection refused

Журнал Redis из docker после запуска:

 redis-integration-test | 1:C 17 Sep 2021 15:59:31.767 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis-integration-test | 1:C 17 Sep 2021 15:59:31.767 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis-integration-test | 1:C 17 Sep 2021 15:59:31.767 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
redis-integration-test | 1:M 17 Sep 2021 15:59:31.768 * Increased maximum number of open files to 10032 (it was originally set to 1024).
redis-integration-test | 1:M 17 Sep 2021 15:59:31.768 * monotonic clock: POSIX clock_gettime
redis-integration-test | 1:M 17 Sep 2021 15:59:31.769 * Running mode=standalone, port=6379.
redis-integration-test | 1:M 17 Sep 2021 15:59:31.769 # Server initialized
redis-integration-test | 1:M 17 Sep 2021 15:59:31.769 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
redis-integration-test | 1:M 17 Sep 2021 15:59:31.770 * Loading RDB produced by version 6.2.5
redis-integration-test | 1:M 17 Sep 2021 15:59:31.770 * RDB age 178208 seconds
redis-integration-test | 1:M 17 Sep 2021 15:59:31.770 * RDB memory usage when created 0.78 Mb
redis-integration-test | 1:M 17 Sep 2021 15:59:31.770 * DB loaded from disk: 0.001 seconds
redis-integration-test | 1:M 17 Sep 2021 15:59:31.770 * Ready to accept connections
 

Когда тесты выполняются, это записывается в журналы, а затем redis становится невозможным:

 redis-integration-test | 1:M 17 Sep 2021 16:04:32.037 * 100 changes in 300 seconds. Saving...
redis-integration-test | 1:M 17 Sep 2021 16:04:32.042 * Background saving started by pid 18
redis-integration-test | 18:C 17 Sep 2021 16:04:32.046 * DB saved on disk
redis-integration-test | 18:C 17 Sep 2021 16:04:32.046 * RDB: 0 MB of memory used by copy-on-write
redis-integration-test | 1:M 17 Sep 2021 16:04:32.142 * Background saving terminated with success
 

Моя конфигурация подключения redis (для приложения Kotlin/Java Spring-boot):

 {
  "singleServerConfig": {
    "address": "redis://localhost:6369"
  }
}
 

Окончательная трассировка стека из теста:

 Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsRegistrarConfiguration': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsRegistrarConfiguration]: Constructor threw exception; nested exception is org.redisson.client.WriteRedisConnectionException: Unable to write command into connection! Node source: NodeSource [slot=0, addr=null, redisClient=null, redirect=null, entry=MasterSlaveEntry [masterEntry=[freeSubscribeConnectionsAmount=1, freeSubscribeConnectionsCounter=value:50:queue:0, freeConnectionsAmount=23, freeConnectionsCounter=value:63:queue:0, freezed=false, freezeReason=null, client=[addr=redis://localhost:6369], nodeType=MASTER, firstFail=0]]], connection: RedisConnection@302607802 [redisClient=[addr=redis://localhost:6369], channel=[id: 0x5c66895a, L:0.0.0.0/0.0.0.0:42564], currentCommand=null], command: (HSET), params: [{naturePromiseBioRepository-findAll}:redisson_options, max-size, 0] after 3 retry attempts
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:315)
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:296)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:63)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
    at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:123)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    ... 34 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.autoconfigure.metrics.cache.CacheMetricsRegistrarConfiguration]: Constructor threw exception; nested exception is org.redisson.client.WriteRedisConnectionException: Unable to write command into connection! Node source: NodeSource [slot=0, addr=null, redisClient=null, redirect=null, entry=MasterSlaveEntry [masterEntry=[freeSubscribeConnectionsAmount=1, freeSubscribeConnectionsCounter=value:50:queue:0, freeConnectionsAmount=23, freeConnectionsCounter=value:63:queue:0, freezed=false, freezeReason=null, client=[addr=redis://localhost:6369], nodeType=MASTER, firstFail=0]]], connection: RedisConnection@302607802 [redisClient=[addr=redis://localhost:6369], channel=[id: 0x5c66895a, L:0.0.0.0/0.0.0.0:42564], currentCommand=null], command: (HSET), params: [{naturePromiseBioRepository-findAll}:redisson_options, max-size, 0] after 3 retry attempts
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:225)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:311)
    ... 54 more
Caused by: org.redisson.client.WriteRedisConnectionException: Unable to write command into connection! Node source: NodeSource [slot=0, addr=null, redisClient=null, redirect=null, entry=MasterSlaveEntry [masterEntry=[freeSubscribeConnectionsAmount=1, freeSubscribeConnectionsCounter=value:50:queue:0, freeConnectionsAmount=23, freeConnectionsCounter=value:63:queue:0, freezed=false, freezeReason=null, client=[addr=redis://localhost:6369], nodeType=MASTER, firstFail=0]]], connection: RedisConnection@302607802 [redisClient=[addr=redis://localhost:6369], channel=[id: 0x5c66895a, L:0.0.0.0/0.0.0.0:42564], currentCommand=null], command: (HSET), params: [{naturePromiseBioRepository-findAll}:redisson_options, max-size, 0] after 3 retry attempts
    at org.redisson.command.RedisExecutor.checkWriteFuture(RedisExecutor.java:270)
    at org.redisson.command.RedisExecutor.access$100(RedisExecutor.java:58)
    at org.redisson.command.RedisExecutor$1.operationComplete(RedisExecutor.java:155)
    at org.redisson.command.RedisExecutor$1.operationComplete(RedisExecutor.java:152)
    at io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:578)
    at io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:552)
    at io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:491)
    at io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:616)
    at io.netty.util.concurrent.DefaultPromise.setFailure0(DefaultPromise.java:609)
    at io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:117)
    at io.netty.channel.AbstractChannel$AbstractUnsafe.safeSetFailure(AbstractChannel.java:1009)
    at io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:870)
    at io.netty.channel.DefaultChannelPipeline$HeadContext.write(DefaultChannelPipeline.java:1367)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:717)
    at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:764)
    at io.netty.channel.AbstractChannelHandlerContext$WriteTask.run(AbstractChannelHandlerContext.java:1071)
    at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:164)
    at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:472)
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989)
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: io.netty.channel.StacklessClosedChannelException
    at io.netty.channel.AbstractChannel$AbstractUnsafe.write(Object, ChannelPromise)(Unknown Source)
 

Комментарии:

1. Вероятно, было бы полезно опубликовать ваш . docker-compose.yml В любом случае, я нахожу hostname: localhost директиву странной. Вы пробовали просто подключиться redis://redis:6379 (в конечном итоге указав ссылки или сети )?

2. Я обновил комментарий конфигурацией файла YAML. И я могу подключиться к redis через «redis-cli» в первый раз, затем он выдает мне ошибку подключения. @нитрин0