Spring micro services eureka client is not registering with eureka server - spring

I'm very new to everything about docker, spring framework... They run on localhost environment successfully, now I want to push them into docker, but always gave me error there, i got stuck on it about a week. Can someone help me figure it out PLEASE!!!
now I have eureka-server and cloud-config-server services, here is my code:
eureka-server application.yml
server:
port: 8761
spring:
application:
name: service-registration
eureka:
client:
register-with-eureka: false
fetch-registry: false
management:
security:
enabled: false
and Dockerfile:
FROM openjdk:11
COPY target/service-registration-0.0.1-SNAPSHOT.jar service-registration.jar
EXPOSE 8761
CMD ["java", "-jar", "service-registration.jar"]
Now I have cloud-config-server application.yml:
In this file, i tried to backup it on github.
From the start, I tried to change the hostname as localhost or any other hostname like service-registration, eureka-server etc... but not work, and "service-url" or "serviceUrl'....
server:
port: 9291
spring:
application:
name: cloud-config-server
profiles:
active: production
# cloud:
# config:
# server:
# git:
# uri: https://github.com/sshaunn/config-server
# clone-on-start: true
eureka:
client:
register-with-eureka: true
fetch-registry: true
serviceUrl:
defaultZone: http://service-registration:8761/eureka/
instance:
# prefer-ip-address: true
# ip-address: http://eureka-server:8761/eureka/å
hostname: service-registration
Dockerfile:
FROM openjdk:11
COPY target/cloud-config-server-0.0.1-SNAPSHOT.jar cloud-config-server.jar
EXPOSE 9291
ENTRYPOINT ["java", "-jar", "cloud-config-server.jar"]
and the docker-compose.yml file:
version: '3.7'
services:
service-registration:
image: service-registration
networks:
- eureka-server
ports:
- "8761:8761"
container_name: service-registration
hostname: service-registration
cloud-config-server:
build: cloud-config-server
networks:
- eureka-server
ports:
- "9291:9291"
depends_on:
- service-registration
container_name: cloud-config-server
hostname: service-registration
environment:
EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://service-registration:8761/eureka
networks:
eureka-server:
name: eureka-server
driver: bridge
# route:
# image: route
# ports:
# - "9191:9191"
# container_name: api-gateway
# environment:
# EUREKA_CLIENT_SERVICEURL_DEFAULTZONE: http://service-registration:8761/eureka
The error logs here:
2022-05-29 07:22:41.713 WARN 1 --- [freshExecutor-0] c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with message: I/O error on GET request for "http://localhost:8761/eureka/apps/": Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused (Connection refused);
2022-05-29 07:06:11.565 WARN 1 --- [tbeatExecutor-0] c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with message: I/O error on PUT request for "http://localhost:8761/eureka/apps/CONFIG-SERVER/service-registration:CONFIG-SERVER:9291": Connect to localhost:8761 [localhost/127.0.0.1] failed: Connection refused (Connection refused);

Ok, I dont know why but I set up a docker-compose.yml file like this and it works:
Note, I set up environment about "service-url" defaultZone in docker-compose.yml file.
version: '3.7'
services:
service-registration:
container_name: service-registration
build:
context: ./service-registration
dockerfile: Dockerfile
ports:
- "8761:8761"
cloud-config-server:
container_name: cloud-config-server
build:
context: ./cloud-config-server
dockerfile: Dockerfile
environment:
- eureka.client.service-url.defaultZone=http://service-registration:8761/eureka
depends_on:
- service-registration
ports:
- "9191:9191"
route:
container_name: route
build:
context: ./route
dockerfile: Dockerfile
environment:
- eureka.client.service-url.defaultZone=http://service-registration:8761/eureka
depends_on:
- service-registration
- employee
- income
ports:
- "9291:9291"
employee:
container_name: employee
build:
context: ./employee
dockerfile: Dockerfile
environment:
- eureka.client.service-url.defaultZone=http://service-registration:8761/eureka
depends_on:
- service-registration
ports:
- "9001:9001"
income:
container_name: income
build:
context: ./income
dockerfile: Dockerfile
environment:
- eureka.client.service-url.defaultZone=http://service-registration:8761/eureka
depends_on:
- service-registration
ports:
- "9002:9002"

You should use your host ip address to run in your local machine. For the docker it should not be localhost
Try with the below configuration in your application.properties file.
eureka.client.service-url.defaultZone=http://host.docker.internal:8761/eureka/

Related

I am running zuul in a docker container, but I cant connect to it via postman on my local host (windows 10)

I have 3 services
zuul, eureka, and a rest service.
Running the code locally via springboot works and I can use the url
http://localhost:8080/account/bank/account/
But when I dockerise it
The eurka application.yml
server:
port: '8761'
spring:
application:
name: eureka-service
eureka:
client:
fetchRegistry: 'false'
registerWithEureka: 'false'
Eureka local.dockerfile
FROM adoptopenjdk:16.0.1_9-jdk-hotspot-focal
EXPOSE 8761
RUN mkdir /app
COPY target/eureka-service.jar /app
WORKDIR /app
ENTRYPOINT ["java", "-jar", "/app/eureka-service.jar"]
The zuul application.yml
spring:
application:
name: zuul-service
server:
port: '8080'
eureka:
client:
instance:
preferIpAddress: 'true'
fetchRegistry: 'true'
registerWithEureka: 'true'
serviceUrl:
defaultZone: http://172.21.0.2:8761/eureka
zuul:
routes:
account:
path: /account/**
serviceId: account-service
zuul local.dockerfile
FROM adoptopenjdk:16.0.1_9-jdk-hotspot-focal
EXPOSE 8080
RUN mkdir /app
COPY target/zuul-service.war /app
WORKDIR /app
ENTRYPOINT ["java", "-jar", "/app/zuul-service.war"]
the rest application.yml
spring:
application:
name: account-service
server:
port: '8082'
eureka:
client:
instance:
preferIpAddress: 'true'
fetchRegistry: 'true'
registerWithEureka: 'true'
serviceUrl:
defaultZone: http://172.21.0.2:8761/eureka
rest local.dockerfile
FROM adoptopenjdk:16.0.1_9-jdk-hotspot-focal
EXPOSE 8082
RUN mkdir /app
COPY target/account-service.war /app
WORKDIR /app
ENTRYPOINT ["java", "-jar", "/app/account-service.war"]
I have two docker-compose files. Mainly through experimenting with trying to publish the ports.
Eureka and rest
version: "3"
services:
eureka-service:
image: eureka-service
container_name: eureka-service
build:
context: ../../eureka-service
dockerfile: local.dockerfile
ports:
- "8761:8761"
networks:
- banking-network
account-service:
image: account-service
container_name: account-service
build:
context: ../../account-service
dockerfile: local.dockerfile
ports:
- "8082:8082"
depends_on:
- eureka-service
environment:
- eureka.client.service-url.defaultZone=http://172.21.0.2:8761/eureka
networks:
- banking-network
networks:
banking-network:
external:
name: banking-network
zuul
version: "3"
services:
zuul-service:
image: zuul-service
container_name: zuul-service
build:
context: ../../zuul-service
dockerfile: local.dockerfile
ports:
- "8080:8080"
environment:
- eureka.client.service-url.defaultZone=http://172.21.0.2:8761/eureka
networks:
- banking-network
networks:
banking-network:
external:
name: banking-network
when I run docker-compose up on both docker-compose
zuul and application is registered with eureka
But in postman when I run http://localhost:8080/account/bank/account/
it fails.
If I am correct I need to publish zuul-service port to the host.
But this is where I am having real problems
So following the docker docs
I run the following on the zuul docker-compose
docker-compose run --publish 8080:8080 zuul-service
running docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea2957b56646 zuul-service "java -jar /app/zuul…" About a minute ago Up About a minute 0.0.0.0:8080->8080/tcp gateway_zuul-service_run_c3988736de38
Zuul and account are registered
But neither URLs work
http://localhost:8080/account/bank/account/
http://0.0.0.0:8080/account/bank/account/
I created a simple health page on zuul. to see if I can connect to zuul
This url works, when its run from springboot
http://localhost:8080/gateway/health/
But neither works from docker
http://localhost:8080/gateway/health/
http://0.0.0.0:8080/gateway/health/
I thought publishing 8080 to my local host, would mean I could use localhost
A few suggestions to debug the issue:
Put all services in one docker compose file and make sure they are on the same network
For defaultZone, change ip to eureka docker image name so instead of:
defaultZone: http://172.21.0.2:8761/eureka
Try:
defaultZone: http://eureka-service:8761/eureka/
I’d try without preferIpAddress: true If you don’t have multiple replicas of your registered services
After Exposing zuul gateway port, check what routes are available at localhost:8080/routes
The code does work. I found out that because I was developing my code in increments. I thought I was rebuilding a new image. I was not, I removed the images, rebuilt and the code works.

Microservice not able to connect to AXON Server running as docker image

I have discovery service: https://github.com/Naresh-Chaurasia/API-MicroServices-Kafka/tree/master/Microservices-CQRS-SAGA-Kafka/DiscoveryService
I have product service: https://github.com/Naresh-Chaurasia/API-MicroServices-Kafka/tree/master/Microservices-CQRS-SAGA-Kafka/ProductsService
Following is my docker-compose.yml file: https://github.com/Naresh-Chaurasia/API-MicroServices-Kafka/tree/master/Microservices-CQRS-SAGA-Kafka/docker-compose.yml
version: "3.8"
services:
axon-server:
image: axoniq/axonserver
container_name: axon-server
ports:
- 8124:8124
- 8024:8024
networks:
- axon-demo
discovery-service:
build:
context: ./DiscoveryService
container_name: discovery-service
ports:
- 8010:8010
networks:
- axon-demo
networks:
axon-demo:
driver: bridge
When i run the following command docker-compose up, I get axon-server and discovery-service running.
I now run ProductService using the following file src/main/java/com/appsdeveloperblog/estore/ProductsService/ProductsServiceApplication.java which is under https://github.com/Naresh-Chaurasia/API-MicroServices-Kafka/tree/master/Microservices-CQRS-SAGA-Kafka/ProductsService.
It works fine.
The problem start when I try to run ProductService as a microservice, and it fails to connect to axon server. To do that I modify the docker-compose.yml as follows:
version: "3.8"
services:
axon-server:
image: axoniq/axonserver
container_name: axon-server
ports:
- 8124:8124
- 8024:8024
networks:
- axon-demo
discovery-service:
build:
context: ./DiscoveryService
container_name: discovery-service
ports:
- 8010:8010
networks:
- axon-demo
product-service:
build:
context: ./ProductsService
ports:
- 8090:8090
depends_on:
- axon-server
- discovery-service
networks:
- axon-demo
networks:
axon-demo:
driver: bridge
Now if I try to run docker-compose up, I get the following error:
product-service_1 | 2021-08-20 03:06:27.973 INFO 1 --- [#29db04f6c0a4-0] i.a.a.c.impl.AxonServerManagedChannel : Requesting connection details from localhost:8124
product-service_1 | 2021-08-20 03:06:30.004 WARN 1 --- [#29db04f6c0a4-0] i.a.a.c.impl.AxonServerManagedChannel : Connecting to AxonServer node [localhost:8124] failed: UNAVAILABLE: io exception
I have gone through the following link, Spring Boot Microservices are unable to connect to Axon Server, which looks like similar problem but still not able to fix my problem.
Please guide.
Thanks.
I made the following changes and it works fine now.
In the file ProductsService\src\main\resources\application.properties
Old Entry: eureka.client.serviceUrl.defaultZone = http://localhost:8010/eureka
New Entry: eureka.client.serviceUrl.defaultZone = http://discovery-service:8010/eureka
Added the following line in the above file: axon.axonserver.servers=axon-server:8124
In the File: Microservices-CQRS-SAGA-Kafka\docker-compose.yml
Old Entry: version: "3.8"
New Entry: version: "2"
Added following code in yml file
product-service:
build:
context: ./ProductsService
ports:
- 8090:8090
networks:
- axon-demo
I have also checked in the code in git. The micro services can talk to each other, and connect to axon server and the problem reported earlier is resolved.
Thanks.
Just a thought.
Try to add to each service in application.properties
axon.axonserver.servers=localhost:8124
and to each service which connects to axon-server in docker-compose.yml
in environment variables section
axon-server:
image: axoniq/axonserver
container_name: axon-server
ports:
- 8124:8124
- 8024:8024
product-service:
...
environment:
AXON_AXONSERVER_SERVERS: axon:8124

Spring boot microservices and Docker, unable to connect to database

I'm writing a test app in microservices to learn Spring Boot. I'm done with the app and looking to deploy it on AWS using docker/docker-compose to host each microservices.
I'm unable to connect any of the Spring Boot instances to their Mysql database. I've been stuck on it for a few days and I can't see what's wrong.
Here is my docker-compose.yml
version: "3.3"
services:
card_mysql:
image: "mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "cards"
ports:
- "33061:3306"
auth_mysql:
image: "mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "auth"
ports:
- "33062:3306"
market_mysql:
image: "mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "market"
ports:
- "33063:3306"
user_mysql:
image: "mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "user"
ports:
- "33064:3306"
adminer:
image: adminer
restart: always
ports:
- 8000:8080
user:
image: "openjdk:11"
restart: always
entrypoint: java -jar /app/services/user/target/cardmarket-user-0.0.1-SNAPSHOT.jar
volumes:
- ./:/app
depends_on:
- "user_mysql"
card:
image: "openjdk:11"
restart: always
entrypoint: java -jar /app/services/card/target/cardmarket-card-0.0.1-SNAPSHOT.jar
volumes:
- ./:/app
depends_on:
- "card_mysql"
market:
image: "openjdk:11"
restart: always
entrypoint: java -jar /app/services/market/target/cardmarket-market-0.0.1-SNAPSHOT.jar
volumes:
- ./:/app
depends_on:
- "market_mysql"
proxy:
image: "openjdk:11"
restart: always
entrypoint: java -jar /app/zuul-proxy/target/zuul-proxy-0.0.1-SNAPSHOT.jar
volumes:
- ./:/app
auth:
image: "openjdk:11"
restart: always
entrypoint: java -jar /app/zuul-proxy/zuul-proxy-0.0.1-SNAPSHOT.jar
volumes:
- ./:/app
depends_on:
- "auth_mysql"
Here are each of my spring datasource settings:
user application.properties
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://user_mysql:3306/user
spring.datasource.initialization-mode=always
spring.datasource.username=root
spring.datasource.password=root
market application.properties
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://market_mysql:3306/market
spring.datasource.initialization-mode=always
spring.datasource.username=root
spring.datasource.password=root
Auth application.properties
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://auth_mysql:3306/auth
spring.datasource.initialization-mode=always
spring.datasource.username=root
spring.datasource.password=root
Card application.properties
spring.jpa.hibernate.ddl-auto=create-drop
spring.datasource.url=jdbc:mysql://card_mysql:3306/cards
spring.datasource.initialization-mode=always
spring.datasource.username=root
spring.datasource.password=root
I'm sure it is something small I'm missing or misunderstood, but I can't figure out what.
Edit 1
I ran a couple more tests but still wasn't able to figure out what's wrong.
I commented out everything but the card and card_mysql containers. The spring boot instance throw an exception for every configuration possible (using docker dns name, localhost, shared and network ip), none of them seems to work.
The two containers do communicate, I can ping between them using the dns name.
I'm using docker-desktop with wsl and deploy my containers on a wsl Ubuntu 20.04. I haven't tried on an actual Linux machine.
I've also tried to make the spring boot instance wait for the mysql one using wait-for-it.sh, it does wait correctly but the exception still occurs.
Here is parts of the exception spring boot triggers:
2021-05-16 12:23:48.948 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-05-16 12:23:50.277 ERROR 1 --- [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
...
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
...
Caused by: java.net.ConnectException: Connection refused (Connection refused)
Edit 2
I tried having just one instance of spring boot and mysql and linking them using a network, unfortunately the exception still triggers. The two containers still pings. Here is the docker-compose I used for this test:
version: "3.9"
services:
card_mysql:
image: "mysql"
restart: always
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "cards"
networks:
- "card_network"
card:
image: "openjdk:11"
restart: always
entrypoint: ["/app/wait-for-it.sh", "card_mysql:3306", "--", "java", "-jar", "/app/services/card/target/cardmarket-card-0.0.1-SNAPSHOT.jar"]
# entrypoint: ["/app/wait-for-it.sh", "card_mysql:3306", "--", "ping", "card_mysql"]
volumes:
- ./:/app
depends_on:
- "card_mysql"
networks:
- "card_network"
networks:
card_network: {}
Hi Spinarial please add network and gather it all in one network
version: "3.4"
services:
springMvcRestApi:
image: springbootproject:0.0.1
container_name: app
networks:
- postgres
ports:
- 8085:8085
depends_on:
- postgres
- pgadmin
links:
- postgres
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/reactiveDbcd?createDatabaseIfNotExist=true
SPRING_DATASOURCE_USERNAME: postgres
SPRING_DATASOURCE_PASSWORD: 12345
postgres:
container_name: postgres
image: postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 12345
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
expose:
- 5432
ports:
- 5432:5432
networks:
- postgres
restart: unless-stopped
pgadmin:
container_name: pgadmin_container
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4#pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
volumes:
- pgadmin:/root/.pgadmin
ports:
- "${PGADMIN_PORT:-80}:80"
networks:
- postgres
restart: unless-stopped
networks:
postgres:
driver: bridge
volumes:
postgres:
pgadmin:

Docker RabbitMQ and Spring doesn't create password with new user

I'm trying to deploy a spring app with rabbitmq. I get a Connection Refused Error When I use the default user:password (guest:guest) but when the user is created it makes it without password
Created new connection: rabbitConnectionFactory#60d8c0dc:5/SimpleConnection#358e0edb [delegate=amqp://guest#192.168.64.2:5672/, localPort= 53472]
Docker-compose.yml
version: '3'
services:
rabbitmq:
image: rabbitmq:management
environment:
RABBITMQ_DEFAULT_USER: "guest"
RABBITMQ_DEFAULT_PASS: "guest"
ports:
- "5672:5672" #JMS Port
- "15672:15672" #Management Port - default user:pass = guest:guest
networks:
- rabbit_mq
db:
image: mysql:5.7.22
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "hospital"
MYSQL_PASSWORD: "root"
ports:
- "3306:3306"
networks:
- mysql_bridge
restart: always
springboot-docker-compose-app-container:
image: app-image
build:
context: ./
dockerfile: Dockerfile
environment: # Pass environment variables to the service
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/hospital?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&allowPublicKeyRetrieval=true
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
SPRING_RABBITMQ_HOST: rabbitmq
depends_on:
- rabbitmq
- db
volumes:
- /data/VerzorgerSOAP
ports:
- "8080:8080"
networks:
- mysql_bridge
- rabbit_mq
networks:
mysql_bridge:
rabbit_mq:
Spring Boot provides default values for some application properties:
spring.rabbitmq.password=guest
spring.rabbitmq.username=guest
In this case, it is only a coincidence.
Change these properties to something else and you will see that the auth will fail.

Docker RabbitMQ Spring java.net.UnknownHostException: rabbitmq: Name or service not known

i m trying to deploy a Spring Server on Docker. The Spring server is connecting to a RabbitMQ Server that runs in another app. I get an error while connecting to the rabbitmq. I added the host in my application.properties, didnt work. Than added to the app-container as environment variable and still doesnt work. I also rebuilded the jar and changed the images version.
version: '3'
services:
rabbitmq:
image: rabbitmq:management
ports:
- "5672:5672" #JMS Port
- "15672:15672" #Management Port - default user:pass = guest:guest
db:
image: mysql:5.7.22
environment:
MYSQL_ROOT_PASSWORD: "root"
MYSQL_DATABASE: "hospital"
MYSQL_PASSWORD: "root"
ports:
- "3306:3306"
networks:
- mysql_bridge
restart: always
springboot-docker-compose-app-container:
image: app-image
build:
context: ./
dockerfile: Dockerfile
environment: # Pass environment variables to the service
SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/hospital?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&allowPublicKeyRetrieval=true
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
SPRING_RABBITMQ_HOST: rabbitmq
depends_on:
- rabbitmq
- db
volumes:
- /data/VerzorgerSOAP
ports:
- "8080:8080"
networks:
- mysql_bridge
- rabbiy_mq
networks:
mysql_bridge:
rabbiy_mq:
Also this is my application.properties
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.generate-ddl=true
spring.datasource.url = jdbc:mysql://db:3306/hospital?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false&allowPublicKeyRetrieval=true
spring.jpa.hibernate.ddl-auto = update
spring.datasource.username = root
spring.datasource.password = root
spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS = false
server.ssl.enabled=false
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.properties.hibernate.globally_quoted_identifiers=true
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
spring.rabbitmq.host= rabbitmq
This is the error i get
org.springframework.amqp.AmqpIOException: java.net.UnknownHostException: rabbitmq
The networks configuration was missing for rabbitmq in the dockercompose file:
rabbitmq:
image: rabbitmq:management
ports:
- "5672:5672" #JMS Port
- "15672:15672" #Management Port - default user:pass = guest:guest
networks:
- rabbiy_mq
Another solution suggested by #david-maze is to remove all networks

Resources