Microservice Registration and Discovery with Eureka
In the previous post we learned how to create microservice and how multiple microservices can communicate with each other.
In the last section(Issue Section) of previous post i have mentioned one issue where we have hard coded other microservices urls like http://localhost:8081 or http://localhost:8082 and in real time environment it may vary.
The typical solution of above problem is we need to use Eureka Service Discovery.
What is Eureka
Eureka is a REST based service used to middle tier load balancing and to handle fail-over. It is primarily used in AWS cloud for service discovery.
Boot will provide a starter for eureka which comes under cloud.springframework
umbrella.
Let’s see how to achieve it in microservice based architecture.
You can clone repository from below git.
GitHub Repo: https://github.com/yogeshmprajapati/kode12-spring-boot.git
To demonstrate whole scenario i have create 2 different microservices.
- EurekaServer
- EurekaClient
EurekaServer
Git Module: spring-boot-eureka-server
Runs on: Port 8761
The main task of this application is to
- Start eureka server.
- Discovery of other clients.
- Balancing of incoming request.
- Periodically check heartbeat of registered services.
We will see required code change in detail.
pom.xml
You need to add below tags in your pom.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
SpringBootEurekaServerApplication.java
Class with main method to run an application.
package com.kode12; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class SpringBootEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootEurekaServerApplication.class, args); } }
Here, we have applied @EnableEurekaServer
annotation on class to enable eureka server, Now eureka engine will able to perform its task.
application.properties
server.port=8761 eureka.client.register-with-eureka=false
Property eureka.client.register-with-eureka
is set to false
because we are preventing current eureka instance to be register with itself, it tries to register with itself if this property is ignored or set to true
.
Run an application
That’s It, we can run an application using class having main method.
Hit url http://localhost:8761/ in your favorite browser.
It shows information about current eureka instance and other statistics.
Here Instances currently registered with Eureka section shows No instances available, it means no client registered with server yet.
EurekaClient
Git Module: spring-boot-eureka-client
Runs on: Port 8080
The main task of this microservice is
- Register itself with server.
- Sample call via eureka server
We will see required code change in detail.
pom.xml
You need to add below tags in your pom.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
application.properties
spring.application.name=CLIENT-SERVICE
CLIENT-SERVICE
is a unique name to current instance which is used by Eureka server to identify instance.
TestController.java
Controller to test web services.
package com.kode12.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.Date; @RestController public class TestController { @Autowired RestTemplate restTemplate; @GetMapping("/test") public String test(){ return "Test: " + new Date(); } @GetMapping("/do-test") public String doTest(){ return restTemplate.getForObject("http://client-service/test", String.class); } }
Here i have created 2 web services
http://localhost:8080/test
Will returns Test:
http://localhost:8080/do-test
This service will actually shows use of balancing and how it resolve url using Eureka server.
The notifiable thing here is we have used client-service
as a host name in http://client-service/test url, technically there is no url available with host name client-service
but RestTemplate
will resolve it using Eureka Server as current instance is registered with server with name client-service
.
So internally it resolve client-service with actual host name and then do the rest of work.
SpringBootEurekaClientApplication.java
Class with main method to run an application.
package com.kode12; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient public class SpringBootEurekaClientApplication { public static void main(String[] args) { SpringApplication.run(SpringBootEurekaClientApplication.class, args); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
2 new annotation used here
@EnableEurekaClient:
This annotation will declare current instance eureka client and also make eureka client eligible for discovery.
@LoadBalanced:
This annotation will intercept all requests made by RestTemplate
to resolve actual end points.
Run an application
Let’s run the application.
Once client application is up and running, it register itself with server.
Server shows registered clients in Instances currently registered with Eureka section which is shown in image below.
Here it shows Application: CLIENT-SERVICE
which is configured via application.properties
.
At last hit the method which actually use this mechanism.
Url: http://localhost:8080/do-test
Output:
And here we go!.
Share current post by copy: https://goo.gl/t2G1DG
Happy Learning!
Thanks,
Yogesh P