spring cloud 配置中心
1. spring cloud配置中心server
1.1 创建git仓库
首先在github上搭建一个存储配置中心的仓库,需要创建两个分支,一个是master,一个是dev分支。自己学习可以用公开库,真实环境使用的话,还是需要私库,或者自己搭建git服务器。

1.2 搭建server
使用spring cloud搭建服务器,工具使用idea,新建maven工程,配置pom.xml文件
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
创建springboot启动类,启动类要加上@SpringBootApplication 和 @EnableConfigServer 这两个注解。
@SpringBootApplication: springboot启动注解
@EnableConfigServer: springcloud config server的注解,必须要加上
@SpringBootApplication
@EnableConfigServer
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
创建配置文件bootstrap.yml
spring:
cloud:
config:
server:
git:
uri: https://github.com/hanggle/ConfigCenter.git #git仓库地址,就是刚才创建的git仓库
skipSslValidation: true #跳过校验
basedir: d:///myspace///config-center///config #从git仓库拉取到的文件在本地存储的位置,可自行修改或删掉,默认存储在C盘
# bootstrap: true
server:
port: 8889
config-server的目录结构:

启动springboot,启动成功后可以在浏览器查看拉取到的配置信息,路径的访问有以下几种:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
我创建的文件名是:application-dev.yml ,application-prod.yml
路径中占位符的表示是:
application 对应我文件中的 application
profile 对应 dev或prod
label 对应分支 master(默认是master 分支) 访问示例:
默认master分支

dev分支:


有兴趣的同学可以都试试,配置没问题都可以访问得到。
到此单机配置服务中心搭建完成了。
2. spring cloud配置中心client
使用spring cloud搭建服务器,工具使用idea,新建maven工程,配置pom.xml文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <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>
创建springboot启动类
@SpringBootApplication
public class ClientApplication { public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
创建测试controller
@RestController
public class TestController { @Value("${mydev}")
private String userName;
@Value("${profile}")
private String profile;
@Value("${name}")
private String name; @GetMapping("/test")
public String home() {
return "mydev: " + userName +" profile:" + profile + " name:" + name;
} }
配置文件
application.yml
server:
port: 8081
name: config-client
mydev: ${profile}
bootstrap.yml
spring:
application:
name: application # 指的是application-dev.xml中的application
cloud:
config:
uri: http://localhost:8889 # config-server 地址
profile: dev # 后缀 指的是application-dev.xml中的dev
label: dev # git 分支
项目结构

启动成功后访问

我在Github上的dev分支配置

在上面我通过两种方式读取配置属性:
1、直接在java文件中使用(peofile属性)
2、是在xml文件中陪之后再在java中使用(mydev属性)
比较推荐第二种,虽然多了一步,但是可以清除的知道来源,方便找问题。
3. 总结
到此初步完成简单的使用和测试,复杂的使用还需要再研究官方文档:http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html
单一服务肯定没办法保证高可用性,具体方案待续。。。。。。
spring cloud 配置中心的更多相关文章
- 记录一个 spring cloud 配置中心的坑,命令行端口参数无效,被覆盖,编码集问题无法读取文件等.
spring cloud 配置中心 结合GIT , 可以运行时更新配置文件.发送指令让应用重新读取配置文件. 最近在测试服务器实现了一套,结果CPU 实用率暴增,使用docker compose启动 ...
- Spring Cloud配置中心(Config)
Spring Cloud配置中心(Config) Spring Cloud是现在流行的分布式服务框架,它提供了很多有用的组件.比如:配置中心.Eureka服务发现. 消息总线.熔断机制等. 配置中心在 ...
- springcloud(五):Spring Cloud 配置中心的基本用法
Spring Cloud 配置中心的基本用法 1. 概述 本文介绍了Spring Cloud的配置中心,介绍配置中心的如何配置服务端及配置参数,也介绍客户端如何和配置中心交互和配置参数说明. 配置中心 ...
- springcloud(六):Spring Cloud 配置中心采用数据库存储配置内容
Spring Cloud 配置中心采用数据库存储配置内容 转自:Spring Cloud Config采用数据库存储配置内容[Edgware+] Spring Cloud Server配置中心采用了G ...
- (七)Spring Cloud 配置中心config
spring cloud config是一个基于http协议的远程配置实现方式. 通过统一的配置管理服务器进行配置管理,客户端通过http协议主动的拉取服务的的配置信息,完成配置获取. 下面我们对 ...
- Spring Cloud配置中心搭建(集成Git)
1. 在Github(或其他)创建配置中心仓库bounter-config-repo,然后在仓库创建两个配置文件:simon.properties.susan.properties,链接如下: htt ...
- Spring Cloud配置中心内容加密
从配置获取的配置默认是明文的,有些像数据源这样的配置需要加密的话,需要对配置中心进行加密处理. 下面使用对称性加密来加密配置,需要配置一个密钥,当然也可以使用RSA非对称性加密,但对称加密比较方便也够 ...
- Spring Cloud配置中心高可用搭建
本文通过config server连接git仓库来实现配置中心,除了git还可以使用svn或者系统本地目录都行. 引入依赖 <dependencies> <dependency> ...
- Spring Cloud配置中心客户端读取配置
微服务连接配置中心来实现外部配置的读取. 引入依赖 <dependencies> <dependency> <groupId>org.springframework ...
随机推荐
- 2-Seventh Scrum Meeting20151207
任务分配 闫昊: 今日完成:完成数据库设计. 明日任务:和唐彬讨论接口如何在android实现. 唐彬: 今日完成:读了IOS讨论区后台接口. 明日任务:和闫昊讨论接口如何在android实现. 史烨 ...
- OO第三阶段作业总结
调研: 最早的程序设计是直接采用机器语言来编写的,或者使用二进制码来表示机器能够识别和执行的指令和数据.机器语言的优点在于速度快,缺点在于写起来实在是太困难了,编程效率低,可读性差,并且 ...
- Docker基础教程
一.Docker是什么? KVM, Virtualbox, Vmware是虚拟出机器,让每个实例看到一个单独的机器:而Docker是虚拟出操作系统,实现应用之间的隔离,让各个应用觉得自己有一个自己的操 ...
- Java 多生产者消费者问题
/* 生产者,消费者. 多生产者,多消费者的问题. if判断标记,只有一次,会导致不该运行的线程运行了.出现了数据错误的情况. while判断标记,解决了线程获取执行权后,是否要运行! not ...
- iOS UIView性能最优的设计圆角并且绘制边框颜色
//以给cell切圆角为例- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollection ...
- charles抓取移动端app数据
pc端为mac 移动端为android pc端 1.下载charles并安装 安利一个超好的良心网站(好多好用的软件都可以在上面找到,并且免费): http://xclient.info/search ...
- Vue2.0组件之间通信(转载)
Vue中组件这个特性让不少前端er非常喜欢,我自己也是其中之一,它让前端的组件式开发更加合理和简单.笔者之前有写过一篇Vue2.0子父组件通信,这次我们就来聊一聊平级组件之间的通信. 首先我们先搭好开 ...
- [转帖] IIS 与 HTTP/2 的介绍.
HTTP/2 on IIS https://blogs.iis.net/davidso/http2 Friday, September 11, 2015 Windows 10 HTTP2 In Oct ...
- JS 日期 自动补齐 “2017-11-22 14:43”
var myDate = new Date(); var myN = myDate.getFullYear(); var myY = myDate.getMonth(); var myR = myDa ...
- BZOJ3277 串(后缀数组+二分答案+主席树)
因为不会SAM,考虑SA.将所有串连起来并加分隔符,每次考虑计算以某个位置开始的子串有多少个合法. 对此首先二分答案,找到名次数组上的一个区间,那么只需要统计有多少个所给串在该区间内出现就可以了.这是 ...