Consul and Spring Boot

In this lesson, we'll discuss Consul and Spring boot.

Introduction #

The Consul integration in Spring Boot is comparable to the integration of Eureka (see Eureka: Service Discovery).

There is a configuration file application.properties. Here is the relevant section:

spring.application.name=catalog
spring.cloud.consul.host=consul
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.preferIpAddress=true
spring.cloud.consul.discovery.instanceId=\${spring.application.name}:
\${spring.application.instance_id:\${random.value}}

The section configures the following values:

  • spring.application.name defines the name under which the application is registered in Consul.

  • spring.cloud.consul.host and spring.cloud.consul.port determine at which port and on which host Consul can be accessed.

  • Via spring.cloud.consul.discovery.preferIpAddress the services register with their IP address and not with the host name. This circumvents problems that arise because host names cannot be resolved in the Docker environment.

  • spring.cloud.consul.discovery.instanceId assigns an unambiguous ID to each microservice instance for discriminating between instances for load balancing.

Get hands-on with 1200+ tech skills courses.