2017-09-09 16 views
0

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接続で正しく設定されていないと思われますが、確認する方法はわかりません。

答えて

0

は最後に、私はこの問題を認識しました。私はsudo docker-compose upコマンドを使って問題のアプリケーションを起動しました。このコマンドは以前に開始されたコンテナを再実行します(毎回イメージを再構築しません)。したがって、私が提供した変更はすべて考慮されませんでした。プロジェクトコードが再コンパイルされると思いました。

上記の設定が問題なく機能します。ちょうどsudo docker-compose down && mvn clean installを実行し、次にsudo docker-compose upを実行してください。

0

ドッカネットワークを作成し、このネットワークにコンテナを添付してください:

docker network create 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 
+0

ありがとうございますが、私は全く同じ例外があります。 – Geeky

+0

@Geeky更新ドッカー作成ファイル。セキュリティモードはデフォルトで有効になっているようです。保護されたモードのセクションを確認してください。https://redis.io/topics/security – yamenk

+0

残念ながら、問題は解決されず、同じ例外がスローされます。 – Geeky

関連する問題