介绍
Spring Cloud Config
是Spring Cloud
团队创建的一个全新的项目, 用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持, 为服务端与客户端两个部分.
服务端也成为分布式配置中心
, 是一个独立的微服务应用, 用来连接配仓库并为客户端提供获取配置信息、加密/解密等访问接口.
客户端则是微服务架构中的各个微服务应用或基础设施, 通过指定配置中心来管理应用资源与业务相关的配置内容, 并在启动的时候从配置中心获取和加载配置信息.
Spring Cloud Config
实现了对服务端和客户端中环境变量和属性配置的抽象映射, 除了适用于Spring构建的应用程序外, 也可以在其他语言运行的应用程序中运行.
Spring Cloud Config
默认采用Git
存储配置信息, 也提供了对SVN
、本地文件系统
存储方式的支持.
使用
-
服务端
-
Maven 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
添加依赖并在应用程序主类中添加
@EnableConfigServer
启动服务端功能.-
简单配置
spring.application.name=anybbo-config-server server.port=7001 spring.cloud.config.server.git.uri=https://github.com/R-anybbo/spring-cloud-anybbo/ spring.cloud.config.server.git.search-paths=anybbo-config-server/src/main/resources/config-repo spring.cloud.config.server.git.username=anybbo@gmail.com spring.cloud.config.server.git.password=....
spring.cloud.config.server.git.uri
配置仓库地址spring.cloud.config.server.git.search-paths
配置仓库路径下的相对搜索位置, 可以配置多个spring.cloud.config.server.git.username
Git用户名spring.cloud.config.server.git.password
Git密码
-
配置规则
在指定路径下存在4个配置文件, 分别对应不同环境,
anybbo.properties
,anybbo-dev.properties
,anybbo-test.properties
,anybbo-prod.properties
简单的配置属性from=git-test-1.0
至此可以通过浏览器访问配置内容. 访问配置URL和配置文件的映射关系
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
例:
http://localhost:7001/anybbo/prod
{ name: "anybbo", profiles: [ "prod" ], label: null, version: "ad6e9be60d3428a2b9e45991a05366d0726e6587", state: null, propertySources: [{ name: "https://github.com/R-anybbo/spring-cloud-anybbo//anybbo-config-server/src/main/resources/config-repo/anybbo-prod.properties", source: { from: "git-prod-1.0" } }, { name: "https://github.com/R-anybbo/spring-cloud-anybbo//anybbo-config-server/src/main/resources/config-repo/anybbo.properties", source: { from: "git-default-1.0" } } ] }
-
-
客户端
-
Maven 依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
-
配置
创建
bootstrap.properties
, 指定配置中心的位置spring.application.name=anybbo spring.cloud.config.profile=dev spring.cloud.config.label=master spring.cloud.config.uri=http://localhost:7001/ server.port=7002
spring.application.name
对应规则中的{application}
spring.cloud.config.profile
对应规则中的{profile
spring.cloud.config.label
对应规则中的{label}
spring.cloud.config.uri
配置中心地址
需要注意配置必须在
bootstrap.properties
中, 对于jar
包之外的配置文件加载会优先于应用jar
包内的配置, 而通过bootstrap.properties
对配置中心配置, 使应用从配置中心获取一些外部配置, 这些信息的优先级比本地内容高, 从而实现外部化配置.
创建一个RESTful
接口来返回配置中心的from
属性, 通过@Value("${from}")
绑定配置服务中心配置的from
属性@RefreshScope @RestController public class TestController { @Value("${from}") private String from; @GetMapping("/from") public String from() { return this.from; } }
启动并访问
http://localhost:7002/from
, 可以得到对于环境的from
内容.
-
本文由 anybbo 创作,采用 知识共享署名4.0
国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: Dec 17,2020