springcloud demo---config-server

1.概念

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Springcloud使用git或svn存放配置文件,默认情况下使用git。

2.创建一个config-server服务

  2.1 application.yml

server:
  port: 8090
spring:
  application:
    name: config-server  #指定服务名
  cloud:
    label: master   #指定分支(默认为master)
    config:
      profile: dev  #指定的环境(dev开发环境配置文件/test测试环境/pro正式环境)
      server:  
        git:  #配置git仓库地址
          uri: https://github.com/RainSakuraWetc/springcloudtest/  #配置仓库路径:仓库
          searchPaths: config  #指定的是匹配查询的路径名:仓库中有个文件夹为config
          username: 
          password: 

  2.2 依赖

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

  3.3 启动类

    开启config-server注解

package com.transsion.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

  3.4 访问git中配置文件的命名规则:

    {application}-{profile}.properties

  3.4 访问config-server的url规则: 

    /{application}/{profile}[/{label}]
    /{application}-{profile}.yml
    /{label}/{application}-{profile}.yml
    /{application}-{profile}.properties
    /{label}/{application}-{profile}.properties

      application:表示应用名称,在client中通过spring.config.name配置profile:表示获取指定环境下配置,

      例如开发环境、测试环境、生产环境 默认值default,实际开发中可以是 dev、test、demo、production等。label: git标签,默认值master

   3.5 服务开启访问地址

      访问地址http://localhost:8090/config-server/dev,可以读取到git中config文件夹下的配置文件夹config-server-dev.properties

     git配置文件如下

猜你喜欢

转载自www.cnblogs.com/rainsakura/p/10406489.html