2014-02-19 02:05:18 +00:00
---
layout: "docs"
page_title: "Service Definition"
sidebar_current: "docs-agent-services"
2014-10-19 23:40:10 +00:00
description: |-
2016-07-02 05:29:00 +00:00
One of the main goals of service discovery is to provide a catalog of available services. To that end, the agent provides a simple service definition format to declare the availability of a service and to potentially associate it with a health check. A health check is considered to be application level if it is associated with a service. A service is defined in a configuration file or added at runtime over the HTTP interface.
2014-02-19 02:05:18 +00:00
---
# Services
One of the main goals of service discovery is to provide a catalog of available
services. To that end, the agent provides a simple service definition format
2015-01-29 00:43:52 +00:00
to declare the availability of a service and to potentially associate it with
2016-07-02 05:29:00 +00:00
a health check. A health check is considered to be application level if it is
2015-01-29 00:43:52 +00:00
associated with a service. A service is defined in a configuration file
2014-02-19 02:05:18 +00:00
or added at runtime over the HTTP interface.
## Service Definition
2017-12-31 08:47:05 +00:00
A service definition is a script that looks like:
2014-02-19 02:05:18 +00:00
2014-10-19 23:40:10 +00:00
```javascript
{
"service": {
"name": "redis",
2016-11-25 16:00:02 +00:00
"tags": ["primary"],
2016-11-22 16:29:29 +00:00
"address": "",
2018-04-24 19:56:35 +00:00
"meta": {
"meta": "for my service"
}
2014-10-19 23:40:10 +00:00
"port": 8000,
2017-10-10 23:40:59 +00:00
"enable_tag_override": false,
2015-01-14 01:52:17 +00:00
"checks": [
{
"script": "/usr/local/bin/check_redis.py",
"interval": "10s"
}
]
2014-10-19 23:40:10 +00:00
}
}
```
2014-02-19 02:05:18 +00:00
2016-11-25 16:00:02 +00:00
A service definition must include a `name` and may optionally provide an
2018-04-24 19:56:35 +00:00
`id` , `tags` , `address` , `port` , `check` , `meta` and `enable_tag_override` .
The `id` is set to the `name` if not provided. It is required that all
2016-11-25 16:00:02 +00:00
services have a unique ID per node, so if names might conflict then
unique IDs should be provided.
2014-02-19 02:05:18 +00:00
2017-10-10 23:40:59 +00:00
For Consul 0.9.3 and earlier you need to use `enableTagOverride` . Consul 1.0
supports both `enable_tag_override` and `enableTagOverride` but the latter is
deprecated and will be removed in Consul 1.1.
2016-11-25 16:00:02 +00:00
The `tags` property is a list of values that are opaque to Consul but
can be used to distinguish between `primary` or `secondary` nodes,
different versions, or any other service level labels.
2015-01-29 00:43:52 +00:00
The `address` field can be used to specify a service-specific IP address. By
default, the IP address of the agent is used, and this does not need to be provided.
The `port` field can be used as well to make a service-oriented architecture
simpler to configure; this way, the address and port of a service can
2014-02-19 02:05:18 +00:00
be discovered.
2018-04-24 19:56:35 +00:00
The `meta` object is a map of max 64 key/values with string semantics. Key can contain
2018-04-24 20:40:41 +00:00
only ASCII chars and no special characters (`A-Z` `a-z` `0-9` `_` and `-` ).
2018-04-24 19:56:35 +00:00
For performance and security reasons, values as well as keys are limited to 128
2018-04-24 20:40:41 +00:00
characters for keys, 512 for values. This object has the same limitations as the node
2018-04-24 20:55:34 +00:00
meta object in node definition.
2018-04-24 20:40:41 +00:00
All those meta data can be retrieved individually per instance of the service
2018-04-24 19:56:35 +00:00
and all the instances of a given service have their own copy of it.
2015-04-28 21:26:22 +00:00
Services may also contain a `token` field to provide an ACL token. This token is
used for any interaction with the catalog for the service, including
[anti-entropy syncs ](/docs/internals/anti-entropy.html ) and deregistration.
2015-01-09 05:34:16 +00:00
A service can have an associated health check. This is a powerful feature as
2015-01-29 00:43:52 +00:00
it allows a web balancer to gracefully remove failing nodes, a database
2016-11-25 16:00:02 +00:00
to replace a failed secondary, etc. The health check is strongly integrated in
2015-01-09 05:34:16 +00:00
the DNS interface as well. If a service is failing its health check or a
node has any failing system-level check, the DNS interface will omit that
2014-02-19 02:05:18 +00:00
node from any service query.
2015-07-27 00:53:52 +00:00
The check must be of the script, HTTP, TCP or TTL type. If it is a script type,
`script` and `interval` must be provided. If it is a HTTP type, `http` and
`interval` must be provided. If it is a TCP type, `tcp` and `interval` must be
provided. If it is a TTL type, then only `ttl` must be provided. The check name
is automatically generated as `service:<service-id>` . If there are multiple
service checks registered, the ID will be generated as
`service:<service-id>:<num>` where `<num>` is an incrementing number starting
from `1` .
2014-02-19 02:05:18 +00:00
2016-11-25 16:00:02 +00:00
-> **Note:** There is more information about [checks here ](/docs/agent/checks.html ).
2017-10-10 23:40:59 +00:00
The `enable_tag_override` can optionally be specified to disable the
anti-entropy feature for this service. If `enable_tag_override` is set to
2016-11-25 16:00:02 +00:00
`TRUE` then external agents can update this service in the
2017-04-04 16:33:22 +00:00
[catalog ](/api/catalog.html ) and modify the tags. Subsequent
2016-11-25 16:00:02 +00:00
local sync operations by this agent will ignore the updated tags. For
example, if an external agent modified both the tags and the port for
2017-10-10 23:40:59 +00:00
this service and `enable_tag_override` was set to `TRUE` then after the next
2016-11-25 16:00:02 +00:00
sync cycle the service's port would revert to the original value but the
tags would maintain the updated value. As a counter example: If an
external agent modified both the tags and port for this service and
2017-10-10 23:40:59 +00:00
`enable_tag_override` was set to `FALSE` then after the next sync cycle the
2016-11-25 16:00:02 +00:00
service's port *and* the tags would revert to the original value and all
modifications would be lost.
It's important to note that this applies only to the locally registered
service. If you have multiple nodes all registering the same service
2017-10-10 23:40:59 +00:00
their `enable_tag_override` configuration and all other service
2016-11-25 18:25:09 +00:00
configuration items are independent of one another. Updating the tags
2016-11-25 16:00:02 +00:00
for the service registered on one node is independent of the same
2017-10-10 23:40:59 +00:00
service (by name) registered on another node. If `enable_tag_override` is
2016-11-25 18:25:09 +00:00
not specified the default value is false. See [anti-entropy
2016-11-25 16:00:02 +00:00
syncs](/docs/internals/anti-entropy.html) for more info.
2017-10-10 23:40:59 +00:00
For Consul 0.9.3 and earlier you need to use `enableTagOverride` . Consul 1.0
supports both `enable_tag_override` and `enableTagOverride` but the latter is
deprecated and will be removed in Consul 1.1.
2016-11-25 16:00:02 +00:00
To configure a service, either provide it as a `-config-file` option to
the agent or place it inside the `-config-dir` of the agent. The file
2017-10-10 23:40:59 +00:00
must end in the `.json` or `.hcl` extension to be loaded by Consul. Check
2016-11-25 16:00:02 +00:00
definitions can be updated by sending a `SIGHUP` to the agent.
Alternatively, the service can be registered dynamically using the [HTTP
2017-04-04 16:52:00 +00:00
API](/api/index.html).
2014-02-23 02:53:31 +00:00
2014-10-24 02:22:25 +00:00
## Multiple Service Definitions
2016-11-25 16:00:02 +00:00
Multiple services definitions can be provided at once using the plural
`services` key in your configuration file.
2014-10-24 02:22:25 +00:00
```javascript
{
"services": [
{
"id": "red0",
"name": "redis",
"tags": [
2016-11-25 16:00:02 +00:00
"primary"
2014-10-24 02:22:25 +00:00
],
2016-11-22 16:29:29 +00:00
"address": "",
2014-10-24 02:22:25 +00:00
"port": 6000,
2015-01-14 01:52:17 +00:00
"checks": [
{
"script": "/bin/check_redis -p 6000",
"interval": "5s",
"ttl": "20s"
}
]
2014-10-24 02:22:25 +00:00
},
{
"id": "red1",
"name": "redis",
"tags": [
"delayed",
2016-11-25 16:00:02 +00:00
"secondary"
2014-10-24 02:22:25 +00:00
],
2016-11-22 16:29:29 +00:00
"address": "",
2014-10-24 02:22:25 +00:00
"port": 7000,
2015-01-14 01:52:17 +00:00
"checks": [
{
"script": "/bin/check_redis -p 7000",
"interval": "30s",
"ttl": "60s"
}
]
2014-10-24 02:22:25 +00:00
},
...
]
}
```
2015-06-25 16:07:55 +00:00
## Service and Tag Names with DNS
Consul exposes service definitions and tags over the [DNS ](/docs/agent/dns.html )
interface. DNS queries have a strict set of allowed characters and a
well-defined format that Consul cannot override. While it is possible to
register services or tags with names that don't match the conventions, those
services and tags will not be discoverable via the DNS interface. It is
recommended to always use DNS-compliant service and tag names.
DNS-compliant service and tag names may contain any alpha-numeric characters, as
well as dashes. Dots are not supported because Consul internally uses them to
delimit service tags.