学习微服务的统一配置管理-springCloud config+git

因为微服务设计到的服务个数较多,而众多的配置文件便会须要一个统一的配置管理工具来正确的管理,避免人工的过多参与,而spring cloud全家桶中的spring cloud config便充当了这个功能,解决分布式系统配置管理问题。下面开始学习springcloud config的相关信息。git

spring cloud config包含server端和client端,以及能够存储配置文件的诸如SVN,github,gitlab上的文件。github

如下用实例来验证之:web

1.server端:spring

须要springcloud config相关的依赖app

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.cloud:spring-cloud-starter-config')
    compile('org.springframework.cloud:spring-cloud-config-server')
    testCompile group: 'junit', name: 'junit', version: '4.12'
}

须要开启@EnableConfigServer分布式

@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

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

}

yml配置文件spring-boot

server:
  port: 8888  #服务端口需为8888,改成其余端口失败,还不知为什么

spring:
  cloud:
    config:
      name: config-server   #项目名称
      label: master    
      server:
        git:
          #uri: https://@gitlab.com/xxx.git   #若为gitlab的地址,以git为后缀
          uri: https://github.com/gholly/configProperties  #github的地址
          username: xxx
          password: xxx
          searchPaths: configPath   //配置文件的路径

2.配置文件以下:微服务

地址为:https://github.com/gholly/configProperties   此项目文件为公共文件无需输入用户名密码方可测试工具

3.客户端gitlab

依赖文件

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.cloud:spring-cloud-starter-config')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

客户端文件

@RestController
@RefreshScope
@SpringBootApplication
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);

    }



    @Value("${test}")
    public String hh;

    @RequestMapping("/hjk")
    public String  hhh(){
        return hh;
    }
}

配置文件:

server:
  port: 9000   

spring:
  cloud:
    config:
      label: master
      uri: http://localhost:8888/   #config服务端的服务
      profile: dev   配置文件前缀之一
  application:
    name: config    #此名字需与git上配置文件的名字相一致

4.测试结果:

相关文章
相关标签/搜索