Consul Server Discovery Service

HashiCorp Consul has multiple components, but as a whole, it is a tool for discovering and configuring services in your infrastructure. It provides key features:

  • Service Discovery: Clients of Consul can provide a service, such as api or mysql, and other clients can use Consul to discover providers of a given service. Using either DNS or HTTP, applications can easily find the services they depend upon.

  • Health Checking: Consul clients can provide any number of health checks, either associated with a given service (“is the webserver returning 200 OK”), or with the local node (“is memory utilization below 90%”). This information can be used by an operator to monitor cluster health, and it is used by the service discovery components to route traffic away from unhealthy hosts.

  • KV Store: Applications can make use of Consul’s hierarchical key/value store for any number of purposes, including dynamic configuration, feature flagging, coordination, leader election, and more. The simple HTTP API makes it easy to use.

  • Multi Datacenter: Consul supports multiple datacenters out of the box. This means users of Consul do not have to worry about building additional layers of abstraction to grow to multiple regions.

CAS provides a Consul-enabled service discovery server that is based on Spring Cloud Consul and bootstrapped via Spring Cloud.

Installation

  • To run the Consul discovery server, please see this guide for installation instructions. A simple Consul installation may be run as consul agent -dev -ui
  • Look for a suitable and relevant ready-made Docker image via docker search consul.

When deployed and assuming default settings, the Consul dashboard would be available at: http://localhost:8500/ui.

Note that a Consul Agent client must be available to all CAS server nodes. By default, the agent client is expected to be at localhost:8500.

CAS Discovery Service Clients

Each individual CAS server is given the ability to auto-register itself with the discovery server, provided configuration is made available to instruct the CAS server how to locate and connect to the discover server service.

Support is added by including the following dependency in the WAR overlay:

1
2
3
4
5
<dependency>
    <groupId>org.apereo.cas</groupId>
    <artifactId>cas-server-support-consul-client</artifactId>
    <version>${cas.version}</version>
</dependency>
1
implementation "org.apereo.cas:cas-server-support-consul-client:${project.'cas.version'}"
1
2
3
4
5
6
7
8
9
dependencyManagement {
    imports {
        mavenBom "org.apereo.cas:cas-server-support-bom:${project.'cas.version'}"
    }
}

dependencies {
    implementation "org.apereo.cas:cas-server-support-consul-client"
}
1
2
3
4
5
6
7
8
9
10
dependencies {
    /*
        The following platform references are included automatically and are listed for reference only.

        implementation enforcedPlatform("org.apereo.cas:cas-server-support-bom:${project.'cas.version'}")
        implementation platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES)
        
    */
    implementation "org.apereo.cas:cas-server-support-consul-client"
}

The following settings and properties are available from the CAS configuration catalog:

spring.cloud.config.discovery.enabledFlag to indicate that config server discovery is enabled (config server URL will be looked up via discovery).
false
Third party

Flag to indicate that config server discovery is enabled (config server URL will be looked up via discovery).

Type
Boolean
Default
false
Defined by
ConfigClientProperties$Discovery
spring.cloud.config.discovery.service-idService id to locate config server.
configserver
Third party

Service id to locate config server.

Type
String
Default
configserver
Defined by
ConfigClientProperties$Discovery
spring.cloud.consul.config.acl-token
no default
Third party
Type
String
Default
none
Defined by
ConsulConfigProperties
spring.cloud.consul.config.data-keyIf format is Format.PROPERTIES or Format.YAML then the following field is used as key to look up consul for configuration.
data
Third party

If format is Format.PROPERTIES or Format.YAML then the following field is used as key to look up consul for configuration.

Type
String
Default
data
Defined by
ConsulConfigProperties
spring.cloud.consul.config.default-context
application
Third party
Type
String
Default
application
Defined by
ConsulConfigProperties
spring.cloud.consul.config.enabled
true
Third party
Type
Boolean
Default
true
Defined by
ConsulConfigProperties
spring.cloud.consul.config.fail-fastThrow exceptions during config lookup if true, otherwise, log warnings.
true
Third party

Throw exceptions during config lookup if true, otherwise, log warnings.

Type
Boolean
Default
true
Defined by
ConsulConfigProperties
spring.cloud.consul.config.format
key-value
Third party
Type
ConsulConfigProperties.Format
Default
key-value
Defined by
ConsulConfigProperties
spring.cloud.consul.config.nameAlternative to spring.application.name to use in looking up values in consul KV.
no default
Third party

Alternative to spring.application.name to use in looking up values in consul KV.

Type
String
Default
none
Defined by
ConsulConfigProperties
spring.cloud.consul.config.prefix
no default
Third partyDeprecated
Type
String
Default
none
Defined by
ConsulConfigProperties
Deprecation
WARNING, replaced by spring.cloud.consul.config.prefixes
spring.cloud.consul.config.prefixes
no default
Third party
Type
List<String>
Default
none
Defined by
ConsulConfigProperties
spring.cloud.consul.config.profile-separator
,
Third party
Type
String
Default
,
Defined by
ConsulConfigProperties
spring.cloud.consul.config.watch.delayThe value of the fixed delay for the watch in millis.
1000
Third party

The value of the fixed delay for the watch in millis. Defaults to 1000.

Type
Integer
Default
1000
Defined by
ConsulConfigProperties$Watch
spring.cloud.consul.config.watch.enabledIf the watch is enabled.
true
Third party

If the watch is enabled. Defaults to true.

Type
Boolean
Default
true
Defined by
ConsulConfigProperties$Watch
spring.cloud.consul.config.watch.wait-timeThe number of seconds to wait (or block) for watch query, defaults to 55.
55
Third party

The number of seconds to wait (or block) for watch query, defaults to 55. Needs to be less than default ConsulClient (defaults to 60). To increase ConsulClient timeout create a ConsulClient bean with a custom ConsulRawClient with a custom HttpClient.

Type
Integer
Default
55
Defined by
ConsulConfigProperties$Watch
spring.cloud.consul.discovery.acl-token
no default
Third party
Type
String
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.catalog-services-watch-delayThe delay between calls to watch consul catalog in millis, default is 1000.
1000
Third party

The delay between calls to watch consul catalog in millis, default is 1000.

Type
Integer
Default
1000
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.catalog-services-watch-timeoutThe number of seconds to block while watching consul catalog, default is 2.
2
Third party

The number of seconds to block while watching consul catalog, default is 2.

Type
Integer
Default
2
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.consistency-modeConsistency mode for health service request.
default
Third party

Consistency mode for health service request.

Type
ConsulDiscoveryProperties.ConsistencyMode
Default
default
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.datacentersMap of serviceId's -> datacenter to query for in server list.
no default
Third party

Map of serviceId's -> datacenter to query for in server list. This allows looking up services in another datacenters.

Type
Map<String,String>
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.default-query-tagTag to query for in service list if one is not listed in serverListQueryTags.
no default
Third party

Tag to query for in service list if one is not listed in serverListQueryTags. Multiple tags can be specified with a comma separated value.

Type
String
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.default-zone-metadata-nameService instance zone comes from metadata.
zone
Third party

Service instance zone comes from metadata. This allows changing the metadata tag name.

Type
String
Default
zone
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.deregisterDisable automatic de-registration of service in consul.
true
Third party

Disable automatic de-registration of service in consul.

Type
Boolean
Default
true
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.enable-tag-overrideEnable tag override for the registered service.
no default
Third party

Enable tag override for the registered service.

Type
Boolean
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.enabledIs service discovery enabled?
true
Third party

Is service discovery enabled?

Type
Boolean
Default
true
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.fail-fastThrow exceptions during service registration if true, otherwise, log warnings (defaults to true).
true
Third party

Throw exceptions during service registration if true, otherwise, log warnings (defaults to true).

Type
Boolean
Default
true
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.health-check-critical-timeoutTimeout to deregister services critical for longer than timeout (e.g. 30m).
no default
Third party

Timeout to deregister services critical for longer than timeout (e.g. 30m). Requires consul version 7.x or higher.

Type
String
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.health-check-headersHeaders to be applied to the Health Check calls.
no default
Third party

Headers to be applied to the Health Check calls.

Type
Map<String,List<String>>
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.health-check-intervalHow often to perform the health check (e.g. 10s), defaults to 10s.
10s
Third party

How often to perform the health check (e.g. 10s), defaults to 10s.

Type
String
Default
10s
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.health-check-pathAlternate server path to invoke for health checking.
/actuator/health
Third party

Alternate server path to invoke for health checking.

Type
String
Default
/actuator/health
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.health-check-timeoutTimeout for health check (e.g. 10s).
no default
Third party

Timeout for health check (e.g. 10s).

Type
String
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.health-check-tls-skip-verifySkips certificate verification during service checks if true, otherwise runs certificate verification.
no default
Third party

Skips certificate verification during service checks if true, otherwise runs certificate verification.

Type
Boolean
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.health-check-urlCustom health check url to override default.
no default
Third party

Custom health check url to override default.

Type
String
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.heartbeat.actuator-health-groupThe actuator health group to use (null for the root group) when determining system health via Actuator.
no default
Third party

The actuator health group to use (null for the root group) when determining system health via Actuator.

Type
String
Default
none
Defined by
HeartbeatProperties
spring.cloud.consul.discovery.heartbeat.enabled
false
Third party
Type
Boolean
Default
false
Defined by
HeartbeatProperties
spring.cloud.consul.discovery.heartbeat.interval-ratio
no default
Third party
Type
Double
Default
none
Defined by
HeartbeatProperties
spring.cloud.consul.discovery.heartbeat.reregister-service-on-failure
false
Third party
Type
Boolean
Default
false
Defined by
HeartbeatProperties
spring.cloud.consul.discovery.heartbeat.ttl
30s
Third party
Type
Duration
Default
30s
Defined by
HeartbeatProperties
spring.cloud.consul.discovery.heartbeat.use-actuator-healthWhether or not to take the current system health (as reported via the Actuator Health endpoint) into account when reporting the application status to the Consul TTL check.
true
Third party

Whether or not to take the current system health (as reported via the Actuator Health endpoint) into account when reporting the application status to the Consul TTL check. Actuator Health endpoint also has to be available to the application.

Type
Boolean
Default
true
Defined by
HeartbeatProperties
spring.cloud.consul.discovery.hostnameHostname to use when accessing server.
no default
Third party

Hostname to use when accessing server.

Type
String
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.include-hostname-in-instance-idWhether hostname is included into the default instance id when registering service.
false
Third party

Whether hostname is included into the default instance id when registering service.

Type
Boolean
Default
false
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.instance-groupService instance group.
no default
Third party

Service instance group.

Type
String
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.instance-idUnique service instance id.
no default
Third party

Unique service instance id.

Type
String
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.instance-zoneService instance zone.
no default
Third party

Service instance zone.

Type
String
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.ip-addressIP address to use when accessing service (must also set preferIpAddress to use).
no default
Third party

IP address to use when accessing service (must also set preferIpAddress to use).

Type
String
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.lifecycle.enabled
true
Third party
Type
Boolean
Default
true
Defined by
ConsulDiscoveryProperties$Lifecycle
spring.cloud.consul.discovery.management-enable-tag-overrideEnable tag override for the registered management service.
no default
Third party

Enable tag override for the registered management service.

Type
Boolean
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.management-metadataMetadata to use when registering management service.
no default
Third party

Metadata to use when registering management service.

Type
Map<String,String>
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.management-portPort to register the management service under (defaults to management port).
no default
Third party

Port to register the management service under (defaults to management port).

Type
Integer
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.management-suffixSuffix to use when registering management service.
management
Third party

Suffix to use when registering management service.

Type
String
Default
management
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.management-tagsTags to use when registering management service.
no default
Third party

Tags to use when registering management service.

Type
List<String>
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.metadataMetadata to use when registering service.
no default
Third party

Metadata to use when registering service.

Type
Map<String,String>
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.orderOrder of the discovery client used by `CompositeDiscoveryClient` for sorting available clients.
0
Third party

Order of the discovery client used by `CompositeDiscoveryClient` for sorting available clients.

Type
Integer
Default
0
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.portPort to register the service under (defaults to listening port).
no default
Third party

Port to register the service under (defaults to listening port).

Type
Integer
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.prefer-agent-addressSource of how we will determine the address to use.
false
Third party

Source of how we will determine the address to use.

Type
Boolean
Default
false
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.prefer-ip-addressUse ip address rather than hostname during registration.
false
Third party

Use ip address rather than hostname during registration.

Type
Boolean
Default
false
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.query-passingAdd the 'passing` parameter to /v1/health/service/serviceName.
false
Third party

Add the 'passing` parameter to /v1/health/service/serviceName. This pushes health check passing to the server.

Type
Boolean
Default
false
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.registerRegister as a service in consul.
true
Third party

Register as a service in consul.

Type
Boolean
Default
true
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.register-health-checkRegister health check in consul.
true
Third party

Register health check in consul. Useful during development of a service.

Type
Boolean
Default
true
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.schemeWhether to register an http or https service.
http
Third party

Whether to register an http or https service.

Type
String
Default
http
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.server-list-query-tagsMap of serviceId's -> tag to query for in server list.
no default
Third party

Map of serviceId's -> tag to query for in server list. This allows filtering services by one more tags. Multiple tags can be specified with a comma separated value.

Type
Map<String,String>
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.service-nameService name.
no default
Third party

Service name.

Type
String
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.discovery.tagsTags to use when registering service.
no default
Third party

Tags to use when registering service.

Type
List<String>
Default
none
Defined by
ConsulDiscoveryProperties
spring.cloud.consul.enabledIs spring cloud consul enabled.
true
Third party

Is spring cloud consul enabled.

Type
Boolean
Default
true
Defined by
ConsulProperties
spring.cloud.consul.hostConsul agent hostname.
localhost
Third party

Consul agent hostname. Defaults to 'localhost'.

Type
String
Default
localhost
Defined by
ConsulProperties
spring.cloud.consul.pathCustom path if consul is under non-root.
no default
Third party

Custom path if consul is under non-root.

Type
String
Default
none
Defined by
ConsulProperties
spring.cloud.consul.portConsul agent port.
8500
Third party

Consul agent port. Defaults to '8500'.

Type
Integer
Default
8500
Defined by
ConsulProperties
spring.cloud.consul.retry.enabledIf consul retry is enabled.
true
Third party

If consul retry is enabled.

Type
Boolean
Default
true
Defined by
RetryProperties
spring.cloud.consul.retry.initial-intervalInitial retry interval in milliseconds.
1000
Third party

Initial retry interval in milliseconds.

Type
Long
Default
1000
Defined by
RetryProperties
spring.cloud.consul.retry.max-attemptsMaximum number of attempts.
6
Third party

Maximum number of attempts.

Type
Integer
Default
6
Defined by
RetryProperties
spring.cloud.consul.retry.max-intervalMaximum interval for backoff.
2000
Third party

Maximum interval for backoff.

Type
Long
Default
2000
Defined by
RetryProperties
spring.cloud.consul.retry.multiplierMultiplier for next interval.
1.1
Third party

Multiplier for next interval.

Type
Double
Default
1.1
Defined by
RetryProperties
spring.cloud.consul.ribbon.enabledEnables Consul and Ribbon integration.
true
Third party

Enables Consul and Ribbon integration.

Type
Boolean
Default
true
spring.cloud.consul.schemeConsul agent scheme (HTTP/HTTPS).
no default
Third party

Consul agent scheme (HTTP/HTTPS). If there is no scheme in address - client will use HTTP.

Type
String
Default
none
Defined by
ConsulProperties
spring.cloud.consul.service-registry.auto-registration.enabledEnables Consul Service Registry Auto-registration.
true
Third party

Enables Consul Service Registry Auto-registration.

Type
Boolean
Default
true
spring.cloud.consul.service-registry.enabledEnables Consul Service Registry functionality.
true
Third party

Enables Consul Service Registry functionality.

Type
Boolean
Default
true
spring.cloud.consul.tls.certificate-passwordPassword to open the certificate.
no default
Third party

Password to open the certificate.

Type
String
Default
none
Defined by
ConsulProperties$TLSConfig
spring.cloud.consul.tls.certificate-pathFile path to the certificate.
no default
Third party

File path to the certificate.

Type
String
Default
none
Defined by
ConsulProperties$TLSConfig
spring.cloud.consul.tls.key-store-instance-typeType of key framework to use.
no default
Third party

Type of key framework to use.

Type
KeyStoreInstanceType
Default
none
Defined by
ConsulProperties$TLSConfig
spring.cloud.consul.tls.key-store-passwordPassword to an external keystore.
no default
Third party

Password to an external keystore.

Type
String
Default
none
Defined by
ConsulProperties$TLSConfig
spring.cloud.consul.tls.key-store-pathPath to an external keystore.
no default
Third party

Path to an external keystore.

Type
String
Default
none
Defined by
ConsulProperties$TLSConfig

Required settings may be needed to activate or affect the feature; review them even when they have a default. Optional settings only need to be set to change a default or to turn on the behavior they control. Third party settings belong to libraries such as Spring Boot that CAS builds on; their own documentation may have more detail.

Notes on configuration

Configuration Metadata

The collection of configuration properties listed in this section are automatically generated from the CAS source and components that contain the actual field definitions, types, descriptions, modules, etc. This metadata may not always be 100% accurate, or could be lacking details and sufficient explanations.

Be Selective

This section is meant as a guide only. Do NOT copy/paste the entire collection of settings into your CAS configuration; rather pick only the properties that you need. Do NOT enable settings unless you are certain of their purpose and do NOT copy settings into your configuration only to keep them as reference. All these ideas lead to upgrade headaches, maintenance nightmares and premature aging.

YAGNI

Note that for nearly ALL use cases, declaring and configuring properties listed here is sufficient. You should NOT have to explicitly massage a CAS XML/Java/etc configuration file to design an authentication handler, create attribute release policies, etc. CAS at runtime will auto-configure all required changes for you. If you are unsure about the meaning of a given CAS setting, do NOT turn it on without hesitation. Review the codebase or better yet, ask questions to clarify the intended behavior.

Naming Convention

Property names can be specified in very relaxed terms. For instance cas.someProperty, cas.some-property, cas.some_property are all valid names. While all forms are accepted by CAS, there are certain components (in CAS and other frameworks used) whose activation at runtime is conditional on a property value, where this property is required to have been specified in CAS configuration using kebab case. This is both true for properties that are owned by CAS as well as those that might be presented to the system via an external library or framework such as Spring Boot, etc.

:information_source: Note

When possible, properties should be stored in lower-case kebab format, such as cas.property-name=value. The only possible exception to this rule is when naming actuator endpoints; The name of the actuator endpoints (i.e. ssoSessions) MUST remain in camelCase mode.

Settings and properties that are controlled by the CAS platform directly always begin with the prefix cas. All other settings are controlled and provided to CAS via other underlying frameworks and may have their own schemas and syntax. BE CAREFUL with the distinction. Unrecognized properties are rejected by CAS and/or frameworks upon which CAS depends. This means if you somehow misspell a property definition or fail to adhere to the dot-notation syntax and such, your setting is entirely refused by CAS and likely the feature it controls will never be activated in the way you intend.

Validation

Configuration properties are automatically validated on CAS startup to report issues with configuration binding, especially if defined CAS settings cannot be recognized or validated by the configuration schema. Additional validation processes are also handled via Configuration Metadata and property migrations applied automatically on startup by Spring Boot and family.

Indexed Settings

CAS settings able to accept multiple values are typically documented with an index, such as cas.some.setting[0]=value. The index [0] is meant to be incremented by the adopter to allow for distinct multiple configuration blocks.

Troubleshooting

To enable additional logging, configure the log4j configuration file to add the following levels:

1
2
3
4
<Logger name="org.springframework.cloud.consul" level="debug" additivity="false">
  <AppenderRef ref="casConsole"/>
  <AppenderRef ref="casFile"/>
</Logger>

Actuator Endpoints

The following endpoints are provided by CAS:

clusterTopology
CAS endpoint2 operationsNot exposed by default
1

Turn the endpoint on and expose it over the web. One entry covers every operation. By default only info, health and status are exposed.

1
2
management.endpoint.clusterTopology.access=UNRESTRICTED
management.endpoints.web.exposure.include=clusterTopology

Endpoints may be mapped to other paths. For example, to serve health at healthcheck:

1
management.endpoints.web.path-mapping.health=healthcheck