Using Netflix Eureka with Spring Cloud

In the world of microservices it becomes more and more important to easily find / locate services. They can run anywhere in the cloud, even and often multiple times (multiple instances per service). Fortunately Netflix did open source some of its great internal tools that can help us with these problems.

The is about using an Eureka server to maintain a registry of microservices. All with the help of the spring cloud project.

BTW: When you search for infos about eureka you can discover funny things!

Setting up an Eureka registry

Eureka can run standalone. You can download the code and build the war file, put it on a web container and run it.

If you are running spring boot applications, there is an equally easy way of running it:

Create a spring boot application with dependency to spring-cloud-eureka

and a starter class

and your registry is a spring boot application. See this great example for details. There is even a docker container with it.

For my project I started by using this docker container and ended up having my own spring boot application inside a docker container. Mainly because I needed to fine tune to JVM settings (memory set to only 256MB).

Security

Securing an eureka instance is quite easy.

Authentication

Add the following dependency to your pom:

Add the following to its application.yml:

You probably can use any spring security solution here (see this github examples). But basic auth makes it quite simple to use in the client:

Encryption

Since we should not send the authentication in plain text over the line, we need to switch the eureka server to SSL:

You can create the self-signed certificate with

You have to import it into your environment or configure your JVM to use it. See these docs for examples.

Service Naming

I found out that it is wise to name services like domain names. Spring let you use service names in RestTemplates or FeignClients and some of them check for valid URLs before querying the registry.

For example for MOS ERP the domain moserp.org is used as base name, with services having names like products.moserp.org . This could even be migrated to a DNS based service registry.

Clients

For spring boot clients, add the following maven dependency

And the following lines to the bootstrap.yml:

To activate discovery client your spring boot application class should look like

Other Clients

The eureka server exposes a simple REST api. That can be used from any program to check for registering or querying services.