Microservices – Service Registration and Discovery with Eureka

As you create more microservices, it is hard to keep track. Eureka helps in discovering and locating the services. It acts as a load balancer and service registry. All the services are identified by their names without port information. If service A running on port 8080 at URL http://localhost:8080 registers itself as SERVCIEA on Eureka, other services on Eureka can call it as SERVCIEA instead of calling http://localhost:8080.

If you rather watch the video on what’s in this post, see below, otherwise continue reading.

Eureka acts as an internal DNS and middle tier load balancer. With Eureka, load balancing happens at the instance level and the client instances know the information about which servers they need to talk to, making it ideal for the client-based load balancer.

To implement it follow the steps listed below.
Go to https://start.spring.io/ and download the project with Eureka Server as a dependency.
Add the following text to the application.properties.

Below, we are configuring the name for this service, port to run at and telling it not to register itself as a service.

Add the following annotation to SpringBoot main class and run the application.

Now go to http://localhost:8761/ and you should see a screen like this.

Create 2 additional microservices, so they can talk to each other via Eureka.

Go to https://start.spring.io/ and download 2 projects with Eureka Discovery as a dependency.  One would be ServiceA and other would ServiceB.

Add the following annotation to Spring boot main classe for both ServiceA and ServiceB applications.

Create application.yml in both the applications in resource folders.

Add the following in serviceA’s applcation.yml, the name of this application, port and Eureka information to register itself as a service with Eureka.

Add the following in serviceB’s applcation.yml

Add the following endpoint to Spring boot main class in ServiceA.

We will be calling this endpoint  “/contact” from ServiceB.

Go ahead and run ServiceA and go to http://localhost:8761/  you should see ServiceA listed as below with Eureka.

Add the following to Spring boot main class in ServiceB to call the ServiceA’s endpoint above.

Notice the URL in the code above, we are not referencing ServiceA with complete URL as http://localhost:8080, rather with the name that it registered itself with Eureka.

Run ServiceB and check Eureka http://localhost:8761, it should show both the services.

Go to the above endpoint http://localhost:8081/cotactserver it will call SERVICEA and you should see the output as below.

Download eureka.zip for source code in my Microservices Git repo

Here are few links for additional information.






Author: Seenu Kamisetty

I have experience in Microservices, Deep Learning, Cloud, Docker, Mobile, Java, Scala, Python. I greatly enjoy helping businesses to take advantage of technologies. Check me out on Twitter @AddictedAi

Leave a Reply

Your email address will not be published. Required fields are marked *