欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringCloud(Hoxton) Config--服务端配置与测试、配置文件读取规则

程序员文章站 2022-07-03 20:01:47
...

SpringCloud Config服务端配置

在GitHub新建一个名为springcloud-config的新Repository。

本地初始化一个仓库,将Repository克隆下来

git clone https://github.com/xxxx/springcloud-config.git

准备3个配置文件,并push到github:
SpringCloud(Hoxton) Config--服务端配置与测试、配置文件读取规则

新建config配置中心微服务cloud-config-center-3344

依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2020</artifactId>
        <groupId>pers.zhang.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-center-3344</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

配置:application.yml

server:
  port: 3344

spring:
  application:
    name: cloud-config-center #注册进eureka的微服务名
  cloud:
    config:
      server:
        git:
          uri: aaa@qq.com.com:xxxx/springcloud-config.git
          search-paths:
            - springcloud-config #指定搜索目录
      label: master #读取分支

eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

启动类:

@SpringBootApplication
@EnableConfigServer
public class MainAppConfigCenter3344 {
    public static void main(String[] args) {
        SpringApplication.run(MainAppConfigCenter3344.class, args);
    }
}

测试:
启动eureka,再启动3344配置中心:
SpringCloud(Hoxton) Config--服务端配置与测试、配置文件读取规则

访问:localhost:3344/master/config-dev.yml
SpringCloud(Hoxton) Config--服务端配置与测试、配置文件读取规则
访问:localhost:3344/master/config-test.yml
SpringCloud(Hoxton) Config--服务端配置与测试、配置文件读取规则
访问:localhost:3344/master/config-prod.yml
SpringCloud(Hoxton) Config--服务端配置与测试、配置文件读取规则

配置文件读取规则

SpringCloud(Hoxton) Config--服务端配置与测试、配置文件读取规则

/{label}/{application}-{profile}.yml 示例:

  • master分支
http://config-3344.com:3344/master/config-dev.yml
http://config-3344.com:3344/master/config-test.yml
http://config-3344.com:3344/master/config-prod.yml
  • dev分支
http://config-3344.com:3344/dev/config-dev.yml
http://config-3344.com:3344/dev/config-test.yml
http://config-3344.com:3344/dev/config-prod.yml

/{application}-{profile}.yml 示例

http://config-3344.com:3344/config-dev.yml
http://config-3344.com:3344/config-test.yml
http://config-3344.com:3344/config-prod.yml
http://config-3344.com:3344/config-xxx.yml(不存在,返回  {}  )

默认找master分支,也可以在配置文件中指定label属性修改

/{application}/{profile}[/{label}] 示例

http://config-3344.com:3344/config/dev/master
http://config-3344.com:3344/config/test/master
http://config-3344.com:3344/config/prod/master

注意:该方式返回为JSON
SpringCloud(Hoxton) Config--服务端配置与测试、配置文件读取规则

相关标签: SpringCloud Config