当前位置:   article > 正文

配置中心(Config-Server)_config server

config server

目录

搭建配置中心

引入依赖

创建启动类

添加配置

创建Git仓库

配置Git仓库

搭建客户端

引入依赖

修改配置

在Git仓库创建配置文件

参考文章


Spring Cloud 配置中心为分布式系统中的服务器端和客户端提供外部化配置支持。通过Config-Server,你可以在一个地方集中对所有环境中的应用程序的外部化配置进行管理。例如,当一个应用程序从开发环境切换到测试环境,然后再从测试环境切换到生产环境,你可以使用Config-Server统一管理这些环境之间的配置,并确保应用程序在迁移时能够拥有它运行所需要的一切配置。简而言之:Config-Server 就是用来实现配置统一管理和不同环境间配置的统一切换的。Config-Server 服务器的后端存储默认使用Git,因此它很容易支持配置环境的标签版本,同时可供多数的内容管理工具去访问。你也可以很容易地添加其他的替代实现,并将它们插入到Spring配置中。

相关产品:

来自淘宝的Diamond:https://github.com/takeseem/diamond

来自百度的Disconf:https://disconf.readthedocs.io/zh_CN/latest/

来自Springcloud的Config-Server:https://cloud.spring.io/spring-cloud-stream/

搭建配置中心

Config-Server配置中心的工作原理如下图所示:

引入依赖

新建一个maven项目,起名为config-center,在其 pom.xml 文件中引入如下依赖:

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.0.6.RELEASE</version>
  5. </parent>
  6. <properties>
  7. <spring-cloud.version>Finchley.SR2</spring-cloud.version>
  8. </properties>
  9. <dependencies>
  10. <!-- Eureka-Client 依赖 -->
  11. <dependency>
  12. <groupId>org.springframework.cloud</groupId>
  13. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  14. </dependency>
  15. <!-- Config-Server 依赖 -->
  16. <dependency>
  17. <groupId>org.springframework.cloud</groupId>
  18. <artifactId>spring-cloud-config-server</artifactId>
  19. </dependency>
  20. </dependencies>
  21. <dependencyManagement>
  22. <dependencies>
  23. <!-- SpringCloud 版本控制依赖 -->
  24. <dependency>
  25. <groupId>org.springframework.cloud</groupId>
  26. <artifactId>spring-cloud-dependencies</artifactId>
  27. <version>${spring-cloud.version}</version>
  28. <type>pom</type>
  29. <scope>import</scope>
  30. </dependency>
  31. </dependencies>
  32. </dependencyManagement>

创建启动类

新建一个Springboot应用的启动类ConfigCenterApplication类,并在上增加@EnableConfigServer注解,用来启用Config-Server。

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.config.server.EnableConfigServer;
  4. @SpringBootApplication
  5. @EnableConfigServer
  6. public class ConfigCenterApplication {
  7. public static void main(String[] args) {
  8. SpringApplication.run(ConfigCenterApplication.class, args);
  9. }
  10. }

添加配置

application.yml 添加配置如下: 

  1. server:
  2. port: 9001
  3. spring:
  4. application:
  5. name: config-center
  6. eureka:
  7. client:
  8. serviceUrl:
  9. defaultZone: http://localhost:8761/eureka/

此时,由于尚未配置用来作为服务器后端存储的Git仓库地址,若启动应用会报如下错误:

  1. ***************************
  2. APPLICATION FAILED TO START
  3. ***************************
  4. Description:
  5. Invalid config server configuration.
  6. Action:
  7. If you are using the git profile, you need to set a Git URI in your configuration. If you are using a native profile and have spring.cloud.config.server.bootstrap=true, you need to use a composite configuration.

 与一般的Spring Boot应用相同,默认情况下Config-Server也通过8080端口启动。为了客户端读取配置方便,你可以把启动端口改为8888(客户端默认会从http://localhost:8888/加载与服务ID相同的配置)。此外,Config-Server还通过spring.config.name=configserver(Config-Server Jar包中有一个configserver.yml 配置文件)配置为我们设置了一个默认配置库,客户端通过配置  spring.cloud.config.discovery.serviceId=configserver 便可直接使用。当然你也可以通过application.yml来对配置中心进行配置。

 

创建Git仓库

本文使用开源中国的码云来创建我们的Git仓库,当然你也可以选择其他的Github或者阿里云Git等创建自己的Git仓库。

点击导航栏中的“+”按钮==>新建仓库,填入仓库信息,完成创建。

 点击 克隆/下载 ==>复制,将Git仓库地址复制下来备用。

配置Git仓库

接下来,在application.yml中添加Git仓库配置如下:

  1. spring:
  2. cloud:
  3. config:
  4. server:
  5. git:
  6. uri: https://gitee.com/pengjunlee/config-cloud.git
  7. username: 你的码云账号
  8. password: 你的账号密码

 再次启动ConfigCenterApplication,发现可以正常启动了。启动完成之后,Eureka注册中心中注册的服务列表如下:

搭建客户端

接下来,我们通过对上一章《微服务下的链路追踪(Sleuth+Zipkin)》中的product-service服务进行改造,来示例如何从配置中心获取配置。

引入依赖

 在product-service的pom.xml中添加配置中心客户端依赖:

  1. <!-- Config-Client 依赖 -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-config-client</artifactId>
  5. </dependency>

或者: 

  1. <!-- Starter-Config 依赖 -->
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-config</artifactId>
  5. </dependency>

修改配置

先将product-service中的application.yml文件改名为bootstrap.yml(bootstrap.yml在应用上下文启动阶段加载,比application.yml早),然后再对其内容进行修改:

  1. # 设置服务(应用)名称
  2. spring:
  3. application:
  4. name: product-service
  5. # 指定用于获取配置的配置中心服务(应用)名称
  6. cloud:
  7. config:
  8. discovery:
  9. enabled: true
  10. serviceId: config-center
  11. profile: dev
  12. # 指定分枝版本,默认为master
  13. label: master
  14. # 指定注册中心地址
  15. eureka:
  16. client:
  17. serviceUrl:
  18. defaultZone: http://localhost:8761/eureka/

注意:如果不指定配置中心,客户端默认会从http://localhost:8888 加载与服务ID相同的配置。

在Git仓库创建配置文件

客户端通过发送Http请求来从配置中心读取配置,这些Http请求的URI遵循以下规则:

  1. /{name}-{profiles}.properties
  2. /{name}-{profiles}.yml || /{name}-{profiles}.yaml
  3. /{label}/{name}-{profiles}.properties
  4. /{label}/{name}-{profiles}.json
  5. /{name}/{profiles}/{label:.*}
  6. /{name}-{profiles}.json
  7. /{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml
  8. /{name}/{profiles:.*[^-].*}
  9. /{name}/{profile}/{label}/**
  10. /{name}/{profile}/{label}/**
  11. /{name}/{profile}/**

其中各个参数的含义如下:

name 服务的ID,即spring.application.name的值,本例中为 product-service;
           profiles 激活的profile,通过spring.cloud.config.profile指定,本例中为 dev;
           label 分枝的版本,通过spring.cloud.config.label指定,本例中为默认值 master; 

接下来我们需要按照上述规则,在Git仓库的相应位置创建配置文件。根据bootstrap.yml中的配置,资源请求地址可以为 /master/product-service-dev.yml 。

在Git仓库的master分枝中创建product-service-dev.ymlproduct-service-test.yml两个文件,分别将服务的启动端口指定为87718772

  1. # product-service-dev.yml
  2. server:
  3. port: 8771
  4. # product-service-test.yml
  5. server:
  6. port: 8772

文件创建完成之后,启动配置中心,先在浏览器对两个文件进行访问。

此时,启动product-service将读取http://localhost:8888/product-service-dev.yml中的配置,即启动端口为8771。若将 bootstrap.yml中的spring.cloud.config.profile的值设置为test,则将读取http://localhost:8888/product-service-test.yml中的配置,应用的启动端口也会相应地变为8772,证明从配置中心读取配置成功。

参考文章

《一篇好TM长的关于配置中心的文章》

https://cloud.spring.io/spring-cloud-stream/

https://spring.io/projects/spring-cloud-config

https://cloud.spring.io/spring-cloud-static/Finchley.SR2/single/spring-cloud.html

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/84055
推荐阅读
相关标签
  

闽ICP备14008679号