springcloud-配置中心config

为什么要使用配置中心

  • 集中管理配置。一个使用微服务架构的应用系统可能会包含成百上千个微服务,因此集中管理配置是非常有必要的;
  • 不同环境,不同配置。例如,数据源配置在不同的环境(开发、测试、预发布、生产等)中是不同的;
  • 运行期间可动态调整。例如,我们可根据各个微服务的负载情况,动态调整某自定义的配置文件参数
  • 配置修改后可自动更新。如配置内容发生变化,微服务能够自动更新配置。

Spring Cloud Config简介

Spring Cloud Config为分布式系统外部化配置提供了服务器端和客户端的支持,它包括Config Server和Config Client两部分。由于Config Server和Config Client都实现了对Spring Environment和PropertySource抽象的映射,因此,Spring Cloud Config非常适合Spring应用程序,当然也可与任何其他语言编写的应用程序配合使用。

Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置内容(也可使用Subversion、MySQL、本地文件系统或Vault存储配置,本博客以Git为例进行讲解),因此可以很方便地实现对配置的版本控制与内容审计。

创建spring-cloud-config模块:

pom.xml:



    4.0.0
    
        com.example
        spring-cloud-wsl
        0.0.1-SNAPSHOT
    
    com.example
    spring-cloud-config
    0.0.1-SNAPSHOT
    spring-cloud-config
    Demo project for Spring Cloud Config
    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-config-server
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
    

配置文件application.yml

server:
  port: 8885
spring:
  application:
    name: config
  cloud:
    config:
      server:
        git:
          uri: 你的github地址 .git结尾的
          username: 你的账号
          password: 你的密码
         # default-label: master #配置文件分支
         # search-paths: config  #配置文件所在根目录
eureka:
  client:
    service-url:
      #注册中心地址
      defaultZone: http://localhost:8761/eureka/
  instance:
    #将ip注册到eureka上
    prefer-ip-address: true

启动类添加注解:

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

spring-cloud-config模块到此创建完毕


我是在码云上存放的配置文件,因为国内码云会比github的访问速度要快一些

登录码云(没有账号的可以免费注册一个),首页的右上角创建仓库:

填上仓库名选择公开:

然后在该仓库下创建yml文件,具体的git使用请自行百度,此处不进行过多说明

我是提交到仓库了两个provider的yml文件和一个consumer-feign的yml文件,我们用provider举例:

provider-dev1.yml:

server:
  port: 8880
#eureka客户端连接配置
eureka:
  client:
    service-url:
      #注册中心地址
      defaultZone: http://localhost:8761/eureka/
  instance:
    #将ip注册到eureka上
    prefer-ip-address: true

provider-dev2.yml:

server:
  port: 8881
#eureka客户端连接配置
eureka:
  client:
    service-url:
      #注册中心地址
      defaultZone: http://localhost:8761/eureka/
  instance:
    #将ip注册到eureka上
    prefer-ip-address: true

回到项目中,在spring-cloud-provider模块中创建bootstrap.yml配置文件:

spring:
  application:
    name: provider
  cloud:
    config:
      discovery:
        enabled: true
        serviceId: config
      profile: dev1
      # 指定分枝版本,默认为master
      label: master

注:这里的profile: dev1 对应的是码云上的配置文件名字的后缀,
具体名称和配置的对应关系如下:
/{name}-{profiles}.properties
/{name}-{profiles}.yml || /{name}-{profiles}.yaml
/{label}/{name}-{profiles}.properties
/{label}/{name}-{profiles}.json
/{name}/{profiles}/{label:.*}
/{name}-{profiles}.json
/{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml
/{name}/{profiles:.*[^-].*}
/{name}/{profile}/{label}/**
/{name}/{profile}/{label}/**
/{name}/{profile}/**

把application.yml文件随便命名个别的名:


然后pom里添加config相关依赖:



    org.springframework.cloud
    spring-cloud-config-client

启动注册中心、配置中心、provider,打开浏览器输入http://localhost:8880/test/wangshilin

修改spring-cloud-provider模块的配置文件bootstrap.yml中profile属性改为dev2

重启spring-cloud-provider:

浏览器输入http://localhost:8881/test/wangshilin


后续会抽时间更新一下配置加密方面的内容,敬请期待

你可能感兴趣的:(springcloud-配置中心config)