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

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

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

以下用实例来验证之:

1.server端:

需要springcloud config相关的依赖

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配置文件

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.客户端

依赖文件

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.测试结果:

220922_llvX_2263272.png

 

转载于:https://my.oschina.net/u/2263272/blog/1633100

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值