I have a Eureka Server, Config Server and Spring-Boot Service. I have created a docker compose file like this:
version: '3'
services:
user-service:
container_name: user-service-container
image: user-service:latest
build:
context: ./user-service
dockerfile: DockerFile
hostname: user-service
ports:
- "8082:8082"
depends_on:
- postgresdb
- eureka-service
- config-server
environment:
management.context-path : /userservice
hostName : user-service
EUREKA_HOST: eureka-service
EUREKA_PORT: 8761
config-server:
container_name: config-server-container
image: config-server:latest
build:
context: ./config-server
dockerfile: DockerFile
hostname: config-server
ports:
- "8081:8081"
depends_on:
- eureka-service
environment:
management.context-path : /config
hostName : config-server
SPRING_PROFILES_ACTIVE : registration-first
EUREKA_HOST: eureka-service
EUREKA_PORT: 8761
eureka-service:
container_name: eureka-service-container
image: eureka-service:latest
build:
context: ./eureka-service
dockerfile: DockerFile
hostname: eureka-service
ports:
- "8761:8761"
postgresdb:
container_name: postgres-db-container
image: postgres:10.5
hostname: postgres
ports:
- "5432:5432"
With this configuration Config Server can register itself to Eureka Server, however configuration part of the User Service throws below exception:
2018-09-07T18:53:17.869040200Z 2018-09-07 18:53:17.868 INFO 6 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2018-09-07T18:53:18.331651100Z 2018-09-07 18:53:18.331 INFO 6 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2018-09-07T18:53:18.332055700Z 2018-09-07 18:53:18.331 WARN 6 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/user-service/registered-config-server,dev": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2018-09-07T18:53:18.334406300Z 2018-09-07 18:53:18.334 INFO 6 --- [ main] c.k.d.u.UserServiceApplication : The following profiles are active: registered-config-server,dev
2018-09-07T18:53:18.371334800Z 2018-09-07 18:53:18.370 INFO 6 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4d1c00d0: startup date [Fri Sep 07 18:53:18 UTC 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@13969fbe
From this exception, I understand that User Service connects to Euroka Server but it tries to connect to Config Server via a wrong URL (i.e. http://localhost:8888 )
Euroka Server App Properties:
spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
Config Server App Properties:
spring:
cloud:
config:
server:
git :
uri: https://bitbucket.org/blabla
username: blabla
password: ***
discovery:
enabled: true
service-id: config-server
server:
port: 8081
---
spring:
application:
name: config-server
profiles: registration-first
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
instance:
appname: config-server
hostname: ${hostName}
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
preferIpAddress: true
metadataMap:
instanceId: ${spring.application.name}:${server.port}
User Service Application Properties:
endpoints:
enabled: false
info:
enabled: true
health:
enabled: true
metrics:
enabled: true
refresh:
enabled: true
eureka:
client:
fetchRegistry: true
registerWithEureka: true
serviceUrl:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
instance:
hostname: ${hostName}
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
preferIpAddress: true
metadataMap:
instanceId: ${spring.application.name}:${server.port}
spring.jpa.database: POSTGRESQL
spring.datasource.platform: postgres
spring.jpa.show-sql: true
spring.jpa.hibernate.ddl-auto: update
spring.database.driverClassName: org.postgresql.Driver
spring.datasource.url: jdbc:postgresql://postgresdb:5432/postgres
spring.datasource.username: postgres
spring.datasource.password: postgres
User Service Bootstrap.yml:
spring:
profiles:
active: registered-config-server,dev
application:
name: user-service
server.port: 8082
---
spring:
profiles: known-config-server
cloud:
config:
uri: http://localhost:8080
---
spring:
profiles: registered-config-server
cloud:
config:
discovery:
enabled: true
serviceId: config-server
But when I run these applications on my local PC without docker they work properly.
Does anyone have any suggestions?
EDIT: UserService Docker File:
FROM java:8
EXPOSE 8082
ADD /target/user-service-0.0.1-SNAPSHOT.jar user-service.jar
CMD java -jar user-service.jar
config-server docker file
FROM java:8
EXPOSE 8081
ADD /target/config-server-0.0.1-SNAPSHOT.jar config-server.jar
CMD java -jar config-server.jar
eureka-server docker file
FROM java:8
EXPOSE 8761
ADD /target/eureka-service-0.0.1-SNAPSHOT.jar eureka-service.jar
CMD java -jar eureka-service.jar
localhost
means the host; with docker,localhost
means the container, so it's different for each container. – Constantin Galbenuspring.application.name
environment variable accordingly for each service (user and config) in each service environment. In this way the eureka client register itself correctly. I.e. for user service, this should beuser-service
, the hostname of this service in the docker network. – Constantin Galbenu