DEV Community

Kainan Gabriel
Kainan Gabriel

Posted on

MICROSERVICES WITH SPRING BOOT, EUREKA AND DOCKER - PART 1

HOW TO COMMUNICATE SPRING BOOT MICROSERVICES WITHIN THE SAME DOCKER NETWORK?

FIRST THINGS FIRST, WE NEED TO CREATE OUR DOCKER NETWORK.

Image description

NOW WE NEED TO SET-UP THE DOCKERFILE.

Image description

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:

Image description

"DOCKER BUILD ." WILL CREATE YOUR IMAGE THEN YOU RUN IT WITH:

Image description

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

Image description

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?

Image description

GATEWAY CONFIGURATION

Image description

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.

Top comments (0)