Spring Boot Microservice -> Docker Issue : API Gateway cannot connect to configserver (ResourceAccessException) - spring-boot

I have a problem about running all services on docker. The only thing which I cannot handle is related with connection problem between api gateway and configserver. Config server is based on git file based system and it fetchs all values from github repo.
When I try to run api gateway, I get this issue shown below.
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://configserver:9296/API-GATEWAY/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
Here is the related part of docker-compose.yml file
configserver:
image: 'microservicedailybuffer/configserver:0.0.1'
container_name: configserver
ports:
- '9296:9296'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
healthcheck:
test: [ "CMD", "curl", "-f", "http://configserver:9296/actuator/health" ]
interval: 10s
timeout: 5s
retries: 5
depends_on:
- serviceregistry
networks:
- backend
apigateway:
image: 'microservicedailybuffer/apigateway:0.0.1'
container_name: apigateway
ports:
- '9090:9090'
environment:
- EUREKA_SERVER_ADDRESS=http://serviceregistry:8761/eureka
- CONFIG_SERVER_URL=configserver
- ZIPKIN_URL=http://zipkin:9411
- REDIS_URL=redis://redis:6379
depends_on:
- configserver
- zipkin
- redis
- serviceregistry
networks:
- backend
How can I fix the issue?
Here is the application.yml file of api gateway : Link
Here is the git based file system file : Link
Here is the docker-compose.yml file : Link

Related

Register Dockerized microservice to Eureka server - Springboot

In my production environment I have an Eureka Server running inside a docker container.
I can register to it other basic microservices with this kind of Application.yml
Application.yml:
server:
port: '8095'
spring:
application:
name: sap-listener
eureka:
instance:
preferIpAddress: true
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://172.17.0.2:8761/eureka
I create a DockerImage with this Dockerfile:
Dockerfile
FROM openjdk:17-jdk
ARG JAR_FILE=target/*.jar
COPY target/sap-listener-*.jar /sap-listener.jar
ENTRYPOINT ["java", "-jar", "/sap-listener.jar" ]
EXPOSE 8095
and then I run it in production with this command:
docker run -d -p 8095:8095 --name sap myrepo/sap-listener1.0:latest
The service is successfully registered to the Eureka server.
I came across to some problems when I try to run a bigger microservice which have a docker-compose file.
I send directly this docker-compose file in production:
Docker-compose
version: "3.3"
services:
docker-mysql:
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'password'
MYSQL_DATABASE: 'db'
ports:
- "3007:3306"
phpmyadmin:
image: phpmyadmin
restart: always
container_name: php-my-admin-users
ports:
- "8081:80"
ldap-app:
image: myRepo/service1:latest
ports:
- "8090:8090"
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://docker-mysql:3306/db
depends_on:
- docker-mysql
And I run it with docker-compose -f docker-compose.yml up -d
service1 application.yml have the same type of connection with Eureka server of the previous microservice.
service1 is correctly deployed but It can't register himself to the Eureka server, if I log out the container output I have this error:
2022-06-29 15:45:20.551 INFO 1 --- [ main] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://172.17.0.2:8761/eureka/}, exception=I/O error on GET request for "http://172.17.0.2:8761/eureka/apps/": Connect to 172.17.0.2:8761 [/172.17.0.2] failed: Connection timed out; nested exception is org.apache.http.conn.ConnectTimeoutException: Connect to 172.17.0.2:8761 [/172.17.0.2] failed: Connection timed out stacktrace=org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://172.17.0.2:8761/eureka/apps/": Connect to 172.17.0.2:8761 [/172.17.0.2] failed: Connection timed out; nested exception is org.apache.http.conn.ConnectTimeoutException: Connect to 172.17.0.2:8761 [/172.17.0.2] failed: Connection timed out
I read that someone directly insert the Eureka Server data as a service inside the Docker-Compose.yml file, but my Eureka Server is already deployed and is already listening to a specific port.
That is probably happening because docker-compose automatically assign a network to the containers.
Try adding network_mode: host to your services in the compose file, like so:
version: "3.3"
services:
docker-mysql:
network_mode: host
image: mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: 'password'
MYSQL_DATABASE: 'db'
ports:
- "3007:3306"
phpmyadmin:
network_mode: host
image: phpmyadmin
restart: always
container_name: php-my-admin-users
ports:
- "8081:80"
ldap-app:
network_mode: host
image: myRepo/service1:latest
ports:
- "8090:8090"
environment:
SPRING_DATASOURCE_URL: jdbc:mysql://docker-mysql:3306/db
depends_on:
- docker-mysql
First I would suggest you get familiar with how networking in Docker works and then have a look at networking in Docker-Compose.
When you run docker network ls when your containers are deployed you will see that they are running on different networks, which isolates them. Inspect the networks using docker network inspect <id> and you'll see they have different subnets. So for the services to be able to communicate they need to be on the same network.
You can manually create a network and use it in both compose and the docker cli.

feign.RetryableException Connection refused executing ... Spring Boot + Feign + Docker Compose

I'm facing this issue when trying to use a microservice in docker (this doesn't happen in local).
An exception occurred in RetryableException, in the line -2:
Connection refused executing GET http://localhost:8082/api/car/findAll
this is my docker-compose file
version: '3.9'
services:
people:
build:
context: peoplems
ports:
- '8081:8081'
networks:
- host
vehicles:
build:
context: vehiclesms
ports:
- '8082:8082'
networks:
- host
api:
build:
context: apigateway
dockerfile: Dockefile
ports:
- '8080:8080'
networks:
- host
networks:
host:
driver: bridge
The services are running
The error was that I was trying to call "localhost" using feignclient
url = http://localhost:8081/api/car
So, when the app was 'deployed' in docker, docker wouldn't find the localhost address. I had to change "localhost" by the name of the container where the service was deployed: parking-people-1. My new url in feignclient is now
url = http://parking-people-1:8081/api/car
and it works.
NOTE: with this solution, you don't have to create a new network anymore. (at least in this case.)
I hope this could be useful for someone.

Dockerizing SpringBoot Mongo Exception opening socket

I am trying to dockerize my spring boot and mongo.
This is my application.yml
spring:
application:
name: service-app
data:
mongodb:
uri: mongodb://localhost:27017/user
I have found this to be recommended in other posts.
The error I get is:
Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~.
[mongodb-driver-core-4.1.1.jar:na]
version: '3'
services:
mongo:
image: library/mongo:latest
container_name: mongo
restart: always
ports:
- 27017:27017
network_mode: host
volumes:
- $HOME/mongo:/data/db
healthcheck:
test: "exit 0"
user-service:
build:
context: .
dockerfile: Dockerfile
image: user-service
depends_on:
- mongo
network_mode: "host"
hostname: localhost
restart: always
ports:
- 8082:8082
healthcheck:
test: "exit 0"
FROM openjdk:8-jdk-alpine
ADD ./target/demo-0.0.1-SNAPSHOT.jar /usr/src/user-service-0.0.1-SNAPSHOT.jar
WORKDIR usr/src
ENTRYPOINT ["java -Djdk.tls.client.protocols=TLSv1.2","-jar", "user-service-0.0.1-SNAPSHOT.jar"]
It seems to be a common problem for noobsters, I couldn't solve it with what was on the internet already.
The problem is in not building the url properly.
By the information I gave, I should have gotten a proper answer, instead I got -1.
Some people are true trolls with privileges.
uri: mongodb://mongo/user
I feel like it became a gamble on Stackoverflow, depends on the type of people crossing over your question.

Connection refused when connecting to keycloak container from backend container

I have two containers backend (spring boot application) and Keycloak. if I run keycloak in a container and backend locally : it works
If both of them are run in container the backend doesn't start and shows the following error :
Failed to instantiate [org.springframework.security.oauth2.jwt.JwtDecoder]: Factory method 'jwtDecoderByIssuerUri' threw exception; nested exception is java.lang.IllegalArgumentException: Unable to resolve the Configuration with the provided Issuer of "http://keycloak:8082/auth/realms/myrealm"
Caused by: org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://keycloak:8082/auth/realms/myrealm/.well-known/openid-configuration": Connection refused (Connection refused);
following are my configs :
docker-compose :
services:
keycloak:
image: jboss/keycloak:8.0.1
command:
- " -b 0.0.0.0"
container_name: "keycloak"
networks:
- myproject
volumes:
- "./keycloak/realm-export.json:/opt/jboss/keycloak/bin/keycloak_export_dir/realm-export.json"
environment:
KEYCLOAK_USER: admin
KEYCLOAK_PASSWORD: admin
KEYCLOAK_IMPORT: /opt/jboss/keycloak/bin/keycloak_export_dir/realm-export.json
ports:
- "8082:8080"
backend:
build:
context: ./backend
dockerfile: Dockerfile
container_name: "backend"
environment:
- spring.oauth2.resourceserver.jwt.issuer-uri= http://keycloak:8082/auth/realms/myrealm
links:
- keycloak
networks:
- myproject
restart: on-failure
ports:
- "8080:8080"
networks:
myproject:
driver: bridge
application.yml:
application:
name: backend
security:
oauth2:
resourceserver:
jwt:
issuer-uri: http://localhost:8082/auth/realms/myrealm
do you have any Idea why do I get connection refused ?
any help is appreciated :)
Your Keycloak container using the following port configuration
ports:
- "8082:8080"
That mean:
Keycloak is reachable from Outside via Port 8082.
But internally (in this docker network), keycloak is only reachable via the exposed 8080 port. So your backend application need to connect (internally) to http://keycloak:8080

Docker : Trying to connect Spring Java Container app to MS Sql Server container : getting error : connection failed

I am trying to connect to docker container of ms sql server 2017, through java spring boot application, In containerized environment its not getting connected and giving me error :
com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.
However If I run Spring app locally it gets connected to Ms sql server container.
My spring boot application.properties:
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost;databaseName=master
spring.datasource.username=sa
spring.datasource.password=Ms#12345
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql = true
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.SQLServer2012Dialect
spring.jpa.hibernate.ddl-auto = update
spring.main.allow-bean-definition-overriding=true
Configured docker-compose.yml as below
version: '3'
services:
myteamapp-mssqlserver:
image: "mcr.microsoft.com/mssql/server:2017-latest"
networks:
- myteamapp-net
volumes:
- myteamapp-data:/var/opt/mssql
ports:
- 1433:1433
environment:
- SA_PASSWORD=Ms#12345
- ACCEPT_EULA=Y
- MSSQL_PID=Express
myteamapp-app:
image: webservices:latest
networks:
- myteamapp-net
ports:
- 8080:8080
depends_on:
- myteamapp-mssqlserver
myteamapp-ui:
image: myteamapp
networks:
- myteamapp-net
depends_on:
- myteamapp-app
ports:
- 4200:4200
networks:
myteamapp-net:
volumes:
myteamapp-data:
Let me know what is I am missing here.

Resources