spring-cloud Eureka

Micro-services are all good until they come with their distributed challenges which normally nosotros don't face up in monolithic based applications. Only like this one!

Understanding the problem

In order to fully go the principal purpose of the solution let us first sympathize the underlying trouble.

spring cloud

As the diagram above shows, nosotros accept to micro-services communicating via a sure network:

  • Micro-service I with the IP address ten.10.ten.ten , and port 8080
  • Micro-service II with the IP address 20.twenty.20.20 , and port 8090

All working peacefully until the 2d micro-service changed its accost and port for some reason.

spring cloud

As result the micro-service I cannot communicate with micro-service II since it doesn't have the new address and port (xxx.thirty.30.xxx:5000). To resolve this we demand to provide manually the micro-service I with the micro-service II'southward new address and port. What if we can do that automatically?

The answer is Aye, and that'south exactly what Netflix Eureka does.

Eureka under the hood

The first thing MS2 will practise is to register to the naming registry server (Eureka server) providing mainly its IP, port and status (UP or DOWN). [A]

Earlier MS1 makes the call to MS2 [D], it will fetch the MS2 coordinates from the naming registry [B] which volition allow the advice to happen even if MS2 changes its location a million times equally long as its registers to the Eureka server. The server will respond with the required information. [C]

You should as well empathize that by fetching the naming registry or registering to it, the micro-service becomes its customer (Eureka customer).

eureka

Let's talk lawmaking

For this tutorial I'll exist using:

Spring Boot two.six.2

                                                  1                                                    2                                                    3                                                    4                                                    5                                                    6                                                                        
                                                                                                            <parent>                                                                                                                                          <groupId>org.springframework.kick</groupId>                                                                                                                                          <artifactId>spring-kick-starter-parent</artifactId>                                                                                                                                          <version>2.vi.2</version>                                                                                                                                          <relativePath/>                              <!-- lookup parent from repository -->                                                                                                                                          </parent>                                                                                                    

Java 8 && Spring cloud 2021.0.0

                                                  1                                                    two                                                    three                                                    four                                                                        
                                                                                                            <backdrop>                                                                                                                                          <java.version>1.eight</java.version>                                                                                                                                          <leap-cloud.version>2021.0.0</spring-cloud.version>                                                                                                                                          </properties>                                                                                                    

and of course the dependency management for the spring cloud modules

                                                                              ane                                                                                2                                                                                iii                                                                                4                                                                                5                                                                                6                                                                                7                                                                                viii                                                                                nine                                                    10                                                    11                                                                        
                                                                                                            <dependencyManagement>                                                                                                                                          <dependencies>                                                                                                                                          <dependency>                                                                                                                                          <groupId>org.springframework.deject</groupId>                                                                                                                                          <artifactId>jump-cloud-dependencies</artifactId>                                                                                                                                          <version>${spring-cloud.version}</version>                                                                                                                                          <type>pom</type>                                                                                                                                          <scope>import</scope>                                                                                                                                          </dependency>                                                                                                                                          </dependencies>                                                                                                                                          </dependencyManagement>                                                                                                    

Eureka server configuration

The Eureka server require the Bound Cloud starter netflix eureka server dependency

                                                  1                                                    2                                                    3                                                    four                                                                        
                                                                                                            <dependency>                                                                                                                                          <groupId>org.springframework.cloud</groupId>                                                                                                                                          <artifactId>spring-deject-starter-netflix-eureka-server</artifactId>                                                                                                                                          </dependency>                                                                                                    

Then, we demand a set of properties as the post-obit

                                                                              i                                                                                2                                                                                3                                                                                iv                                                                                5                                                                                half dozen                                                                                7                                                                                8                                                                                ix                                                    10                                                                        
                                                                                                            # Setting a name for out app                                                                                                                                                                                                                                    bound                              :                                                                                                                                                                                                                                                                  application                              :                                                                                                                                                                                                                                                                  name                              :                                                                                          registry-server                                                                                                                                                                                                                                                                                                                                                                                                            # Simply telling Eureka server to Non fetch or register to any server since information technology's not a client.                                                                                                                                                                                                                                    eureka                              :                                                                                                                                                                                                                                                                  client                              :                                                                                                                                                                                                                                                                  register-with-eureka                              :                                                                                          faux                                                                                                                                                                                                                                                                  fetch-registry                              :                                                                                          false                                                                                                                                                                

Finally, we need to declare our app as a registry service using the @EnableEurekaServer note:

                                                  1                                                    2                                                    iii                                                    4                                                    5                                                    6                                                    vii                                                    8                                                    9                                                                        
                                                                                                            @SpringBootApplication                                                                                                                                          @EnableEurekaServer                                                                                                                                          public                              class                              RegistryEurekaServerApplication                              {                                                                                                                                                                                                                                                      public                              static                              void                              master                              (                              String                              []                              args                              )                              {                                                                                                                                          SpringApplication                              .                              run                              (                              RegistryEurekaServerApplication                              .                              class                              ,                              args                              );                                                                                                                                          }                                                                                                                                                                                                                                                      }                                                                                                    

You can find a user interface generated past Eureka, for our example is on localhost:8099

eureka ui

Eureka client configuration

The Eureka customer require the Spring Cloud starter netflix eureka client dependency

                                                  i                                                    two                                                    three                                                    iv                                                                        
                                                                                                            <dependency>                                                                                                                                          <groupId>org.springframework.cloud</groupId>                                                                                                                                          <artifactId>jump-cloud-starter-netflix-eureka-customer</artifactId>                                                                                                                                          </dependency>                                                                                                    

Too for this one, nosotros need a prepare of properties:

                                                                              1                                                                                two                                                                                3                                                                                4                                                                                5                                                                                6                                                                                7                                                                                8                                                                                9                                                    ten                                                    11                                                    12                                                    13                                                    xiv                                                    15                                                    16                                                    17                                                    xviii                                                    xix                                                    20                                                    21                                                    22                                                    23                                                    24                                                    25                                                    26                                                    27                                                    28                                                                        
                                                                                                            # Setting a proper noun for out app                                                                                                                                                                                                                                    spring                              :                                                                                                                                                                                                                                                                  awarding                              :                                                                                                                                                                                                                                                                  name                              :                                                                                          registry-eureka-customer                                                                                                                                                                                                                                                                                                                                                                                                            eureka                              :                                                                                                                                                                                                                                                                  customer                              :                                                                                                                                                                                                                                                                  # To register to the eureka server                                                                                                                                                                                                                                                                  registerWithEureka                              :                                                                                          truthful                                                                                                                                                                                                                                                                                                                                                                                                                                          # To fetch the registry from the eureka server                                                                                                                                                                                                                                                                  fetchRegistry                              :                                                                                          truthful                                                                                                                                                                                                                                                                                                                                                                                                                                          # The location of the eureka server                                                                                                                                                                                                                                                                  serviceUrl                              :                                                                                                                                                                                                                                                                  defaultZone                              :                                                                                          http://localhost:8099/eureka/                                                                                                                                                                                                                                                                                                                                                                                                                                          instance                              :                                                                                                                                                                                                                                                                  # The case hostname                                                                                                                                                                                                                                                                  hostname                              :                                                                                          localhost                                                                                                                                                                                                                                                                                                                                                                                                                                          # The instance unique id                                                                                                                                                                                                                                                                  instanceId                              :                                                                                          ${eureka.instance.hostname}:${leap.application.name}                                                                                                                                                                                                                                                                                                                                                                                                                                          # A bench of health data finish-points provided by eureka                                                                                                                                                                                                                                                                  statusPageUrl                              :                                                                                          http://${eureka.hostname}/                                                                                                                                                                                                                                                                  healthCheckUrl                              :                                                                                          http://${eureka.hostname}/actuator/health                                                                                                                                                                                                                                                                  secureHealthCheckUrl                              :                                                                                          http://${eureka.hostname}/actuator/health                                                                                                                                                                

Finally, we demand to declare our app equally a discovery client (Eureka customer) using the @EnableDiscoveryClient annotation:

                                                  1                                                    2                                                    3                                                    4                                                    5                                                    vi                                                    7                                                    eight                                                                        
                                                                                                            @SpringBootApplication                                                                                                                                          @EnableDiscoveryClient                                                                                                                                          public                              class                              RegistryEurekaClientApplication                              {                                                                                                                                                                                                                                                      public                              static                              void                              primary                              (                              String                              []                              args                              )                              {                                                                                                                                          SpringApplication                              .                              run                              (                              RegistryEurekaClientApplication                              .                              class                              ,                              args                              );                                                                                                                                          }                                                                                                                                          }                                                                                                    

Once, we first our client app, we will observe that information technology successfully registered to the running eureka server.

eureka server

This may seem incomplete , actually information technology is, since we're non fetching the eureka server for registered service instances and exploit them. That's the main purpose of the next commodity when nosotros will be doing then using a reverse proxy called Netflix Zuul .

Reference https://elattar.me/posts/spring-cloud-introduction-to-service-discovery-netflix-eureka/