Microservice can not reach to Config Server on Docker Compose

只谈情不闲聊 提交于 2019-12-08 16:17:38
himarg

I am too facing the same issue when trying to fetch configurations from Spring Cloud Config Server running in a Docker container. My Spring Boot app tries to access it on http://localhost:8888 even though I have specified the docker container name in the app's application.proprties.

Below is my app's application.properties:

spring.application.name=stock-app
spring.profiles.active=default
spring.cloud.config.uri=http://configserver:8888
server.port=8301

As a workaround, I am passing config server URL as a parameter in my docker compose.

This is my docker-compose.yml:

version: '2'
services:
  configserver:
    image: himarg/config-server:0.0.1-SNAPSHOT
    ports:
       - "8888:8888"
    environment:
      ENCRYPT_KEY:       "IMSYMMETRIC"

  database:
    image: himarg/stock-db:latest
    ports:
      - "3307:3306"
    environment:
      MYSQL_ROOT_PASSWORD: "Welcome1"
      MYSQL_DATABASE: "inventory"

  stockapp:
    image: himarg/stock-app:0.0.1-SNAPSHOT
    ports:
      - "8301:8301"
    environment:
      PROFILE: "default"
      CONFIGSERVER_URI: "http://configserver:8888"
      CONFIGSERVER_PORT:   "8888"
      DATABASESERVER_PORT: "3306"
      ENCRYPT_KEY:       "IMSYMMETRIC"

Then I use this parameter in my shell script that I use to start my app.

Below are the Dockerfile and the script:

Dockerfile

FROM openjdk:8-jdk-alpine
RUN apk update && apk upgrade && apk add netcat-openbsd
RUN mkdir -p /usr/local/stock-app
ADD @project.build.finalName@.jar /usr/local/stock-app/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh

run.sh

#!/bin/sh

echo "********************************************************"
echo "Waiting for the database server to start on port $DATABASESERVER_PORT"
echo "********************************************************"
while ! `nc -z database $DATABASESERVER_PORT`; do sleep 3; done
echo "******** Database Server has started "

echo "********************************************************"
echo "Waiting for the configuration server to start on port $CONFIGSERVER_PORT"
echo "********************************************************"
while ! `nc -z configserver $CONFIGSERVER_PORT`; do sleep 3; done
echo "*******  Configuration Server has started"

echo "********************************************************"
echo "Starting stock-app "
echo "********************************************************"
java -Dspring.cloud.config.uri=$CONFIGSERVER_URI -jar /usr/local/stock-app/@project.build.finalName@.jar

After making this change, I am able to successfully run my app, though I am not sure if this kind of workaround suits your requirement.

Change this

eureka:
  client:
    fetchRegistry: true
    registerWithEureka: true
    serviceUrl:
      defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/

To this

eureka:
  client:
    fetchRegistry: true
    registerWithEureka: true
    serviceUrl:
      defaultZone: http://${EUREKA_HOST:localhost}:${EUREKA_PORT:8761}/eureka/

You also have to create a bridge network so that your apps can talk to each other. Think of dockerizing your apps as running each of the services on different computers. That means using "localhost" won't really help you.

Like this one below (source: https://developer.okta.com/blog/2017/10/11/developers-guide-to-docker-part-3)

services:
  app:
    image: sample:1.0
    container_name: sample_app
    build: .
    ports:
      - 80:3000
    environment:
      - MONGO_URI=mongodb://sampledb/sample
    depends_on:
      - db
    networks:
      - samplenet
  db:
    image: mongo:3.0.15
    container_name: sample_db
    volumes:
      - ./db:/data/db
    networks:
      samplenet:
        aliases:
          - "sampledb"
networks:
  samplenet:
    driver: bridge

Try moving eureka.client properties to bootstrap.yml instead of application.properties for config server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!