一、背景介绍

在分布式系统中动态配置中,可以避免重复重启服务,动态更改服务参数等。一句话非常重要。 另外一篇文章也是这样说的,哈哈。 Consul 作为Spring 推荐的分布式调度系统其也具备配置中心的功能, 我们也可以利用其作为配置中心,其client端主动定时发起与配置中心同步机制,实现动态配置的的更新。

环境依赖:

名称 备注
JDK 1.8
Consul 1.5.2 注册中心,Consul安装及介绍 https://mp.csdn.net/mdeditor/95372805#
SpringCloud Greenwich.SR1

二、项目实战

1) pom依赖(主要)

  1. <properties>
  2. <java.version>1.8</java.version>
  3. <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
  4. </properties>
  5. <dependencyManagement>

  6. <dependencies>

  7. <dependency>

  8. <groupId>org.springframework.cloud</groupId>

  9. <artifactId>spring-cloud-dependencies</artifactId>

  10. <version>${spring-cloud.version}</version>

  11. <type>pom</type>

  12. <scope>import</scope>

  13. </dependency>

  14. </dependencies>

  15. </dependencyManagement>
  16. <dependency>

  17. <groupId>org.springframework.boot</groupId>

  18. <artifactId>spring-boot-starter-web</artifactId>

  19. </dependency>

  20. <dependency>

  21. <groupId>org.springframework.cloud</groupId>

  22. <artifactId>spring-cloud-starter-consul-discovery</artifactId>

  23. </dependency>

  24. <dependency>

  25. <groupId>org.springframework.cloud</groupId>

  26. <artifactId>spring-cloud-starter-consul-config</artifactId>

  27. </dependency>

2)配置文件

application.properties

  1. #0表示服务器随机端口
  2. server.port=8090
  3. 本次演示的kv的默认值(老板默认给你0元)

  4. company.pay.money=0

bootstrap.properties

  1. #服务名称
  2. spring.application.name=waiter-service
  3. #consul 地址
  4. spring.cloud.consul.host=localhost
  5. #consul 端口
  6. spring.cloud.consul.port=8500
  7. spring.cloud.consul.discovery.prefer-ip-address=true
  8. #consul配置中心功能,默认true
  9. spring.cloud.consul.config.enabled=true
  10. #consul配置中心值的格式
  11. spring.cloud.consul.config.format=yaml

3)动态参数接收类

  1. @ConfigurationProperties("company.pay")
  2. @RefreshScope
  3. @Data
  4. @Component
  5. public class PayMoneyProperties {
  6. //key结尾部分,以小数点为间隔
  7. Integer money ;
  8. }

备注:

ConfigurationProperties 表示这个类关联动态配置,“company.pay”表示key的前缀部分。

@RefreshScope 表示动态刷新config server 值

@Component 表示将该类加载到IOC容器中

在实战中尝试用@Value的方式获取动态,只能实现服务重启后获取动态的config server 的值,最终找到解决方案在相应的取值类上加@RefreshScope注解,完美解决。

4)对外接口(便于直观验证)

方式一:

  1. @RestController
  2. @RequestMapping("consul")
  3. public class ConsulConfigController {
  4. @Autowired
  5. private PayMoneyProperties payMoneyProperties ;
  6. @RequestMapping("/pay/money")
  7. public  Object getConfig(HttpRequest request){
  8.     String money ="项目顺利上线,老板开始发奖金:";
  9.   return money + payMoneyProperties.getMoney();
  10. }
  11. }

方式二:

@RestController

@RequestMapping(“consul”)

//启用动态配置刷新

@RefreshScope

public class ConsulConfigController {

//获取配置的值

@Value("${company.pay.money}")

private String moneyConfig;

@RequestMapping("/pay/money")

public Object getConfig(HttpRequest request){

String money =“项目顺利上线,老板开始发奖金:”;

return money +moneyConfig;

}

}

5)启动项目



上图可以通过日志看出config server 的连接信息

6)consul config server 还没设置对应节点值时演示(获取的是本地配置文件值)



备注:Spring boot 在加载配置顺序:本地配置文件 --> Config Server -->application

7) consul 中创建数据节点

请求地址:http://localhost:8500

创建数据节点:config/waiter-service/data



注意:YAML数据中,通过空格、“:” 表示数据层级关系, 在设置这个值前,可以在网上校验一下YAML内容的有效性;

8)验证项目里是有有收到动态配置

如下图,表示已经通知到项目更新的值



在验证接口中请求一下对应接口,发现值已经和consul config server 中动态设置的值相同了

三、总结

1) 如果在你们的微服务中已经使用consul 作为注册中心, 那么推荐使用上文的方案, 毕竟可以少维护一套系统。

2) consul 作为注册中心、相比zookeeper 作为注册中心,有了更友好的web页面,如果有版本或回滚的一些操作就更完美了。

3)client 会定时拉取consul config server 值,与本地值对比

ConfigWatch 类核心代码

  1. @Override
  2. public void start() {
  3. if (this.running.compareAndSet(false, true)) {
  4. this.watchFuture = this.taskScheduler.scheduleWithFixedDelay(
  5. this::watchConfigKeyValues, this.properties.getWatch().getDelay());
  6. }
  7. }
  1. @Timed("consul.watch-config-keys")
  2. public void watchConfigKeyValues() {
  3. if (this.running.get()) {
  4. for (String context : this.consulIndexes.keySet()) {
  5. 			// turn the context into a Consul folder path (unless our config format
  6. 			// are FILES)
  7. 			if (this.properties.getFormat() != FILES &amp;&amp; !context.endsWith("/")) {
  8. 				context = context + "/";
  9. 			}
  10. 			try {
  11. 				Long currentIndex = this.consulIndexes.get(context);
  12. 				if (currentIndex == null) {
  13. 					currentIndex = -1L;
  14. 				}
  15. 				log.trace("watching consul for context '" + context + "' with index "
  16. 						+ currentIndex);
  17. 				// use the consul ACL token if found
  18. 				String aclToken = this.properties.getAclToken();
  19. 				if (StringUtils.isEmpty(aclToken)) {
  20. 					aclToken = null;
  21. 				}
  22. 				Response&lt;List&lt;GetValue&gt;&gt; response = this.consul.getKVValues(context,
  23. 						aclToken,
  24. 						new QueryParams(this.properties.getWatch().getWaitTime(),
  25. 								currentIndex));
  26. 				// if response.value == null, response was a 404, otherwise it was a
  27. 				// 200
  28. 				// reducing churn if there wasn't anything
  29. 				if (response.getValue() != null &amp;&amp; !response.getValue().isEmpty()) {
  30. 					Long newIndex = response.getConsulIndex();
  31. 					if (newIndex != null &amp;&amp; !newIndex.equals(currentIndex)) {
  32. 						// don't publish the same index again, don't publish the first
  33. 						// time (-1) so index can be primed
  34. 						if (!this.consulIndexes.containsValue(newIndex)
  35. 								&amp;&amp; !currentIndex.equals(-1L)) {
  36. 							log.trace("Context " + context + " has new index "
  37. 									+ newIndex);
  38. 							RefreshEventData data = new RefreshEventData(context,
  39. 									currentIndex, newIndex);
  40. 							this.publisher.publishEvent(
  41. 									new RefreshEvent(this, data, data.toString()));
  42. 						}
  43. 						else if (log.isTraceEnabled()) {
  44. 							log.trace("Event for index already published for context "
  45. 									+ context);
  46. 						}
  47. 						this.consulIndexes.put(context, newIndex);
  48. 					}
  49. 					else if (log.isTraceEnabled()) {
  50. 						log.trace("Same index for context " + context);
  51. 					}
  52. 				}
  53. 				else if (log.isTraceEnabled()) {
  54. 					log.trace("No value for context " + context);
  55. 				}
  56. 			}
  57. 			catch (Exception e) {
  58. 				// only fail fast on the initial query, otherwise just log the error
  59. 				if (this.firstTime &amp;&amp; this.properties.isFailFast()) {
  60. 					log.error(
  61. 							"Fail fast is set and there was an error reading configuration from consul.");
  62. 					ReflectionUtils.rethrowRuntimeException(e);
  63. 				}
  64. 				else if (log.isTraceEnabled()) {
  65. 					log.trace("Error querying consul Key/Values for context '"
  66. 							+ context + "'", e);
  67. 				}
  68. 				else if (log.isWarnEnabled()) {
  69. 					// simplified one line log message in the event of an agent
  70. 					// failure
  71. 					log.warn("Error querying consul Key/Values for context '"
  72. 							+ context + "'. Message: " + e.getMessage());
  73. 				}
  74. 			}
  75. 		}
  76. 	}
  77. 	this.firstTime = false;
  78. }

原文地址:https://blog.csdn.net/qq_36918149/article/details/99709397

  1. </div>

Consul作为SpringCloud配置中心的更多相关文章

  1. SpringCloud配置中心实战

    SpringCloud配置中心实战 1.统一配置中心(Config) 1.1 Spring项目配置加载顺序 1.2 配置规则详解 1.3 Git仓库配置 1.3.1 使用占位符 1.3.2 模式匹配 ...

  2. SpringCloud使用Consul作为分布式配置中心

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/qq_36027670/article/de ...

  3. springcloud~配置中心的使用

    配置中心作为springcloud里最底层的框架,所发挥的意思是举足轻重的,所以的组件的配置信息都可以通过springcloud config来管理,它会把配置信息分布式的存储到git上,所以信息安全 ...

  4. springcloud配置中心

    SpringCloud Config简介 Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持 ...

  5. SpringCloud配置中心config

    1,配置中心可以用zookeeper来实现,也可以用apllo 来实现,springcloud 也自带了配置中心config Apollo 实现分布式配置中心 zookeeper:实现分布式配置中心, ...

  6. springcloud~配置中心实例搭建

    server端 build.gradle相关 dependencies { compile('org.springframework.cloud:spring-cloud-config-server' ...

  7. springcloud配置中心客户端配置遇到的坑

    1. 出错信息如下: 在启动配置中心的客户端时,报以下错误信息: Caused by: java.lang.IllegalArgumentException: Could not resolve pl ...

  8. SpringCloud配置中心集成Gitlab(十五)

    一 开始配置config服务 config-server pom.xml <dependency> <groupId>org.springframework.cloud< ...

  9. springcloud~配置中心~对敏感信息加密

    简介 RSA非对称加密有着非常强大的安全性,HTTPS的SSL加密就是使用这种方法进行HTTPS请求加密传输的.因为RSA算法会涉及Private Key和Public Key分别用来加密和解密,所以 ...

随机推荐

  1. Jenkins入门【转】

    一.Jenkins概述 二.安装Jenkins https://pkg.jenkins.io/redhat-stable/ sudo wget -O /etc/yum.repos.d/jenkins. ...

  2. ArcPy python实例教程-条件平差-测量平差

    ArcPy python实例教程-条件平差-测量平差 商务合作,科技咨询,版权转让:向日葵,135-4855__4328,xiexiaokui#qq.com 输入参数:条件方程的系数,观测值,常数项和 ...

  3. 使用Dapper.Contrib

    public T Query(string sql, object param) { using (IDbConnection dbConnection = Connection) { if (dbC ...

  4. 为什么要监控sql语句?如何监控?

    01 为什么要监控sql语句? ① 因为程序大了以后,sql语句有可能被多个地方调用 .你不能确认当前时间是不是只执行了你需要的那条语句 . ② 有的持久层框架采用linq的语法来写sql , 程序中 ...

  5. Docs-.NET-C#-指南-语言参考-关键字-值类型:char

    ylbtech-Docs-.NET-C#-指南-语言参考-关键字-值类型:char 1.返回顶部 1. char(C# 参考) 2019/10/22 char 类型关键字是 .NET System.C ...

  6. Python之Django之views中视图代码重复查询的优化

    Django框架中views视图中如果多个函数都有同样的查询语句,例如: allcategory = Category.objects.all() remen = Article.objects.fi ...

  7. Android webview 写入cookie的解决方法以及一些属性设置

    原文地址:https://www.2cto.com/kf/201703/616868.html Android webview 写入cookie的解决方法以及一些属性设置,webview怎么设置写入C ...

  8. PL-SQL Developer 配置使用Oracle客户端连接

    没有在本地安装Oracle 的用户必须下载一个instantclient(即使客户端), 路径指向这里,才能正常使用 下载 instantclient-basic-nt-11.2.0.3.0.zip ...

  9. Hive Essential (4):DML-project,filter,join,union

    1. Project data with SELECT The most common use case for Hive is to query data in Hadoop. To achieve ...

  10. C#中Invoke的用法(转)

    invoke和begininvoke 区别 一直对invoke和begininvoke的使用和概念比较混乱,这两天看了些资料,对这两个的用法和原理有了些新的认识和理解. 首先说下,invoke和beg ...