Consul tags are used as Spring Cloud metadata while Consul metadata is ignored in include-expression
See original GitHub issueDescribe the bug
spring.cloud.consul.discovery.metadata in services is ignored by spring.cloud.gateway.include-expression which instead uses tags as metadata.
I don’t know if the behaviour is intentional, but is incredible confusing and goes against all of limited examples(1,2) of how to do route filtering with include-expression in scg.
If nothing else, this issue can serve as some documentation for others who are searching to understand how they can restrict routes using metadata without needing to purchase Consul Enterprise to get access to Namespaces.
Sample
Configure the spring cloud gateway to use include-expression: metadata['edge'] == 'true' to include service routes that have metadata key value pair of “edge”,“true”.
spring:
application:
name: api-gateway
cloud:
consul:
host: 192.168.1.100
port: 8500
discovery:
instanceId: ${spring.application.name}:${random.value}
# Basic gateway service mapping
gateway:
discovery:
locator:
enabled: true
include-expression: metadata['edge'] == 'true'
predicates:
- name: Path
args:
pattern: "'/api/' + serviceId + '/**'"
filters:
- name: RewritePath
args:
regexp: "'/api/' + serviceId + '/(?<remaining>.*)'"
replacement: "'/${remaining}'"
The obvious thing to do is to send some metadata that says “edge” is “true” as part of service registration with consul…
spring:
application:
name: webservice
consul:
host: 192.168.1.100
port: 8500
config:
enabled: true
format: YAML
discovery:
instanceId: ${spring.application.name}:${random.value}
metadata:
edge: true
However, this DOES NOT WORK. The metadata is registered and present in the service configuration, but the gateway does not recognize this as a service matching the include-expression.
We must replace this:
metadata: # Doesn't work with scg include-expression
edge: true
with this:
tags: edge=true # this WORKS
Consul service tags are being used as metadata by scg for the purpose of evaluating include-expression.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (3 by maintainers)

Top Related StackOverflow Question
in hoxton tags are still default you need to set
spring.cloud.consul.discovery.tags-as-metadata=false@spencergibb
tags-as-metadata=falsedoes resolve the issue. Upon googling, I see that is documented here: https://docs.spring.io/spring-cloud-consul/docs/current/reference/html/index.html#official-consul-metadataThanks pointing me in the right direction.