Service Replication with Spring Eureka

Article 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 can be connected using a network component, for example, router.

The project is the Eureka client. The project is the Eureka Server. The 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 project. The server will use port number 8070. This configuration is provided in :

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: The name for the application. Because this is the Eureka server, so the name uses “EurekaServer”.

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 annotation on the startup program of SpringBoot application. 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 navigates to http://localhost:8070. The browser will show the Eureka server Web UI:

RESTFul WebService Server

After that, executes the 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 , in 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:. The use of this expression enables RESTFul WebService Server to choose a random port number that is free during runtime.

eureka:client:registerWithEureka: uses TRUE value because the 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 and a random string value.

To create the Eureka client, uses annotation on the startup program of Spring boot application. In project, the startup program is the

 @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 :

RESTFul Gateway

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

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 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: The Service Gateway uses this UR 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 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 project. To enable the load balancing with round-robin method, SpringCloudServiceGateway project creates this configuration class, :

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

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

To use to invoke RESTFul WebService, the bean of is autowired in the ( 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 in , 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. is the value obtained from is in of project. “/service” is the RESTFul web service created in of project.

Sending request to Service Gateway

At this stage, the Eureka Server, two instances of , 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 in the round-robin method. The load-balanced RestTemplate of the service gateway enables the service routing.

Source code

Github: https://github.com/MillionStrengthKnowledge/springcloud
Development environment: Java11, Spring Tool Suite 4, SpringBoot2.1.8, SpringCloud Greenwich.RELEASE

I am very interested in Java, web app and mobile app development. I enjoy doing R&D in Java during my spare time

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store