Spring Cloud Config配置中心服務端的配置

服務端的配置

Git 中URI佔位符

在以前的例子中咱們配置中經過spring.cloud.config.uri配置了配置中心服務端的地址 spring.cloud.config.name配置了配置文件的名稱,spring.cloud.config.profile配置了相應環境的地址java

server:
  port: 8102
spring:
  application:
    name: config-client-1
  cloud:
    config:
      label: master
      uri: http://localhost:8101
      # 此處配置了配置文件的名稱
      name: client-config
      profile: dev
複製代碼

模式匹配和多個存儲庫

在application和profile的名稱上,Spring Cloud Server還支持更加複雜的配置模式,能夠使用通配符{application}/{profile}名稱進行規則匹配,經過逗號分隔。mysql

例子:git

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/chendom/SpringCloudConfig.git
          username: xx
          password: xx
          #配置搜索路徑
          search-paths: config
          repos:
              simple: https://gitee.com/chendom/simple
              special:
                pattern: special*/dev*,*special*/dev*
                uri: https://gitee.com/chendom/SpringCloudConfig-special
               local:
                pattern: local*
                uri: /Users/chendom/test/SpringCloudConfig
複製代碼

路徑搜索

路徑搜索佔位符

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/chendom/SpringCloudConfig.git
          username: xx
          password: xx
          #配置搜索路徑
          search-paths: config,config*
複製代碼

search-paths: config,config*是指在config路徑在或者是以config爲前綴的路徑的搜索全部的配置文件spring

spring:
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/chendom/SpringCloudConfig.git
          username: xx
          password: xx
          #配置搜索路徑
          search-paths: *{application}*
           #表示合法的將遠程倉庫拷貝到本地
          clone-on-start: true
複製代碼

clone-on-start: true表示合法的將遠程倉庫拷貝到本地緩存,爲了匹配不一樣的項目,須要將search-path配置成*{application}*,必定要**,或者使用'{application}'sql

Spring Cloud與MySQL結合實例

pom依賴:緩存

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

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

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
    </dependencies>
複製代碼

配置文件app

server:
  port: 8103

spring:
  cloud:
    config:
      server:
        jdbc:
          sql: SELECT `KEY`, `VALUE` FROM PROPERTIES WHERE application =? AND profile =? AND lable =?
      label: master
    refresh:
      extra-refreshable: none
  profiles:
    #這裏須要配置成是jdbc
    active: jdbc
  application:
    name: config-server-db
  ## 數據配置
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/config_db?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: root@123
    driver-class-name: com.mysql.jdbc.Driver
複製代碼

客戶端依然和以前的例子同樣dom

server:
  port: 8104
spring:
  application:
    name: config-client-db
  cloud:
    config:
      label: master
      uri: http://localhost:8103
      # 此處配置了配置文件的名稱
      name: client-config
      profile: test

# 設置打印的日誌級別
logging:
  level:
    root: debug
複製代碼

@RestController
public class ConfigClientController {

@Value("${springcloud.configserver}")
public String value;
@RequestMapping("/getValue")
public String getValue(){
    return value;
}
}
複製代碼

請求localhost:8104/getValue獲得以下圖所示的結果:ide

Spring Cloud Config的相關使用技能

spring:
    cloud:
      config:
        allowOverride:true
        overrideNone: true
        overrideSystemProperties: false
複製代碼
  • allowOverride:標識overrideSystemProperties屬性是否啓用。默認爲true,設置爲falsespring-boot

  • overrideNone:當allowOverride爲true時,overrideNone設置爲true,外部的配置優先級更低,並且不能覆蓋任何存在的屬性源,默認爲false

  • overrideSystemProperties:用老標識外部配置是否 可以覆蓋洗屬性,默認爲true。

相關文章
相關標籤/搜索