spring cloud系列config

我们知道,微服务每个都是一个单体服务,我们的功能单元会拆分的很细,我们会有很多的微服务,这也就意味着我们将有n多的配置,如果不能将配置进行统一的管理将会是一件很可怕的事情,接下来我们就来搭建一个配置中心
idea搭建非常的快建议使用。
和注册中心一样配置中心也是分为server端和client端,server端负责从仓库拉取配置,client从server获取配置
上图
在这里插入图片描述

依赖
	compile 'org.springframework.cloud:spring-cloud-config-server:2.1.3.RELEASE'
    compile 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:2.1.2.RELEASE'

我们需要引入config server以及eureka client

主类加注解
@SpringBootApplication
@EnableDiscoveryClient
public class GetawayApplication {

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

这里使用@EnableDiscoveryClient,使用@EnableEurekaClient有可能获取不到配置

配置
spring:
  application:
    name: admin-config
  cloud:
    config:
      server:
        git:
          #git仓库地址
          uri: xxx
          #用户名
          username: xxxx
          #密码
          password: 'xxx'
          #配置文件搜索路径
          search-paths: /**
eureka:
  instance:
    non-secure-port: ${server.port}
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}
    lease-renewal-interval-in-seconds: 10
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://regist.byb.com:38001/eureka/
server:
  port: 38002

然后启动服务即可在注册中心看见配置中心微服务
在这里插入图片描述

客服端使用
  • 同样也是加依赖
    implementation('org.springframework.cloud:spring-cloud-starter-config')

注意这里我同样没有加版本,我的依赖版本统一由spring cloud管理。

  • 然后删除原来的application.propertity或application.yaml的配置文件,或者将其移到static目录或者其他目录,别放在项目能读取到的目录就行,为什么呢?因为开发的时候,有时候难免还是需要本地配置测试,会比较方便。
  • 然后添加bootstrap.yaml或者propertity配置文件,此配置文件的优先级高于application的配置文件
    在这里插入图片描述
  • bootstrap.yaml配置
spring:
  application:
    name: ts-log
  cloud:
    config:
      discovery:
        enabled: true
        # 配置中心微服务名称
        service-id: admin-config
        # 仓库的分支
      label: master
      # 开发环境
      profile: dev
eureka:
  client:
    service-url:
      defaultZone: http://regist.byb.com:38001/eureka/
server:
  port: 8080
  • 然后在仓库加入你的微服务配置以微服务名称+开发环境命名,为了方便每个微服务都可以建一个目录,方便对不同环境进行管理
    在这里插入图片描述
  • 我的配置是存在git代码仓库里面的,非常方便管理,当然你也可以存在本地的某个文件夹或者其他地方,只需要修改config的配置即可。
    然后启动项目,在启动图标下面即可看见服务是否拿到配置
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //

//            佛祖保佑       永不宕机     永无BUG                 //


[INFO ] 2019-12-09 14:26:31,435 org.springframework.cloud.config.client.ConfigServicePropertySourceLocator org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.getRemoteEnvironment(ConfigServicePropertySourceLocator.java:207) - Fetching config from server at : http://172.17.0.3:38002/
[INFO ] 2019-12-09 14:26:32,287 org.springframework.cloud.config.client.ConfigServicePropertySourceLocator org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.log(ConfigServicePropertySourceLocator.java:151) - Located environment: name=ts-log, profiles=[dev], label=master, version=84b2687eddebd947f726a69683e7c35744053cad, state=null
[INFO ] 2019-12-09 14:26:32,291 org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:101) - Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://codehub.devcloud.huaweicloud.com/byb2000001/doc_configForTs.git/ts-log/ts-log-dev.yml'}, MapPropertySource {name='https://codehub.devcloud.huaweicloud.com/byb2000001/doc_configForTs.git/application-dev.yaml'}, MapPropertySource {name='https://codehub.devcloud.huaweicloud.com/byb2000001/doc_configForTs.git/application.yml'}]}

默认的启动图标是spring boot(我这里是使用了自己的图标),图标第一行我们可以看见配置中心的地址:http://172.17.0.3:38002/,如果这里显示的是localhost:8888那么证明你没有添加依赖n,默认从本地的8888端口获取配置。最后一行后面可以看见从配置中心获取到的所有配置。

  • 微服务配置文件加载顺序
    扫描的顺序,相同的配置,优先级低的覆盖优先级高的,相同配置文件yml格式会覆盖properties格式
  1. bootstrap.yml/bootstrap.properties
  2. application.yml/application.properties
    • 同级的有application-dev.yml,application-test.yml,application-prod.yml等,使用需要在application中添加spring.profiles.active=xxx,xxx为dev,test…
    • 同一项目下application配置文件放的位置可能不同,扫描循序如下
      • classpath:/(classpath根目录)
      • classpath:/config/(classpath下的config目录)
      • file:./(当前目录)
      • file:./config/(当前目录下的config文件夹)
  3. xxx.yml/xxx.properties,xxx为spring.application.name中定义的名字(spring cloud config)
    • 同级文件在本地bootstrap的spring.profiles.active中配置,同级的配置文件有
    • xxx-dev,开发环境
    • xxx-test,测试环境
    • xxx-exp,体验环境
    • xxx-prod,生成环境

所以我们可以将公共的配置放到仓库的以application开头的配置文件中,所有的微服务都会加载application开头的配置,加载顺序上面已经写的很清楚了。比如你的开发环境是dev,那么会依次加载bootstrap.yaml,application.yaml.application-dev.yaml,xxx.yaml,xxx-dev.yaml
至此配置中心搭建,以及使用介绍完毕,如文章有问题,或有其他疑问欢迎交流。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值