Eureka Service Discovery example
When you are running multiple microservices, you need a mechanism to locate a service. You can register each microservice in a service registry which is a seperate service. A service registry also helps in loadbalancing, and register or unregister a service when it is not available. A service or client application which wants to use another service can lookup the service in the service registry and then call it.
Below Eureka Service Discovery example shows how to register and lookup a microservice using Netflix Eureka service registry. We will create a Eureka Service Registry server and register two microservices named ToyService and ToyClientService respectively. ToyClientService microservice will lookup ToyService microservice in the Eureka Service Registry and invoke its exposed endpoint.
Step 1) Add below dependencies in pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.3</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2021.0.3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
Step 2) Create EurekaServiceApplication class to launch Eureka Service registry
You need to add @EnableEurekaServer
annotation to make sure that the application will run as Eureka server.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class EurekaServiceApplication { public static void main(String[] args) { SpringApplication.run(EurekaServiceApplication.class, args); } }
Step 3) Create application.yml file under src/main/resources
Since the application will run as service registry, we will not register it with service registry by setting register-with-eureka: false
.
server: port: 8761 spring: application: name: discovery-service eureka: client: register-with-eureka: false fetch-registry: false
Step 4) Launch EurekaServiceApplication and open http://localhost:8761/ in the browser.
Step 5) Create ToyService application
Create pom.xml file similar to one create for EurekaServiceApplication but replace 'spring-cloud-starter-netflix-eureka-server' with 'spring-cloud-starter-netflix-eureka-client' as mentioned below.
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
Step 6) Create Toy, ToyServiceApplication & ToyRestController classes
package com.example.demo; public class Toy { private String name; private String type; private double price; public Toy(String name, String type, float price) { this.name = name; this.type = type; this.price = price; } //removed getter setter for brevity }
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ToyServiceApplication { public static void main(String[] args) { SpringApplication.run(ToyServiceApplication.class, args); } }
package com.example.demo; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ToyRestController { private static final Logger LOG = LoggerFactory.getLogger(ToyRestController.class); private List<Toy> toys = new ArrayList<>(); public ToyRestController() { toys.add(new Toy("Barbie", "Doll", 45.0)); toys.add(new Toy("My Little Pony", "Animal", 10.4)); toys.add(new Toy("Hot Wheels", "Car", 5.4)); } @RequestMapping(value = "/toys") // http://localhost:8090/toys public List<Toy> listAll() { LOG.info("returning list of toys"); return toys; } @RequestMapping(value = "/toys/{id}") // http://localhost:8090/toys/1 public Toy getToy(@PathVariable int id) { LOG.info("returning toy"); return toys.get(id); } }
Step 7) Create application.yml file under src/main/resources
Using defaultZone
, we can specify eureka service where the application will be registered.
spring: application: name: toy-service server: port: 8090 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
Step 8) Create pom.xml file for second microservice ToyClientService similar to one for ToyServiceApplication.
<groupId>com.example</groupId> <artifactId>toy-client-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging>
Step 9) Create ToyClientServiceApplication & ToyRestController classes
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class ToyClientServiceApplication { public static void main(String[] args) { SpringApplication.run(ToyClientServiceApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
package com.example.demo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class ToyClientServiceRestController { @Autowired private RestTemplate restTemplate; private static final Logger LOG = LoggerFactory.getLogger(ToyClientServiceRestController.class); @RequestMapping("/toy-client-service/toys") // http://localhost:8090/toys public ResponseEntity<?> getToys() { LOG.info("calling toy service"); return restTemplate.getForEntity("http://toy-service/toys/", String.class); } @RequestMapping("/toy-client-service/{id}") // http://localhost:8090/toys/1 public ResponseEntity<?> getToy(@PathVariable String id) { LOG.info("calling toy service"); return restTemplate.getForEntity("http://toy-service/toys/"+id, String.class); } }
Step 10) Create application.yml file under src/main/resources
spring: application: name: toy-client-service server: port: 8091 eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
Step 11) Refresh http://localhost:8761/ in the browser.
Step 12) Testing eurake service discovery
ToyServiceApplication
& ToyClientServiceApplication
, in browser navigate to http://localhost:8091/toy-client-service/1.Note that
ToyClientServiceApplication
internally calls http://toy-service/toys/1 which means ToyServiceApplication
instance was fetched from Eureka Servie Registry and then it was invoked.
You will see below page displayed in the broswer.
References :
Spring Service Registration and Discovery