Spring Cloud配置中心之Consul
Consul不仅可以作为Spring Cloud中服务的注册中心,也可以作为其配置中心,这样一个系统就可以实现服务发现和统一配置,减少系统维护的麻烦,其中在使用Consul作为配置中心使用的过程中可以说是血泪史,参考其他博客大部分含糊其辞,Spring Cloud中文网站,方便大家参考。
Consul配置中心
引入pom依赖
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--actuator-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- consul discovery-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- consul config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<scope>provided</scope>
</dependency>
<!--configuration-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
依赖说明,创建的maven聚合项目,父pom中引入的Spring Cloud版本为Hoxton.SR3,Spring Boot版本为2.2.5.RELEASE
- actuator:Consul健康检查模块,可视化界面需要引入的模块
- discovery:Consul服务发现模块
- config:Consul作为配置中心需要的模块
- configuration:非必须引入,在使用
@ConfigurationProperties注解时,IDEA会提示错误Spring Boot Configuration Annotation Processor not Configured,引入当前依赖就好。
创建主启动类
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigurationProperties(ConfigBean.class)
public class ConsulApplication {
public static void main(String[] args) {
SpringApplication.run(ConsulApplication.class,args);
}
}
- @EnableDiscoveryClient:作为Spring Cloud服务发现客户端时使用此注解
- @EnableConfigurationProperties(ConfigBean.class):扫描配置类,要指定需要扫描的类否则无法注入,使用@Component注解可以不指定配置类
创建配置类
@Data
@ConfigurationProperties(prefix = "db-config")
@Component
@RefreshScope
public class ConfigBean {
private String driverClassName;
private String url;
private String userName;
private String password;
}
- @ConfigurationProperties(prefix = "db-config"):prefix与Consul服务端k/v存储的yml前缀保持一致
- @Component:此处使用后,主启动的@EnableConfigurationProperties(ConfigBean.class)可不指定.class,目的是为了交给Spring管理,可以注入
创建application.yml
application.yml是项目启动的配置文件,这里只配置Consul作为服务的配置中心,服务注册中心可以参考spring cloud consul
spring:
application:
name: consul-client
profiles:
active: dev
cloud:
consul:
discovery:
hostname: localhost
port: 8500
health-check-path: /actuator
health-check-interval: 10s
health-check-timeout: 30s
enabled: true
server:
port: 8080
创建bootstrap.yml
注意名称一定是bootstrap.yml,和application.yml同样在resources目录下
这也是博主踩坑的地方,网上很多资料将Consul的spring cloud consul config配置放在服务发现spring cloud consul discovery的配置一块,项目启动请求远程配置信息不会报错,但是注入为null,最终找了很久才找到原因。
spring:
cloud:
consul:
config:
enabled: true
prefix: config
default-context: consul-client
profile-separator: ','
data-key: data
format: yaml
host: localhost
port: 8500
- enabled:是否启用consul config的配置项
- prefix:配置文件的存储的根路径,默认为config
- default-context:存储服务时使用的文件目录名称,默认为application,一般设置为与spring.application.name相同
- profile-separator:比较难理解的一点,consul创建key值与环境的分隔符默认
, - data-key:配置文件存储key的值,或者理解为文件的名称,默认为data
- format:配置文件的文件格式,这里选择yaml
创建key/value
启动consul服务端后,访问http://localhost:8500,在这里创建键值对

key:
config/consul-client,dev/data
value:
desc: Consul Confisuration Test
dbconfig:
driverClassName: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@localhost:1521:orcl
userName: system
password: orcl
注意:

- 1:与bootstrap.yml中prefix保持一致
- 2:与bootstrap.yml中default-context保持一致
- 3:与bootstrap.yml中profile-separator保持一致
- 4:与application.yml中spring.profiles.active保持一致
- 5:与bootstrap.yml中data-key保持一致
创建测试类
@RestController
public class TestController {
@Resource
private ConfigBean configBean;
@Value("${desc}")
private String desc;
@RequestMapping("/desc")
public String desc(){
return desc;
}
@RequestMapping("/config")
public ConfigBean getUserInfo(){
return configBean;
}
}
访问http://localhost:8080/desc,查看属性desc的注入情况
Consul Configuration Test
访问http://localhost:8080/config,查看ConfigBean的注入情况
{"driverClassName":"oracle.jdbc.driver.OracleDriver","url":"jdbc:oracle:thin:@localhost:1521:orcl","userName":"system","password":"orcl"}
总结
- Consul既可以作为注册中心,还可以作为配置中心;
- 通过@Value注入的属性,修改Consul的配置后,属性不能立即生效,需要服务重启;
- 通过@ConfigurationProperties注入的属性,修改consul的配置后,属性会立即生效;
最后,Consul的官方文档都有详细的解释,查阅资料时一定要选择合适的资料,建议首先考虑官网。
Spring Cloud配置中心之Consul的更多相关文章
- 记录一个 spring cloud 配置中心的坑,命令行端口参数无效,被覆盖,编码集问题无法读取文件等.
spring cloud 配置中心 结合GIT , 可以运行时更新配置文件.发送指令让应用重新读取配置文件. 最近在测试服务器实现了一套,结果CPU 实用率暴增,使用docker compose启动 ...
- Spring Cloud配置中心(Config)
Spring Cloud配置中心(Config) Spring Cloud是现在流行的分布式服务框架,它提供了很多有用的组件.比如:配置中心.Eureka服务发现. 消息总线.熔断机制等. 配置中心在 ...
- spring cloud 配置中心
1. spring cloud配置中心server 1.1 创建git仓库 首先在github上搭建一个存储配置中心的仓库,需要创建两个分支,一个是master,一个是dev分支.自己学习可以用公开库 ...
- 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注册中心之Consul
Consul简介 Consul是HashiCorp公司使用Golang语言开发的一中多服务解决方案工具,相比于其他服务注册中心来说,Consul的功能更为强大,丰富,其中最基本的功能包含下面几点(翻译 ...
- Spring Cloud配置中心搭建(集成Git)
1. 在Github(或其他)创建配置中心仓库bounter-config-repo,然后在仓库创建两个配置文件:simon.properties.susan.properties,链接如下: htt ...
- Spring Cloud配置中心内容加密
从配置获取的配置默认是明文的,有些像数据源这样的配置需要加密的话,需要对配置中心进行加密处理. 下面使用对称性加密来加密配置,需要配置一个密钥,当然也可以使用RSA非对称性加密,但对称加密比较方便也够 ...
随机推荐
- spring boot: 用redis的消息订阅功能更新应用内的caffeine本地缓存(spring boot 2.3.2)
一,为什么要更新caffeine缓存? 1,caffeine缓存的优点和缺点 生产环境中,caffeine缓存是我们在应用中使用的本地缓存, 它的优势在于存在于应用内,访问速度最快,通常都不到1ms就 ...
- 说明资源路径位置类型无法解析The type javax.servlet.http.HttpServletResponse cannot be resolved.
导入dispatch项目后报错: 解决办法:在项目上单击鼠标右键> Add Libraries 选择 Server Runtime,下一步 选中Apache Tomcat7 完成 切换标 ...
- C++学习---单链表的构建及操作
#include <iostream> using namespace std; typedef struct LinkNode { int elem;//节点中的数据 struct Li ...
- 2. A Distributional Perspective on Reinforcement Learning
本文主要研究了分布式强化学习,利用价值分布(value distribution)的思想,求出回报\(Z\)的概率分布,从而取代期望值(即\(Q\)值). Q-Learning Q-Learning的 ...
- 小白也能看懂的JVM内存区域
前言 最近在准备面试题刷到了JVM这块,作为一个小白,巩固知识点最好的方式就是亲手写出来并分享:相信我的理解,同样是小白的你,一定有很大的帮助.不信,请你往下看! JVM内存区域简介 如果有人问Jav ...
- PHP实现Bitmap的探索 - GMP扩展使用
原文地址:https://blog.fanscore.cn/p/22/ 一.背景 公司当前有一个用户群的系统,核心功能是根据不同的条件组去不同的业务线中get符合条件的uid列表,然后存到redis中 ...
- node运行js获得输出的三种方式
一.通过console.log输出(我最喜欢的) 1.js脚本 1.js var arguments = process.argv.splice(2); //获得入参 var a= arguments ...
- Libevent库基础(2)
带缓冲区的事件 bufferevent #include <event2/bufferevent.h> read/write 两个缓冲. 借助 队列. 创建.销毁bufferevent: ...
- Spring Boot学习笔记(二)——HelloWorld实现
提示:要在Eclipse里使用Spring Boot,首先要安装STS插件,前面我们已经安装了STS插件了,可以创建Spring Boot项目了. 1.创建项目: 新建项目,选择Spring Boot ...
- 利用python3监控服务器状态进行邮件报警
在正式的生产环境中,我们常常会需要监控服务器的状态,以保证公司整个业务的正常运转,常常我们会用到像nagios.zabbix这类工具进行实时监控,那么用python我们怎么进行监控呢?这里我们利用了p ...