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。

相关文章
相关标签/搜索