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非对称性加密,但对称加密比较方便也够 ...
随机推荐
- centos8平台编译安装nginx1.18.0
一,nginx的官网: http://nginx.org/ 说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest 对应的源码 ...
- sql server 2008 r2 直接下载地址,可用迅雷下载
sqlserver 2008 r2 直接下载地址,可用迅雷下载 下载sqlserver 2008 r2 ,微软用了一个下载器,经过从下载器上,将他的地址全部用键盘敲了下来.最终的简体中文版地址如下: ...
- 第10天 | 12天搞定Python,文件操作(超详细)
在开发系统的过程中,经常会用到XML存储和传输数据,XML是一种用于标记电子文件使其具有结构性的标记语言,在博客中经常会见到. JSON是一种轻量级的数据交换格式,常被用在后端和前端的数据交互上,如你 ...
- plsql查询中文乱码
1.查看数据库字符集 select userenv('language') from dual 查看数据库字符集 2.在环境变量中添加并设置变量 变量名:NLS_LANG: 变量值:第一步查询的数据库 ...
- No compatible servers were found,You'll need to cancel this wizard and install one!
原文链接:https://www.jianshu.com/p/a11f93fb16ce 问题原因 笔记本重装的windows系统,重新安装mysql的时候,显示错误,看了一下缺失服务,实际上可能是缺少 ...
- eclipse时一直卡在进程中
(1)今天遇到进入eclipse时一直卡在 进程中,无论是重启电脑,还是重启软件 删除 D:\workspace\.metadata\.lock 文件才有用,特此记录下. (2)还有一种情况就是打开e ...
- JDK1.8特性(更新中..)
"15,5,9,17,99,107,47"转List<Long> List<Long> linkCompanyIds = Arrays.asList(&qu ...
- LuoguP1286 两数之和
题面概括 将n个数两两相加得到n*(n-1)/2个和,给出这些和,求所有原数方案 n<=500 LuoguP1286 题解 此题原题是 n<10, 没啥可做的 先将 \(n*(n-1)/2 ...
- freopen ()函数
1.格式 FILE * freopen ( const char * filename, const char * mode, FILE * stream ); 2.参数说明 filename: 要打 ...
- Miller-Rabin 素数检验算法
算法简介 Miller-Rabin算法,这是一个很高效的判断质数的方法,可以在用\(O(logn)\) 的复杂度快速判断一个数是否是质数.它运用了费马小定理和二次探测定理这两个筛质数效率极高的方法. ...