第五篇 分布式配置中心 (config)

    科技2022-08-06  107

    一、分布式配置中心简介

    在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client

     

    二、构建Config Server

    1、创建一个spring-boot项目,取名为config-server,其pom.xml:

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>my-springcloud</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>config-server</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies> </project>

    2、在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能,代码如下:

    @SpringBootApplication @EnableConfigServer public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }

    3、需要在程序的配置文件application.properties文件配置以下:

    server: port: 5000 spring: application: name: config-server cloud: config: label: master #配置仓库的分支 server: git: uri: https://gitee.com/xiaocheng12/my-springcloud.git/ #配置git仓库地址 searchPaths: config #配置仓库路径 username: #访问git仓库的用户名 password: #访问git仓库的用户名密码 spring.cloud.config.server.git.uri:配置git仓库地址spring.cloud.config.server.git.searchPaths:配置仓库路径spring.cloud.config.label:配置仓库的分支spring.cloud.config.server.git.username:访问git仓库的用户名spring.cloud.config.server.git.password:访问git仓库的用户密码

    如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库https认证的方式需要填写用户名和密码,也可以采用ssh方式通过生成的key并在git仓库中配置实现权限访问。本例子是公开仓库,放心使用

    4、在git远程仓库下面创建一个config文件夹,分别创建4个配置文件,这4个配置文件里面均设置了一个from属性,

    from = dev 1.0

    from = prod 1.0

    from = test 1.0

    下面创建一个test分支

    from = dev 2.0

    from = prod 2.0

    from = test 2.0

    http请求地址和资源文件映射如下:label对应git分支,默认为master,profile为环境

    /{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties

    启动程序证明配置服务中心可以从远程程序获取配置信息。:访问http://localhost:5000/myconfig/dev/test,返回json数据如下:

    {     "name":"myconfig",     "profiles":[         "dev"     ],     "label":"test",     "version":"040acd80f55021c183ca728d88bf53b27d251b67",     "state":null,     "propertySources":[         {             "name":"https://gitee.com/xiaocheng12/my-springcloud.git//config/myconfig-dev.properties",             "source":{                 "from":"dev 2.0"             }         }     ] }

     

    三、构建config client

    1、重新创建一个springboot项目,取名为config-client,其pom文件:

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>my-springcloud</artifactId> <groupId>org.example</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>config-client</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> </dependencies> </project>

    2、配置文件,创建bootstrap.properties配置,来指定获取配置文件的config-server位置

    server: port: 5001 spring: application: name: myconfig #对应配置文件规则中的{application}部分 cloud: config: label: test #指明远程仓库的分支,对应配置文件规则中的{label}部分 profile: dev #指明远程仓库的分支下的环境,对应配置文件规则中的{profile}部分 uri: http://localhost:5000/ #对应配置中心的地址 spring.cloud.config.label 指明远程仓库的分支spring.cloud.config.profile dev开发环境配置文件test测试环境pro正式环境spring.cloud.config.uri= http://localhost:5000/ 指明配置服务中心的网址。

    3、创建一个接口来返回配置中心的from属性

    @RestController public class TestController { @Value("${from}") private String from; @Autowired private Environment environment; /** * 从配置文件中获取属性 * @return */ @RequestMapping("/from") public String from() { return this.from; } /** * 从环境变量中获取属性 * @return */ @RequestMapping("/from/environment") public String fromEnvironment() { String from = environment.getProperty("from"); return from; } }

    启动config-client应用,并访问http://localhost:5001/from   我们就可以根据配置文件的内容输出对应的from内容了

    Processed: 0.015, SQL: 8