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端即可。

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