Xem toàn bộ series bài viết hướng dẫn xây dựng ứng dụng Questions Management tại đây.
Mình đã giới thiệu với bạn về client load balancing sử dụng Spring Cloud Ribbon với những lợi ích của nó. Để đảm bảo tính High Availability cho các service trong ứng dụng Questions Management, mình sẽ apply client load balancing sử dụng Spring Cloud Ribbon này vào những service có gọi tới các service khác.
Các service có gọi tới những service khác trong ứng dụng Questions Management là các service nằm trong phần API Service và Composite Service. Để apply Spring Cloud Ribbon, mình sẽ mở tập tin pom.xml của mỗi project service và thêm Spring Cloud Ribbon như sau:
1 2 3 4 |
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> |
Tiếp theo chúng ta sẽ sửa code để apply client load balancing cho các project service có sử dụng Ribbon.
Chúng ta sẽ sửa API Category Service trước các bạn nhé!
Service này gọi đến Core Category Service trong class CoreCategoryServiceImpl nên mình sẽ autowire interface LoadBalancerClient vào class này rồi sửa lại phương thức getServiceUrl() lại như sau:
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(); } ... } |
Vì bây giờ chúng ta không sử dụng biến coreCategoryServiceUrl nữa, nên mình cũng sẽ remove biến này và property “corecategoryservice.url” trong tập tin application.properties đi.
Tương tự cho 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(); } ... } |
Tương tự cho 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(); } ... } |
Tương tự cho 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(); } ... } |
Tương tự cho 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(); } ... } |
Chạy lại ứng dụng Questions Management, các bạn sẽ thấy các tính năng chúng ta đã xây dựng đều hoạt động bình thường.