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:
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
To execute the replication service in this article, first, execute the
server: port: 8070 spring: application: name: EurekaServer eureka: client: registerWithEureka: false fetchRegistry: false
server:port: The Server port number. Eureka server uses
spring:application:name: Application name.
eureka:client:registerWithEureka:
eureka:client:fetchRegistry:
To create a Eureka server instance, uses
@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
After that, executes the
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:
eureka:client:registerWithEureka:
eureka:client:fetchRegistry:
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
To create the Eureka client, uses
@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
The gateway is the entry point, which invokes the
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
eureka:client:registerWithEureka: Service Gateway uses
eureka:client:fetchRegistry:
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
@Configuration
public class LoadBalanceConfig {
@LoadBalanced
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
To use
@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
At this stage, the Eureka Server, two instances of
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
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