The health check for a consul instance defaults to "/", which is the default location for useful endpoints in the Spring boot executor application. If you use a non-default context path or a servlet path (for example, Server.servletpath=/foo) or manage the endpoint path (for example, management.context-path=/admin), you need to change these, even for the executor application. You can also configure the interval that Consul uses to check the health endpoint. "10s" and "1m" represent 10 seconds and 1 minutes, respectively. Cases:
Application.yml
spring: cloud: consul: discovery: healthCheckPath: ${management.context-path}/health healthCheckInterval: 15s
Meta data and Consul tags
Consul service metadata has not been supported. Spring Cloud's serviceinstance has a map metadata field. Spring Cloud Consul uses consul tags to approximate metadata until consul formally supports metadata. Labels using the Key=value form are split and used as map keys and values, respectively. The label does not have the same = symbol and will be used as both the key and the value.
spring: cloud: consul: discovery: tags: foo=bar, baz
The above configuration will result in mappings with Foo→bar and Baz→baz.
Make Consul Instance ID unique
By default, a consular entity registers an ID equal to its spring application context ID. By default, the spring application context ID is ${spring.application.name}:comma,separated,profiles:${server.port}. In most cases, this will allow multiple instances of a service to run on a single machine. If you need further uniqueness, using Spring Cloud, you can override this by providing a unique identity in spring.cloud.consul.discovery.instanceId. For example:
spring: cloud: consul: discovery: instanceId: ${spring.application.name}:${vcap.application.instance_id:${spring.application.instance_id:${random.value}}}
With this metadata and multiple service instances deployed on localhost, the random values will be there to make the instance unique. In Cloudfoundry, vcap.application.instance_id is automatically populated in the spring boot application, so no random values are required.
Using Discoveryclient
Spring Cloud supports feign (rest client builder), and spring Resttemplate uses the logical service name instead of the physical URL.
You can also use Org.springframework.cloud.client.discovery.DiscoveryClient, which provides a simple API for Netflix-specific discovery clients, such as
@Autowiredprivate DiscoveryClient discoveryClient;public String serviceUrl() { List list = discoveryClient.getInstances("STORES"); if (list != null && list.size() > 0 ) { return list.get(0).getUri(); } return null;}
Spring Cloud consul-http Health Check