spring cloud连载第二篇之Spring Cloud Config

Spring Cloud Config Spring Cloud Config为分布式服务提供了服务侧和客户侧的外部配置支持。通过Spring Cloud Config你可以有一个统一的地方来管理所有应用的外部配置。 默认服务端存储实现用的是git,因此,它很容易支持配置环境的标签版本,并且可以访问各种管理内容的工具。 1. Quick Start 此小结将介绍Spring Cloud Config Server的客户端和服务端。 首先,启动服务端: 复制代码 1 $ cd spring-cloud-config-server 2 $ ../mvnw spring-boot:run 复制代码 接着试一下客户端: 复制代码 1 $ curl localhost:8888/foo/development 2 {"name":"foo","label":"master","propertySources":[ 3 {"name":"https://github.com/scratches/config-repo/foo-development.properties","source":{"bar":"spam"}}, 4 {"name":"https://github.com/scratches/config-repo/foo.properties","source":{"foo":"bar"}} 5 ]} 复制代码 默认定位属性资源的策略是使用git(spring.cloud.config.server.git.uri),并且用它来初始化一个迷你SpringApplication,这个迷你application的Environment是用来枚举属性资源并将它发布到一个JSON端点。 HTTP服务的格式如下: 复制代码 1 /{application}/{profile}[/{label}] 2 /{application}-{profile}.yml 3 /{label}/{application}-{profile}.yml 4 /{application}-{profile}.properties 5 /{label}/{application}-{profile}.properties 复制代码 其中application是是使用spring.config.name属性注入的,profile是一个激活的配置文件(或者是以逗号分隔的属性),label是一个可选的git标签(默认是master)。 Spring Cloud Config Server从git仓库为客户端拉取配置,如下: 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: https://github.com/spring-cloud-samples/config-repo 复制代码 1.1 Client Side Usage(客户端用法) 要在一个应用中使用这些特性,你可以构建一个Spring Boot application并且添加spring-cloud-config-client相关的依赖,最方便的方法是添加org.springframework.cloud:spring-cloud-starter-config依赖。 下面是一个典型的maven配置: 复制代码 1 2 org.springframework.boot 3 spring-boot-starter-parent 4 {spring-boot-docs-version} 5 6 7 8 9 10 11 org.springframework.cloud 12 spring-cloud-dependencies 13 {spring-cloud-version} 14 pom 15 import 16 17 18 19 20 21 22 org.springframework.cloud 23 spring-cloud-starter-config 24 25 26 org.springframework.boot 27 spring-boot-starter-test 28 test 29 30 31 32 33 34 35 org.springframework.boot 36 spring-boot-maven-plugin 37 38 39 复制代码 现在可以创建一个标准的Spring Boot application,如下: 复制代码 1 @SpringBootApplication 2 @RestController 3 public class Application { 4 5 @RequestMapping("/") 6 public String home() { 7 return "Hello World!"; 8 } 9 10 public static void main(String[] args) { 11 SpringApplication.run(Application.class, args); 12 } 13 14 } 复制代码 当这个HHTP服务启动时,它默认从本地的config server(端口8888)加载配置,可以使用bootstrap.properties配置来改变这个默认行为: 1 spring.cloud.config.uri: http://myconfigserver.com 可以使用/env端点来查看bootstrap properties配置: 复制代码 1 $ curl localhost:8080/env 2 { 3 "profiles":[], 4 "configService:https://github.com/spring-cloud-samples/config-repo/bar.properties":{"foo":"bar"}, 5 "servletContextInitParams":{}, 6 "systemProperties":{...}, 7 ... 8 } 复制代码 2. Spring Cloud Config Server(配置服务器的服务端) Spring Cloud Config Server为外部配置提供一个基于资源的HTTP API。使用注解@EnableConfigServer可以在一个Spring Boot application中嵌入配置服务器。例如: 复制代码 1 @SpringBootApplication 2 @EnableConfigServer 3 public class ConfigServer { 4 public static void main(String[] args) { 5 SpringApplication.run(ConfigServer.class, args); 6 } 7 } 复制代码 最简单的方法是通过属性spring.config.name=configserver启动,因为在config server的jar中有一个configserver.yml配置文件,它已经设置好了默认的配置仓库。 或者你可以使用自己的配置:application.properties 复制代码 1 server.port: 8888 2 spring.cloud.config.server.git.uri: file://${user.home}/config-repo 复制代码 其中${user.home}/config-repo是一个包含YAML或者properties格式配置文件的git仓库。 注意: 1)在windows上,你需要在file url前多添加一个“/”,如果是绝对路径的话。(例如:file:///${user.home}/config-repo) 2)使用本地仓库只是为了演示和测试使用,在正式环境中需要使用一台服务器来管理配置 3)初始化克隆配合仓库在只有一个文件时可能比较快,但是如果存储一个二进制文件,特别是比较大的文件时,可能在第一次请求配置时有延迟甚至可能导致服务端内存溢出。 2.1 Environment Repository 在config server中我们应该将配置存储在什么地方?掌控这个行为的策略的就是为Environment对象服务的EnvironmentRepository。 Environment资源由一下三个变量参数化: 1){application} 对应客户端的spring.application.name 2){profile} 对应客户端的spring.profiles.active(以逗号分隔) 3){label} 它是服务器端功能标签“版本化”的配置文件集 Repository的实现通常像一个spring boot application,从一个和{application}相等的spring.config.name参数和与{profiles}相等的spring.profiles.active参数指定的文件来加载配置。 优先级策略和普通spring boot 的应用一样:Active profiles优先级比默认的高,如果有多个profiles,那么最后一个优先级最高。下面是客户端bootstrap 配置bootstrap.yml: 复制代码 1 spring: 2 application: 3 name: foo 4 profiles: 5 active: dev,mysql 复制代码 (和一般Spring Boot application一样,这些参数也可以在环境变量或者命令行参数中指定) 2.1.1 Git Backend 在config server中使用spring.cloud.config.server.git.uri来设置仓库位置,如果uri是以file:开头,则是本地仓库。 但是如果要扩展config server并且高可用的话,必须使所有服务器实例指向同一个仓库。因此需要一个共享的文件系统。在这种情况下,使用ssh协议是比较好的,这样服务器可以克隆一份配置到本地作为缓存使用。 仓库的实现是将HTTP路径中的{label}映射到一个git标签(commit id, branch name, or tag),如果一个git分支或者标签中包含“/”斜杠,那么在HTTP url中需要用一个特殊字符(_)来替代,以免发生混淆。 比如标签为foo/bar,则应该替换成:foo(_)bar。这个规则同样可应用到{application}参数上。 Skipping SSL Certificate Validation(跳过ssl验证) 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: https://example.com/my/repo 7 skipSslValidation: true 复制代码 Setting HTTP Connection Timeout(设置HTTP连接超时时间,以秒为单位) 复制代码 spring: cloud: config: server: git: uri: https://example.com/my/repo timeout: 4 复制代码 Placeholders in Git URI(git URI中的占位符) Spring Cloud Config Server支持git URI中的占位符为:{application},{profile},{label}(记住label是作为git 的标签使用的)。通过下面的配置你可以支持每个应用一个仓库的策略: 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: https://github.com/myorg/{application} 复制代码 Pattern Matching and Multiple Repositories(规则匹配和多仓库) Spring Cloud Config包含对application和profile名称的规则匹配支持,规则格式是包含通配符的以逗号分隔的{application}/{profile}名称。例如: 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: https://github.com/spring-cloud-samples/config-repo 7 repos: 8 simple: https://github.com/simple/config-repo 9 special: 10 pattern: special*/dev*,*special*/dev* 11 uri: https://github.com/special/config-repo 12 local: 13 pattern: local* 14 uri: file:/home/configsvc/config-repo 复制代码 如果{application}/{profile}不匹配任何一个规则,则使用spring.cloud.config.server.git.uri下面的默认URI。在上面的例子中,匹配“simple”仓库的规则是simple/*({application}为simple,{profile}为任意值)。“local”仓库匹配任何一个以local开头的应用名称,如果没有指定{profile}规则,则会自动在规则后面添加“/*”。 由于pattern参数实际上是一个数组,所以使用YAML数组(或者在.properties文件中使用[0], [1]等后缀)来绑定多种规则。如下: 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: https://github.com/spring-cloud-samples/config-repo 7 repos: 8 development: 9 pattern: 10 - '*/development' 11 - '*/staging' 12 uri: https://github.com/development/config-repo 13 staging: 14 pattern: 15 - '*/qa' 16 - '*/production' 17 uri: https://github.com/staging/config-repo 复制代码 每个仓库都可以在子目录中存储配置文件,如果要在子目录中搜索则需要使用searchPaths配置。例如: 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: https://github.com/spring-cloud-samples/config-repo 7 searchPaths: foo,bar* 复制代码 在上面的例子中,服务器将会在顶级目录和foo子目录还有以bar开头的子目录中搜索配置文件。 默认情况下,服务器会在第一次收到配置请求时,克隆远程仓库。也可以在服务启动时克隆远程仓库,如下: 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: https://git/common/config-repo.git 7 repos: 8 team-a: 9 pattern: team-a-* 10 cloneOnStart: true 11 uri: http://git/team-a/config-repo.git 12 team-b: 13 pattern: team-b-* 14 cloneOnStart: false 15 uri: http://git/team-b/config-repo.git 16 team-c: 17 pattern: team-c-* 18 uri: http://git/team-a/config-repo.git 复制代码 在上面的例子中只有team-a的远程仓库会在启动时克隆,其他的都是在第一次接受到配置请求时才克隆。 Authentication(用户验证) 添加username和password属性来使用远程仓库基于HTTP的验证。例如: 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: https://github.com/spring-cloud-samples/config-repo 7 username: trolley 8 password: strongpassword 复制代码 如果不使用HTTPS和用户凭证,也可以使用SHH,在默认目录下存储key(~/.ssh),并且URI指向一个SSH地址(例如:git@github.com:configuration/cloud-configuration)。 Git SSH configuration using properties(使用属性配置GIT SSH) SSH配置可以使用java属性来解决,spring.cloud.config.server.git.ignoreLocalSshSettings值必须设置为true。例如: 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: git@gitserver.com:team/repo1.git 7 ignoreLocalSshSettings: true 8 hostKey: someHostKey 9 hostKeyAlgorithm: ssh-rsa 10 privateKey: | 11 -----BEGIN RSA PRIVATE KEY----- 12 MIIEpgIBAAKCAQEAx4UbaDzY5xjW6hc9jwN0mX33XpTDVW9WqHp5AKaRbtAC3DqX 13 IXFMPgw3K45jxRb93f8tv9vL3rD9CUG1Gv4FM+o7ds7FRES5RTjv2RT/JVNJCoqF 14 ol8+ngLqRZCyBtQN7zYByWMRirPGoDUqdPYrj2yq+ObBBNhg5N+hOwKjjpzdj2Ud 15 1l7R+wxIqmJo1IYyy16xS8WsjyQuyC0lL456qkd5BDZ0Ag8j2X9H9D5220Ln7s9i 16 oezTipXipS7p7Jekf3Ywx6abJwOmB0rX79dV4qiNcGgzATnG1PkXxqt76VhcGa0W 17 DDVHEEYGbSQ6hIGSh0I7BQun0aLRZojfE3gqHQIDAQABAoIBAQCZmGrk8BK6tXCd 18 fY6yTiKxFzwb38IQP0ojIUWNrq0+9Xt+NsypviLHkXfXXCKKU4zUHeIGVRq5MN9b 19 BO56/RrcQHHOoJdUWuOV2qMqJvPUtC0CpGkD+valhfD75MxoXU7s3FK7yjxy3rsG 20 EmfA6tHV8/4a5umo5TqSd2YTm5B19AhRqiuUVI1wTB41DjULUGiMYrnYrhzQlVvj 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- 复制代码 其中hostKeyAlgorithm必须是ssh-dss, ssh-rsa, ecdsa-sha2-nistp256, ecdsa-sha2-nistp384, or ecdsa-sha2-nistp521中的一个。 Force pull in Git Repositories 如果本地copy已经失效(即远程仓库有改动),则Spring Cloud Config Server会强制从从远程库拉取最新的配置。如下: 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: https://github.com/spring-cloud-samples/config-repo 7 force-pull: true 复制代码 如果有多个远程仓库的话,可以使用类似下面的配置: 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: https://git/common/config-repo.git 7 force-pull: true 8 repos: 9 team-a: 10 pattern: team-a-* 11 uri: http://git/team-a/config-repo.git 12 force-pull: true 13 team-b: 14 pattern: team-b-* 15 uri: http://git/team-b/config-repo.git 16 force-pull: true 17 team-c: 18 pattern: team-c-* 19 uri: http://git/team-a/config-repo.git 复制代码 force-pull属性默认是false。 Deleting untracked branches in Git Repositories(删除git仓库中未跟踪的分支) 因为Spring Cloud Config Server会克隆远程仓库到本地并且会一直保持这个分支知道下次重启,所以有一种情况是远程分支已经被删除,但是本地副本还仍然可用。 为了保证本地仓库分支和远程同步,需要设置deleteUntrackedBranches属性,它会使Spring Cloud Config Server强制删除本地未跟踪的分支。例如: 复制代码 1 spring: 2 cloud: 3 config: 4 server: 5 git: 6 uri: https://github.com/spring-cloud-samples/config-repo 7 deleteUntrackedBranches: true 复制代码 deleteUntrackedBranches默认值为false。 3. Spring Cloud Config Client(配置客户端) 3.1 Config First Bootstrap(启动时先读取config server配合) 在classpath中存在Spring Cloud Config Client的任何应用的默认行为如下:当一个config客户端启动
50000+
5万行代码练就真实本领
17年
创办于2008年老牌培训机构
1000+
合作企业
98%
就业率

联系我们

电话咨询

0532-85025005

扫码添加微信