Spring Cloud Config 服务提供了用于外部配置(名称-值对或等效的 YAML 内容)的基于 HTTP 资源的 API。服务器可以通过使用 ConfigServer.java. @SpringBootApplication @EnableConfigServer public class ConfigServer { public static void main(String[] args) { SpringApplication.run(ConfigServer.class, args); } }
像所有 Spring Boot 应用程序一样,默认情况下它在端口 8080上 运行,但是你可以以各种方式将其切换到更传统的端口 8888。设置默认配置存储库最简单的方法是使用 application.properties. server.port: 8888 spring.cloud.config.server.git.uri: file://${user.home}/config-repo
其中
你应该在哪里存储配置服务器的配置数据?管理这种行为的策略是
存储库实现的行为通常类似于 Spring Boot 应用程序,从 下面的示例客户端应用程序具有这种引导配置: bootstrap.yml. spring: application: name: foo profiles: active: dev,mysql (与通常的 Spring Boot 应用一样,这些属性也可以由环境变量或命令行参数设置)。
如果存储库是基于文件的,则服务器从 你可以将 spring.cloud.config.server.accept-empty 设置为 false,这样如果找不到应用,服务器将返回 HTTP 404 状态。
此存储库实现将 HTTP 资源的
通过将 spring: cloud: config: server: git: uri: https://example.com/my/repo skipSslValidation: true
你可以配置配置服务器等待获取 HTTP 连接的时间,以秒为单位。使用 spring: cloud: config: server: git: uri: https://example.com/my/repo timeout: 4
Spring Cloud Config 服务器支持带有 spring: cloud: config: server: git: uri: https://github.com/myorg/{application}
你还可以使用类似的模式支持“每个配置组一个存储库”策略,但是使用
此外,使用 spring: cloud: config: server: git: uri: https://github.com/{application}
Spring Cloud Config 还包括对应用程序和配置组名称进行模式匹配的更复杂需求的支持。模式格式是带有通配符的 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
如果
repo 中的 spring: cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo repos: development: pattern: - '*/development' - '*/staging' uri: https://github.com/development/config-repo staging: pattern: - '*/qa' - '*/production' uri: https://github.com/staging/config-repo
每个存储库还可以选择性地将配置文件存储在子目录中,并且搜索这些目录的模式可以指定为 spring: cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo searchPaths: foo,bar*
在前面的示例中,服务器在顶层和 默认情况下,服务器在第一次请求配置时克隆远程存储库。服务器可以配置为在启动时克隆存储库,如下面的顶层示例所示: spring: cloud: config: server: git: uri: https://git/common/config-repo.git repos: team-a: pattern: team-a-* cloneOnStart: true uri: http://git/team-a/config-repo.git team-b: pattern: team-b-* cloneOnStart: false uri: http://git/team-b/config-repo.git team-c: pattern: team-c-* uri: http://git/team-a/config-repo.git 在前面的示例中,服务器在启动时克隆 team-a 的 config-repo,然后接受任何请求。在请求来自存储库的配置之前,不会克隆所有其他存储库。
要在远程存储库上使用 HTTP 基本身份验证,请分别添加 spring: cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo username: trolley password: strongpassword
如果不使用 HTTPS 和用户凭证,则在默认目录(
Spring Cloud Config 服务还支持 AWS CodeCommit 认证。当从命令行使用 Git 时,AWS CodeCommit 使用认证助手。此助手不与 JGit 库一起使用,因此如果 Git URI 与 AWS CodeCommit 模式匹配,则创建用于 AWS CodeCommit 的 JGit CredentialProvider。AWS CodeCommit URI 遵循以下模式://git-codecommit.${AWS_REGION}.amazonaws.com/${repopath}。 如果使用 AWS CodeCommit URI 提供用户名和密码,则它们必须是提供对存储库访问的 AWS accessKeyId 和 secretAccessKey。如果没有指定用户名和密码,则使用 AWS 默认凭证提供程序链检索 accessKeyId 和 secretAccessKey。 如果 Git URI 与 CodeCommit URI 模式匹配(如前所示),则必须在用户名和密码中或在默认凭证提供程序链支持的位置之一中提供有效的 AWS 凭证。AWS EC2 实例可以为 EC2 实例使用 IAM 角色。
默认情况下,Spring Cloud Config 服务使用的 JGit 库在使用 SSH URI 连接到 Git 存储库时使用 SSH 配置文件,如 spring: cloud: config: server: git: uri: git@gitserver.com:team/repo1.git ignoreLocalSshSettings: true hostKey: someHostKey hostKeyAlgorithm: ssh-rsa privateKey: | -----BEGIN RSA PR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UBtjAoGBAI8bfPgJpyFyMiGBjO6z FwlJc/xlFqDusrcHL7abW5qq0L4v3R+FrJw3ZYufzLTVcKfdj6GelwJJO+8wBm+R gTKYJItEhT48duLIfTDyIpHGVm9+I1MGhh5zKuCqIhxIYr9jHloBB7kRm0rPvYY4 VAykcNgyDvtAVODP+4m6JvhjAoGBALbtTqErKN47V0+JJpapLnF0KxGrqeGIjIRV cYA6V4WYGr7NeIfesecfOC356PyhgPfpcVyEztwlvwTKb3RzIT1TZN8fH4YBr6Ee KTbTjefRFhVUjQqnucAvfGi29f+9oE3Ei9f7wA+H35ocF6JvTYUsHNMIO/3gZ38N CPjyCMa9AoGBAMhsITNe3QcbsXAbdUR00dDsIFVROzyFJ2m40i4KCRM35bC/BIBs q0TY3we+ERB40U8Z2BvU61QuwaunJ2+uGadHo58VSVdggqAo0BSkH58innKKt96J 69pcVH/4rmLbXdcmNYGm6iu+MlPQk4BUZknHSmVHIFdJ0EPupVaQ8RHT -----END RSA PRIVATE KEY----- 下表描述了 SSH 配置属性。 表格 5.1. SSH 配置属性
Spring Cloud Config 服务还支持 spring: cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo searchPaths: '{application}' 前面的清单在存储库中搜索与目录(以及顶层)同名的文件。通配符在具有占位符的搜索路径中也是有效的(搜索中包括任何匹配的目录)。 如前所述,在本地副本变脏(例如,OS 进程更改文件夹内容)的情况下,Spring Cloud Config 服务对远程 git 存储库进行克隆,使得 Spring Cloud Config 服务无法从远程存储库更新本地副本。
为了解决此问题,有一个 spring: cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo force-pull: true
如果具有多个存储库配置,则可以配置每个存储库的 spring: cloud: config: server: git: uri: https://git/common/config-repo.git force-pull: true repos: team-a: pattern: team-a-* uri: http://git/team-a/config-repo.git force-pull: true team-b: pattern: team-b-* uri: http://git/team-b/config-repo.git force-pull: true team-c: pattern: team-c-* uri: http://git/team-a/config-repo.git
由于 Spring Cloud Config 服务在将分支签出到本地 repo(例如通过标签获取属性)之后具有远程 git 存储库的克隆,因此它将永远保留这个分支或者直到下一次服务器重新启动(创建新的本地 repo)。因此,可能存在远程分支被删除但本地副本仍然可用于获取的情况。如果 Spring Cloud Config 服务客户端服务以
为了保持本地存储库分支的整洁以及从远程更新 - 可以设置 spring: cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo deleteUntrackedBranches: true
配置服务中还有一个 “native” 配置组,它不使用 Git,而是从本地类路径或文件系统(你想要用
搜索位置可以包含
如果在搜索位置中不使用占位符,则此存储库还将 HTTP 资源的 Spring Cloud Config 服务还支持 Vault 作为后台。 有关 Vault 的更多详细信息,请参阅 Vault 快速入门指南。
要使配置服务器能够使用 Vault 后台,可以使用
默认情况下,配置服务假定你的 Vault 服务器在
所有可配置的属性都可以在
Vault 0.10.0 引入了版本化的键值后台(k/v backend version 2),与早期版本相比,它公开了不同的 API,现在它需要在挂载路径和实际上下文路径之间提供 在运行配置服务之后,你可以向服务发出 HTTP 请求,以便从 Vault 后台检索值。为此,你需要 Vault 服务器的令牌。 首先,将一些数据放入 Vault,如下面的示例所示: $ vault kv put secret/application foo=bar baz=bam $ vault kv put secret/myapp foo=myappsbar 其次,向配置服务器发出 HTTP 请求以检索这些值,如下面的示例所示:
你应该会看到类似于以下内容的响应: { "name":"myapp", "profiles":[ "default" ], "label":null, "version":null, "state":null, "propertySources":[ { "name":"vault:myapp", "source":{ "foo":"myappsbar" } }, { "name":"vault:application", "source":{ "baz":"bam", "foo":"bar" } } ] }
配置服务器可以通过 HTTP 或 HTTPS 代理访问 Git 或 Vault 后台。通过
下表描述了 HTTP 和 HTTPS 代理的代理配置属性。所有这些属性必须以 表格 5.2. 代理配置属性
以下配置使用 HTTPS 代理访问 Git 存储库。 spring: profiles: active: git cloud: config: server: git: uri: https://github.com/spring-cloud-samples/config-repo proxy: https: host: my-proxy.host.io password: myproxypassword port: '3128' username: myproxyusername nonProxyHosts: example.com 所有应用程序之间的共享配置根据所采用的方法而不同,如以下主题所述:
使用基于文件的(git、svn 和本地)存储库,所有客户端应用程序之间共享具有 #_property_overrides[property overrides] 特性还可以用于设置全局默认值,允许使用占位符以便应用程序在本地覆盖它们。
Spring Cloud Config 服务支持 JDBC(关系数据库)作为配置属性的后台。可以通过将
数据库需要有一个名为 Spring Cloud Config 服务支持 CredHub 作为配置属性的后台。你可以通过向 Spring CredHub 添加依赖项来启用该特性。 pom.xml. <dependencies> <dependency> <groupId>org.springframework.credhub</groupId> <artifactId>spring-credhub-starter</artifactId> </dependency> </dependencies> 以下配置使用共用 TLS 访问 CredHub: spring: profiles: active: credhub cloud: config: server: credhub: url: https://credhub:8844 这些属性应该存储为 JSON,例如: credhub set --name "/demo-app/default/master/toggles" --type=json value: {"toggle.button": "blue", "toggle.link": "red"} credhub set --name "/demo-app/default/master/abs" --type=json value: {"marketing.enabled": true, "external.enabled": false}
所有名为 { toggle.button: "blue", toggle.link: "red", marketing.enabled: true, external.enabled: false }
可以使用 UAA 作为提供者使用 OAuth 2.0 进行身份验证。 pom.xml. <dependencies> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-oauth2-client</artifactId> </dependency> </dependencies> 以下配置使用 OAuth 2.0 和 UAA 访问 CredHub: spring: profiles: active: credhub cloud: config: server: credhub: url: https://credhub:8844 oauth2: registration-id: credhub-client security: oauth2: client: registration: credhub-client: provider: uaa client-id: credhub_config_server client-secret: asecret authorization-grant-type: client_credentials provider: uaa: token-uri: https://uaa:8443/oauth/token
在某些情况下,你可能希望从多个环境存储库中提取配置数据。为此,可以在配置服务器的应用程序属性或 YAML 文件中启用 spring: profiles: active: composite cloud: config: server: composite: - type: svn uri: file:///path/to/svn/repo - type: git uri: file:///path/to/rex/git/repo - type: git uri: file:///path/to/walter/git/repo
使用此配置,优先级由
如果只想从存储库中获取配置数据,这些存储库是各种不同类型的,那么可以在配置服务器的应用程序属性或 YAML 文件中启用相应的配置文件,而不是 spring: profiles: active: git, vault cloud: config: server: git: uri: file:///path/to/git/repo order: 2 vault: host: 127.0.0.1 port: 8200 order: 1
使用此配置,优先级可以由
配置服务有一个 “overrides” 特性,允许操作员向所有应用程序提供配置属性。使用普通 Spring Boot 勾子的应用程序不能意外更改重写的属性。要声明重写,请向 spring: cloud: config: server: overrides: foo: bar
前面的示例使所有配置客户端的应用程序读取
可以通过在远程存储库中设置
配置服务附带了一个健康指标,用于检查配置的 你可以配置健康指标来检查更多应用程序,以及自定义配置组和自定义标签,如下面的示例所示: spring: cloud: config: server: health: repositories: myservice: label: mylabel myservice-dev: name: myservice profiles: development
可以通过设置 你可以以对你有意义的任何方式(从物理网络安全到 OAuth2 承载令牌)来保护配置服务,因为 Spring Security 和 Spring Boot 提供了对许多安全安排的支持。
要使用默认的 Spring Boot 配置的 HTTP 基本安全性,在类路径中包括 Spring Security(例如,通过
如果远程属性源包含加密的内容(以
如果为配置客户机应用程序设置远程配置存储库,那么它可能包含类似于以下内容的 application.yml. spring: datasource: username: dbuser password: '{cipher}FKSAJDFGYOS8F7GLHAKERGFHLSAJ' .properties 文件中的加密值不能用引号包装。否则,该值不被解密。下面的示例展示了可以工作的值: application.properties. spring.datasource.username: dbuser spring.datasource.password: {cipher}FKSAJDFGYOS8F7GLHAKERGFHLSAJ 可以将此纯文本安全地推送到共享 git 存储库,并且保密密码仍然受到保护。
服务器还公开 $ curl localhost:8888/encrypt -d mysecret 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
逆操作也可以通过 $ curl localhost:8888/decrypt -d 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda mysecret
在将
$ spring encrypt mysecret --key foo 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda $ spring decrypt --key foo 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda mysecret 若要在文件中使用密钥(例如用于加密的 RSA 公钥),请在密钥值前面加上 "@",并提供文件路径,如下面的示例所示: $ spring encrypt mysecret --key @${HOME}/.ssh/id_rsa.pub AQAjPgt3eFZQXwt8tsHAVv/QHiY5sI2dRcR+...
配置服务器可以使用对称(共享)密钥或非对称密钥(RSA 密钥对)。非对称选择在安全性方面是优越的,但是使用对称密钥通常更方便,因为它是在
要配置对称密钥,你需要将
要配置非对称密钥,可以使用密钥存储库(例如,由 JDK 附带的
加密用公钥完成,解密需要私钥。因此,原则上,如果仅希望加密(并且准备使用私钥在本地解密值),则只能配置服务器中的公钥。在实践中,你可能不想在本地进行解密,因为它将密钥管理过程分散在所有客户端周围,而不是将其集中在服务器中。另一方面,如果你的配置服务器相对不安全,并且只有少数客户端需要加密的属性,那么它可以是一个有用的选项。 要创建用于测试的密钥存储库,可以使用以下命令: $ keytool -genkeypair -alias mytestkey -keyalg RSA \ -dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US" \ -keypass changeme -keystore server.jks -storepass letmein
将 encrypt: keyStore: location: classpath:/server.jks password: letmein alias: mytestkey secret: changeme
除了加密属性值中的 foo: bar: `{cipher}{key:testkey}...`
定位器查找名为 "testkey" 的密钥。也可以通过在前缀中使用
当密钥仅用于对配置数据的几个字节进行加密时(也就是说,它们不在别处使用),基于密码学的理由,密钥循环几乎从来都不是必需的。但是,你可能偶尔需要更改密钥(例如,在安全性被破坏的情况下)。在这种情况下,所有客户端将需要更改它们的源配置文件(例如,在 git 中),并在所有密码中使用新的
|