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 ...
随机推荐
- bzoj1690
二分+分数规划+dfs判环 跟1486很像,但是我忘记怎么判环了, 我们可以写一个dfs,如果当前节点的距离小于更新的距离,而且这个点已经在当前访问过了,那么就是有环了,如果没有访问过就继续dfs,每 ...
- 【转载】同步与异步--阻塞与非阻塞型I/O
同步阻塞IO 在这个模型中,应用程序(application)为了执行这个read操作,会调用相应的一个system call,将系统控制权交给kernel,然后就进行等待(这其实就是被阻塞了).ke ...
- 仪表盘(Speedometer) icon 设计
- codevs2503失恋28天......(背包dp)
503 失恋28天-缝补礼物 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题目描述 Description 话说上回他给女孩送了n件礼物,由于是廉价的所以 ...
- MySQL数据库笔记总结
MySQL数据库总结 一.数据库简介 1. 数据 所谓数据(Data)是指对客观事物进行描述并可以鉴别的符号,这些符号是可识别的.抽象的.它不仅仅指狭义上的数字,而是有多种表现形式:字母.文字.文本. ...
- 康少带你玩转CSS-1
什么是CSS? 层叠样式表(假如HTML是一个人的话,css就是一个人的装饰品,比如裙子,衣服口红) 用来干什么的 设置标签样式的 css注释 单行注释/**/ 多行注释 /* */ 语法结构 三种引 ...
- linux 查看 cpu
如何获得CPU的详细信息: linux命令:cat /proc/cpuinfo 用命令判断几个物理CPU,几个核等: 逻辑CPU个数:# cat /proc/cpuinfo | grep " ...
- ACM_Power Mouth
Power Mouth Time Limit: 2000/1000ms (Java/Others) Problem Description: Your task is to calculate the ...
- bootstrap图标乱码问题-解决方案
楼主在使用bootstrap时,出现了图标乱码问题,经过多次查找,才解决了问题(最后发现真的是很好解决的问题(^^)) 如果出现乱码 请直接在自己写的CSS中重新引入一下font文件中的字体就好了 @ ...
- Android文件操作报open failed: EBUSY (Device or resource busy)
Android删除文件后重新创建时偶尔出现 open failed: EBUSY (Device or resource busy)错误,该错误是Android系统的一个bug,大概的意思类似于win ...