Spring Cloud (5) 分布式配置中心

Spring Cloud Config

  在分布式系统中,由于服务数量很多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,使用Spring Cloud Config来实现分布式配置中心,它支持配置服务放在配置服务的内存中(本地),也支持放在远程git仓库中,在spring cloud config组件中,分为两个角色,一个是config server,一个是config client。

 

准备配置仓库

  https://gitee.com/baidawei/SpringCloudConfig.git

 

构建配置中心(config server)

新建一个spring boot项目,名为com.david.configserver , pom.xml如下:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.david</groupId>
    <artifactId>configserver</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>configserver</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

在启动类中加上@EnableConfigServer注解开启配置服务器的功能:

@EnableConfigServer
@SpringBootApplication
public class ConfigserverApplication {

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

在application.yml中添加配置服务的基本信息以及git仓库的相关信息:

spring:
  application:
    name: david-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/baidawei/SpringCloudConfig.git
      search-paths: repo username: #如果需要权限username password: #password server: port:
8888

如果项目公开可以不使用账号密码,searchPaths 是文件的路径.

假设我们读取配置中心的应用名为david-config-client,那么我们可以在git仓库中创建如下两个配置文件:

david-config-client.yml 

info:
  profile: default
  form: git-repo

david-config-client-dev.yml dev环境

info:
  profile: dev
  form: git-repo

到现在,使用git管理的分布式配置中心就完成了,可以通过浏览器、postman等工具来访问配置的内容了。访问配置信息的url与配置文件的映射关系如下:

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

  /{application}-{profile}.yml

  /{label}/{application}-{profile}.yml

  /{application}-{profile}.properties

  /{label}/{application}-{profile}.properties

其中,{label}对应git上不同的分支,默认为master。

 

 

访问david-config-client.yml 路径为:http://localhost:8888/david-config-client/master

{"name":"david-config-client","profiles":["master"],"label":null,"version":"b5fdf45844bf09c30e403e7b069f1c243fb1d4a7","state":null,"propertySources":[{"name":"https://gitee.com/baidawei/SpringCloudConfig.git/repo/david-config-client.yml","source":{"info.profile":"default","info.form":"git-repo"}}]}

  dev: http://localhost:8888/david-config-client/dev

{"name":"david-config-client","profiles":["dev"],"label":null,"version":"b5fdf45844bf09c30e403e7b069f1c243fb1d4a7","state":null,"propertySources":[{"name":"https://gitee.com/baidawei/SpringCloudConfig.git/repo/david-config-client-dev.yml","source":{"info.profile":"dev","info.from":"git-repo"}},{"name":"https://gitee.com/baidawei/SpringCloudConfig.git/repo/david-config-client.yml","source":{"info.profile":"default","info.form":"git-repo"}}]}

 

构建客户端

  完成了分布式配置中心后,构建一个david-config-client,获取上述配置信息,在pom.xml中添加依赖:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.david</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

配置文件bootstrap.yml,来指定获取配置中心的配置文件

spring:
  application:
    name: david-config-client #对应{application}位置
  cloud:
    config:
      uri: http://localhost:8888/ #配置中心网址
      profile: default #对应{profile}位置 dev等
      label: master #对应{label}位置 分支
server:
  port: 8881

名字必须是bootstrap.yml或properties才能被正确加载,

新建TestController

@RestController
public class TestController {
    @Value("${info.profile}")
    String info;
    @GetMapping("/test")
    public String test(){
        return info;
    }
}

把server和client都启动起来

http://localhost:8881/test 输出default 切换配置文件中的profile为dev  输出为dev

这说明,config-client是从config-server中获取了info.profile属性,而config-server是从git仓库读取的

posted @ 2018-06-05 18:26  海盗船长  阅读(263)  评论(0编辑  收藏  举报