springcould(7)Config配置中心

Config配置中心学习笔记


文章目录

    • Config配置中心学习笔记
      • 配置中心的概念
      • 1.创建一个新服务,作为config server服务方
          • 1、引入config-server依赖
          • 2、在application.yml中配置config
          • 3、访问github上的配置文件
      • 2.消费方使用config服务方从github上拉取的配置文件
          • 1、消费方引入starter-config依赖
          • 2、消费方在配置文件中获取要使用的配置文件
      • 3.@RefreshScope自动刷新


配置中心的概念

config server可以集中管理各个服务的配置,config server默认将配置存储在git,以实现服务的动态配置,不同环境使用不同的配置文件

config server作为服务方从git上获取服务的配置,其它服务作为消费方从config server中调用配置

1.创建一个新服务,作为config server服务方

1、引入config-server依赖

在引入config配置中心依赖的同时,也要引入eureka-client依赖,将此服务注册到eureka中


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client
    2.1.2.RELEASE



    org.springframework.cloud
    spring-cloud-config-server
   2.1.2.RELEASE

2、在application.yml中配置config
  1. config服务的端口号默认是8888

    如果是使用bootstrap.properties作为配置文件,可以自定义端口号,如果使用的是其它的配置文件则不能修改端口号

    spring boot配置文件优先顺序

    bootstrap.properties—>bootstrap.yml—>bootstrap.yaml—>application.properties—>application.yml—>application.yaml

  2. label:配置文件所在项目的分支

  3. uri:配置文件项目在github的地址,后缀.git可带可不带

server:
  port: 8888 #使用application.yml配置文件,端口号必须是8888
spring:
  application:
    name: jwxt-config #指定服务名
  cloud:
    config:
      label: master #分支
      server: #config服务端的配置
        git:
          uri: https: github.com/qiaozixing/jwxt-config.git #配置文件项目的github地址
          username: github账号
          password: github密码
  
#向eureka注册服务省略
3、访问github上的配置文件

访问config服务的端口http://localhost:8888/目标文件,加载出来配置文件的内容就说明config配置成功了

例如:http://localhost:8888/application-teacher-dev.yml

springcould(7)Config配置中心_第1张图片

2.消费方使用config服务方从github上拉取的配置文件

1、消费方引入starter-config依赖

    org.springframework.cloud
    spring-cloud-starter-config

2、消费方在配置文件中获取要使用的配置文件

项目环境:

  • 开发环境dev:devolpemenet
  • 生产环境prod:production
  • 测试环境test:testing

例:完整的文件名称application-teacher-dev.yml

spring:
  cloud:
    config:
      uri: http://localhost:8888 #config配置中心的路径
      label: master #目标配置文件所在分支
      name: application-teacher #目标文件名称
      profile: dev #指定环境

3.@RefreshScope自动刷新

如果在代码中使用了配置文件中的某节点,而当github上的配置文件修改其节点时,代码中使用的节点并不会改变,因为上面的操作只是实现了动态配置文件,并没有实现代码的动态

在使用了配置文件的节点的类上加入@RefreshScope注解,可以实现自动刷新,当github的上节点修改后,引用该节点的地方也会随之修改

你可能感兴趣的:(springcloud学习笔记)