springCloud学习-分布式配置中心(Spring Cloud Config)
1、简介
Spring Cloud Config :分布式配置中心,方便服务配置文件统一管理,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。
2、config server从本地读取配置文件
2.1、在父工程的pom.xml中的 <modules>节点下 添加如下代码
<module>config-server</module>
<module>config-client</module>
2.2、创建config-server工程,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.lishun</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>config-server</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>com.lishun</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <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>
2.3、在程序的入口Application类加上@EnableConfigServer注解开启配置服务器的功能,代码如下:
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
2.4、application.properties配置文件,因为配置文件内容较多,感觉yml格式配置起来不太直观,所以使用.properties格式
spring.application.name=config-server
server.port=8888 spring.cloud.config.server.native.search-locations= classpath:/shared
spring.profiles.active=native
spring.profiles.active = native指定从本地读取配置,读取的路径为classpath下的shared目录
2.5、在工程的 Resources 目录下建一个 shared 文件夹,用于存放本地配置文件。在 shared 目录下,新建一个 config-client-dev.properties文件,内容如下
id = config-test-native
name = study-cloud
3、构建config-client
3.1、创建config-server工程,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.lishun</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>com.lishun</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <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> </project>
3.2、配置文件bootstrap.properties。
spring.application.name=config-client
spring.cloud.config.uri= http://localhost:8888/
spring.cloud.config.fail-fast=true
spring.profiles.active=dev
server.port=8881
注意这里是bootstrap.properties,而不是application.properties。bootstrap相对于application具有优先的执行顺序,在配置文件中spring.application.name指明了服务名称,spring.cloud.config.uri指定读取配置文件的路径,
如果没有读取成功,执行快速失败(fail-fast),spring.profiles.active指明读取的是dev文件,变量 { spring. application.name}和变量 { spring.profiles.active},两者以“-”相连,构成了向config server读取的配置文件名
所以这里读取的配置文件是config-client-dev.properties
3.3、程序的入口类,写一个API接口“/hi”,返回从配置中心读取的id变量的值,代码如下:
@SpringBootApplication
@RestController
public class ConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
} @Value("${id}")
String id;
@RequestMapping(value = "/hi")
public String hi(){
return id;
}
}
3.4、启动config server 和config client工程,打开网址访问:http://localhost:8881/hi,网页显示:
config-test-native
4、从git远程仓库读取配置文件
4.1、修改config-server的配置文件application.properties,修改内容如下:
spring.application.name=config-server
server.port=8888 #spring.cloud.config.server.native.search-locations= classpath:/shared
#spring.profiles.active=native spring.cloud.config.server.git.uri=https://github.com/lis-ylfy/config-test/
spring.cloud.config.server.git.searchPaths=lis
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
- spring.cloud.config.server.git.uri:配置git仓库地址
- spring.cloud.config.server.git.searchPaths:配置仓库路径
- spring.cloud.config.label:配置仓库的分支
- spring.cloud.config.server.git.username:访问git仓库的用户名
- spring.cloud.config.server.git.password:访问git仓库的用户密码
如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,本例子是公开仓库,放心使用。远程仓库 https://github.com/lis-ylfy/config-test/ 中有个文件config-client-dev.properties文件内容如下:
id = config-test
name = study-cloud
4.2、重启config server 和config client工程,打开网址访问:http://localhost:8881/hi,网页显示:
config-test
springCloud学习-分布式配置中心(Spring Cloud Config)的更多相关文章
- 一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)
上一章节,我们讲解了服务网关zuul,本章节我们从git和本地两种存储配置信息的方式来讲解springcloud的分布式配置中心-Spring Cloud Config. 一.Spring Cloud ...
- 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...
- Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config
目录 Spring Cloud Config(一):聊聊分布式配置中心 Spring Cloud Config Spring Cloud Config(二):基于Git搭建配置中心 Spring Cl ...
- SpringCloud(6)分布式配置中心Spring Cloud Config
1.Spring Cloud Config 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组 ...
- springCloud学习-高可用的分布式配置中心(Spring Cloud Config)
1.简介 高可用的分布式配置中心,即将配置中心做成一个微服务,将其集群化,从而达到高可用.config-server和config-client向eureka-server注册,且将config-se ...
- SpringCloud学习(六)分布式配置中心(Spring Cloud Config)(Finchley版本)
在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服务的配置文件.它就是Spring Cloud Config. 简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理, ...
- SpringCloud学习(七)高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用 准备工作 ...
- SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...
- 史上最简单的SpringCloud教程 | 第六篇: 分布式配置中心(Spring Cloud Config)
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件. 在Spring Cloud中,有分布式配置中心组件spring cloud confi ...
随机推荐
- 微信小程序连接Java后台
有人问我小程序怎么连后台,这里直接贴代码 在app.js里 // api request request(url, params) { return new Promise((resolve, rej ...
- hihocode 编程练习赛17
1. f1 score 首先了解f1 score的计算方法, 我记得是学信息检索知道的, 然后简单处理就行. 由于我写的比较麻烦, 中间处理过程引入了一些除数为0的情况,导致错了很多次.其实是很简单的 ...
- 【转】Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例
概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...
- Android RecyclerView遇到notifyDataSetChanged无效时的解决方案
一.简述 不管AbsListView(ListView.GridView)或是新出的RecyclerView,在使用notifyDataSetChanged方法更新列表数据时,一定要保证数据为同个对象 ...
- 【年终糖果计划】跟风领一波糖果 candy.one 领取教程
糖果领取网址(较为稳定):https://candy.one/i/1474564 用微信和QQ打开的朋友请复制到其他浏览器打开 糖果领取网址(较为稳定):https://candy.one/i/147 ...
- android学习之路资料集合
版权声明:本文为 stormzhang 原创文章,可以随意转载,但必须在明确位置注明出处!!! 这篇博客背后的故事 一路走来很不容易,刚好知乎上被人邀请回答如何自学android编程, 就借这个机会在 ...
- 关于java 关键字enum不识别的解决办法
从别人那儿拷贝过来的myeclipse java工程,打开一看标红了一大片,仔细一看,原来是不识别enum关键字,这就有点尴尬了. 我自己重新建了一个java工程,测试了下,假如我在新建工程的时候选择 ...
- Go 时间相关
>获取当前时间: t := time.Now() >获取当天开始.结束时间: tm1 := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, ...
- 通过PHP怎样取到android系统下apk应用的包名,版本号等信息
公司项目关系,要求在通过PHP解析android系统应用apk包内的一切可用的信息.比如说:APK包名,版本号,版本名,安装权限等一系列关于对应包的信息.通过google查找相关的解决方案,都没有找到 ...
- C# 获取表中最大值
; if (db.LPicture.Any()) { // LPicture Newmode = db.LPicture.Where(n => ).FirstOrDefault(); start ...