Spring Cloud Config in Docker container is not accessible - spring-boot

I have a relatively simple setup. A Spring Could Config server, and a Spring Cloud Eureka server. (I refer Eureka server as Discover everywhere in my config...) The config server uses a Git repository for finding the appropriate config.
Everything is in Docker containers. I use docker-compose to build my services. The Gateway server should wait for config server to come up. I'll provide the source of the file further down.
So, when I build and start the containers, the discovery (Eureka) server doesn't want to come up. It exits with an exception. It is really long, so I just add the most important part here:
019-01-27 23:52:17,494 [INFO ] o.s.c.c.c.ConfigServicePropertySourceLocator.getRemoteEnvironment [main] – Fetching config from server at : http://tao-elszamolas-config:9001
2019-01-27 23:52:17,898 [INFO ] o.s.c.c.c.ConfigServicePropertySourceLocator.getRemoteEnvironment [main] – Connect Timeout Exception on Url - http://tao-elszamolas-config:9001. Will be trying the next url if available
2019-01-27 23:52:17,902 [ERROR ] o.s.b.SpringApplication.reportFailure [main] – Application run failed
java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing
... Something...Something
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://tao-elszamolas-config:9001/discovery-server/prod": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:743) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.getRemoteEnvironment(ConfigServicePropertySourceLocator.java:218) ~[spring-cloud-config-client-2.1.0.RC3.jar!/:2.1.0.RC3]
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:96) ~[spring-cloud-config-client-2.1.0.RC3.jar!/:2.1.0.RC3]
... 15 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:?]
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[?:?]
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242) ~[?:?]
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224) ~[?:?]
at java.net.Socket.connect(Socket.java:591) ~[?:?]
at java.net.Socket.connect(Socket.java:540) ~[?:?]
at sun.net.NetworkClient.doConnect(NetworkClient.java:182) ~[?:?]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:474) ~[?:?]
at sun.net.www.http.HttpClient.openServer(HttpClient.java:569) ~[?:?]
at sun.net.www.http.HttpClient.<init>(HttpClient.java:242) ~[?:?]
at sun.net.www.http.HttpClient.New(HttpClient.java:341) ~[?:?]
at sun.net.www.http.HttpClient.New(HttpClient.java:362) ~[?:?]
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1242) ~[?:?]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1181) ~[?:?]
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1075) ~[?:?]
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1009) ~[?:?]
at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:734) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:669) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:578) ~[spring-web-5.1.3.RELEASE.jar!/:5.1.3.RELEASE]
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.getRemoteEnvironment(ConfigServicePropertySourceLocator.java:218) ~[spring-cloud-config-client-2.1.0.RC3.jar!/:2.1.0.RC3]
at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:96) ~[spring-cloud-config-client-2.1.0.RC3.jar!/:2.1.0.RC3]
... 15 more
The only exception what I found in the Config server log is the one complaining about the connection to Discovery (Eureka) server. Yes, I try to register my config service inEureka for looking it up at startup of further microservices.
The connection is refused which means, the config server is not running at the time when Eureka server is trying to get its config or it is not listening on the given port, 9001.
How is it possible? I used the depends_on in my compose file, so it should wait for that.
Or why it finishes working? If I do the same thing on local without docker then it keeps trying to connect to Config, until the config server starts up. I'd like achieve the same behaviour using Docker.
Any advice would be appreciated!
And finally here are my config files:
docker-compose.yml:
version: "3"
services:
tao-elszamolas-config:
build: ./tao-elszamolas-config
container_name: tao-elszamolas-config
ports:
- "9001:9001"
volumes:
- "/tao-elszamolas/logs:/var/log/tao-elszamolas"
networks:
- taonetwork
tao-elszamolas-discovery:
build: ./tao-elszamolas-discovery
container_name: tao-elszamolas-discovery
depends_on:
- tao-elszamolas-config
ports:
- "9002:9002"
volumes:
- "/tao-elszamolas/logs:/var/log/tao-elszamolas"
networks:
- taonetwork
networks:
taonetwork:
driver: bridge
Dockerfile of Config Server:
FROM openjdk:11-jdk-slim
MAINTAINER BeszterceKK
COPY ./tao-elszamolas-config.jar /usr/src/taoelszamolas/tao-elszamolas-config.jar
WORKDIR /var/log/tao-elszamolas
WORKDIR /usr/src/taoelszamolas
ENV SPRING_PROFILES_ACTIVE prod
EXPOSE 9001
ENTRYPOINT ["java", "-DlogFileLocation=/var/log/tao-elszamolas", "-jar", "tao-elszamolas-config.jar"]
application.yml for Config server:
server:
port: 9001
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/something/something
username: something
password: something
skip-ssl-validation: true
security:
user:
name: something
password: something
eureka:
client:
serviceUrl:
defaultZone: http://something:something#tao-elszamolas-discovery:9002/eureka/
info:
artifact: tao-elszamolas-config
name: TAO Elszamolas Config Application
description: Tao elszamolas microservice elosztott szolgaltatas konfiguracio
version: 1.0.0
Dockerfile of Discovery (Eureka) Server:
FROM openjdk:11-jdk-slim
MAINTAINER BeszterceKK
COPY ./tao-elszamolas-discovery.jar /usr/src/taoelszamolas/tao-elszamolas-discovery.jar
WORKDIR /var/log/tao-elszamolas
WORKDIR /usr/src/taoelszamolas
ENV SPRING_PROFILES_ACTIVE prod
EXPOSE 9002
ENTRYPOINT ["java", "-DlogFileLocation=/var/log/tao-elszamolas", "-jar", "tao-elszamolas-discovery.jar"]
bootstrap.yml for Discovery (Eureka) server:
spring:
application:
name: discovery-server
cloud:
config:
fail-fast: true
name: discovery-server
uri: http://tao-elszamolas-config:9001
username: something
password: something
application.yml for Discovery (Eureka) server:
server:
port: 9002
eureka:
instance:
hostname: tao-elszamolas-discovery
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://tao-elszamolas-discovery:9002/eureka
info:
artifact: tao-elszamolas-discovery
name: TAO Elszamolas Eureka Szolgaltatas monitor
description: Tao elszamolas automatikus szolgaltatas regisztracio
version: 1.0.0
And finally, this is how I start my containers. It is simply:
docker-compose up -d
The whole application stack works on local without Docker. So I guess the problem is with my Docker configuration.
One more thing to mention, generally, I don't want to publish the ports to host, only expose them on my custom network between the containers. This is why I tried to replace the PORTS with EXPOSE, but it didn't really help.

for startup Compose does not wait until a container is “ready” (whatever that means for your particular application) - only until it’s running
So, you should refer : https://docs.docker.com/compose/startup-order/
for wait until for config server container is ready first (already listened on port 9001), then starting Eureka server container.

You registry service try to start before configuration service is up. You have to force him to wait.
You can do it with condition: service_started, like below. But I think it is not working with version 3
version: '2.1'
# ...
tao-elszamolas-discovery:
build: ./tao-elszamolas-discovery
container_name: tao-elszamolas-discovery
depends_on:
- tao-elszamolas-config:
condition: service_started
ports:
- "9002:9002"
volumes:
- "/tao-elszamolas/logs:/var/log/tao-elszamolas"
networks:
- taonetwork
A solution for version 3 is to use healthcheck and restart: on-failure
Take a look at this post
Another solution is to use entrypoint section to launch a batch file at start which is pinging configuration service container while it is not responding.

Related

Spring boot java Docker connecting mongoDB in K8s

I am struggling to connect mongodb using spring boot docket application, Below are the further details.
a) Created a mongoDB cluster in minikube.
b) Exposed an external service which connects to the mongodb. The service exposed was through Nodeport. Below is the code snippet.
mongo-service
---------------
`apiVersion: v1
kind: Service
metadata:
name: mongodb-external-service
spec:
selector:
app: mongodb
type: NodePort
ports:
- port: 27017
protocol: TCP
targetPort: 27017
nodePort: 32000
c) Minikube exposed the service with IP 192.168.49.2
d) I was able to connect mongodb using 192.168.49.2:2000 from my localhost using mongodb compass.
e) Created simple spring boot application which was able to connect the mongodb service using Ip and port.
So far no problems things were moving as expected. Next my objective was to containerize my spring boot application and execute in in the Minikube. Before i execute it as pod in minikube i wanted to test the container using the docker-compose. Below is my docker-compose and Dockerfile code snippets
Dockerfile
FROM adoptopenjdk:11-jre-hotspot as builder
ARG JAR_FILE=target/\*.jar
COPY ${JAR_FILE} tradeprocessor.jar
RUN java -Djarmode=layertools -jar tradeprocessor.jar extract
FROM adoptopenjdk:11-jre-hotspot
COPY --from=builder dependencies/ ./
COPY --from=builder snapshot-dependencies/ ./
COPY --from=builder spring-boot-loader/ ./
COPY --from=builder application/ ./
ENTRYPOINT \["java", "org.springframework.boot.loader.JarLauncher"\]
docker-compose
---------------
`version: '3.7'
services:
trdprocess:
build:
context: .
dockerfile: Dockerfile
image: tradeprocess:latest
ports:
- 8088:8088
extra_hosts:
- dockerhost:192.168.49.2
environment:
- spring_data_mongodb_authentication_database=admin
- spring_data_mongodb_username=mongouser
- spring_data_mongodb_password=mongopassword
- spring_data_mongodb_database=transactions
- spring_data_mongodb_port=32000
- spring_data_mongodb_host=192.168.49.2`
however Upon executing the above step, application is spinning up but it is throwing SocketTimeoutException.
Exception:
`Started TradeprocessingApplication in 2.09 seconds (JVM running for 2.973)`
2023-01-04 20:16:13.529 INFO 1 --- [.168.49.2:32000] o.m.d.cluster : Exception in monitor thread while connecting to server 192.168.49.2:32000
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.6.1.jar:?]
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:180) ~[mongodb-driver-core-4.6.1.jar:?]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:193) ~[mongodb-driver-core-4.6.1.jar:?]
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:157) ~[mongodb-driver-core-4.6.1.jar:?]
at java.lang.Thread.run(Unknown Source) ~[?:?]
Caused by: java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:?]
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[?:?]
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[?:?]
at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[?:?]
at java.net.SocksSocketImpl.connect(Unknown Source) ~[?:?]
at java.net.Socket.connect(Unknown Source) ~[?:?]
at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:107) ~[mongodb-driver-core-4.6.1.jar:?]
It looks like it has to do with network setting but not sure how to correct it. Any help is highly appreciated
Tried to change few option with docker-compose such as extra_hosts port mappings etc but no luck
I finally was able to solve my own problem. A bit of learning curve. Hear are the details.
My entire setup is running on Ubuntu workstation with minikube cluster.
Minikube started with command minikube start --driver=docker. Which means minikube itself is running as container within docker runtime environment.
docker network ls
docker network ls clearly shows that both my custom build application and the minikube are running as containers. Therefore i had to link these containers network. A small change in the docker-compose did the trick. Below is the code:
networks:
default :
external :
name : minikube

Spring Cloud Gateway instance unable to connect to eureka server

I'm following this "Microservices with Spring Boot and Spring Cloud" book by Magnus Larsson, at chapter 10 Spring Cloud Gateway is introduced as an edge server into the microservices landscape, once all the microservices are started SCGateway is unable to connect with the Eureka server:
RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://eureka:8761/eureka/}, exception=I/O error on POST request for "http://eureka:8761/eureka/apps/GATEWAY": Connect to eureka:8761 [eureka/172.23.0.4] failed: Connection refused; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to eureka:8761 [eureka/172.23.0.4] failed: Connection refused stacktrace=org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://eureka:8761/eureka/apps/GATEWAY": Connect to eureka:8761 [eureka/172.23.0.4] failed: Connection refused; nested exception is org.apache.http.conn.HttpHostConnectException: Connect to eureka:8761 [eureka/172.23.0.4] failed: Connection refused...
this is the docker-compose.yml file:
version: '2.1'
services:
product:
...instance configuration
recommendation:
...instance configuration
review:
...instance configuration
product-composite:
...instance configuration
mongodb:
...instance configuration
mysql:
...instance configuration
rabbitmq:
...instance configuration
eureka:
build: spring-cloud/eureka-server
mem_limit: 512m
gateway:
environment:
- SPRING_PROFILES_ACTIVE=docker
build: spring-cloud/gateway
mem_limit: 512m
ports:
- "8085:8085"
Eureka Dockerfile:
FROM adoptopenjdk:16_36-jre-hotspot as builder
WORKDIR extracted
ADD ./build/libs/*.jar app.jar
RUN java -Djarmode=layertools -jar app.jar extract
FROM adoptopenjdk:16_36-jre-hotspot
WORKDIR application
COPY --from=builder extracted/dependencies/ ./
COPY --from=builder extracted/spring-boot-loader/ ./
COPY --from=builder extracted/snapshot-dependencies/ ./
COPY --from=builder extracted/application/ ./
EXPOSE 8761
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
Eureka application.yml file:
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# from: https://github.com/spring-cloud-samples/eureka/blob/master/src/main/resources/application.yml
server:
waitTimeInMsWhenSyncEmpty: 0
response-cache-update-interval-ms: 5000
management.endpoints.web.exposure.include: "*"
Spring Cloud Gateway Dockerfile:
FROM adoptopenjdk:16_36-jre-hotspot as builder
WORKDIR extracted
ADD ./build/libs/*.jar app.jar
RUN java -Djarmode=layertools -jar app.jar extract
FROM adoptopenjdk:16_36-jre-hotspot
WORKDIR application
COPY --from=builder extracted/dependencies/ ./
COPY --from=builder extracted/spring-boot-loader/ ./
COPY --from=builder extracted/snapshot-dependencies/ ./
COPY --from=builder extracted/application/ ./
EXPOSE 8085
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]
Spring Cloud Gateway application.yml file:
server.port: 8085
spring.application.name: gateway
app.eureka-server: localhost
eureka:
client:
serviceUrl:
defaultZone: http://${app.eureka-server}:8761/eureka/
initialInstanceInfoReplicationIntervalSeconds: 5
registryFetchIntervalSeconds: 5
instance:
leaseRenewalIntervalInSeconds: 5
leaseExpirationDurationInSeconds: 5
spring.cloud.gateway.routes:
- id: product-composite
uri: lb://product-composite
predicates:
- Path=/product-composite/**
- id: product-composite-swagger-ui
uri: lb://product-composite
predicates:
- Path=/openapi/**
- id: eureka-api
uri: http://${app.eureka-server}:8761
predicates:
- Path=/eureka/api/{segment}
filters:
- SetPath=/eureka/{segment}
- id: eureka-web-start
uri: http://${app.eureka-server}:8761
predicates:
- Path=/eureka/web
filters:
- SetPath=/
- id: eureka-web-other
uri: http://${app.eureka-server}:8761
predicates:
- Path=/eureka/**
management.endpoint.health.show-details: "ALWAYS"
management.endpoints.web.exposure.include: "*"
logging:
level:
root: INFO
org.springframework.cloud.gateway.route.RouteDefinitionRouteLocator: INFO
org.springframework.cloud.gateway: TRACE
---
spring.config.activate.on-profile: docker
app.eureka-server: eureka
Originally the project uses Spring Boot version 2.5.2 and Spring Cloud version 2020.0.3, also tried with recent versions (with no success) such as:
Spring Boot version: 2.6.6 and 2.7.5
Spring Cloud: 2021.0.4
The Spring Cloud Gateway is always throwing the same exception:
org.apache.http.conn.HttpHostConnectException: Connect to eureka:8761
[eureka/172.23.0.4] failed: Connection refused
Trying to access the Eureka dashboard or http://localhost:8085/actuator/health endpoint hangs forever.
I'll appreciate any hints or help, thanks in advance.

Microservices can't fetch Spring Config Server data using docker in the same network?

I have microservices application, and i'm using Spring cloud and Spring boot v2.3 . i'm depending on config server and eureka, everything is working fine on my local machine from IDE, but when I deployed the stack on docker, my app Auth-Service cannot fetch from spring config server container, and give me the following stack trace
2020-07-05 02:23:52.888 INFO [auth-service,,,] 1 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-07-05 02:23:53.091 INFO [auth-service,,,] 1 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2020-07-05 02:23:53.092 WARN [auth-service,,,] 1 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/auth-service/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
I thought the issue is just waiting until or container-network as mentioned here Spring config is no accessible and this Microservices can't reach config service, but I ensured the containers are within same network, and to simulate, I started the config first, but got same error.
Here are the configurations i'm using
version: '3.8'
services:
eureka-service:
build:
context: ./eureka
image: eureka-service:latest
container_name: eureka
ports:
- 8761:8761
hostname: eureka
networks:
- mynetwork
config-service:
build:
context: ./configServer
image: config-server:latest
ports:
- 8888:8888
networks:
- mynetwork
proxy-service:
build:
context: ./web-cv-proxy
image: zuul-service:latest
ports:
- 8081:8081
depends_on:
- config-service
networks:
- mynetwork
auth-service:
build:
context: ./web-based-cv/auth-service
image: auth-service:latest
ports:
- 8060:8060
depends_on:
- config-service
- eureka-service
restart: on-failure
networks:
- mynetwork
portal-service:
build:
context: ./web-based-cv/cv-portal
image: cv-portal:latest
ports:
- 9090:9090
depends_on:
- config-service
- eureka-service
restart: on-failure
networks:
- mynetwork
networks:
mynetwork:
driver: bridge
Config server docker file
FROM java:openjdk-8-alpine
LABEL version="1.0"
LABEL description="configuration server"
COPY ./target/configServer-0.0.1-SNAPSHOT.jar ./
EXPOSE 8888
CMD ["java", "-jar", "configServer-0.0.1-SNAPSHOT.jar"]
Application docker file
FROM java:openjdk-8-alpine
LABEL version="1.0"
LABEL description="cv authintication service"
COPY ./target/auth-service-1.0.0-exec.jar auth-service-1.0.0-exec.jar
EXPOSE 8060
CMD ["java", "-jar", "/auth-service-1.0.0-exec.jar"]
The application bootstrap file
spring:
datasource:
jpa:
properties:
hibernate:
format_sql: true
ddl-auto: none
application:
name: auth-service
bus:
refresh:
enabled: true
profiles:
active: jdbc
cloud:
retry:
initial-interval: 1500
multiplier: 1.5
max-attempts: 10000
max-interval: 1000
server:
servlet:
context-path: /api/auth
port: 8060
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
register-with-eureka: true
fetch-registry: true
instance:
prefer-ip-address: true
management:
endpoints:
web:
exposure:
include: ["health","info","refresh", "bus-refresh"]
Your advice is highly appreciated , and thanks in advance.
Remember one thing, when you deploy your micro-services on docker,
localhost won't work there as your micoservices run in a container
within a different virtual network. So it won't be identified using
localhost.
So in your docker compose file, you have to provide the name of your
the service of your Eureka server.
For example -
services:
discovery:
image: <Eureka server image name>
ports:
- 8761:8761
ConfigServerService:
image: <Config server image name>
environment:
- JAVA_OPTS=
-DEUREKA_SERVER=http://discovery:8761/eureka
depends_on:
- discovery
links:
- discovery
ports:
- 8888:8888
Microservice1:
image: <Microservice1 image name>
environment:
- JAVA_OPTS=
-DEUREKA_SERVER=http://discovery:8761/eureka
depends_on:
- discovery
- ConfigServerService
links:
- discovery
- ConfigServerService
ports:
- 8000:8000
You need to change auth-service configuration from localhost:8888 to http://config-service:8888, as the error seems not getting the correct endpoint.
docker-compose networking
error on GET request for "http://localhost:8888/auth-service/default": Connection refused (Connection refused);
localhost:8888 means this container (auth-service) not the spring config server.
For some reason my containers didn't be able to connect using direct uri configuration, although I added them to the same network.
I solved the issue with depending on Eureka itself, and connecting to Spring config through eureka discovery with the following config in client apps
spring
config:
discovery:
enabled: true
service-id: configServer
I think it is a good way to give the control to eureka, but I hoped i would know why it didn't work with uri instead? by the way this way will give me more flexibility if I deployed on several clusters.

Could not connect to eureka server from docker

I'm trying connection to eureka server from docker container.
The eureka server is running via "java -jar eureka-server.jar" command.
The eureka client is running on docker container.
Eureka server configuration is:
eureka:
instance:
hostname: eurekaserver
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://eurekaserver:8080/eureka/
Eureka client configuration is:
spring:
application:
name: client-application
server:
port: 8081
eureka:
client:
serviceUrl:
defaultZone: http://eurekaserver:8080/eureka/
I also tried to setup directly ip-address for example
eureka:
client:
serviceUrl:
defaultZone: http://1.2.3.4:8080/eureka/
But, unfortunately, I'm facing with stack trace:
[2019-05-26 09:44:14.714] slf4j - 1 INFO [main] --- o.a.h.i.c.DefaultHttpClient: Retrying connect to {}-> http://eurekaserver:8080
[2019-05-26 09:44:15.717] slf4j - 1 ERROR [main] --- c.n.d.s.t.d.RedirectingEurekaHttpClient: Request execution error
com.sun.jersey.api.client.ClientHandlerException: java.net.NoRouteToHostException: No route to host (Host unreachable)
at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187)
at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123)
at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27)
at com.sun.jersey.api.client.Client.handle(Client.java:652)
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74)
at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:509)
...
Caused by: java.net.NoRouteToHostException: No route to host (Host unreachable)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:120)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:179)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:144)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:134)
at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:612)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:447)
at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:884)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:117)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:173)
... 72 common frames omitted
And it is cause of
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
So my question is how to resolve connectivity?
Docker and eureka are placed on the same machine, because the command curl "http://1.2.3.4:8080/eureka/apps" returns app to me
How can I do some investigating, why a docker cannot resolve the host?
p.s. I've known about issue, when eureka-client looks at localhost hostname, but looks like my issue another.
Thank you.
Resolved.
The cause of the issue was, don't use
registerWithEureka: true
fetchRegistry: true
More about, find at first links after typing properties name in google
But if you use them, then you have to set serviceUrl as hostname and also add
instance.preferUseIpAddress: true
p.s. Not sure that is fully can solve this problem, but that, I have changed at application config.

Eureka, ZUUL, Docker Compose on bridge network java.net.ConnectException: Connection refused

I'm running a Zuul service and Eureka service on separate docker containers with minimalistic configurations (both are spring boot projects).
Eureka:
docker-compose.xml:
version: '2'
services:
app:
build:
context: ./
image: eureka
environment:
eureka.client.serviceUrl.defaultZone: http://eurekaserver_app_1:8070/eureka/
tty: false
network_mode: bridge
ports:
- 8070:8070
DockerFile:
FROM java:8
COPY ./build/libs/eureka-server-0.0.1-SNAPSHOT.jar /usr/src/eureka/
WORKDIR /usr/src/eureka
EXPOSE 8070
CMD ["java","-jar","eureka-server-0.0.1-SNAPSHOT.jar"]
Zuul:
docker-compose.xml:
version: '2'
services:
app:
build:
context: ./
image: zuul-service
environment:
eureka.client.serviceUrl.defaultZone: http://eurekaserver_app_1:8070/eureka/
tty: false
ports:
- 8069:8069
network_mode: bridge
DockerFile:
FROM java:8
COPY ./build/libs/zuul-service-0.0.1-SNAPSHOT.jar /usr/src/item/
WORKDIR /usr/src/item
EXPOSE 8069
ENTRYPOINT ["java","-jar","zuul-service-0.0.1-SNAPSHOT.jar"]
I used the network ls --no-trunc Docker command to make sure they are not spinning up a new network:
NETWORK ID NAME DRIVER SCOPE
ba216f3e01bb168848074a99875666fe382a4eda15daad0c428a8102707ee49f bridge bridge local
a63a8adf0fd162d9b99bdf77bc4a13c0bfcfb8a9aca3c5375e60d5df5c5e305d host host local
2debbe96f8a96fdd8d6da983877609e0bdb7a1df6b25537f35f1608de0739fc7 none null local
Which seems alright, The problem is that Eureka container starts up properly, however zuul-service isn't when I used docker-compose up. It throws:
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)
app_1 | at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar!/:1.19.1]
app_1 | at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123) ~[jersey-client-1.19.1.jar!/:1.19.1]
app_1 | at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.8.6.jar!/:1.8.6]
app_1 | at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar!/:1.19.1]
app_1 | at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar!/:1.19.1]
app_1 | at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar!/:1.19.1]
app_1 | at com.sun.jersey.api.client.WebResource$Builder.get(WebResource.java:509) ~[jersey-client-1.19.1.jar!/:1.19.1]
app_1 | at com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.getApplicationsInternal(AbstractJerseyEurekaHttpClient.java:194) ~[eureka-client-1.8.6.jar!/:1.8.6]
I am quite unsure on what I could be missing here?
The official Docker Compose docamantation says:
By default Compose sets up a single network for your app. Each
container for a service joins the default network and is both
reachable by other containers on that network, and discoverable by
them at a hostname identical to the container name.
I suggest you to create one docker-compose.yaml file for both applications, properly name them and separate Dockerfiles to different folders:
version: '2'
services:
eureka:
build:
context: ./eureka/
image: eureka
environment:
eureka.client.serviceUrl.defaultZone: http://eureka:8070/eureka/
tty: false
ports:
- 8070:8070
zuul:
build:
context: ./zuul/
image: zuul-service
environment:
eureka.client.serviceUrl.defaultZone: http://eureka:8070/eureka/
tty: false
ports:
- 8069:8069
When containers are up, they will be able to communicate between each other by eureka:8070 and zuul:8069 accordingly.

Resources