九:Spring Cloud 之配置中心单机版-config

1. 简介

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments.

  • Spring Cloud Config 支持服务端与客户端
  • 是一个分布式配置中心
  • 提供配置信息多环境切换
  • 配置信息更新,实时同步

2. 代码实现

2.1 涉及的模块及整体步骤

2.1.1 涉及的模块

  • config-server:配置中心服务端
  • config-client:配置中心客户端,验证配置信息访问规则
  • config-another-client:配置中心客户端,验证配置信息访问规则
  • config-repository:放置于GitHub的配置

2.1.2 整体步骤

  1. GitHub创建存放配置信息的config-repository目录与配置信息config-client-dev.propertiesconfig-another-client-test.properties,可通过demo中的config-repository模块关联GitHub上的配置。
  2. 实现config-server:关键是启动Spring Cloud Config Server功能,指定配置仓库的配置信息
  3. 实现config-client:从配置仓库读取配置信息
  4. 实现config-another-client:从配置仓库读取配置信息
  5. 实现config-repository:放置3、4步骤的配置信息

2.2 源代码

2.2.1 Github地址

https://github.com/andyChenHuaYing/spring-cloud-demo

2.2.2 配置信息地址

配置信息在dev-20180827、与master分支都有,但是代码中使用的是dev-20180827分支的代码,如果想修改值验证,记得确认客户端bootstrap.properties中配置信息与想要访问的仓库地址一致。
https://github.com/andyChenHuaYing/spring-cloud-demo/tree/dev-20180827/config-repository

2.3 config-server

2.3.1 整体实现

  1. pom.xml引入spring-cloud-config-server
  2. application.yml指定配置仓库连接信息
  3. 启动类使用注解启动类使用注解@EnableConfigServer开启配置服务功能

2.3.2 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">
    <parent>
        <artifactId>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>

    <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>
</project
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值