Spring Cloud Config(二):基于Git搭建配置中心
1、简述
本文选用Git作为配置仓库,新建两个环境的配置文件夹,dev 和 test,文件夹中分别存放 Config Client 端的配置文件,目录结构如下:
├ ─ ─ dev
└ ─ ─ config-client-dev.properties
├ ─ ─ test
└ ─ ─ config-client-test.properties
2、Config Server 搭建
2.1、Maven依赖
Config Server 是一个基于Spring Boot的web应用,我们首先需要做的就是在pom中引入Spring Cloud Config Server的依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2.2、Config Server配置
# 配置中心名称
spring.application.name=lkf-config-git
# 配置中心端口号
server.port=1000
# 配置git仓库地址
spring.cloud.config.server.git.uri=https://github.com/liukaifeng/lkf-cloud
# 配置文件查找路径
spring.cloud.config.server.git.search-paths=config-repo/dev
# 配置中心api前缀
spring.cloud.config.server.prefix=lkf
2.3、启用Config Server
最后就是启用Config Server,只需要加上@EnableConfigServer即可
@SpringBootApplication
@EnableConfigServer
public class GitConfigApplication {
public static void main(String[] args) {
SpringApplication.run(GitConfigApplication.class, args);
}
}
配置后完成后启动应用,Config Server就开始工作了,访问地址:
http://localhost:1000/lkf/config-client/dev 看到了刚刚配置文件中的配置信息,到此配置中心服务端搭建完成。
3、Config Client 使用
3.1、Maven依赖
Config Client 是一个基于Spring Boot的web应用,我们首先需要做的就是在pom中引入Spring Cloud Config Client的依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
3.2、Config Client配置
我们需要做一些配置使Config Client知道Config Server的地址,以及应用自身配置的信息,如:应用名字,环境,配置的版本等信息,以下是配置:
#配置环境
spring.cloud.config.profile=dev
#配置中心地址
spring.cloud.config.uri=http://localhost:1000/lkf
#配置文件名称
spring.cloud.config.name=config-client
配置完成后,启动应用,Config Client就会自动从Config Server获取配置,并注册到注册中心,访问注册中心地址:http://localhost:8888/
至此,Config Client 已成功从配置中心拉取并解析成功配置并注册到了注册中心。另外在应用启动日志中也可以看到拉取配置信息的日志:
Fetching config from server at : http://localhost:1000/lkf
2018-11-03 22:48:47.951 [main] DEBUG org.springframework.web.client.RestTemplate - Created GET request for "http://localhost:1000/lkf/config-client/dev"
2018-11-03 22:48:48.015 [main] DEBUG org.springframework.web.client.RestTemplate - Setting request Accept header to [application/json, application/*+json]
2018-11-03 22:48:49.130 [main] DEBUG org.springframework.web.client.RestTemplate - GET request for "http://localhost:1000/lkf/config-client/dev" resulted in 200 (null)
2018-11-03 22:48:49.132 [main] DEBUG org.springframework.web.client.RestTemplate - Reading [class org.springframework.cloud.config.environment.Environment] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@273842a6]
2018-11-03 22:48:49.150 [main] INFO o.s.c.c.client.ConfigServicePropertySourceLocator - Located environment: name=config-client, profiles=[dev], label=null, version=0eaed01201a6f2c5fd2b0fd3b637d8384f3c79b9, state=null
2018-11-03 22:48:49.150 [main] DEBUG o.s.c.c.client.ConfigServicePropertySourceLocator - Environment config-client has 1 property sources with 13 properties.
2018-11-03 22:48:49.150 [main] INFO o.s.c.b.c.PropertySourceBootstrapConfiguration - Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource@466319810 {name='configClient', properties={config.client.version=0eaed01201a6f2c5fd2b0fd3b637d8384f3c79b9}}, MapPropertySource@219812012 {name='https://github.com/liukaifeng/lkf-cloud/config-repo/dev/config-client-dev.properties', properties={server.port=8001, spring.application.name=lkf-eureka-client, eureka.client.serviceUrl.defaultZone=${EUREKA_SERVICE_URL:http://localhost:8888}/lb/eureka/, management.endpoint.conditions.enabled=true, eureka.instance.prefer-ip-address=true, eureka.instance.instanceId=${spring.application.name}@${spring.cloud.client.ip-address}@${server.port}, management.endpoints.web.exposure.include=*, management.endpoint.health.show-details=always, management.endpoint.logfile.enabled=true, management.endpoint.auditevents.enabled=true, management.endpoint.loggers.enabled=true, info.project.name=@project.name@, info.project.version=@project.version@}}]}
2018-11-03 22:48:49.150 [main] DEBUG o.s.core.env.PropertySourcesPropertyResolver - Could not find key 'logging.config:' in any property source
Spring Cloud Config(二):基于Git搭建配置中心的更多相关文章
- Spring Cloud Config、Apollo、Nacos配置中心选型及对比
Spring Cloud Config.Apollo.Nacos配置中心选型及对比 1.Nacos 1.1 Nacos主要提供以下四大功能 2.Spring Cloud Config 3.Apollo ...
- Spring Cloud Config 使用Bus的动态配置中心
server端配置 POM文件 <dependency> <groupId>org.springframework.boot</groupId> <artif ...
- Spring cloud config client获取不到配置中心的配置
Spring cloud client在配置的时候,配置文件要用 bootstrap.properties 贴几个说明的链接.但是觉得说的依然不够详细,得空详查. 链接1 链接2 链接3 原文地址:h ...
- Spring Cloud Config(三):基于JDBC搭建配置中心
1.简介 本文主要内容是基于jdbc搭建配置中心,使应用从配置中心读取配置信息并成功注册到注册中心,关于配置信息表结构仅供参考,大家可以根据具体需要进行扩展. 2.Config Server 搭建 2 ...
- 9.Spring Cloud Config统一管理微服务配置
Spring Cloud Config统一管理微服务配置 9.1. 为什么要统一管理微服务配置 9.2. Spring Cloud Config简介 Spring Cloud Config为分布式系统 ...
- 一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)
上一章节,我们讲解了服务网关zuul,本章节我们从git和本地两种存储配置信息的方式来讲解springcloud的分布式配置中心-Spring Cloud Config. 一.Spring Cloud ...
- Spring Cloud学习笔记【九】配置中心Spring Cloud Config
Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端 ...
- spring boot 2.0.3+spring cloud (Finchley)6、配置中心Spring Cloud Config
https://www.cnblogs.com/cralor/p/9239976.html Spring Cloud Config 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持, ...
- Spring Cloud系列(六):配置中心
在使用Spring Boot的时候,我们往往会在application.properties配置文件中写一些值,供应用使用,这样做的好处是可以在代码中引用这些值,当这些值需要作出修改的时候,可以直接修 ...
随机推荐
- dotnetcore下解压zip文件,解决中文文件名乱码问题
(迄今为止网上那些说的用Encoding.Default解决中文文件名乱码的都不能真正解决问题!) 1.在程序开始处 Encoding.RegisterProvider(CodePagesEncodi ...
- Apache Flink 任意jar包上传漏洞
目前受影响版本:version 1.9.1(最新),官方未发布补丁. Apache Flink仪表板- >上传恶意的JAR- >提交新工作- >getshell 生成jar包,用nc ...
- python3爬虫之requests库基本使用
官方文档链接(中文) https://2.python-requests.org/zh_CN/latest/ requests 基于 urllib3 ,python编写. 安装 pip insta ...
- 7.Redis的发布订阅
Redis消息的订阅/发布 a)是什么 进程间的一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接收消息. b)命令 SUBSCRIBE订阅 PUBLISH发布 c)案例 先订阅后发布后才 ...
- javascript 给事件任务一个缓冲区
在编写前端的过程中,经常会监听事件并执行任务,我在这抛出2个比较常见的场景: 1.输入关键字搜索如果你监听input的chage事件,会有一个问题,在使用中文输入法时,你输入的几个拼音字母都会被触发我 ...
- linux 基础12-程序与资源管理
1. 基础概念 可执行的二进制文件就是程序 执行程序的时候因触发事件而获取的ID,称为PID 在登入并执行bash时,系统依据登录者的UID/GID给登录者一个PID/GPID/SID等 启动程序时, ...
- 解决Centos /boot过小无法更新内核
Centos7默认安装时,/boot目录设置只有150M左右,这样编译几个版本的内核/boot空间就不够用了.报错大致如下: Disk Requirements: At least 3MB more ...
- Selenium(四)使用xpath定位元素
1.什么是xpath: 2.xpath的节点类型 3.xpath的表达式 4.开始定位 浏览器打开本地文件: (python3.7的打开语法) 查找根节点: (绝对路径)查找子节点: 查找type ...
- rabbitmq 公平分发和消息接收确认(转载)
原文地址:http://www.jianshu.com/p/f63820fe2638 当生产者投递消息到broker,rabbitmq把消息分发到消费者. 如果设置了autoAck=true 消费者会 ...
- Socket嵌套字通讯
一.socket是什么 Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后 ...