Check out the full series of Questions Management tutorial here.
I introduced you to client load balancing using Spring Cloud Ribbon with its benefits. To ensure High Availability for services in the Questions Management application, I will apply the client load balancing using Spring Cloud Ribbon to those services that call other services.
Services that call other services in the Questions Management application are services that are in the API Service and Composite Service. To apply Spring Cloud Ribbon, I will open the pom.xml file of each service project and add Spring Cloud Ribbon dependency as follows:
1 2 3 4 |
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> |
Next, we will modify the code to apply the client load balancing for the service project using the Ribbon.
We will modify the API Category Service first!
This service calls the Core Category Service in the CoreCategoryServiceImpl class so I will autowire the interface LoadBalancerClient into this class and then edit the getServiceUrl() method as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
... @Service public class CoreCategoryServiceImpl implements CoreCategoryService { private static final String SERVICE_NAME = "Core Category Service"; @Autowired private LoadBalancerClient loadBalancerClient; @Override public String getServiceUrl() { ServiceInstance serviceInstance = loadBalancerClient.choose(SERVICE_NAME); if (serviceInstance == null) { throw new RuntimeException(String.format("Couldn't get any instance of %s from Eureka Server! ", SERVICE_NAME)); } return serviceInstance.getUri().toString(); } ... } |
Because now we don’t use the coreCategoryServiceUrl variable anymore, I will also remove this variable and the “corecategoryservice.url” property in the application.properties file.
Same for the API Option Service
CompositeOptionServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
... @Service public class CompositeOptionServiceImpl implements CompositeOptionService { private static final String SERVICE_NAME = "Composite Option Service"; @Autowired private LoadBalancerClient loadBalancerClient; @Override public String getServiceUrl() { ServiceInstance serviceInstance = loadBalancerClient.choose(SERVICE_NAME); if (serviceInstance == null) { throw new RuntimeException(String.format("Couldn't get any instance of %s from Eureka Server! ", SERVICE_NAME)); } return serviceInstance.getUri().toString(); } ... } |
CoreOptionServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
... @Service public class CoreOptionServiceImpl implements CoreOptionService { private static final String SERVICE_NAME = "Core Option Service"; @Autowired private LoadBalancerClient loadBalancerClient; @Override public String getServiceUrl() { ServiceInstance serviceInstance = loadBalancerClient.choose(SERVICE_NAME); if (serviceInstance == null) { throw new RuntimeException(String.format("Couldn't get any instance of %s from Eureka Server! ", SERVICE_NAME)); } return serviceInstance.getUri().toString(); } ... } |
Same for API Question Service
CompositeQuestionServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
... @Service public class CompositeQuestionServiceImpl implements CompositeQuestionService { private static final String SERVICE_NAME = "Composite Question Service"; @Autowired private LoadBalancerClient loadBalancerClient; @Override public String getServiceUrl() { ServiceInstance serviceInstance = loadBalancerClient.choose(SERVICE_NAME); if (serviceInstance == null) { throw new RuntimeException(String.format("Couldn't get any instance of %s from Eureka Server! ", SERVICE_NAME)); } return serviceInstance.getUri().toString(); } ... } |
Same for Composite Option Service
CoreOptionServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
... @Service public class CoreOptionServiceImpl implements CoreOptionService { private static final String SERVICE_NAME = "Core Option Service"; @Autowired private LoadBalancerClient loadBalancerClient; @Override public String getServiceUrl() { ServiceInstance serviceInstance = loadBalancerClient.choose(SERVICE_NAME); if (serviceInstance == null) { throw new RuntimeException(String.format("Couldn't get any instance of %s from Eureka Server! ", SERVICE_NAME)); } return serviceInstance.getUri().toString(); } ... } |
CoreQuestionServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
... @Service public class CoreQuestionServiceImpl implements CoreQuestionService { private static final String SERVICE_NAME = "Core Question Service"; @Autowired private LoadBalancerClient loadBalancerClient; @Override public String getServiceUrl() { ServiceInstance serviceInstance = loadBalancerClient.choose(SERVICE_NAME); if (serviceInstance == null) { throw new RuntimeException(String.format("Couldn't get any instance of %s from Eureka Server! ", SERVICE_NAME)); } return serviceInstance.getUri().toString(); } ... } |
Same for Composite Question Service
CoreCategoryServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
... @Service public class CoreCategoryServiceImpl implements CoreCategoryService { private static final String SERVICE_NAME = "Core Category Service"; @Autowired private LoadBalancerClient loadBalancerClient; @Override public String getServiceUrl() { ServiceInstance serviceInstance = loadBalancerClient.choose(SERVICE_NAME); if (serviceInstance == null) { throw new RuntimeException(String.format("Couldn't get any instance of %s from Eureka Server! ", SERVICE_NAME)); } return serviceInstance.getUri().toString(); } ... } |
CoreOptionServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
... @Service public class CoreOptionServiceImpl implements CoreOptionService { private static final String SERVICE_NAME = "Core Option Service"; @Autowired private LoadBalancerClient loadBalancerClient; @Override public String getServiceUrl() { ServiceInstance serviceInstance = loadBalancerClient.choose(SERVICE_NAME); if (serviceInstance == null) { throw new RuntimeException(String.format("Couldn't get any instance of %s from Eureka Server! ", SERVICE_NAME)); } return serviceInstance.getUri().toString(); } ... } |
CoreQuestionServiceImpl:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
... @Service public class CoreQuestionServiceImpl implements CoreQuestionService { private static final String SERVICE_NAME = "Core Question Service"; @Autowired private LoadBalancerClient loadBalancerClient; @Override public String getServiceUrl() { ServiceInstance serviceInstance = loadBalancerClient.choose(SERVICE_NAME); if (serviceInstance == null) { throw new RuntimeException(String.format("Couldn't get any instance of %s from Eureka Server! ", SERVICE_NAME)); } return serviceInstance.getUri().toString(); } ... } |
Rerun the Questions Management application, you will see the features we have built are working normally.