2019-07-09 02:11:19 +00:00
|
|
|
---
|
|
|
|
layout: "docs"
|
2019-08-21 21:23:08 +00:00
|
|
|
page_title: "Configuration Entry Kind: Service Router"
|
2019-07-09 02:11:19 +00:00
|
|
|
sidebar_current: "docs-agent-cfg_entries-service_router"
|
|
|
|
description: |-
|
|
|
|
The service-router config entry kind controls Connect traffic routing and manipulation at networking layer 7 (e.g. HTTP).
|
|
|
|
---
|
|
|
|
|
2019-08-21 21:23:08 +00:00
|
|
|
-> **1.6.0+:** This config entry is available in Consul versions 1.6.0 and newer.
|
|
|
|
|
|
|
|
# Service Router
|
2019-07-09 02:11:19 +00:00
|
|
|
|
|
|
|
The `service-router` config entry kind controls Connect traffic routing and
|
|
|
|
manipulation at networking layer 7 (e.g. HTTP).
|
|
|
|
|
|
|
|
If a router is not explicitly configured or is configured with no routes then
|
|
|
|
the system behaves as if a router were configured sending all traffic to a
|
|
|
|
service of the same name.
|
|
|
|
|
|
|
|
## Interaction with other Config Entries
|
|
|
|
|
|
|
|
- Service router config entries are a component of [L7 Traffic
|
|
|
|
Management](/docs/connect/l7-traffic-management.html).
|
|
|
|
|
|
|
|
- Service router config entries are restricted to only services that define
|
|
|
|
their protocol as http-based via a corresponding
|
|
|
|
[`service-defaults`](/docs/agent/config-entries/service-defaults.html) config
|
|
|
|
entry or globally via
|
|
|
|
[`proxy-defaults`](/docs/agent/config-entries/proxy-defaults.html) .
|
|
|
|
|
|
|
|
- Any route destination that omits the `ServiceSubset` field is eligible for
|
|
|
|
splitting via a
|
|
|
|
[`service-splitter`](/docs/agent/config-entries/service-splitter.html) should
|
|
|
|
one be configured for that service, otherwise resolution proceeds according
|
|
|
|
to any configured
|
|
|
|
[`service-resolver`](/docs/agent/config-entries/service-resolver.html).
|
|
|
|
|
|
|
|
## Sample Config Entries
|
|
|
|
|
|
|
|
Route HTTP requests with a path starting with `/admin` to a different service:
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
kind = "service-router"
|
|
|
|
name = "web"
|
|
|
|
routes = [
|
|
|
|
{
|
|
|
|
match {
|
|
|
|
http {
|
|
|
|
path_prefix = "/admin"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destination {
|
|
|
|
service = "admin"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
# NOTE: a default catch-all will send unmatched traffic to "web"
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
|
|
|
Route HTTP requests with a special url parameter or header to a canary subset:
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
kind = "service-router"
|
|
|
|
name = "web"
|
|
|
|
routes = [
|
|
|
|
{
|
|
|
|
match {
|
|
|
|
http {
|
|
|
|
header = [
|
|
|
|
{
|
|
|
|
name = "x-debug"
|
|
|
|
exact = "1"
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
destination {
|
|
|
|
service = "web"
|
|
|
|
service_subset = "canary"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match {
|
|
|
|
http {
|
|
|
|
query_param = [
|
|
|
|
{
|
|
|
|
name = "x-debug"
|
2019-07-24 01:55:26 +00:00
|
|
|
exact = "1"
|
2019-07-09 02:11:19 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
destination {
|
|
|
|
service = "web"
|
|
|
|
service_subset = "canary"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
# NOTE: a default catch-all will send unmatched traffic to "web"
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
Re-route a gRPC method to another service. Since gRPC method calls [are
|
|
|
|
HTTP2](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md), we can use an HTTP path match rule to re-route traffic:
|
|
|
|
|
|
|
|
```hcl
|
|
|
|
kind = "service-router"
|
|
|
|
name = "billing"
|
|
|
|
routes = [
|
|
|
|
{
|
|
|
|
match {
|
|
|
|
http {
|
|
|
|
path_exact = "/mycompany.BillingService/GenerateInvoice"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destination {
|
|
|
|
service = "invoice-generator"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
# NOTE: a default catch-all will send unmatched traffic to "billing"
|
|
|
|
]
|
|
|
|
```
|
|
|
|
|
2019-07-09 02:11:19 +00:00
|
|
|
## Available Fields
|
|
|
|
|
|
|
|
- `Kind` - Must be set to `service-router`
|
|
|
|
|
|
|
|
- `Name` `(string: <required>)` - Set to the name of the service being configured.
|
|
|
|
|
|
|
|
- `Routes` `(array<ServiceRoute>)` - The list of routes to consider when
|
|
|
|
processing L7 requests. The first route to match in the list is terminal and
|
|
|
|
stops further evaluation. Traffic that fails to match any of the provided
|
|
|
|
routes will be routed to the default service.
|
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Match` `(ServiceRouteMatch: <optional>)` - A set of criteria that can
|
|
|
|
match incoming L7 requests. If empty or omitted it acts as a catch-all.
|
|
|
|
|
|
|
|
- `HTTP` `(ServiceRouteHTTPMatch: <optional>)` - A set of
|
|
|
|
[http-specific match criteria](#serviceroutehttpmatch).
|
|
|
|
|
|
|
|
- `Destination` `(ServiceRouteDestination: <optional>)` - Controls [how to
|
|
|
|
proxy](#serviceroutedestination) the matching request(s) to a
|
|
|
|
service.
|
|
|
|
|
|
|
|
### `ServiceRouteHTTPMatch`
|
|
|
|
|
|
|
|
- `PathExact` `(string: "")` - Exact path to match on the HTTP request path.
|
|
|
|
|
|
|
|
At most only one of `PathExact`, `PathPrefix`, or `PathRegex` may be configured.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `PathPrefix` `(string: "")` - Path prefix to match on the HTTP request path.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
At most only one of `PathExact`, `PathPrefix`, or `PathRegex` may be configured.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `PathRegex` `(string: "")` - Regular expression to match on the HTTP
|
|
|
|
request path.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
The syntax when using the Envoy proxy is [documented here](https://en.cppreference.com/w/cpp/regex/ecmascript).
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
At most only one of `PathExact`, `PathPrefix`, or `PathRegex` may be configured.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Header` `(array<ServiceRouteHTTPMatchHeader>)` - A set of criteria that can
|
|
|
|
match on HTTP request headers. If more than one is configured all must match
|
|
|
|
for the overall match to apply.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Name` `(string: <required>)` - Name of the header to match.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Present` `(bool: false)` - Match if the header with the given name is
|
|
|
|
present with any value.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
At most only one of `Exact`, `Prefix`, `Suffix`, `Regex`, or `Present`
|
|
|
|
may be configured.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Exact` `(string: "")` - Match if the header with the given name is this
|
|
|
|
value.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
At most only one of `Exact`, `Prefix`, `Suffix`, `Regex`, or `Present`
|
|
|
|
may be configured.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Prefix` `(string: "")` - Match if the header with the given name has
|
|
|
|
this prefix.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
At most only one of `Exact`, `Prefix`, `Suffix`, `Regex`, or `Present`
|
|
|
|
may be configured.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Suffix` `(string: "")` - Match if the header with the given name has
|
|
|
|
this suffix.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
At most only one of `Exact`, `Prefix`, `Suffix`, `Regex`, or `Present`
|
|
|
|
may be configured.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Regex` `(string: "")` - Match if the header with the given name matches
|
|
|
|
this pattern.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
The syntax when using the Envoy proxy is [documented here](https://en.cppreference.com/w/cpp/regex/ecmascript).
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
At most only one of `Exact`, `Prefix`, `Suffix`, `Regex`, or `Present`
|
|
|
|
may be configured.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Invert` `(bool: false)` - Inverts the logic of the match.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `QueryParam` `(array<ServiceRouteHTTPMatchQueryParam>)` - A set of criteria
|
|
|
|
that can match on HTTP query parameters. If more than one is configured all
|
|
|
|
must match for the overall match to apply.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Name` `(string: <required>)` - The name of the query parameter to match on.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Present` `(bool: false)` - Match if the query parameter with the given
|
|
|
|
name is present with any value.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
At most only one of `Exact`, `Regex`, or `Present` may be configured.
|
2019-07-24 01:55:26 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Exact` `(string: "")` - Match if the query parameter with the given name
|
|
|
|
is this value.
|
2019-07-24 01:55:26 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
At most only one of `Exact`, `Regex`, or `Present` may be configured.
|
2019-07-24 01:55:26 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Regex` `(string: "")` - Match if the query parameter with the given name
|
|
|
|
matches this pattern.
|
2019-07-24 01:55:26 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
The syntax when using the Envoy proxy is [documented here](https://en.cppreference.com/w/cpp/regex/ecmascript).
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
At most only one of `Exact`, `Regex`, or `Present` may be configured.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Methods` `(array<string>)` - A list of HTTP methods for which this match
|
|
|
|
applies. If unspecified all http methods are matched.
|
2019-07-24 01:56:39 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
### `ServiceRouteDestination`
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Service` `(string: "")` - The service to resolve instead of the default
|
|
|
|
service. If empty then the default service name is used.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `ServiceSubset` `(string: "")` - A named subset of the given service to
|
|
|
|
resolve instead of the one defined as that service's `DefaultSubset`. If empty,
|
|
|
|
the default subset is used.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `Namespace` `(string: "")` - The namespace to resolve the service from
|
|
|
|
instead of the current namespace. If empty the current namespace is assumed.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `PrefixRewrite` `(string: "")` - Defines how to rewrite the HTTP request path
|
|
|
|
before proxying it to its final destination.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
This requires that either `Match.HTTP.PathPrefix` or
|
|
|
|
`Match.HTTP.PathExact` be configured on this route.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `RequestTimeout` `(duration: 0s)` - The total amount of time permitted for
|
|
|
|
the entire downstream request (and retries) to be processed.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `NumRetries` `(int: 0)` - The number of times to retry the request when a
|
|
|
|
retryable result occurs.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `RetryOnConnectFailure` `(bool: false)` - Allows for connection failure
|
|
|
|
errors to trigger a retry.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
2019-08-21 17:17:38 +00:00
|
|
|
- `RetryOnStatusCodes` `(array<int>)` - A flat list of http response status
|
|
|
|
codes that are eligible for retry.
|
2019-07-09 02:11:19 +00:00
|
|
|
|
|
|
|
## ACLs
|
|
|
|
|
|
|
|
Configuration entries may be protected by
|
|
|
|
[ACLs](https://learn.hashicorp.com/consul/security-networking/production-acls).
|
|
|
|
|
|
|
|
Reading a `service-router` config entry requires `service:read` on itself.
|
|
|
|
|
|
|
|
Creating, updating, or deleting a `service-router` config entry requires
|
|
|
|
`service:write` on itself and `service:read` on any other service referenced by
|
|
|
|
name in these fields:
|
|
|
|
|
|
|
|
- [`Routes[].Destination.Service`](#service)
|