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仓库读取的

Spring Cloud (5) 分布式配置中心的更多相关文章

  1. Spring Cloud Config 分布式配置中心使用教程

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...

  2. Spring Cloud 2-Config 分布式配置中心(七)

    Spring Cloud  Config  1.github配置 2.服务端配置 pom.xml application.xml Application.java 3.配置和命名 1. 配置加载顺序 ...

  3. Spring Cloud Config 分布式配置中心【Finchley 版】

    一. 介绍 1,为什么需要配置中心? 当服务部署的越来越多,规模越来越大,对应的机器数量也越来越庞大,靠人工来管理和维护服务的配置信息,变得困难,容易出错. 因此,需要一个能够动态注册和获取服务信息的 ...

  4. spring cloud 集成分布式配置中心 apollo(单机部署apollo)

    一.什么是apollo? Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用 ...

  5. Spring Cloud (6) 分布式配置中心-高可用

    高可用 现在已经可以从配置中心读取配置文件了,当微服务很多时都从配置中心读取配置文件,这时可以将配置中心做成一个微服务,将其集群化,从而达到高可用. 改造config-server 加入eureka ...

  6. Spring Cloud之分布式配置中心

    用服务的方式来实现 ConfigAppApplication.java package com.packtpub.ConfigApp; import org.springframework.boot. ...

  7. 跟我学SpringCloud | 第六篇:Spring Cloud Config Github配置中心

    SpringCloud系列教程 | 第六篇:Spring Cloud Config Github配置中心 Springboot: 2.1.6.RELEASE SpringCloud: Greenwic ...

  8. Spring Cloud Config 实现配置中心,看这一篇就够了

    Spring Cloud Config 是 Spring Cloud 家族中最早的配置中心,虽然后来又发布了 Consul 可以代替配置中心功能,但是 Config 依然适用于 Spring Clou ...

  9. Spring Cloud Config的配置中心获取不到最新配置信息的问题

    Spring Cloud Config的配置中心获取不到最新配置信息的问题 http://blog.didispace.com/spring-cloud-tips-config-tmp-clear/

随机推荐

  1. PAT 1134 Vertex Cover

    A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...

  2. 升级 HTTPS,价值何在?

    HTTPS 实质上是一种面向安全信息通信的协议.从最终的数据解析的角度上看,HTTPS 与 HTTP 没有本质上的区别.对于接收端而言,SSL/TSL 将接收的数据包解密,将数据传给 HTTP 协议层 ...

  3. 【Codeforces 501C】Misha and Forest

    [链接] 我是链接,点我呀:) [题意] 给你一棵树 但是每个节点只告诉你出度个数 以及所有和它相连的点的异或和. 让你还原这棵树 [题解] 叶子节点的话,他所有节点的异或和就是它那唯一的一个爸爸 因 ...

  4. 树上启发式合并(DSU on tree)

    //heavy-light decomposition style .//http://codeforces.com/blog/entry/44351 int cnt[maxn]; bool big[ ...

  5. Parallelized coherent read and writeback transaction processing system for use in a packet switched cache coherent multiprocessor system

    A multiprocessor computer system is provided having a multiplicity of sub-systems and a main memory ...

  6. 20180710使用gh

    转自:http://www.ywnds.com/?p=14265 一.背景 GitHub正式宣布以开源的方式发布gh-ost:GitHub的MySQL无触发器在线更改表定义工具!下面是官方给出gh-o ...

  7. iOS: 学习笔记, Swift操作符定义

    Swift操作符能够自行定义, 仅仅须要加上简单的标志符就可以. @infix 中置运算. 如+,-,*,/运算 @prefix 前置运算. 如- @postfix 后置运算. a++, a-- @a ...

  8. LeetCode 8. String to Integer (atoi) (字符串到整数)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  9. 常用的java方法,为程序添点小功能

    一.生成随机数 Random类使用示例 使用Random类,一般是生成指定区间的随机数字,下面就一一介绍如何生成对应区间的随机数字.以下生成随机数的代码均使用以下Random对象r进行生成: Rand ...

  10. ubuntu下使用crontab定时器

    crontab 定时工具(周期性执行的任务列表称为Cron Table),其中每一个任务,被称为Cron Job. 可以,每分钟执行,每小时执行,每天执行,每周执行,每月执行. 检查服务 1.查看任务 ...