MICROSERVICES WITH SPRING BOOT, EUREKA AND DOCKER - PART 1
Kainan Gabriel
Posted on January 19, 2024
HOW TO COMMUNICATE SPRING BOOT MICROSERVICES WITHIN THE SAME DOCKER NETWORK?
FIRST THINGS FIRST, WE NEED TO CREATE OUR DOCKER NETWORK.
NOW WE NEED TO SET-UP THE DOCKERFILE.
WILL LOOK LIKE THIS INSIDE THE CONTAINER:
/app
├── .mvn/
├── pom.xml
├── target/
│ └── [compiled JAR files and other build artifacts]
└── src/
NOW WE NEED TO BUILD AND RUN OUR DOCKER CONTAINER:
"DOCKER BUILD ." WILL CREATE YOUR IMAGE THEN YOU RUN IT WITH:
REMEMBER TO RUN YOUR CONTAINER WITH THE SAME NETWORK YOU JUST CREATED. IT WILL PROBABLY FAIL TO RUN SO STOP THE CONTAINER FOR NOW.
CONFIGURING OUR SPRING BOOT APPLICATIONS
DISCOVERY/SERVICE-REGISTRY
REMEMBER TO ANNOTATE YOUR MAIN JAVA CLASS WITH @EnableEurekaServer
I RECOMMEND YOU TO NOT USE THE IPV4 BECAUSE EVERYTIME YOU REASSIGN YOUR CONTAINER TO THE NETWORK THE IP WILL CHANGE. USE THE DOCKER CONTAINER INSTEAD.
http://YOUR_CONTAINER_NAME:8761/eureka/
HOW TO GET THE APPLICATION CONTAINER IPV4?
GATEWAY CONFIGURATION
ON "serviceUrl.defaultZone" USE THE NETWORK IPV4 FROM THE DISCOVERY/SERVICE-REGISTRY CONTAINER OR JUST USE THE CONTAINER NAME(its better trust me).
CREATE YOUR DOCKERFILE FOR THE GATEWAY TOO AND MAKE SURE TO SET-UP YOUR PORTS CORRECTLY FOR THE CONTAINERS. IN THIS CASE WOULD LOOK LIKE THIS:
docker run --name CONTAINER_NAME --network=YOUR_NETWORK_NAME -p 8080:8080 YOUR_DOCKER_IMAGE_NAME
NOW YOU ONLY THE GATEWAY TO RECEIVE CONNECTIONS FROM THE OUTSIDE.
💖 💪 🙅 🚩
Kainan Gabriel
Posted on January 19, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.