Your task is to implement a Discovery Service using the Spring Cloud component Spring Cloud Netflix Eureka and integrate this Discovery Service with the existing services of our architecture. The resulting system should look like this:
- Go to start.spring.io and create a new Maven project using the following coordinates:
- Group ID:
workshop.spring.discovery
- Artifact ID:
gtd-discovery-service
(or choose your own)
-
What dependencies do you need to turn this Spring Boot application into a fully-fledged Spring Cloud Netflix Eureka server? Add the necessary dependencies to the build.
-
Generate the Maven
pom.xml
using the Spring Initializr. Copy the resulting code into the local checkout of this repository and integrate it with the Maven build (cf.<modules>
section of the parent POM). -
Enable the Eureka server component for your Spring Boot application.
-
Configure the Eureka server (cf.
application.yaml
). -
Launch the discovery service and verify that the service starts up properly. Go to http://localhost:8761/ to open up its dashboard.
With the Eureka server up and running, it is now time to register our application services with it. Before we do so, we need to run the Eureka server as part of our local Docker setup. This means that you need to extend the build-containers.{bat|sh}
script as well as the docker-compose.yaml
file.
- Add the following
Dockerfile
to the root of the Discovery Service's Maven module.
FROM eclipse-temurin:17.0.7_7-jre-jammy
WORKDIR /opt/app
RUN adduser --system -shell /usr/sbin/nologin --group javauser
COPY target/gtd-discovery-service-1.0.0-SNAPSHOT.jar app.jar
RUN chown -R javauser:javauser .
USER javauser
ENTRYPOINT ["java", "-jar", "app.jar"]
- Add the following line to
build-containers.{bat|sh}
:
docker build -t getting-things-done/eureka/discovery-service ../gtd-discovery-service
- Add the following lines to
docker-compose.yaml
:
discovery-service:
image: getting-things-done/eureka/discovery-service
mem_limit: 256m
ports:
- "127.0.0.1:8761:8761"
environment:
eureka.instance.hostname: discovery-service
-
Configure the Command Service such that it is able to register itself with the Eureka server.
-
Configure the Query Service such that it is able to register itself with the Eureka server.
-
Launch the whole setup to verify that everything connects up as expected. Go to http://localhost:8761/ to open up its dashboard and check if both application instances have registered themselves.
-
What happens if you scale the number of instances for a specific application up? What happens if you scale back down? How frequently does the Eureka server detect churn? (wrt. its configuration)
docker-compose up --scale query-service=2
You have completed all assignments. If you have any further questions or need clarification, please don't hesitate to reach out to us. We're here to help.