Springブートフレームワークに基づくREST APIを使用して、スタンドアロンの単純なWebアプリケーションを実装しました。私のアプリはRedisを使ってデータをキャッシュしています。今、私はwebappとRedisキャッシュをドッキングして、作業環境でRedisインストールを強制することなく、簡単な実行手順を提供したいと思います。Spring BootとRedisコンテナ間の通信
私は多くのアプローチを試みましたが、私はまだRedisConnectionFailureException: Cannot get Jedis connection
と根本原因ConnectException: Connection refused
で苦労しています。
の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>
ドッカー-compose.yml:
version: '3'
services:
web:
build: .
ports:
- "8080:8080"
links:
- redis
redis:
image: redis
ports:
- "6379:6379"
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
私は、テンプレートと簡単な使用それ(エキストラRedisの構成)からValueOperations
を取得し、コンストラクタを介してStringRedisTemplate
をautowire。私はコマンドsudo docker-compose up
でアプリを実行し、すべてのSprin Bootアプリコールで上記の例外を取得します。 Redisキャッシュはホストマシンから到達可能です。 Dockerなしで、Redisサービスがインストールされている場合、すべて正常に動作しています。
更新:前に貼り付けたapplication.propertiesと、アプリケーションのロジックに関連するカスタム設定(フレームワーク設定ではありません)を保存するapplication.ymlを使用しています。私はSpringブートが両方のファイルをロードしてマージするので、問題ではないはずだと読んでいます。 RedisのホストがJedis接続で正しく設定されていないと思われますが、確認する方法はわかりません。
ありがとうございますが、私は全く同じ例外があります。 – Geeky
@Geeky更新ドッカー作成ファイル。セキュリティモードはデフォルトで有効になっているようです。保護されたモードのセクションを確認してください。https://redis.io/topics/security – yamenk
残念ながら、問題は解決されず、同じ例外がスローされます。 – Geeky