springcloud应用之config配置中心

阅读提示

config项目
请先阅读eureka

config是什么

我们项目里面每一个微服务都有一个yml文件,我们希望这个yml文件能够交给一个config中心管理,而config中心又是从第三方如github读取项目所有的配置文件,基本架构思路如图
springcloud应用之config配置中心_springcloud源码

单体config

我们先来建一个config-server-8400
new module -->spring initialzr —>填好信息---->搜索config server

启动类加上@EnableConfigServer

server:
  port: 8400
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lrydl/spring-cloud-config.git
          clone-on-start: true #启动的时候就去git clone过来
          search-paths: foo,ba* #搜索 foo文件,以ba开头的目录,以及foo的子目录,ba*的子目录 所有文件

我的git文件 test-config.yml如下

server:
  port: 12345
spring:
  profiles:
    active: test
---

spring:
  profiles: dev
  application:
    name: dev-server
---
spring:
  profiles: test
  application:
    name: test-server

启动该项目访问
http://localhost:8400/test-config.yml
http://localhost:8400/test-config-dev.yml
http://localhost:8400/test-config-test.yml
http://localhost:8400/test-config.properties
http://localhost:8400/test-config-dev.properties
http://localhost:8400/test-config-test.properties
都试试看

config访问规则

/{application}/{profile}[/{label}]
 /{application}-{profile}.yml 
 /{label}/{application}-{profile}.yml 
 /{application}-{profile}.properties
  /{label}/{application}-{profile}.properties

config-server-8400成功可以从浏览器看到git的配置信息

下面我们建一个config-client,让这个项目用git上面的配置端口12345跑起来
new module -->spring initialzr —>填好信息---->搜索config client

pom添加,这是大坑

 		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

application.yml

spring:
  application:
    name: config-client

bootstrap.yml

spring:
  cloud:
    config:
      name: test-config #git的文件名
      profile: dev #profiles拿test还是dev
      label: master # 分支
      uri: http://localhost:8400/ # 单体的config-server-8400路径

启动config-client项目用到了git上的配置
springcloud应用之config配置中心_spring cloud_02

集群config

因为只有一个config-server不能保证高可用,我们再建一个config-server-8401

config-client
config-server-8400
config-server-8401
pom都加上

	 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

config-client启动类加上@EnableEurekaClient
config-server-8400,config-server-8401加上@EnableConfigServer,@EnableEurekaClient

config-client的bootstrap.yml

spring:
  cloud:
    config:
      name: test-config #git的文件名
      profile: dev #profiles拿test还是dev
      label: master # 分支
      #uri: http://localhost:8400/ # 单体的config-server-8400路径
      discovery:
        enabled: true
        service-id: config-server # 配置中心的application name

eureka:
  client:
    serviceUrl:
      defaultZone: http://server7000:7000/eureka,http://server7001:7001/eureka,http://server7002:7002/eureka

config-server-8400 application.yml

server:
  port: 8400
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lrydl/spring-cloud-config.git
          clone-on-start: true #启动的时候就去git clone过来
          search-paths: foo,ba* #搜索 foo文件,以ba开头的目录,以及foo的子目录,ba*的子目录 所有文件

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://server7000:7000/eureka,http://server7001:7001/eureka,http://server7002:7002/eureka

config-server-8401 application.yml

server:
  port: 8401
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lrydl/spring-cloud-config.git
          clone-on-start: true #启动的时候就去git clone过来
          search-paths: foo,ba* #搜索 foo文件,以ba开头的目录,以及foo的子目录,ba*的子目录 所有文件

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://server7000:7000/eureka,http://server7001:7001/eureka,http://server7002:7002/eureka

先跑两个server,再跑client发现还是用到了12345端口,这里用的负载均衡和我之前配置zuul的集群一样,都是采用eureka