Docker Spring Boot和Redis容器之间的通信

Docker Spring Boot和Redis容器之间的通信,docker,spring-boot,redis,Docker,Spring Boot,Redis,我使用基于SpringBoot框架的RESTAPI实现了简单、独立的webapp。我的应用程序正在使用Redis缓存一些数据。现在,我想将webapp和Redis缓存对接,以提供简单的运行过程,而不必在工作环境中强制安装Redis 我尝试了很多方法,但我仍然在与RedisConnectionFailureException:cannotgetjedisconnection和根本原因ConnectException:connectionseeded作斗争 pom.xml: <parent&g

我使用基于SpringBoot框架的RESTAPI实现了简单、独立的webapp。我的应用程序正在使用Redis缓存一些数据。现在,我想将webapp和Redis缓存对接,以提供简单的运行过程,而不必在工作环境中强制安装Redis

我尝试了很多方法,但我仍然在与
RedisConnectionFailureException:cannotgetjedisconnection
和根本原因
ConnectException:connectionseeded
作斗争

pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
        <plugin>
            <groupId>com.spotify</groupId>
            <artifactId>dockerfile-maven-plugin</artifactId>
            <version>1.3.5</version>
            <configuration>
                <repository>${project.artifactId}</repository>
            </configuration>
        </plugin>
    </plugins>
</build>
Dockerfile:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/xxx.jar abc.jar
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS - Djava.security.egd=file:/dev/./urandom -jar /abc.jar" ]
application.properties:

spring.redis.host=redis
spring.redis.port=6379
我通过构造函数autowire
StringRedisTemplate
,从模板中获取
ValueOperations
,并简单使用它(无需额外的Redis配置)。我使用命令
sudocker compose up
运行应用程序,每次调用Redis的Sprin引导应用程序都会出现上面列出的异常。可以从主机访问Redis缓存。没有Docker,安装Redis服务后,一切正常


更新:我正在使用我之前粘贴的application.properties和application.yml,其中我存储了一些与我的应用程序逻辑相关的自定义配置(不是框架配置)。我读到这应该不是问题,因为SpringBoot加载这两个文件并合并它们。我怀疑绝地连接中的Redis主机设置不正确,但我不知道如何检查它。

尝试创建docker网络并将容器连接到此网络:

docker网络创建spring

version: '3'
  services:
    web:
      build: .
      ports:
       - "8080:8080"
      networks:
       - spring
    redis:
      image: redis
      command: [ "redis-server", "--protected-mode", "no" ]
      ports:
        - "6379:6379"
      networks:
        - spring
  networks:
    spring:
      external: true

我终于意识到了这个问题。我使用了
sudocker-compose-up
命令来启动上面提到的应用程序。此命令重新运行以前启动的容器(并非每次都重新生成映像),因此未考虑我提供的所有更改。我以为项目代码会被重新编译


以上配置对我来说很好。只需运行
sudo docker compose down&&mvn clean install
,然后运行
sudo docker compose up

谢谢您的回答,但我得到了完全相同的异常。@Geeky更新了docker compose文件。显然有一个默认启用的安全模式。如果无法解决问题,则会引发相同的异常。请注意,
网络
结尾的缩进错误(尝试编辑但无法编辑)。它应该在根级别。请注意,将docker/docker compose与sudo一起使用是一种不好的做法,您应该将其配置为与普通用户一起使用
version: '3'
  services:
    web:
      build: .
      ports:
       - "8080:8080"
      networks:
       - spring
    redis:
      image: redis
      command: [ "redis-server", "--protected-mode", "no" ]
      ports:
        - "6379:6379"
      networks:
        - spring
  networks:
    spring:
      external: true