天天看点

分布式配置中心Config - Spring Cloud系列(六)

本文章基于spring-boot-starter-parent 2.0.6RELEASE,spring-cloud-dependencies Finchley.SR2。

Config是什么

Spring Cloud Config用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,分为服务端和客户端两个部分。其中服务端又称为分布式配置中心,是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定配置中心来管理应用资源和业务相关的配置内容。服务器存储后端的默认实现使用git,也可以使用SVN仓库或者本地文件系统。

结合我们之前的示例,用一张图展示下config的运作机制:

分布式配置中心Config - Spring Cloud系列(六)

Config使用

构建配置中心(基于github)

新建一个Spring Boot工程,命名为config-server。

第一步:pom文件中引入config依赖

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

第二步:新增Spring Boot启动类AppConfigServer

@SpringBootApplication
@EnableConfigServer
public class AppConfigServer {

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

第三步:application.properties添加配置服务相关信息

server:
  port: 8080

spring:
  application:
    name: config-server

  cloud:
    config:
      server:
        git:
          uri: https://github.com/zqhao/my-spring-cloud-config.git 
          search-paths: eureka #配置仓库路径下的相对搜索位置,可以配置多个
         # username: username
         # password: password
           

关于git配置信息如下:

  • spring.cloud.config.server.git.uri: 配置Git仓库位置
  • spring.cloud.config.server.git.search-paths: 配置仓库路径下的相对搜索位置,默认是只在根目录下寻找,配置相对位置后,除了根目录还回去配置的位置下寻找。
  • spring.cloud.config.server.git.username: 访问Git仓库的用户名
  • spring.cloud.config.server.git.password: 访问Git仓库的密码

上面的配置文件连接的是github公有仓库,所以不需要填写用户名/密码。

到这里,分布式配置中心就搭建完成了。

下图展示了我在github仓库上预先放置的一些文件:

分布式配置中心Config - Spring Cloud系列(六)

下面来看下如何获取github上存放的配置信息:

浏览器访问

http://8080/order-dev.yml

,即可访问到配置内容。

分布式配置中心Config - Spring Cloud系列(六)

访问配置信息的URL与配置文件的映射关系如下:

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

其中

{lable}

对应Git上不同的分支,默认为master。

{application}

代表应用名,

{profile}

代表环境名。针对上面访问的路径

http://8080/order-dev.yml

,代表order应用的dev环境下的配置。

浏览器访问

http://8080/order/dev

,可以看出该JSON中返回了应用名order,环境名dev,分支名为null(代表master),version版本号,以及default和dev环境的配置内容。

{
	"name": "order",
	"profiles": ["dev"],
	"label": null,
	"version": "12beda073b30fbd55f52d7c12983c58ecdafd34c",
	"state": null,
	"propertySources": [{
		"name": "https://github.com/zqhao/my-spring-cloud-config.git/order-dev.yml",
		"source": {
			"server.port": 7000,
			"eureka.client.serviceUrl.defaultZone": "http://localhost:8081/eureka",
			"eureka.instance.instance-id": "order",
			"eureka.instance.prefer-ip-address": true
		}
	}, {
		"name": "https://github.com/zqhao/my-spring-cloud-config.git/order.yml",
		"source": {
			"spring.application.name": "order-micro",
			"server.port": 7000
		}
	}]
}
           

对于yml 和properties类型config可以完美转换, 也就是说你存的是yml但是可以读取为properties类型的反过来也是如此,访问

http://localhost:8080/order-dev.properties

分布式配置中心Config - Spring Cloud系列(六)

客户端从config上获取配置

我们改造下之前的Spring Boot应用order,让order的配置改为从config上获取。

第一步:pom.xml中引入依赖

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

第二步:创建bootstrap.yml,该文件用来指定获取配置文件的位置

spring cloud有一个“引导上下文"的概念,这是主应用程序的父上下文。引导上下文负责从配置服务器加载配置属性,以及解密外部配置文件中的属性。和主应用程序加载application.(yml 或properties)中的属性不同,引导上下文加载(bootstrap.)中的属性。配置在 bootstrap.*中的属性有更高的优先级,因此本地配置无法覆盖它们。

改造前

application.yml

server:
  port: 7000

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8081/eureka
  instance:
    instance-id: order #此实例注册到eureka服务端的唯一的实例ID
    prefer-ip-address: true #是否显示IP地址
    
spring:
  application:
    name: order-micro #此实例注册到eureka服务端的name
           

改造后

application.yml文件置空

bootstrap.yml

spring:
  application:
    name: order
  cloud:
    config:
      profile: dev
      label: master
      uri: http://localhost:8080
           

github上order-dev.yml

分布式配置中心Config - Spring Cloud系列(六)

启动order工程,访问

http://localhost:7000/getOrder.do

,正常访问到页面,说明从config-server中成功读取到配置信息。(eureka不启动控制台会报错,但是不影响我们访问系统接口)

分布式配置中心Config - Spring Cloud系列(六)

Config高可用集群

传统模式

分布式配置中心Config - Spring Cloud系列(六)

服务模式

将config-server作为一个普通的微服务应用,纳入Eureka服务治理体系中。

演示步骤:

第一步:Config Server中引入Eureka

新建一个Spring Boot应用,命名为config-server1,内容和config-server一样,更改下端口号即可。config-server/config-server1中均引入eureka依赖,增加服务注册与发现功能。

第二步:修改client的配置文件(order工程的bootstrap.yml)修改如下

spring:
  cloud:
    config:
      name: order
      profile: dev
      label: master
      discovery:
        enabled: true # 开启通过服务来访问Config Server功能
        service-id: config-server # 指定Config Server服务注册名
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8081/eureka/
           

启动eureka、config-server、config-server1、order,访问

http://localhost:7000/getOrder.do

。这里不再演示。

------------本文结束感谢您的阅读------------

继续阅读