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非对称性加密,但对称加密比较方便也够 ...
随机推荐
- hbase的Java基本操作
hbase的Java基本操作 建表,建列簇操作 private static Connection connection; private static Admin admin; public sta ...
- Python之tuple元组详解
元组:有序,一级元素不可以修改.不能被增加或删除(元组是可迭代对象) 一般写法括号内最后面加个英文逗号用来区分: test = (,) test1 = (11,22,) 例: test = (12 ...
- log4j日志级别怎么搞
日志的级别之间的大小关系如右所示:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF Log4j建 ...
- vue学习第一部
目录 基础操作 vue基础使用 步骤 vue的框架思想(mvvm) 显示数据 vue 常用指令 属性操作 事件绑定 操作样式 条件渲染指令 列表渲染指令 vue对象提供的属性功能 过滤器 计算和侦听属 ...
- 自定义View(进度条)
继承View重写带两个参数的构造方法,一个参数的不行不会加载视图,构造方法中初始化画笔这样不用没次刷新都要初始化浪费内存,在ondraw方法中绘图,invalidate方法会掉用ondraw方法重新绘 ...
- Redis【一】 RESP协议
https://redis.io/topics/protocol RESP:redis序列化协议 client-server交流 二进制安全的 网络层 client端建立tcp连接到Server po ...
- 注意由双大括号匿名类引起的serialVersionUID编译告警
问题描述 最近版本组织清理编译告警,其中有这么一条比较有意思,之前没见过,拿出来说一说 "serializable class anonymous com.demo.Main$1 has n ...
- Redis---07主从复制(哨兵模式)
一.什么是哨兵模式 基于主从复制的一般模式(一主二从)下,当发生主机发生宕机时,会通过流言协议判断主机是不是宕机,是的话则会通过投票协议自动把某一个从机转换成主机. 二.设置哨兵模式的配置文件 通过r ...
- liunx命令的运用
工作中用到了一些命令,记忆才深刻 1.查看服务器内存:free -h 2.查看服务器磁盘空间:df -h 3.切root用户:sudo su root 输入密码 4.查看liunx服务器下的所有用户: ...
- day79:luffy:注册之对手机号的验证&实现基本的注册功能逻辑&点击获取验证码&redis
目录 1.前端和后端对于手机号的验证 2.实现基本的注册功能-不包括验证码 3.点击获取验证码 4.解决登录不上Xadmin的bug 5.redis register.vue页面 <templa ...