一、简介

Spring Cloud Config是一个配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。 Spring Cloud Config分为两部分

  • config-server:配置服务端,负责管理配置信息
  • config-client:配置客户端,通过调用server端暴露的接口来换取配置信息

二、项目实例

创建maven工程microservice-config-server

1、在pom.xml中加入以下依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.4.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

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

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Camden.SR5</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

2、创建ConfigServerApplication.java

package com.baibei.config.server;

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

/**、
* @date: 17/2/19 下午2:35
 * @description:分布式配置中心启动主类
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

添加@EnableConfigServer注解开启Config Server

3、在resource文件夹下创建application.properties文件

#config-server对外提供的端口
server.port=9001

#git repo的url地址
spring.cloud.config.server.git.uri=
#指定搜索路径,config-server会自动搜索根目录和指定目录(逗号分隔)下的文件
spring.cloud.config.server.git.searchPaths=api,backend
#有读取权限的git用户
spring.cloud.config.server.git.username=username
#git用户密码
spring.cloud.config.server.git.password=password

4、服务端验证

为了完成服务端的验证,我们需要在git上创建一个项目作为配置仓库,目录结构如下:

.
  ├── api
  │   ├── business-dev.properties
  │   ├── business-prod.properties
  │   └── business-test.properties
  ├── exchange
  │   ├── business-dev.properties
  │   ├── business-prod.properties
  │   └── business-test.properties
  ├── component-dev.properties
  ├── component-prod.properties
  ├── component-test.properties
  ├── database-dev.properties
  ├── database-prod.properties
  ├── database-test.properties

接下来,我们就可以通过URL来访问到我们配置的内容了。 例如我们可以通过http://localhost:9001/database-dev.properties来访问database-dev.properties文件的内容,也可以通过http://localhost:9001/database/dev来访问

URL与配置文件的映射关系如下:

  • /{application}/{profile}[/{label}] 如: http://localhost:9001/database/dev
  • /{application}-{profile}.yml 如: http://localhost:9001/database-dev.yml
  • /{label}/{application}-{profile}.yml 如: http://localhost:9001/f-branch/database-dev.yml (f-branch分支下的database-dev.yml文件)
  • /{application}-{profile}.properties 如: http://localhost:9001/database-dev.properties (不指定分支默认是MASTER分支)
  • /{label}/{application}-{profile}.properties 如: http://localhost:9001/f-branch/database-dev.properties

例如database-dev.properties 对应{application}-{profile}.properties, {application} 就是 database, {profile}就是dev。 {label}对应git上不同的分支,默认为master。

5.使用本地配置

开发人员在本机进行开发时,可能会用到自已本地的一些配置信息,如果把这些配置提交至git上的话,又会影响到其他同事的开发,那么在这种场景下,开发人员是可以选择使用本地配置,而不是git上的配置信息的,做法也很简单,只需要在config-server的application.properties中添加spring.profiles.active=native,然后把相关的配置文件放在src/main/resource下即可

6、配置中心客户端

以 Spring Cloud(2)—服务提供者 中的microservice-provider-user作为配置中心客户端

6.1. 在pom.xml中增加Config Server所需要的jar包
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
6.2. 创建bootstrap.properties来指定Config Server配置,内容如下:
spring.cloud.config.uri=http://localhost:9001/
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.name=database,component,business

内容含义如下:

  • spring.application.name:对应前配置文件中的{application}部分
  • spring.cloud.config.profile:对应前配置文件中的{profile}部分
  • spring.cloud.config.label:对应前配置文件的git分支
  • spring.cloud.config.uri:配置中心的地址

以上工作完成后,在代码中我们就可以使用@Value注解来调用相关的配置信息,

@Value("${datasource.url}")
private String dataSourceUrl;

配置中心完成之后,在microservice-provider-user工程的application.properties中的数据库配置就可以换为

spring.datasource.url=${datasource.url}
spring.datasource.username=${datasource.username}
spring.datasource.password=${datasource.password}

在配置中心客户端启动的时候,我们也可以看到相关的日志信息输出

2017-02-19 15:17:28.220  INFO 11206 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at: http://localhost:9001/
2017-02-19 15:17:29.565  INFO 11206 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=database,component,business, profiles=[dev], label=master, version=a9f8f18e682a03f4d7f046c754472f394313fa82, state=null
2017-02-19 15:17:29.565  INFO 11206 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='configClient'], MapPropertySource [name='/database-dev.properties']]]
2017-02-19 15:17:29.595  INFO 11206 --- [           main] c.b.p.user.UserProviderApplication       : No active profile set, falling back to default profiles: default

附项目目录截图:

springboot改分布式部署_java