Shop Resource          
Service Replication with Spring Eureka
Created on Nov 2019

This article demonstrates the use of Spring Eureka to implement service replication. Eureka Server provides service discovery that enables the service replication capability to the application architecture. With the use of service replication, when one server downs, another server with the same service can take over the down server to continue to serve the client request.


The application architecture must have two or more instances to enable service replication. These service servers must register themselves with the Eureka server. These service servers are the Eureka client.


The application architecture requires these components to create the service replication architecture:

  • • Eureka Server
  • • RESTFul Service Server
  • • Service Gateway

The diagram shows the project development structures for implementing these components, which consist of three projects:


In this article, the Eureka server, RESTFul Web Service server, and Service Gateway are all executed in a single host machine. For example, a laptop that uses Windows 8 operating system. The diagram shows the architecture that will be used by the application, which deploys one instance of Eureka server, two instances of RESTFul service servers, and one instance of service gateway:


In a production environment, these servers can be deployed into different server machines, or in cloud infrastructure, and they should be connected using a network component, for example, router.


The SpringCloudRestfulServiceServer project is the Eureka client. The SpringCloudEurekaServer project is the Eureka Server. The SpringCloudServiceGateway project is the service gateway. The responsibility of the service gateway is to receive the service request from the client and routes the service request to the RESTFul web service server instance. The service client can be a user that uses the Internet browser to send the HTTP request:



Eureka Server

To execute the replication service in this article, first, execute the SpringCloudEurekaServer project. The server will use port number 8070. This configuration is provided in application.yml:

server:
   port: 8070

spring:
   application:
      name: EurekaServer

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false  
    

server:port: The Server port number. Eureka server uses 8070. If the port number is not specified, the default port number is 8761.


spring:application:name: Application name.


eureka:client:registerWithEureka: FALSE means the server will not register with the Eureka server. Eureka server uses false value because the Eureka server should not register itself. Only Eureka's client is required to register.


eureka:client:fetchRegistry: FALSE means the server will not retrieve the instance name list from the Eureka server. The Eureka server itself should not retrieve the list.


To create a Eureka server instance, uses @EnableEurekaServer annotation on the startup program of SpringBoot application. EurekaServerApplication.java is the startup program:

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
   
public static void main(String[] args) {
       
SpringApplication.run(EurekaServerApplication.class, args);
   
}
}


Starts the Eureka server, then uses the Internet browser and navigate to http://localhost:8070. The browser will show the Eureka server Web UI:



RESTFul WebService Server

After that, executes the SpringCloudRestfulServiceServer project as Spring Boot App about two times to produce two service instances. When the service instances start, they will automatically register themselves to the Eureka server. The configuration of the instance is provided in application.yml, in SpringCloudRestfulServiceServer project:

server:
  port: ${PORT:0}
  
spring:
  application:
    name: restfulserver

eureka:
  client:
    registerWithEureka: true
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:8070/eureka/
  instance:
    instance-id: ${spring.application.name}${random.value}

server:port: The configuration uses Spring expression to defines its port number: ${PORT:0}. The use of this expression enables RESTFul WebService Server to choose a random port number that is free during runtime.


eureka:client:registerWithEureka: RESTFulServiceServer uses TRUE value because the RESTFulServiceServer is the Eureka client. Eureka client should register itself to Eureka server.


eureka:client:fetchRegistry: FALSE means the RESTFulServiceServer will not retrieve the instance name list from the Eureka server. The eureka registry contains all the instance names that the Eureka server registers.


eureka:client:serviceUrl:defaultZone: This URL is used to connect to the Eureka Server. The connection enables Eureka client to register itself with the Eureka server. Eureka client is also required to send a signal to the Eureka server in 30 seconds interval. The signal indicates that the Eureka client is active. If the Eureka server receives no signal from the Eureka client after a certain period, the Eureka server will remove the inactive Eureka client from the registry. This is because no signal means the Eureka client is not available for service. However, by default, Eureka server needs five minutes or longer time to detect the inactiveness of the Eureka client.


eureka:instance:instance-id: This configuration creates the unique instance id for the Eureka client. The configuration uses Spring expression. The result of the expression is the combination of string that combines the value of spring:application:name and a random string value.


To create the Eureka client, uses @EnableEurekaClient annotation on the startup program of Spring boot application. In SpringCloudRestfulServiceServer project, the startup program is the RestfulServiceServer.java:

@EnableEurekaClient
@SpringBootApplication
public class RestfulServiceServer {
   
public static void main(String[] args) {
       
SpringApplication.run(RestfulServiceServer.class, args);
   
}
}

After RESTfulServiceServer are started, uses Internet browser and navigate to http://localhost:8070. The "Instance currently registered with Eureka" section lists the registered Eureka client. Observed that there are two instances with up status, and the instance name appended with a random string. This random string is generated via Spring expression via eureka:instance:instance-id:



RESTFul Gateway

The gateway is the entry point, which invokes the RESTFulServiceServer endpoint that is implemented in SpringCloudRestfulServiceServer project. To execute the gateway, execute the SpringCloudServiceGateway project. This project uses another configuration, which is provided in application.yml:

server:
  port: 8080

spring:
  application:
     name: restfulclient

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8070/eureka/

server:port: Service Gateway uses port 8080. In this article, the SpringCloudServiceGateway uses RESTFul WebService to implement the gateway.


eureka:client:registerWithEureka: Service Gateway uses FALSE value because the gateway choose not to register itself to Eureka server.


eureka:client:fetchRegistry: TRUE means the Service Gateway will retrieve the instance name list from the Eureka server.


eureka:client:serviceUrl:defaultZone: This URL is used to connnect to the Eureka Server. The URL is used to retrieve the eureka registry.


Service gateway adds the load balancing capability to the application infrastructure. There are two instances of SpringCloudRestfulServiceServer in this configuration. When the service gateway receives the service request, the first request will go to the first instance, the second request will go to the second instance, then the third request will go to the first instance in turn, and so on. Service gateway uses round robin method to invoke the RESTFul WebService in SpringCloudRestfulServiceServer project. To enable the load balancing with round-robin method, SpringCloudServiceGateway project creates this configuration class, LoadBalanceConfig.java:

@Configuration
public class LoadBalanceConfig {
   
@LoadBalanced
    @Bean
   
public RestTemplate restTemplate() {
       
return new RestTemplate();
   
}
}

@LoadBalanced annotation enables load balancing with round robin method for the RestTemplate. RestTemplate is the springboot's RESTFul client API. The client API is specifically used to invoke RESTFul WebService programmatically. However, RestTemplate is going to be deprecated. RestTemplate will be replaced by WebClient.


To use RestTemplate to invoke RESTFul WebService, the bean of RestTemplate is autowired in the RestfulClientService.java (SpringCloudServiceGateway project):

@RestController
@RequestMapping
("/restfulgateway")
public class RestfulGateway {
   
@Autowired
   
private RestTemplate restTemplate;

   
@GetMapping
   
public String restTemplate() {
       
String url = "http://restfulserver/service";
       
return restTemplate.getForObject(url, String.class);
   
}
}

getForObject() method sends the HTTP GET request to the RESTFul WebService. The first parameter specifies the web service URL, and the second parameter specifies the data type of response. Because the load balancing is enabled, the method will retrieve the IP address of service instance from the eureka registry, and then send the HTTP GET request to the service instance. The load balancing routes the request to that two service instances in a round-robin manner.


Observed that the URL used is http://restfulserver/service. restfulserver is the value obtained from spring:application:name in application.yml, in SpringCloudRestfulServiceServer project. "/service" is the RESTFul web service created in RestfulService.java, in SpringCloudRestfulServiceServer project.



Sending request to Service Gateway

At this stage, the Eureka Server, two instances of RESTFulServiceServer, and the Service gateway are in started state. Uses Internet browser and send a request to http://localhost:8080/restfulgateway. The browser should show a response with a port number:


Try to refresh the browser, the browser should show the same response message, and the port number will change.


The different port number proves that the service gateway has routed the request to service endpoint provided by the two instances of RESTFulServiceServer in the round-robin method. The load-balanced RestTemplate of the service gateway enables the service routing.




Source code

SpringCloudEureka.zip
File size: 12.7 KB (13,026 bytes)
MD5 checksum: 4FB899145409AEB35C4837D3836387C7
Development environment: Java11, Spring Tool Suite 4, SpringBoot2.1.8, SpringCloud Greenwich.RELEASE






Copyright© 2019 by MillionStrengthKnowledge.com

Legal Agreement