大家好, 有一段时间没有写技术博客了. 由于工作上的事情, 这方面很难分配时间. 近几年随着服务化的兴起, 一批服务化的框架应运而生, 像 dubbo,thrift,spring-cloud 等. 在国内使用 dubbo 的公司非常多, dubbo 也是 java 程序员面试时必备知识点, 而且它的官方文档写的非常清晰易懂, 这都使得 dubbo 的普及非常容易. thrift 是 apache 贡献的, 似乎也流行了一段时间, 小编对这个框架不是很了解. 随后 spring-cloud 一经推出, 便在技术圈流行起来, 这段时间小编也在学习 spring-cloud, 将学到的东西和大家分享一下, 如果有不对的地方, 还请大家多多指正.
一, 简介
Spring Cloud Config 为分布式系统中的外部配置提供服务器和客户端支持. 使用 Config Server, 您可以为所有环境中的应用程序管理其外部属性. 它非常适合 spring 应用, 也可以使用在其他语言的应用上. 随着应用程序通过从开发到测试和生产的部署流程, 您可以管理这些环境之间的配置, 并确定应用程序具有迁移时需要运行的一切. 服务器存储后端的默认实现使用 git, 因此它轻松支持标签版本的配置环境, 以及可以访问用于管理内容的各种工具.
Spring Cloud Config 服务端特性
HTTP, 为外部配置提供基于资源的 API(键值对, 或者等价的 YAML 内容)
属性值的加密和解密(对称加密和非对称加密)
通过使用 @EnableConfigServer 在 Spring boot 应用中非常简单的嵌入.
Config 客户端的特性(特指 Spring 应用)
绑定 Config 服务端, 并使用远程的属性源初始化 Spring 环境.
属性值的加密和解密(对称加密和非对称加密)
入门示例:
只要 classpath 下有 Spring Boot Actuator 和 Spring Config Client,Spring Boot 应用就会尝试连接配置服务 http://localhost:8888, 这个地址是 spring.cloud.config.uri 的默认地址. 如果你想修改这个地址, 你可以在 bootstrap.[yml 或 properties]中设置 spring.cloud.config.uri 或者通过系统属性或者通过环境变量.
- @Configuration
- @EnableAutoConfiguration
- @RestController
- public class Application {
- @Value("${config.name}")
- String name = "World";
- @RequestMapping("/")
- public String home() {
- return "Hello" + name;
- }
- public static void main(String[] args) {
- SpringApplication.run(Application.class, args);
- }
- }
上面例子中的 config.name 可以来自本地的配置文件, 也可以来自远程的配置服务. 默认情况下, 远程的配置服务将优先使用.
为了运行你自己的配置服务中心, 你可以使用 spring-cloud-config-server 依赖, 和 @EnableConfigServer 注解. 如果你设置了 spring.config.name=configserver, 应用将会运行在 8888 端口, 并且从一个样本仓库提供数据. 你需要设置 spring.cloud.config.server.git.uri 来指定你自己的配置数据. 默认的, 它是一个 git 仓库, 也可以配置成本地的文件系统.
二, Spring Cloud Config 服务端
服务器为外部配置 (键称值对或等效的 YAML 内容) 提供了基于资源的 HTTP. 它可以在 Spring Boot 应用中使用 @EnableConfigServer 内嵌. 例子如下:
- @SpringBootApplication
- @EnableConfigServer
- public class SpringCloudConfigServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(SpringCloudConfigServerApplication.class, args);
- }
- }
像所有的 Spring Boot 应用一样, 它默认运行在 8080 端口, 你可以通过多种方式将其切换到 8888 端口. 最简单的可以设置 spring.config.name=configserver(在 Config Server 的 jar 包有一个 configserver.yml), 它设置了一个默认的配置仓库. 另外一种方式是使用你自己的 application.properties, 这也是小编推荐的方式:
server.port: 8888
spring.cloud.config.server.git.uri: git 地址
git 地址中是你的 YAML 或者 properties 文件.
环境仓库
你想在哪里存储配置数据? 支持这种行为的策略是 EnvironmentRepository, 它服务于 Environment 实例. 这个 Environment 是 Spring Environment 的一个浅副本. Environment 通过 3 个变量被参数化.
{application}映射客户端的 "spring.application.name"
{profile}映射客户端的 "spring.profiles.active"(逗号分隔列表)
{label}它是服务端的特性, 标记版本的一组配置文件
仓库的实现通常表现的像 Spring boot 加载配置文件一样,"spring.config.name" 等于 {application} 参数, "spring.profiles.active" 等于 {profile} 参数. profiles 的优先规则和正常的规则是一样的, 活动的 profiles 优于默认的. 如果有多个 profiles, 则最后一个胜出.
客户端的配置实例:
- spring:
- application:
- name: foo
- profiles:
- active: dev,mysql
在 Spring Boot 应用中, 这些参数也可以通过环境变量或者命令行参数设置.
git 后端
EnvironmentRepository 的默认实现是使用 git 后端, 它对管理更新, 物理环境和审核更改非常的方便. 要改变仓库的地址, 你可以在配置服务端设置 "spring.cloud.config.server.git.uri" 属性(在 application.properties 文件中). 如果你用 file: 开头设置它, 它将从本地仓库运行, 这样可以在没有服务端的情况下非常快速和简单的启动. 这种情况, 服务端将直接在本地仓库中运行. 为了扩展配置服务并使它高可用, 你需要把服务的所有实例指向同一个仓库, 因此只有共享文件系统可以工作. 即使在这种情况下, 最好使用共享文件系统存储库的 ssh: 协议, 以便服务器可以将其克隆并使用本地工作副本作为缓存.
该仓库的实现将 HTTP 资源中的 {label} 参数映射到 git 的标签 (提交 id, 分支名称或者 tag). 如果 git 分支或者 tag 名称中包含 "/", 则 HTTP URL 中的 label 要使用特殊字符 "(_)" 代替. 例如: 如果分支的名称是 foo/bar, 则 HTTP 中的 label 的格式为 foo(_)bar. 这个特殊字符也可以用到{application} 参数中.
git URI 中的占位符
Spring Cloud Config Server 支持在 git URL 中使用占位符, 使用{application} 和 {profile}(如果使用{label}, 请记住它是使用在 git 标签中的). 因此你可以轻松的支持 "一个应用一个仓库" 的原则. 如下:
- spring:
- cloud:
- config:
- server:
- git:
- uri: https://github.com/myorg/{application}
或者一个环境一个仓库的原则, 使用 {profile} 代替 {application}. 另外在{application} 参数中使用特殊字符 "(_)" 可以支持多组织.
- spring:
- cloud:
- config:
- server:
- git:
- uri: https://github.com/{application}
{application}参数的格式为 "organization(_)application".
模式匹配和多仓库
在 {application} 和{profile}参数中使用模式匹配可以支持更多复杂的需求. 模式的格式是一组逗号分隔的{application}/{profile}, 其中的参数可以使用通配符. 例如:
- 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
如果 {application}/{profile} 没有匹配到任何模式, 它将使用默认的仓库地址: spring.cloud.config.server.git.uri. 上面的例子中,"simple" 仓库匹配的是 "simple/*"(它仅仅匹配一个仓库 simple, 在所有的环境下)."local" 仓库将匹配所有 {application} 的名字以 "local" 开头的, 并且也是在所有的环境下."/*" 前缀自动添加到所有没有设置 {profile} 的模式中.
每一个仓库也可以在子目录下存储配置文件, 模式匹配也可以用于搜索这些目录, 需要制定 searchPaths, 如下:
- spring:
- cloud:
- config:
- server:
- git:
- uri: https://github.com/spring-cloud-samples/config-repo
- searchPaths: foo,bar*
上面的例子中, 将在 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 的仓库将在服务端启动时进行克隆, 其他的仓库将在第一次请求时克隆.
认证
如果远程的 git 仓库需要用户名和密码, 可以参照下面的例子
- spring:
- cloud:
- config:
- server:
- git:
- uri: https://github.com/spring-cloud-samples/config-repo
- username: trolley
- password: strongpassword
到此, Spring Cloud Config 服务端就介绍到这里, 还有一些不常用的功能在这里就不介绍了, 大家可以参照 spring cloud 官网. Spring Cloud Config 服务端的代码示例可以参照我的 GitHub 地址: https://github.com/bigbugliu/spring-cloud-config-server.
三, Spring Cloud Config 客户端
Spring Boot 应用可以立即使用 Spring Config Server. 只要在 classpath 中有 Spring Cloud Config Client 的 jar 包, 这个应用就会请求配置的服务端. 他将使用绑定的配置服务器 (spring.cloud.config.uri 中配置的) 的属性初始化 spring 环境.
在某些情况下, 如果服务无法连接到配置服务器, 则可能希望启动服务失败. 如果这是所需的行为, 请设置引导配置属性
spring.cloud.config.failFast=true
, 客户端将以异常停止.
如果您希望配置服务器在您的应用程序启动时可能偶尔不可用, 您可以要求它在发生故障后继续尝试. 首先, 您需要设置 spring.cloud.config.failFast=true, 然后您需要将 spring-retry 和 spring-boot-starter-aop 添加到您的类路径中. 默认行为是重试 6 次, 初始退避间隔为 1000ms, 指数乘数为 1.1, 用于后续退避. 您可以使用 spring.cloud.config.retry.* 配置属性配置这些属性(和其他).
详细代码请参考我的 GitHub:https://github.com/bigbugliu/spring-boot-demo.
Spring Cloud Config 就介绍完了, 欢迎大家在评论区讨论.
来源: https://www.cnblogs.com/boboooo/p/8796636.html