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. 教你 Shiro 整合 SpringBoot,避开各种坑

    教你 Shiro 整合 SpringBoot,避开各种坑-----https://www.cnblogs.com/HowieYuan/p/9259638.html

  2. ceph rbd 入门

    1.一个现成的ceph cluster 参考之前写的ceph-deploy 部署ceph cluster 2.配置client与ceph cluster对接 在ceph cluster的管理节点上安装 ...

  3. SQL to MongoDB Mapping Chart

    http://docs.mongodb.org/manual/reference/sql-comparison/ In addition to the charts that follow, you ...

  4. 洛谷—— P2196 挖地雷

    https://www.luogu.org/problem/show?pid=2196 题目背景 NOIp1996提高组第三题 题目描述 在一个地图上有N个地窖(N<=20),每个地窖中埋有一定 ...

  5. RMAN RECOVERY

    Data Recovery Advisor The health monitor and the ADR The capabilities and limitations of DRA using t ...

  6. 类似于SVN的文档内容差异对比工具winmerge

    原文:http://www.jianshu.com/p/99282a4f3870 https://sourceforge.net/projects/winmerge/?source=typ_redir ...

  7. MyEclipse 9.0 正式版公布新闻 下载

    MyEclipse 9.0 正式版公布 新闻 ============================================================================ ...

  8. [LeetCode][Java] N-Queens II

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  9. Codeforces Round #142 (Div. 2)B. T-primes

    B. T-primes time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  10. 解决无线网卡 RTL8723BE ubuntu环境下不稳定情况

    jiqing@ThinkPad:~$ lspci | grep -i net 00:19.0 Ethernet controller: Intel Corporation Ethernet Conne ...