Trong bài viết trước, mình đã hướng dẫn các bạn cách đăng ký một service vào Consul sử dụng Consul API. Trong bài viết này, chúng ta sẽ cùng nhau tìm hiểu làm thế nào để connect tới Consul. lấy thông tin của các service đã đăng ký với Consul và gọi tới chúng, sử dụng Spring Cloud Consul các bạn nhé!
Đầu tiên, mình sẽ tạo mới một Spring Boot project với Spring Cloud Consul Service Discovery như sau:
Với cơ chế auto-configuration của Spring Cloud Consul Service Discovery, các bạn chỉ cần khai báo thông tin của Consul server trong tập tin cấu hình của Spring Boot:
1 2 3 4 5 |
spring: cloud: consul: port: '8500' host: localhost |
và khai báo annotation @EnableDiscoveryClient trong class main của ứng dụng Spring Boot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.huongdanjava.springcloud.consul; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class SpringCloudConsulServiceDiscoveryApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudConsulServiceDiscoveryApplication.class, args); } } |
là ứng dụng của chúng ta đã có thể connect tới Consul server và discover các service đã đăng ký bên trong Consul server này rồi.
Để lấy thông tin của các registered service trong Consul, các bạn có thể sử dụng bean của class DiscoveryClient, ví dụ như sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.huongdanjava.springcloud.consul; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Bean; @SpringBootApplication @EnableDiscoveryClient public class SpringCloudConsulServiceDiscoveryApplication { @Autowired private DiscoveryClient discoveryClient; public static void main(String[] args) { SpringApplication.run(SpringCloudConsulServiceDiscoveryApplication.class, args); } @Bean CommandLineRunner commandLineRunner() { return args -> { System.out.println(discoveryClient.getServices()); }; } } |
Chạy ứng dụng ví dụ này, các bạn sẽ thấy kết quả như mình như sau:
Này là do, Consul server của mình đang đăng ký 2 service này đó các bạn:
huongdanjava-service là service mà mình đã đăng ký trong bài viết Đăng ký service với Consul sử dụng Consul API đó các bạn!
Để lấy thông tin của một service và call tới nó, các bạn có thể sử dụng phương thức getInstances() của class DiscoveryClient. Tham số của phương thức getInstances() này là tên của service các bạn nhé! Thông tin trả về sẽ là danh sách các instance tương ứng của service này.
Ví dụ của mình như sau:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
package com.huongdanjava.springcloud.consul; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.context.annotation.Bean; @SpringBootApplication @EnableDiscoveryClient public class SpringCloudConsulServiceDiscoveryApplication { @Autowired private DiscoveryClient discoveryClient; public static void main(String[] args) { SpringApplication.run(SpringCloudConsulServiceDiscoveryApplication.class, args); } @Bean CommandLineRunner commandLineRunner() { return args -> { List<ServiceInstance> instances = discoveryClient.getInstances("huongdanjava-service"); System.out.println(instances.getFirst().getUri()); }; } } |
Kết quả:
Như vậy là chúng ta đã lấy được thông tin của các service được đăng ký trong Consul server rồi đó các bạn!