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

服务端的配置

Git 中URI占位符

在之前的例子中我们配置中通过spring.cloud.config.uri配置了配置中心服务端的地址 spring.cloud.config.name配置了配置文件的名称,spring.cloud.config.profile配置了相应环境的地址

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}名称进行规则匹配,通过逗号分隔。

例子:

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:
  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}'

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>
复制代码

配置文件

 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
复制代码

客户端依然和之前的例子一样

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得到如下图所示的结果:

Spring Cloud Config的相关使用技能

spring:
    cloud:
      config:
        allowOverride:true
        overrideNone: true
        overrideSystemProperties: false
复制代码
  • allowOverride:标识overrideSystemProperties属性是否启用。默认为true,设置为false

  • overrideNone:当allowOverride为true时,overrideNone设置为true,外部的配置优先级更低,而且不能覆盖任何存在的属性源,默认为false

  • overrideSystemProperties:用老标识外部配置是否 能够覆盖洗属性,默认为true。

转载于:https://juejin.im/post/5ceb2da96fb9a07ee16905f8

猜你喜欢

转载自blog.csdn.net/weixin_33690963/article/details/91461249
今日推荐