微服务SpringConfig配置中心详解
目录
- 前言
- 一、SpringConfig概念介绍
- 二、配置中心服务器端搭建
- 三、配置中心客户端搭建
- 总结
前言
提示:这里可以添加本文要记录的大概内容:
例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。
一、SpringConfig概念介绍
在实际操作中会出现多个微服务操作同一个数据库的情况,那么对于每个微服务都需要重复配置数据库信息,而且当数据库信息改动时也难以维护。因此一套集中的、动态的配置管理设施是必不可少的。
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。SpringCloud Config 具体分为客户端和服务端两部分。
具体作用:
- 集中管理配置文件;
- 不同运行环境不同的配置,动态化地进行配置更新,分环境部署(dev/test/prod/release)
- 运行期间动态调整配置,不需要在每个服务部署的机器上编写配置文件,配置变动各个微服务能够自动拉取信息
二、配置中心服务器端搭建
配置中心服务端
1、SpringCloud Server使用Git 作为远程的配置文件仓库存储地址,我们需要先在远程建立好仓库
2、修改pom.XMLhttp://www.devze.com、application.yml
<!-- SpringConfig Server --> <dependency> &lphpt;groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
server: port: 3344 spring: application: name: cloud-config-server cloud: config: server: git: #配置远程仓库的地址 uri: https://gitee.com/loserii/my_-spring-cloud_config.git password: xjxhhxJava2048 username: loserii search-paths: xxxxx #git仓库下相对搜索地址 label: master #配置远程分支名 eureka: instance: instance-id: configServer prefer-ip-address: true client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka
配置完成后启动服务,本地通过HTTP请求访问远程仓库上的配置文件时遵循以下格式的请求资源:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
{label}android
作为远程仓库的分支;{application}-{profile}
作为远程仓库上的配置文件名,比如config-test.yml
3、此外还可以配置多个存储库,配置相应的匹配规则来读取文件
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
patterphpn表示模式匹配,是带有通配符{application}/{profile}
名称的逗号分隔列表。如果pattern不匹配任何模式,那么它就会使用spring.cloud.config.server.git.uri
下的默认uri。
三、配置中心客户端搭建
3.1 配置同步机制介绍
客户端通过SpringCloud Config Server来获取到远程的Config环境配置,但是如果想要自己定义一些额外的配置,就需要另外创建一个配置文件。
客户端项目中包含两种配置文件:application.yml
(用户级的资源配置项)、bootstrap.yml
(系统级的资源配置项,优先级更高)
SpringCloud 会创建一个 Bootstrap Context
上下文,该上下文作为 Application Context
的父上下文。初始化的时候,Bootstrap Context
负责从外部源加载配置属性并且解析配置。这两个上下文共享一个从外部获取的Environment
。
什么是Environment
?
Environment用于存储服务器的配置数据,管理此行为的是EnvironmentRepository。Environment是Spring Environment(包括PropertySources作为主要功能)的域的浅层副本。
Environment资源由三个变量参数化:
{application}
映射到客户端的spring.application.name
;{profile}
映射到客户端上的spring.profiles.active
;{label}
标记版本这一组件;
3.2 详细配置
客户端从配置服务中心获取配置。
- 修改 pom.xml、bootstrap.yml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-sthttp://www.devze.comarter-web</artifactId> </dependency>
spring: application: name: ConfigCenterClient cloud: config: fail-fast: false #客户端连接失败时开启重试,需要结合spring-retry、spring-aop label: master #获取配置文件的分支名 name: config #获取的文件名 profile: test #文件后缀 uri: http://localhost:3344 #配置中心服务端地址 eureka: instance: instance-id: config-client prefer-ip-address: true client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://localhost:7001/eureka server: port: 3355
注意与eureka、cloud相关的配置需要放在bootstrap.yml
文件中,程序启动时会先读取bootstrap.yml
然后再读取application.yml
- 测试配置读取
@RestController @RequestMapping("/test") public class TestController { @Value("${config.info}") private String info; @GetMapping("/testInfo") public String test(){ return info; } }
3.3 分布式下的问题
测试时发现当在远程仓库修改配置文件数据后,本地的SpringCloud Config Server
能够在不重启的情况下接收到新配置,但是本机的SpringCloud Config Client
就不能直接获取,必须进行重启。这在微服务很多时是不方便处理的。
解决方法: 客户端向外部暴露监控的端点,结合@RefreshScope
注解来解决
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。
精彩评论