Spring Boot 配置 - Consul 配置中心

▶ Spring Boot 依赖与配置
Maven 依赖
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
.....
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
</dependencies>
▶ 使用说明
1、部署 Consul
- 参考文档:上一篇
2、在 resources 路径下添加配置文件 bootstrap.properties,示例如下:
# consul 基本配置
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
# 启用 consul 配置中心
spring.cloud.consul.config.enabled=true
# 基础文件夹,默认值 config
spring.cloud.consul.config.prefix=config
# 应用文件夹,默认值 application,consul 会加载 config/<applicationName> 和 config/<defaultContext> 两份配置,设置为相同值,则只加载一份
spring.cloud.consul.config.default-context=testApp
spring.application.name=testApp
# 环境分隔符,默认值 ","
spring.cloud.consul.config.profile-separator=-
# 配置转码方式,默认 key-value,其他可选:yaml/files/properties
spring.cloud.consul.config.format=properties
# 配置 key 值,value 对应整个配置文件
spring.cloud.consul.config.data-key=data
# 启用配置自动刷新
spring.cloud.consul.config.watch.enabled=true
# 【疑问】请求 consul api 的延迟,单位:秒
spring.cloud.consul.config.watch.wait-time=1
# 刷新频率,单位:毫秒
spring.cloud.consul.config.watch.delay=10000
3、在 Consul Key/Value 中添加应用配置
配置项 spring.cloud.consul.config.prefix 指定了基本文件夹为 config,需要先创建文件夹 config:

新建分两种类型:文件夹、Key/Value,创建文件夹只需在后面加上 "/" 即可
配置项 spring.cloud.consul.config.default-context 和 spring.cloud.consul.config.profile-separator 指定了应用名和环境分隔符,例如应用 testApp 有环境 default、dev、prod,只需在 config 目录下创建 testApp、testApp-dev、testApp-prod 三个文件夹即可:

配置项 spring.cloud.consul.config.format 指定了 Value 的转化方式,依据个人喜好,可以配置为 yaml 或 properties,若选择这两种方式,需要配置 spring.cloud.consul.config.data-key,默认为 data,示例配置:

Tips:如需单独配置每个 Key/Value,spring.cloud.consul.config.format 和 spring.cloud.consul.config.data-key 均不用设置
4、配置刷新
spring.cloud.consul.config.watch.delay 设置了配置的刷新间隔,在 Consul 修改了配置,会动态同步到应用内部。
▶ Github Demo URL
▶ 相关文档
- Spring Cloud Consul Config

天天玩微信,Spring Boot 开发私有即时通信系统了解一下
● Spring Boot 定制 parent 快速构建应用
● Spring Boot 日志处理你还在用Logback?
● 【双11狂欢的背后】微服务注册中心如何承载大型系统的千万级访问?

本文由博客一文多发平台 OpenWrite 发布!
Spring Boot 配置 - Consul 配置中心的更多相关文章
- spring boot web相关配置
spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何web相关的配置便能提供web服务,这还得归于spri ...
- 初识Spring Boot框架(二)之DIY一个Spring Boot的自动配置
在上篇博客初识Spring Boot框架中我们初步见识了SpringBoot的方便之处,很多小伙伴可能也会好奇这个Spring Boot是怎么实现自动配置的,那么今天我就带小伙伴我们自己来实现一个简单 ...
- Spring Boot 2.0 配置图文教程
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...
- Spring boot 的自动配置
Xml 配置文件 日志 Spring Boot对各种日志框架都做了支持,我们可以通过配置来修改默认的日志的配置: #设置日志级别 logging.level.org.springframework=D ...
- spring boot多数据源配置(mysql,redis,mongodb)实战
使用Spring Boot Starter提升效率 虽然不同的starter实现起来各有差异,但是他们基本上都会使用到两个相同的内容:ConfigurationProperties和AutoConfi ...
- Spring Boot SSL [https]配置例子
前言 本文主要介绍Spring Boot HTTPS相关配置,基于自签证书实现: 通过本例子,同样可以了解创建SSL数字证书的过程: 本文概述 Spring boot HTTPS 配置 server. ...
- spring boot 系列之六:深入理解spring boot的自动配置
我们知道,spring boot自动配置功能可以根据不同情况来决定spring配置应该用哪个,不应该用哪个,举个例子: Spring的JdbcTemplate是不是在Classpath里面?如果是,并 ...
- 转-spring boot web相关配置
spring boot web相关配置 80436 spring boot集成了servlet容器,当我们在pom文件中增加spring-boot-starter-web的maven依赖时,不做任何w ...
- spring boot日志管理配置
spring Boot在所有内部日志中使用Commons Logging,但是默认配置也提供了对常用日志的支持,如:Java Util Logging,Log4J,Log4J2和Logback.每种L ...
随机推荐
- github代码仓库提示:“We found a potential security vulnerability in one of your dependencies”
问题描述: Github上传代码后出现这样的错误: We found a potential security vulnerability in one of your dependencies. A ...
- Django+小程序技术打造微信小程序助手 ✌✌
Django+小程序技术打造微信小程序助手 (一个人学习或许会很枯燥,但是寻找更多志同道合的朋友一起,学习将会变得更加有意义✌✌) 从零到一的完整项目开发实战过程,项目开发聚焦重要知识点,先原理后实战 ...
- nm 命令能够显示目标文件中重载函数的名字改变(C++)
#include <stdio.h> #include <iostream> using std::cout; using std::endl; //这里的两个不同的add函数 ...
- 渗透测试-基于白名单执行payload--Msiexec
复现亮神课程 基于白名单执行payload--Msiexec 0x01 关于msiexec Msiexec 是 Windows Installer 的一部分.用于安装 Windows Install ...
- USACO环绕岛屿Surround the Islands 并查集 枚举暴力
题目描述 Farmer John has bought property in the Caribbean and is going to try to raise dairy cows on a b ...
- TestLink+Jenkins在Ubuntu16.04搭建集成测试环境
序章 序1:TestLink和TestLink-API-Python-client 目前TestLink的最新版本是1.9.19 TestLink-API-Python-client支持的TestLi ...
- java集合之HashSet哈希SET基础
hashSet实现set接口.serializable.cloneable接口.不允许有重复值,但可以null,不保证与插入顺序一致. HashSet迭代的时间与HashSet实例的大小(元素的数量) ...
- Flink 从 0 到 1 学习 —— 如何自定义 Data Source ?
前言 在 <从0到1学习Flink>-- Data Source 介绍 文章中,我给大家介绍了 Flink Data Source 以及简短的介绍了一下自定义 Data Source,这篇 ...
- fullpage.js报如下错的解决办法
控制台报错:fullPage: Fullpage.js version 3 has changed its license to GPLv3 and it requires a `licenseKey ...
- Codeforces--Books Exchange (hard version)
题目链接http://codeforces.com/contest/1249/problem/B2 .并查集思想,将数分成多个集合,每个集合的大小就是一轮的所需天数. Map[i]存储数据. flag ...