Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Microservices Flexible Software Architecture
Source

Eureka Client

For a Spring Boot application to be able to register with a Eureka server and to find other microservices, the application has to be annotated with @Enable DiscoveryClient or @EnableEurekaClient. In addition, a dependency from spring-cloud-starter-eureka has to be included in the file pom.xml. The application registers automatically with the Eureka server and can access other microservices. The example application accesses other microservices via a load balancer. This is described in detail in section 13.11.

Configuration

Configuring the application is necessary to define, for instance, the Eureka server to be used. The file application.properties (see Listing 13.6) is used for that. Spring Boot reads it out automatically in order to configure the application. This mechanism can also be used to configure one’s own code. In the example application the values serve to configure the Eureka client:

  • • The first line defines the Eureka server. The example application uses the Docker link, which provides the Eureka server under the host name “eureka.”
  • • leaseRenewallntervallnSeconds determines how often data is updated between client and server. As the data has to be held locally in a cache on each client, a new service first needs to create its own cache and replicate it onto the server. Afterwards the data is replicated onto the clients. Within a test environment it is important to track system changes rapidly so that the example application uses five seconds instead of the preset value of 30 seconds. In production with many clients, this value should be increased. Otherwise the updates of information will use a lot of resources, even though the information remains essentially unchanged.
  • • spring.application.name serves as the name for the service during the registration at Eureka. During registration the name is converted into capital letters. This service would thus be known by Eureka under the name “CUSTOMER.”
  • • There can be several instances of each service to achieve fail over and load balancing. The instanceld has to be unique for each instance of a service. Because of that it contains a random number, which ensures unambiguousness.
  • • preferlpAddress makes sure that microservices register with their IP addresses and not with their host names. Unfortunately in a Docker environment host names are not easily resolvable by other hosts. This problem is circumvented by the use of IP addresses.

Listing 13.6 Part of application.properties with Eureka Configuration

eureka.client.serviceUrl.defaultZone=http://eureka:8761/eureka/

eureka.instance.leaseRenewalIntervalInSeconds=5

spring.application.name=catalog

eureka.instance.metadataMap.instanceId=catalog:${random.value}

eureka.instance.preferIpAddress=true

Eureka Server

The Eureka server (Listing 13.7) is a simple Spring Boot application, which turns into a Eureka server via the @EnableEurekaServer annotation. In addition, the server needs a dependency on spring-cloud-starter-eureka-server.

Listing 13.7 Eureka Server

@EnableEurekaServer

@EnableAutoConfiguration

public class EurekaApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaApplication.class, args);

}

}

The Eureka server offers a dashboard that shows the registered services. In the example application, this can be found at http://localhost:18761/ (Vagrant) or on Docker host under port 8761 (Docker Compose). Figure 13.7 shows a screenshot of

Eureka Dashboard

Figure 13.7 Eureka Dashboard

the Eureka dashboards for the example application. The three microservices and the Zuul-Proxy, which is discussed in the next section, are present on the dashboard.

 
Source
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Mathematics
Political science
Philosophy
Psychology
Religion
Sociology
Travel