SpringCloudConfig分布式配置中心-基本使用

一、基本使用

1. Config-Server端

(1)pom:

  1. parent依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>
  2. dependencyManagement

    复制代码

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    复制代码

  3. dependencies
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
    </dependencies>

 查看全部pom代码

(2)启动类:增加注解@EnableConfigServer,当然@SpringBootApplication也是必不可少的

 查看全部启动类代码

(3)application.yml

如果用git作配置仓库,则:

复制代码

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/LOVE0612/SpringCloud.git
          username: [email protected]
          password: 123@abc

复制代码

  • 如果希望ConfigServer启动的时候就将配置文件clone下来,则可增加配置:spring.cloud.config.server.git.clone-on-start=true。
  • 如果配置文件不在git仓库的根目录下, 例如:如果配置文件在git仓库的properties目录下,则可增加配置spring.cloud.config.server.git.search-paths=properties。当有多个目录时逗号分隔。
  • 如果需要制定git clone后在本地存储位置,则可增加配置spring.cloud.config.server.git.baseDir。

如果用svn作配置仓库,则:

复制代码

spring:
  cloud:
    config:
      server:
        svn:
          uri: https://192.168.1.111/svn/SpringCloud
          username: luyanchao
          password: abc@123

复制代码

  • 同样存在spring.cloud.config.server.svn.search-paths配置项,使用方法同git。
  • 同样存在spring.cloud.config.server.git.baseDir配置项,使用方法同git。

如果使用本地文件系统作配置仓库,则:

复制代码

spring:
  cloud:
    config:
      server:
        native:
          search-locations: properties
      profile: native

复制代码

  • search-locations:可以是绝对路径也可以是classpath路径。
  • 使用本地文件系统作为配置仓库的最大缺点在于:如果ConfigServer多点部署以保证高可用时,需要将文件系统进行共享。

如果使用多种/多个配置仓库,则:

复制代码

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/LOVE0612/SpringCloud.git
          username: [email protected]
          password: 1226ai0612
          order: 1
        svn:
          uri: https://192.168.1.111/svn/SpringCloud
          username: luyanchao
          password: abc@123
          order: 2
        native:
          search-locations: properties
          order: 3
      profile: native, git, svn

复制代码

  • profile:设定哪些配置仓库可用,逗号分隔
  • order:配置仓库的优先级顺序,数字越低优先级越高

复制代码

server:
  port: 51011

spring:
  application:
    name: ConfigServer
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/LOVE0612/SpringCloud.git
          search-paths: properties
          username: [email protected]
          password: 123@abc
          clone-on-start: true

复制代码

(3)run,并测试

ConfigServer提供多种rest接口访问配置信息,可通过run日志查看:

复制代码

Mapped "{[/{name}-{profiles}.properties],methods=[GET]}" onto ...
Mapped "{[/{name}-{profiles}.yml || /{name}-{profiles}.yaml],methods=[GET]}" onto ...
Mapped "{[/{name}/{profiles:.*[^-].*}],methods=[GET]}" onto ...
Mapped "{[/{label}/{name}-{profiles}.properties],methods=[GET]}" onto ...
Mapped "{[/{label}/{name}-{profiles}.json],methods=[GET]}" onto ...
Mapped "{[/{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml],methods=[GET]}" onto...
Mapped "{[/{name}/{profiles}/{label:.*}],methods=[GET]}" onto ...
Mapped "{[/{name}-{profiles}.json],methods=[GET]}" onto ...
Mapped "{[/{name}/{profile}/{label}/**],methods=[GET],produces=[application/octet-stream]}" onto ...
Mapped "{[/{name}/{profile}/{label}/**],methods=[GET]}" onto ...
Mapped "{[/{name}/{profile}/**],methods=[GET],params=[useDefaultLabel]}" onto...

复制代码

  • 参数name:即ConfigClient中定义的spring.application.name的值,后续会讲到
  • 参数profiles:这个不需要多说,用过SpringBoot的应该都知道
  • 参数label:如果是git或svn,label对应的是分之;如果是本地文件系统,label对应子目录名,例如:classpath:properties/{label}

2. Config-Client端

(1)pom:

  1. parent依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
    </parent>
  2. dependencyManagement

    复制代码

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    复制代码

  3. dependencies
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>
    </dependencies>

(2)启动类:有@SpringBootApplication即可

(3)bootstrap.yml,注意这里不是application.yml哦

复制代码

spring:
  application:
    name: ServiceGateway
  cloud:
    config:
      uri: http://127.0.0.1:51011
      profile: company

复制代码

  • spring.application.name:项目名称
  • spring.cloud.config.uri:ConfigServer的地址
  • spring.cloud.config.profile:这个不需要多说,用过SpringBoot的应该都知道

根据以上配置内容,可得知最终调用ConfigServer的接口地址为:http://127.0.0.1:51011/ServiceGateway-company.yml

3. 多个Config-Client端公用配置

如果多个Config-Client中有部分配置是相同的,为了避免出现重复代码,我们可以把这部分相同的配置抽出来单独做一个配置文件,命名为:application.yml、application.properties,或application-*.yml、application-*.properties,然后放到Config-Server端即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章