Spring Cloud Config

参考个人项目

参考个人项目 : (希望大家能给个star~)
https://github.com/FunriLy/springcloud-study/tree/master/%E6%A1%88%E4%BE%8B5

什么是 Spring Cloud Config?

配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion。
场景介绍:在一个 Application
中,很经常需要连接资源和其它应用,经常有很多需要外部设置的信息去调整Application行为。我们在实际开发应用中的会经常见到的xml、properties、yaml等就是配置信息,但这种做法有一定的缺陷:每次更新需要重新打包和重启。

创建 Spring Cloud Config Server(Git 存储)

  • 这里我用了我原本的”服务注册中心”(Eureka Server),将其改造为”配置中心”(Config Server)。

  • 引入依赖

       <!-- Config Server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
  • 在启动主类添加@EnableConfigServer注解,开启 Config Server。
@SpringBootApplication
@EnableEurekaServer
@EnableConfigServer
public class MySpringCloudApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringCloudApplication.class, args);
}
}
  • 在Github上创建一个项目,并在其中添加配置文件 config-client.properties,在里面添加一个属性config=hello world !

  • application.properties中配置服务信息以及git信息(这里不包括了 Eureka Server 的配置,自行补充)

server.port=8761
spring.cloud.config.server.git.uri=https://github.com/FunriLy/springcloud-study/
spring.cloud.config.server.git.searchPaths=config-repo
spring.cloud.config.server.git.username=Username
spring.cloud.config.server.git.password=Password
{
"name": "config-client",
"profiles": [
"default"
],
"label": null,
"version": "af7ce2a15dcdea9dab42e6b44d37e401072382d8",
"propertySources": [
{
"name": "https://github.com/FunriLy/springcloud-study/config-repo/config-client.properties",
"source": {
"configword": "hello world !"
}
}
]
}

创建一个Spring Cloud Config Client

  • 这里我用到了原来的”服务提供者”(Eureka Client),将其改造为 Config Client。

  • resource下创建bootstrap.properties,并设置信息,具体如下:

spring.application.name=config-client
spring.cloud.config.profile=default
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:8761/
注意这里是bootstrap.properties而不是appliction.properties。
因为bootstrap.properties会在应用启动之前读取,而spring.cloud.config.uri会影响应用启动
  • 创建一个Controller来进行测试。
@RestController
public class ConfigController { @Value("${configword}")
String configword; @RequestMapping("/config")
public String printfConfig(){
return "The Config Word Is : "+configword;
}
}
The Config Word Is : hello world !

附录

来源于 Spring Cloud Config 中文文档。

URL与配置文件的映射关系

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

application是SpringApplicationspring.config.name,(一般来说’application’是一个常规的Spring Boot应用),profile是一个active的profile(或者逗号分隔的属性列表),label是一个可选的git标签(默认为”master”)。
比如,我的文件名是”config-client”,一般在Github上都是default环境,默认为master分支。所以就是/config-client/default/master

Config Server 配置文件

  • spring.cloud.config.server.git.uri:配置git仓库位置
  • spring.cloud.config.server.git.searchPaths:配置仓库路径下的相对搜索位置,可以配置多个
  • spring.cloud.config.server.git.username:访问git仓库的用户名
  • spring.cloud.config.server.git.password:访问git仓库的用户密码

Config Client 配置文件

  • spring.application.name:对应前配置文件中的{application}部分
  • spring.cloud.config.profile:对应前配置文件中的{profile}部分
  • spring.cloud.config.label:对应前配置文件的git分支  
  • spring.cloud.config.uri:配置中心的地址    

其他

在Config Server中,还有一种不使用Git的”native”的配置方式,这种方式是从本地classpath 或文件系统中加载配置文件(使用 “spring.cloud.config.server.native.searchLocations”配置项进行设置)。 加载Config Server 的”spring.profiles.active=native”配置项可以开启native配置。如:

spring.profiles.active=native
spring.cloud.config.server.native.searchLocations=file:D:/properties

注意 : 牢记使用file:前缀来指示资源(默认没有前缀是从classpath中去文件)。也可以嵌入${}环境参数占位符,但是windows系统下使用绝对路径,前缀后面需要多加个”/”。

spring cloud学习(六)Spring Cloud Config的更多相关文章

  1. Spring Cloud 学习 (六) Spring Cloud Config

    在实际开发过程中,每个服务都有大量的配置文件,例如数据库的配置.日志输出级别的配置等,而往往这些配置在不同的环境中也是不一样的.随着服务数量的增加,配置文件的管理也是一件非常复杂的事 在微服务架构中, ...

  2. spring boot 学习(六)spring boot 各版本中使用 log4j2 记录日志

    spring boot 各版本中使用 log4j2 记录日志 前言 Spring Boot中默认日志工具是 logback,只不过我不太喜欢 logback.为了更好支持 spring boot 框架 ...

  3. Spring boot 学习六 spring 继承 mybatis (基于注解)

    MyBatis提供了多个注解如:@InsertProvider,@UpdateProvider,@DeleteProvider和@SelectProvider,这些都是建立动态语言和让MyBatis执 ...

  4. Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)

    Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...

  5. Spring Cloud 学习 之 Spring Cloud Eureka(搭建)

    Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 文章目录 搭建服务注册中心: 注册服务提供者: 高可用注册中心: 搭建服务注册中心: ...

  6. spring cloud学习(六) 配置中心-自动更新

    上一篇学习了spring cloud config的基本使用,但发现有个问题,就是每次更改配置后,都需要重启服务才能更新配置,这样肯定是不行的.在上网查资料了解后,spring cloud支持通过AM ...

  7. Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...

  8. Spring Cloud 学习 (九) Spring Security, OAuth2

    Spring Security Spring Security 是 Spring Resource 社区的一个安全组件.在安全方面,有两个主要的领域,一是"认证",即你是谁:二是& ...

  9. spring学习 六 spring与mybatis整合

    在mybatis学习中有两种配置文件 :全局配置文件,映射配置文件.mybatis和spring整合,其实就是把mybatis中的全局配置文件的配置内容都变成一个spring容器的一个bean,让sp ...

随机推荐

  1. 使用 Vue.js 结合bootstrap 实现的分页控件

    原文链接:http://blog.csdn.net/qiuhaotc/article/details/53031884 源码下载: http://pan.baidu.com/s/1i4XgH6H 密码 ...

  2. 面向对象第一话,大战java正则表达式

    本周我们迎来第一项任务,java面向对象之实现对一串字符的匹配以及构造出计算的多项式,最终得出计算的结果.简而言之,可以用以下的要求来看题目: 输入的多项式字符串中不得出现非法字符 多项式的输入型式, ...

  3. Linux必备知识

    一.Linux命令行常用快捷键 在企业工作中,管理Linux时—般不会直接采用键盘.显示器登录系统,而是会通过网络在远程进行管理,因此,需要通过远程连接具连接到Linux系统中.目前最常用的Linux ...

  4. bootstrap的carousel图片轮播

    整个轮播是放在一个div .carousel和.slide的div中的, 包括3个部分: 1. 第一个部分indicator位于下方的指示器部分. 结构是一个ol和li, ol的类是carousel- ...

  5. hadoop项目实战--ETL--(三)实现mysql表到HIVE表的全量导入与增量导入

    一 在HIVE中创建ETL数据库 ->create database etl; 二 在工程目录下新建MysqlToHive.py 和conf文件夹 在conf文件夹下新建如下文件,最后的工程目录 ...

  6. mysql时间格式化函数日期格式h和H区别

    本文为博主原创,未经允许不得转载: 今天碰到一个问题,发现项目中有一个统计图的数据和时间格式没有对应准确,统计图要描述的是操作次数和操作时间的关系, 但很奇怪的是操作次数对应的时间却是凌晨,实际应用中 ...

  7. U盘中病毒了怎么办

    声明: 该方案仅是我处理自己U盘的心得,如果您看到这篇文件,并使用此方法,有任何数据丢失或者损失,我概不负责 问题: U盘里面显示占用了空间,就是看不到文件,或者仅有一两个自己不认识的文件 处理方案: ...

  8. LA 3027 合作网络

    https://vjudge.net/problem/UVALive-3027 题意: 有n个结点,初始时每个结点的父节点都不存在.你的任务是执行一次I操作和E操作,格式如下: I u v:把结点u的 ...

  9. UVa 11292 勇者斗恶龙

    https://vjudge.net/problem/UVA-11292 题意:有n条任意个头的恶龙,你希望雇一些其实把它杀死.一个能力值为x的骑士可以砍掉恶龙一个直径不超过x的头,且需要支付x个金币 ...

  10. UVa 247 电话圈(Floyd传递闭包)

    https://vjudge.net/problem/UVA-247 题意: 如果两个人相互打电话,则说他们在同一个电话圈里.例如,a打给b,b打给c,c打给d,d打给a,则这4个人在同一个圈里:如果 ...