Connecting the Service Consumer microservice with Eureka

The Eureka starter project needs to be added as a dependency in the pom.xml file of the Service Consumer microservice:

    <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

Currently, the URLs of the different instances of Microservice A are hardcoded in the Service Consumer microservice, as shown here, in application.properties:

    microservice-a.ribbon.listOfServers=
http://localhost:8080,http://localhost:8081

However, we don't want to hardcode Microservice A URLs. Instead, we want the Service Consumer microservice to get the URLs from Eureka Server. We do that by configuring the URL of Eureka Server in application.properties of the Service Consumer microservice. We will comment out the hardcoding of the Microservice A URLs:

    #microservice-a.ribbon.listOfServers=
http://localhost:8080,http://localhost:8081
eureka.client.serviceUrl.defaultZone=
http://localhost:8761/eureka

Next, we will add EnableDiscoveryClient on the ServiceConsumerApplication class, as shown here:

    @SpringBootApplication
@EnableFeignClients("com.mastering.spring.consumer")
@EnableDiscoveryClient
public class ServiceConsumerApplication {

Once the Service Consumer microservice is restarted, you will see that it will register itself with Eureka Server. The following is an extract from the log of Eureka Server:

    Registered instance SERVICE-CONSUMER/192.168.1.5:
service-consumer:8100 with status UP (replication=false)

In RandomServiceProxy, we have already configured a name for microservice-a on FeignClient, as shown here:

    @FeignClient(name ="microservice-a")
@RibbonClient(name="microservice-a")
public interface RandomServiceProxy {

The Service Consumer microservice will use this ID (Microservice A) to query Eureka Server for instances. Once it gets the URLs from Eureka Service, it will invoke the service instance selected by Ribbon.

When the add service is invoked at http://localhost:8100/add, it returns an appropriate response.

Here's a quick review of the different steps that are involved:

  1. As each instance of Microservice A starts up, it registers with the Eureka name server.
  2. The Service Consumer microservice requests the Eureka name server for instances of Microservice A.
  3. The Service Consumer microservice uses the Ribbon client-side load balancer to decide the specific instance of Microservice A to call.
  4. The Service Consumer microservice calls a specific instance of Microservice A.

The biggest advantage of Eureka Service is that the Service Consumer microservice is now decoupled from Microservice A. Whenever new instances of Microservice A come up or an existing instance goes down, the Service Consumer microservice doesn't need to be reconfigured.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset