配置集中化管理--配置中心(Spring Cloud Config)

294 阅读5分钟

这是我参与2022首次更文挑战的第20天,活动详情查看:2022首次更文挑战

 1、简介

在基于微服务架构下开发,由于服务数量巨多、在高可用性的驱使下要进行集群时,众多节点及服务的配置管理,变得苦不堪言,简直成了体力活,稍微不细心将会出现配置错误。为了方便服务配置信息的统一集中化管理,实时更新,SpringCloud中Spring Cloud Config组件(配置中心),就用来解决这类问题,以达到配置集中化管理,让你可以把配置集中放到远程服务器,集中化管理集群配置。

从配置中心Spring Cloud Config的源码(spring-cloud-config-server)中,可以看出目前配置中心支持本地存储、Git仓库存储、SVN仓库存储、数据库存储方式,其他存储方式可参考源码自行实现即可。

2、配置中心服务端Config Server

配置中心服务端依赖于spring-cloud-config-server包,创建一个普通的springBoot项目springCloudConfig-git(此处以git方式存储为例),额外添加如下依赖包:

<!-- config-server配置中心 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-config-server</artifactId>
	<version>2.0.0.RELEASE</version>
</dependency>

在启动类ConfigServerGitApplication中添加@EnableConfigServer注解,即:开启支持配置中心的功能,如下:

package com.xcbeyond.springcloud.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * 配置中心。</br>
 * 以git方式存储配置
 * @author xcbeyond
 * 2018年9月17日下午11:44:10
 */
@SpringBootApplication
//开启配置服务器的支持
@EnableConfigServer
//开启作为Eureka Server的客户端的支持
@EnableEurekaClient
public class ConfigServerGitApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConfigServerGitApplication.class, args);
	}
}

在配置文件application.yml中进行如下配置:

此处为git仓库集中管理配置文件的配置方式。

(本例中配置了注册中心,如果不需要,则可删除注册中心的配置)

#当前配置服务器端口
server:
  port: 8888
spring:
  application:
    name: config-center

  cloud:
  #配置中心配置
    config:
      server:
        git:
          #配置git仓库地址
          uri: https://github.com/xcbeyond/springCloudLearning.git
          #配置仓库路径
          search-paths: config-repo
          #配置文件本地临时存储目录
#          basedir: target/config
          #访问git仓库的用户名.如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
#          username: xxx
#          password: xxx
      #配置仓库的分支
      label: master

#Eureka配置 
eureka:
  client:
    serviceUrl:
      #服务注册中心地址,需按照注册中心IP进行对应修改
      defaultZone: http://register-center:8761/eureka/
  instance:
    prefer-ip-address: true

配置说明:

  • spring.cloud.config.server.git.uri  配置git仓库地址
  • spring.cloud.config.server.git.search-paths 配置仓库路径
  • spring.cloud.config.server.git.basedir 配置文件本地临时存储目录。一般无需配置。
  • spring.cloud.config.server.git.username 访问git仓库的用户名.
  • spring.cloud.config.server.git.password 访问git仓库的密码
  • spring.cloud.config.label 配置仓库的分支

如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写。本例中使用的公开仓库,故未配置用户名和密码,git仓库地址为github.com/xcbeyond/sp…, 在目录config-repo下用来存放各个服务的配置文件进行集中管理,其中有一个config-client.yml配置文件,内容如下:

server:
port: 8889

eureka:
client:
serviceUrl:
#服务注册中心地址,需按照注册中心IP进行对应修改
defaultZone: http://register-center:8761/eureka/\       
test: this is a test message!

启动配置中心springCloudConfig-git服务,访问http://localhost:8888/config-client/test, 则会返回如下结果,即:包含git上config-client.yml配置文件的内容。

{"name":"config-client","profiles":["test"],"label":null,"version":"46cfa51b88a1f5b08ee69bc67af187b1dc054ddf","state":null,"propertySources":[{"name":"https://github.com/xcbeyond/springCloudLearning.git/config-repo/config-client.yml","source":{"server.port":8889,"eureka.client.serviceUrl.defaultZone":"http://register-center:8761/eureka/","test":"this is a test message!"}}]}

由此证明从配置中心获取配置信息成功,说明配置中心搭建成功。

http请求地址和资源文件映射如下:

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

3、配置中心客户端ConfigClient

创建另外一个springboot项目springCloudConfigClient,作为配置中心的客户端,即从通过配置中心获取集中配置信息的服务。客户端依赖spring-cloud-starter-netflix-eureka-client包,pom.xml中额外添加如下依赖:

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

其配置文件为bootstrap.yml,注意该文件名是bootstrap,不再是application。应该bootstrap配置文件优先于application配置文件加载。bootstrap.ym中只配置配置中心相关信息,其他配置统一集中存放在git上统一管理,以供该服务使用。

bootstrap.yml配置文件内容如下:

spring:
  application:
    name: config-client
  cloud:
    config:
      #配置中心地址
      uri: http://config-center:8888/
      #远程配置中心配置文件名
      name: config-client
      #远程仓库的分支
      label: master
  • spring.cloud.config.uri 配置中心地址,用于明确访问的是哪个配置中心
  • spring.cloud.config.name 远程配置中心配置文件名
  • spring.cloud.config.label 远程仓库的分支

写一个测试接口/hi,返回配置文件中test的值,用来测试从配置中心的配置文件获取配置。

package com.xcbeyond.springcloud.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 从服务配置中心获取配置信息
 * @author xcbeyond
 * 2018年8月7日下午4:11:00
 */
@RestController
public class GetConfigServerController {
	//通过服务配置中心获取git上配置文件里的key
	@Value("${test}")
	private String test;
	
	@RequestMapping("/hi")
	public String hi() {
		return test;
	}
}

启动springCloudConfigClient,通过访问http://localhost:8889/hi, 则会返回“this is a test message!”