簡體   English   中英

Redis 拒絕通過 Docker 連接到 Spring 引導

[英]Redis refuses connection to Spring Boot via Docker Compose

我開發了一個 spring 引導應用程序來通過 redis 存儲登錄信息。 我有以下 docker-compose.yml:

version: "3.9"
services:
  web:
    build: .
    ports:
      - "8082:8082"
    links:
      - redis
  redis:
    image: redis
    container_name: redis
    hostname: redis-db
    ports:
      - "6379:6379"
    command: redis-server --port 6379 --bind 0.0.0.0 --protected-mode no

Dockerfile:

FROM openjdk:8-jre-alpine
VOLUME /tmp
COPY app.jar app.jar
EXPOSE 8082

ENTRYPOINT ["java", "-jar",  "app.jar"]

在 Spring 引導應用程序中,我只使用以下 application.properties 來設置 Redis 連接:

server.port = 8082

spring.redis.host=redis-db
spring.redis.port=6379

我通過 CrudRepository 和 QueryByExampleExecutor 訪問存儲庫。 每次我嘗試訪問數據時,都會收到以下錯誤:

sgartner-web-1  | 2021-11-25 01:22:53.600 ERROR 1 --- [nio-8082-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] 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
sgartner-web-1  | 
sgartner-web-1  | java.net.ConnectException: Connection refused
sgartner-web-1  |       at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method) ~[na:1.8.0_212]
sgartner-web-1  |       at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717) ~[na:1.8.0_212]
sgartner-web-1  |       at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:330) ~[netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
sgartner-web-1  |       at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334) ~[netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
sgartner-web-1  |       at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:707) ~[netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
sgartner-web-1  |       at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:655) ~[netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
sgartner-web-1  |       at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:581) ~[netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
sgartner-web-1  |       at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) ~[netty-transport-4.1.69.Final.jar!/:4.1.69.Final]
sgartner-web-1  |       at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:986) ~[netty-common-4.1.69.Final.jar!/:4.1.69.Final]
sgartner-web-1  |       at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.69.Final.jar!/:4.1.69.Final]
sgartner-web-1  |       at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.69.Final.jar!/:4.1.69.Final]
sgartner-web-1  |       at java.lang.Thread.run(Thread.java:748) [na:1.8.0_212]

我已經測試了 redis 和應用程序的應用程序“正常”實例。 對我來說奇怪的是“無法連接到 localhost:6379]”,即使我沒有嘗試連接到 localhost。

在容器外運行應用程序時測試此行為表明,它可以識別更改的主機名並且不會嘗試連接到 localhost...

指定的 redis 命令似乎也不是問題:使用相同的命令在容器外部運行 redis 並嘗試通過 VM 連接到它來對此進行測試。

我真的很感激一個解決方案或另一個可能的原因來調查。 感謝回復這個的人!

根據日志,它清楚地表明 web 應用程序無法連接到 redis 端口 6379。您可以嘗試在 Z3254677A7917C6C01F5?5212F86C57FFFZ 中公開 6379 ,請先檢查您是否可以從本地機器上提取 6379。 好像是網絡問題。

看看這有幫助嗎。

Docker 無法從其他服務連接到 redis

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM