第七篇:SpringCloud之分布式配置中心(Spring Cloud Config)

版权声明:本文为博主原创文章,欢迎转载,转载请注明作者、原文超链接 ,博主地址:https://blog.csdn.net/mr_chenjie_c。 https://blog.csdn.net/Mr_Chenjie_C/article/details/85323857

随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多。某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错。配置中心便是解决此类问题的灵丹妙药。

Spring Cloud Config

在我们了解spring cloud config之前,我可以想想一个配置中心提供的核心功能应该有什么

  • 提供服务端和客户端支持
  • 集中管理各环境的配置文件
  • 配置文件修改之后,可以快速的生效
  • 可以进行版本管理
  • 支持大的并发查询
  • 支持各种语言
  • Spring Cloud Config可以完美的支持以上所有的需求。

Spring Cloud Config项目是一个解决分布式系统的配置管理方案,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。它包含了Client和Server两个部分,Server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,Client通过接口获取数据、并依据此数据初始化自己的应用。

首先在github上面创建了一个文件夹config-repo用来存放配置文件,为了模拟生产环境,我们创建以下三个配置文件:

// 开发环境
config-dev.properties
// 测试环境
config-test.properties
// 生产环境
config-pro.properties

每个配置文件中都写一个属性config.hello,属性值分别是 hello im dev/test/pro 。下面我们开始配置server端。

构建Config Server

创建一个spring-boot项目,取名为springcloud-config-server,添加依赖

<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-server</artifactId>
</dependency>

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

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

在程序的配置文件application.yml配置以下:

server:
  port: 8088

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/chenjary/SpringCloud/
          searchPaths: config-repo
          username:
          password:
      label: master
  • 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仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,放心使用。

Spring Cloud Config也提供本地存储配置的方式。我们只需要设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:E:/properties/属性来指定配置文件的位置。虽然Spring Cloud Config提供了这样的功能,但是为了支持更好的管理内容和版本控制的功能,还是推荐使用git的方式。

启动程序:访问http://localhost:8088/config/dev
在这里插入图片描述
证明配置服务中心可以从远程程序获取配置信息。

上述的返回的信息包含了配置文件的位置、版本、配置文件的名称以及配置文件中的具体内容,说明server端已经成功获取了git仓库的配置信息。

如果直接查看配置文件中的配置信息可访问:http://localhost:8088/config-dev.properties,返回
在这里插入图片描述
修改配置文件config-dev.properties中配置信息为:config.hello=hello im dev update,再次在浏览器访问http://localhost:8088/config-dev.properties,返回:neo.hello: hello im dev update。说明server端会自动读取最新提交的内容

仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

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

以config-dev.properties为例子,它的application是config,profile是dev。client会根据填写的参数来选择读取对应的配置。

构建Config Client

再创建一个spring-boot项目,取名为springcloud-config-client,添加依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>

该工程需要配置两个配置文件,application.yml和bootstrap.properties
application.yml:


bootstrap.properties:

spring.cloud.config.name=config
spring.cloud.config.profile=test
spring.cloud.config.uri=http://localhost:8088/
spring.cloud.config.label=master
  • spring.cloud.config.name:对应{application}部分
  • spring.cloud.config.profile:对应{profile}部分
  • spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
  • spring.cloud.config.uri:配置中心的具体地址
  • spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。

特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.yml,而bootstrap.properties的加载也是先于application.yml。

程序的入口类,写一个API接口“/hello”,返回从配置中心读取的config.name变量的值,代码如下:

@SpringBootApplication
@RestController
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }

    @Value("${config.hello}")
    String hello;

    @RequestMapping(value = "/hello")
    public String hello() {
        return hello;
    }
}

打开网址访问:http://localhost:8089/hello,网页显示:
在这里插入图片描述
这就说明,config-client从config-server获取了config.name的属性,而config-server是从git仓库读取的,如图:
在这里插入图片描述

源码下载:https://github.com/chenjary/SpringCloud/tree/master/springcloud-config

猜你喜欢

转载自blog.csdn.net/Mr_Chenjie_C/article/details/85323857