一 介绍
模式匹配指的是带有通配符的{application}/{profile}名称的列表。如果{application}/{profile}不匹配任何模式,它将会使用spring.cloud.server.git.uri定义的URI。
![](https://i-blog.csdnimg.cn/blog_migrate/1cad75d1b9e9269f88b8a8ac43754f8a.png)
其中application.yml内容如下
info:
description: Spring Cloud Samples
url: https://github.com/spring-cloud-samples
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
foo.properties的内容如下:
foo: from foo props
democonfigclient.message: hello spring io
三 Config Server的配置如下:
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
repos:
simple: https://github.com/simple/config-repo
special:
pattern: special*/dev*,*special*/dev*
uri: https://github.com/special/config-repo
local:
pattern: local*
uri: file:/home/configsvc/config-repo
logging:
level:
org.springframework.cloud: DEBUG
org.springframework.boot: DEBUG
该例中,对应simple仓库(该仓库其实在github上不存在),它只匹配所有配置文件中名为simple的应用程序。
local仓库匹配所有配置文件中以local开头的应用程序的名称。
四 测试
1 输入:http://localhost:8080/foo-default.yml,返回内容如下:
democonfigclient:
message: hello spring io
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
foo: from foo props
info:
description: Spring Cloud Samples
url: https://github.com/spring-cloud-samples
从结果来看:匹配到了application.yml和foo.properties
2 输入 http://localhost:8080/special/dev,返回内容如下:
{
"name": "special",
"profiles": [
"dev"
],
"label": "master",
"version": "a611374438e75aa1b9808908c57833480944e1a8",
"state": null,
"propertySources": [
{
"name": "https://github.com/spring-cloud-samples/config-repo/application.yml",
"source": {
"info.description": "Spring Cloud Samples",
"info.url": "https://github.com/spring-cloud-samples",
"eureka.client.serviceUrl.defaultZone": "http://localhost:8761/eureka/",
"foo": "baz"
}
}
]
}
从结果来看:匹配到了application.yml,因为
https://github.com/special/config-repo这个仓库不存在。所以匹配到application.yml。