首先 官网地址:http://cloud.spring.io/spring-cloud-config/

配置中心服务端

添加gradle依赖:

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-config-server'
}

Application启动类添加注解

添加@EnableConfigServer注解,启用配置中心:

package com.zoo.config;

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

@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(ConfigApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
        System.out.println("config start complete........");
    }

}

配置文件

application.yml或者application.properties添加配置信息:

server:
  port: 8888

#加载本地配置
#spring:
#  application:
#    name: config-server
#  profiles:
#    active: native
#  cloud:
#    config:
#      server:
#        native:
#          search-locations: /home/springcloud-config/

#加载github配置
spring:
  application:
    name: zoo-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xie-fei/zoo.git
          search-paths: springcloud-config/test #仓库文件夹目录,如果是/**,就是所有目录所有文件
          username: xxxxxx
          #如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写
          password: xxxxxx
          #配置仓库的分支
          default-label: config
          basedir: target/config  #配置文件拉去到本地的目录位置

启动测试

首先在git里面添加一个application-dev.yml配置文件,内容如此下:

test: 我是配置中心配置信息

已经配置完成了,启动一波试试,看效果咋样,正常情况下是可以正常启动的,然后获取配置文件试试 
访问地址:http://localhost:8888/test/dev 
如果返回如下,就是成功了:

Spring Cloud Config分布式配置中心的简单使用_bootstrap

HTTP服务具有以下格式的资源:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
配置中心客户端使用

gradle添加依賴

dependencies {
    implementation('org.springframework.boot:spring-boot-starter')//Core starter,包括 自动配置支持、 logging and YAML
    implementation 'org.springframework.boot:spring-boot-starter-web'//构建Web,包含RESTful风格框架SpringMVC和默认的嵌入式容器Tomcat
    compile 'org.springframework.cloud:spring-cloud-starter-config'//config客户端

}


Application启动类添加注解

package com.zoo.client;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(ClientApplication.class);
        app.setBannerMode(Banner.Mode.OFF);// 关闭启动Banner
        app.run(args);
        System.out.println("client start completed.........");
    }

}

配置文件

创建bootstrap.yml文件,切记,最好是bootstrap.yml文件bootstrap.yml文件,我就因为写到了application.yml这个里面,各种出现问题啊,添加如下配置:

server:
  port: 8010

spring:
  application:
    name: zoo-client
  cloud:
    config:
      name: application  #配置名称,一般和git仓库的application-dev.yml对应
      profile: dev      #指定不同环境配置文件,和git仓库的 application-dev.yml对应
      label: config     #指明远程仓库的分支
      fail-fast: true   #失败快速响应
      uri: http://localhost:8888    #上面的配置中心服务地址

启动测试

先添加一个获取配置信息的类:

package com.zoo.client.controller;

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

/**
 * @Author: xf
 * @Date: 2019/5/28 18:02
 * @Version 1.0
 */
@RestController
public class ConfigController {

    @Value("${test}") // git配置文件里的key
    private String test;

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

找个地方随便调用一下,输出这个test,就会打印上面git里面配置的信息了

Spring Cloud Config分布式配置中心的简单使用_config_02

 

客户端配置一定要配置在bootstrap.yml里面 ,因为bootstrap.yml会比application.yml先执行,uri默认会调用端口为8888的地址http://localhost:8888/ 
启动的时候,会加载label和uri,profile配置,profile可以在启动参数添加,profile也可以加在application.yml添加 
name也可以加在application.yml添加