Netflix has always been a proud contributor to the open source world. It's fascinating to see how each of their libraries facilitate a lot of tasks and can help create your development in a tremendous way.

In this series of blog posts - The Netflix stack, using Spring Boot - I'll be going over some of the libraries which Netflix has created and how to incorporate them into your Spring applications. As always, it'll be more of a hands-on experience as this blog post will basically just be an overview of what you can find in the accompanying repository.

Eureka

Today we'll be looking at Eureka. The Netflix Eureka Github introduction:

Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers.

Basically, the Eureka infrastructure is set up as a client-server model. You can have one or multiple Eureka Servers and multiple Eureka Clients. It's a registry where clients (your microservices) can connect to (register), making your Eureka server aware of where your microservices are located, how many there are and if they're healthy or not.

As I have always done in my blog posts, I'll accompany this with a Github Repository serving the sole purpose of giving you a working example.

It's the first time I'm doing a series of blog posts on the same subject. Therefore, I'll create branches for each part, which can individually be checked out. I'll try to keep everything up to date with the newest versions like I'm doing with all of my other examples.

Gradle Setup

My build tool of choice is Gradle, so the entire example will be based on a Gradle configuration. The configuration will consist of a parent, which will include all the microservices, who can be deployed individually. For this example, we'll only have one microservice (the Eureka Client) and one Eureka Service.

Our parent project will be just like a basic parent pom in maven and will facilitate the build of the entire project and microservices. I choose for this setup as it is easily approachable by someone who just wants to check out the code. Check out the Gradle structure in the repository. It's not hard to set this up, but it can be an example of how to set this up.

Eureka Server

By default, a Eureka server will also be a Eureka Client, trying to connect to the Registry.

application.yml - our configuration

In our setup, however, we want 1 main Eureka Discovery Service other clients can connect to. This is the minimal configuration we would need.

server: port: 8761 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

EurekaServer.java - Bootstrap the application

@SpringBootApplication @EnableEurekaServer public class EurekaServer { public static void main(String[] args) { SpringApplication eurekaServer = new SpringApplication(EurekaServer.class); eurekaServer.addListeners(new ApplicationPidFileWriter("eureka-server.pid")); eurekaServer.run(); } }

Registering a Service

Registering a microservice is just as easy as creating a Eureka Server. Here's an example of the configuration.

server: port: 9000 spring: application: name: notification-service eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ instance: preferIpAddress: true

All you need to register the application to the Eureka server would be

@SpringBootApplication @EnableEurekaClient //or @EnableDiscoveryClient public class NotificationMicroService { public static void main(String[] args) { SpringApplication notificationMicroService = new SpringApplication(NotificationMicroService.class); notificationMicroService.addListeners(new ApplicationPidFileWriter("notification-micro-service.pid")); notificationMicroService.run(args); } }

Running the Example

We provided a startup script which you can build and run all the services. the only prerequisite is that you need Gradle in order for this to run.

./startup.sh

to stop the services, simply run

./stop.sh

Watching Our Services

By default, spring boot Netflix provides a UI on top of the Eureka Server. In our example, we've deployed it to http://localhost:8761/, so navigate to it and watch the health of your Eureka Server and Client.

You'll see something like this.

As you can see, after starting up the server and client, the notification service has registered itself.

Coming Up

This model of microservices that register themselves to a global registry will have a lot of advantages when it comes to building one or multiple applications using a microservice architectural approach. Eureka on its own won't have that much of use, but as you'll see in the future blog posts, Eureka will be the key element to locate all of our microservices.

The Github Repository

As we said before, this is not just an ordinary blog post. It's more of a guide on how to set up your environment to quickly start working with the discussed technology. That's why we're always making sure we have an accompanying GitHub repository available, so people can easily see how it works and have a working example at hand.

The repository that's accompanying this blog post is a bit different. Each time I'm releasing a new part of this series of blog posts, the repository will have a new branch, which will contain the new technology that will be discussed. In the end, I'll hope to come up with a nice example of how all the technologies can work together.