一、前言
本文是根据笔者上篇文章项目进行修改,若有不懂,请转《Spring Cloud 入门 之 Zuul(五)附源码》
二、介绍
Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。
特征:
Spring Cloud Config Server功能:
用于外部配置的HTTP,基于资源的API(名称 - 值对或等效的YAML内容)
加密和解密属性值(对称或非对称)
使用可轻松嵌入Spring Boot应用程序
@EnableConfigServer
Config Client功能(适用于Spring应用程序):
绑定到Config Server并
Environment
使用远程属性源初始化Spring加密和解密属性值(对称或非对称)
三、实战演练
服务实例 | 端口 | 描述 |
---|---|---|
eureka-server | 9000 | 注册中心(Eureka 服务端) |
config-server | 10000 | 配置中心(Eureka 客户端、Config 服务端) |
api | 80 | 服务接口(Eureka 客户端、Config 客户端) |
3.1上传配置
在 GitHub 上新建一个私有仓库,名为config
创建application-dev.yml和application-test.yml,这是之前api中application.yml的参数。
两者之间只有env 的值不同,其中一个是 dev ,另一个是 test。
provider: # 名称 name: provider # 版本 version: 1.0 # 版权年份 copyrightYear: 2019 # 文件上传路径 profile: profile/ # 获取ip地址开关 addressEnabled: trueenv: dev1 server: port: 80spring: application: name: api eureka: client:# register-with-eureka: false # 不向注册中心注册自己 fetch-registry: true # 是否检索服务 service-url: defaultZone: http://localhost:9000/eureka/ # 注册中心访问地址feign: hystrix: enabled: true #开启服务降级功能 client: config: default: connect-timeout: 10000 read-timeout: 20000 service-test: connect-timeout: 10000 read-timeout: 20000management: endpoints: web: exposure: include: "*"
3.2 创建config 服务端
新建一个 spring boot 项目,名为 config-server
3.2.1 添加依赖
<!-- eureka 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.1.0.RELEASE</version> </dependency> <!-- config 服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> <version>2.1.0.RELEASE</version> </dependency>
3.2.2 application.yml
server: port: 10000spring: application: name: CONFIG cloud: config: server: git: uri: https://github.com/Uncle-LiuY/config.git username: ****** password: ****** eureka: instance: instance-id: config-api client: service-url: defaultZone: http://localhost:9000/eureka/ 服务注册中心ַ
3.2.3 修改启动类
@EnableConfigServer @EnableEurekaClient
3.2.4 启动测试
依次启动eureka-server、config-server
在浏览器访问 http://localhost:10000/application-dev.yml 和 http://localhost:10000/application-test.yml
其中,访问规则如下:
<IP:PORT>/{name}-{profiles}.yml
<IP:PORT>/{label}/{name}-{profiles}.yml
name:文件名,可当作服务名称
profiles: 环境,如:dev,test,pro
lable: 分支,指定访问某分支下的配置文件,默认拉去 master 分支。
3.3 config 客户端
对api进行修改
3.3.1 添加配置:
<!-- config 客户端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> <version>2.1.0.RELEASE</version> </dependency>
3.3.2 删除 application.yml,并新建 bootstrap.yml,保存如下内容:
spring: application: name: api cloud: config: discovery: enabled: true service-id: CONFIG # config-server 在注册中心的名称 profile: dev # 指定配置文件的环境 eureka: client: service-url: defaultZone: http://localhost:9000/eureka/ # 注册中心访问地
配置中,通过 spring.cloud.config.discovery.service-id 确定配置中心,再通过 spring.application.name 的值拼接 spring.cloud.config.profile 的值,从而确定需要拉去从配置中心获取的配置文件。(如:application-dev)
注意:必须保留 eureka 注册中心的配置,否则 API 无法连接注册中心,也就无法获取配置中心(config-server)的访问信息。
3.3.4 测试类
@RestController@RequestMapping("/test")public class TestController { @Value("${env}") private String env; // 从配置中心获取 @RequestMapping("/getConfigInfo") public String getConfigInfo() { return env; } }
打开浏览器访问 http://localhost/test/getConfigInfo,结果如下图:
根据配置文件中的端口进行访问,这里配置的是80端口
成功获取 config-server 从远程私有仓库拉去的数据,由于在 bootstrap.yml 中配置了 spring.cloud.config.profile=dev,因此拉取到的数据就是 application-dev.yml 中的数据。
在GitHub上修改配置文件的参数,依次启动config-server、api
4.项目源码
下载
当我们修改远程私有仓库的配置文件时,Config Server 如何知道是否该重新获取远程仓库数据呢?
现在已知唯一的解决方式就是重启 Config Client 项目,在项目启动时会请求 Config Server 重新拉去远程私有仓库数据。但是,如果是在生产环境下随便重启项目必定会影响系统的正常运行,那有没有更好的方式解决上述的问题呢?
详情请查看《Spring Cloud 整合 Bus(附源码)》
评论 (0)