简体   繁体   English

将 Spring 引导和 redis 与 docker-compose 组合:连接被拒绝

[英]Combining Spring boot and redis with docker-compose: Connection refused

I am trying to do a basic docker container(s) with spring boot and MongoDB and redis. Mongo is just fine but connection can't be established between redis and spring boot although the configuration looks ok to me.我正在尝试使用 spring 引导和 MongoDB 和 redis 来做一个基本的 docker 容器。Mongo 很好,但是无法在 redis 和 spring 引导之间建立连接,虽然我觉得配置没问题

docker-compose.yml docker-compose.yml

version: "3.9"
services:
  web:
    build: .
    ports:
      - "1234:1234"
    depends_on:
      - redis
      - db
  db:
    image: mongo
    hostname: mongodb
    ports:
      - "27017:27017"
  redis:
    hostname: redis
    image: redis
    restart: always
    ports:
      - "6379:6379"

Dockerfile: Dockerfile:

FROM adoptopenjdk:11
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

Config.java:配置.java:

@Configuration
@EnableRedisRepositories
public class Config
{

    RedisStandaloneConfiguration standaloneConfiguration() {
        return new RedisStandaloneConfiguration("redis", 6379);
    }

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory(standaloneConfiguration());
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }
}

Problem: All the containers are running after docker compose up.问题:所有容器在 docker 组合后都在运行。 but the connection between spring and redis can't be established.但是spring和redis无法建立连接。

java.net.ConnectException: Connection refused (Connection refused)
slot-web-1    |     at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
slot-web-1    |     at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]
slot-web-1    |     at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[na:na]
slot-web-1    |     at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[na:na]
slot-web-1    |     at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:na]

First of all, if your container is on a remote server, you can use te.net on the server where the container is deployed to test whether it can connect to port 6379 of redis. If so, you can check whether the server has open port 6379, or a whitelist.首先,如果你的容器在远程服务器上,你可以在部署容器的服务器上使用te.net测试是否可以连接到redis的6379端口,如果可以,可以查看服务器是否开启端口 6379,或白名单。 Secondly, you can try to connect to the 6379 port of the container redis by te.net on the computer.其次,可以尝试在电脑上通过te.net连接容器redis的6379端口。 If yes, you can check if the ip, password, username and other information of your redis configuration are correct.如果是,你可以检查你的redis配置的ip、密码、用户名等信息是否正确。

This error may occur if your redis server is starting later than your spring boot.如果您的 redis 服务器启动晚于 spring 启动,则可能会发生此错误。 Make sure redis is started first by the following command.确保 redis 首先通过以下命令启动。

docker compose down && docker compose up -d redis && sleep 5 && docker compose up --build -d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM