In the previous tutorial, I showed you how to register a service to Consul using the Consul API. In this tutorial, we will learn how to connect to Consul to get information about services registered with Consul and call them, using Spring Cloud Consul!
First, I will create a new Spring Boot project with Spring Cloud Consul Service Discovery as follows:
With the auto-configuration mechanism of Spring Cloud Consul Service Discovery, you just need to declare the information of the Consul server in the configuration file of Spring Boot:
1 2 3 4 5 |
spring: cloud: consul: port: '8500' host: localhost |
and declare the @EnableDiscoveryClient annotation in the main class of the Spring Boot application:
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); } } |
our application can connect to the Consul server and discover registered services inside this Consul server.
To get information about registered services in Consul, you can use the bean of the DiscoveryClient class, for example, as follows:
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()); }; } } |
Run this example application, you will see the result as follows:
This is because my Consul server is registering these 2 services:
huongdanjava-service is the service that I registered in the tutorial Register service with Consul using Consul API!
To get information about a service and call it, you can use the getInstances() method of the DiscoveryClient class. The parameter of this getInstances() method is the name of the service! The returned information will be a list of corresponding instances of this service.
My example is as follows:
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()); }; } } |
Result:
So we have got the information about the services registered in the Consul server.