Spring RestTemplate can be used for client side load balancing.
Client side vs server side load balancing
The multiple instances of the same microservice is run on different computers for high reliability and availability.
Server side load balancing is distributing the incoming requests towards multiple instances of the service.
Client side load balancing is distributing the outgoing request from the client itself.
Spring client side load balancing
Spring Netflix Eureka has a built-in client side load balancer called Ribbon.
Ribbon can automatically be configured by registering RestTemplate as a bean and annotating it with @LoadBalanced .
In this example we will focus on how to access a microservice instance transparently using RestTemplate and @LoadBalance .
Example
Eureka Server Instance
src/main/resources/application.ymlserver:
port: 7777
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
@SpringBootApplication
@EnableEurekaServer
public class HelloEurekaServerMain {
public static void main(String[] args) {
SpringApplication.run(HelloEurekaServerMain.class, args);
}
}
Run above class from IDE.
Creating hello microservice
@RestController
public class HelloController {
private AtomicLong counter = new AtomicLong();
@GetMapping("/hello")
public HelloObject getHelloWordObject() {
HelloObject hello = new HelloObject();
hello.setMessage("Hi there! you are number " + counter.incrementAndGet());
return hello;
}
}
src/main/resources/application.propertieseureka.client.serviceUrl.defaultZone=http://localhost:7777/eureka/
src/main/resources/bootstrap.propertiesspring.application.name=hello-service
@SpringBootApplication
@EnableDiscoveryClient
public class HelloServiceMain{
public static void main(String[] args) {
SpringApplication.run(HelloServiceMain.class, args);
}
}
Run above main class from IDE
Creating hello web client service
src/main/resources/application.propertiesserver.port=9080
eureka.client.serviceUrl.defaultZone=http://localhost:7777/eureka/
src/main/resources/bootstrap.propertiesspring.application.name=hello-web-client-service
package com.logicbig.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
public class HelloWebClientServiceMain {
@LoadBalanced
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(HelloWebClientServiceMain.class, args);
}
}
@Controller
public class HelloWebClientController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/")
public String handleRequest(Model model) {
//accessing hello-service
HelloObject helloObject = restTemplate.getForObject("http://hello-service/hello", HelloObject.class);
model.addAttribute("msg", helloObject.getMessage());
model.addAttribute("time", LocalDateTime.now());
return "hello-page";
}
}
hello-web-client-service/src/main/resources/templates/hello-page.html<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<h2>Hello Page</h2>
<div th:text="${msg}"/>
<div>Time: <span th:text="${time}"/></div>
</body>
</html>
Run above main class from IDE.
Accessing Eureka at http://localhost:7777/
Accessing web client service at http://localhost:9080/
The next example shows how to run multiple instances of our hello-service and access them via RestTemplate load balancer.
Example ProjectDependencies and Technologies Used: - Spring Boot 2.0.4.RELEASE
Corresponding Spring Version 5.0.8.RELEASE - Spring Cloud Finchley.SR1
- spring-cloud-starter-netflix-eureka-server 2.0.1.RELEASE: Spring Cloud Starter Netflix Eureka Server.
- spring-boot-starter-web : Starter for building web, including RESTful, applications using Spring
MVC. Uses Tomcat as the default embedded container.
- spring-cloud-starter-netflix-eureka-client 2.0.1.RELEASE: Spring Cloud Starter Netflix Eureka Client.
- spring-boot-starter-thymeleaf : Starter for building MVC web applications using Thymeleaf views.
Uses org.thymeleaf:thymeleaf-spring5 version 3.0.9.RELEASE - JDK 1.8
- Maven 3.5.4
|