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 ...
随机推荐
- 实验五Java网络编程及安全——20135337朱荟潼
实验五 Java网络编程及安全 结对伙伴:20135317韩玉琪(负责服务器方)http://www.cnblogs.com/hyq20135317/p/4567241.html 实验内容 1.掌握S ...
- 20172324 2017-2018-2《程序设计与数据结构》第五周 n!的计算
20172324 2017-2018-2<程序设计与数据结构>实验2报告 课程:<程序设计与数据结构> 班级: 1723 姓名: 曾程 学号:20172324 实验教师:王志强 ...
- Leetcode题库——7.反转整数
@author: ZZQ @software: PyCharm @file: IntReverse.py @time: 2018/9/16 16:36 要求:整数反转(给定一个 32 位有符号整数,将 ...
- 团队作业5-Alpha版本测试报告(彼岸芳华队)
请根据团队项目中软件的需求文档.功能说明.系统设计和测试计划,写出软件的测试过程和测试结果,并回答下述问题. 一.在测试过程中总共发现了多少Bug?每个类别的Bug分别为多少个?(10分) 在测试过程 ...
- 软工网络15团队作业8——Beta阶段敏捷冲刺(Day4)
提供当天站立式会议照片一张 每个人的工作 1.讨论项目每个成员的昨天进展 赵铭: 在知晓云上建立数据表 吴慧婷:做了背单词界面并学习了词库界面的设计. 陈敏: 我的词库-全部词汇功能/新建词汇功能全部 ...
- phantomjsDriver的初始化
public static void main(String[] args) { File file=new File("src/main/resources/drivers"); ...
- 使用 Vagrant 打造跨平台开发环境fffff
Vagrant 是一款用来构建虚拟开发环境的工具,非常适合 php/python/ruby/java 这类语言开发 web 应用,“代码在我机子上运行没有问题”这种说辞将成为历史. 我们可以通过 Va ...
- UVA11027_Palindromic Permutation
此题不错.给你一些字字符,要求你用这些字符构成一个回文串,求字典序第k大的回文串. 首先通过给定的字符,我们可以直接判断能否构成回文串(奇数的字符不超过一种),其次可以统计每个字符在回文串的左边应该出 ...
- poj3320 Jessica's Reading Problem
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
- 每日一问(如何在List中加入、设置、获取和删除其中的元素?)
作为集合接口的一部分,对List接口所做的操作,最常见的就是增删查改了.这里总结下JAVA 中List接口及实现该接口的类实现这些操作的方法. 一.增加新的元素的方法 在Collection接口中定义 ...