zoukankan      html  css  js  c++  java
  • Consul作为SpringCloud配置中心

    一、背景介绍

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

    环境依赖:

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

    二、项目实战

    1) pom依赖(主要)

    <properties>
    		<java.version>1.8</java.version>
    		<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>
    

    <dependencyManagement>
    <dependencies>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
    </dependency>
    </dependencies>
    </dependencyManagement>

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-config</artifactId>
    </dependency>

    2)配置文件

    application.properties

    #0表示服务器随机端口
    server.port=8090
    

    本次演示的kv的默认值(老板默认给你0元)

    company.pay.money=0

    bootstrap.properties

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

    3)动态参数接收类

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

    备注:
    ConfigurationProperties 表示这个类关联动态配置,“company.pay”表示key的前缀部分。
    @RefreshScope 表示动态刷新config server 值
    @Component 表示将该类加载到IOC容器中
    在实战中尝试用@Value的方式获取动态,只能实现服务重启后获取动态的config server 的值,最终找到解决方案在相应的取值类上加@RefreshScope注解,完美解决。

    4)对外接口(便于直观验证)
    方式一:

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

    }

    方式二:

    @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 类核心代码

    @Override
    	public void start() {
    		if (this.running.compareAndSet(false, true)) {
    			this.watchFuture = this.taskScheduler.scheduleWithFixedDelay(
    					this::watchConfigKeyValues, this.properties.getWatch().getDelay());
    		}
    	}
    
    @Timed("consul.watch-config-keys")
    	public void watchConfigKeyValues() {
    		if (this.running.get()) {
    			for (String context : this.consulIndexes.keySet()) {
    
    			// turn the context into a Consul folder path (unless our config format
    			// are FILES)
    			if (this.properties.getFormat() != FILES &amp;&amp; !context.endsWith("/")) {
    				context = context + "/";
    			}
    
    			try {
    				Long currentIndex = this.consulIndexes.get(context);
    				if (currentIndex == null) {
    					currentIndex = -1L;
    				}
    
    				log.trace("watching consul for context '" + context + "' with index "
    						+ currentIndex);
    
    				// use the consul ACL token if found
    				String aclToken = this.properties.getAclToken();
    				if (StringUtils.isEmpty(aclToken)) {
    					aclToken = null;
    				}
    
    				Response&lt;List&lt;GetValue&gt;&gt; response = this.consul.getKVValues(context,
    						aclToken,
    						new QueryParams(this.properties.getWatch().getWaitTime(),
    								currentIndex));
    
    				// if response.value == null, response was a 404, otherwise it was a
    				// 200
    				// reducing churn if there wasn't anything
    				if (response.getValue() != null &amp;&amp; !response.getValue().isEmpty()) {
    					Long newIndex = response.getConsulIndex();
    
    					if (newIndex != null &amp;&amp; !newIndex.equals(currentIndex)) {
    						// don't publish the same index again, don't publish the first
    						// time (-1) so index can be primed
    						if (!this.consulIndexes.containsValue(newIndex)
    								&amp;&amp; !currentIndex.equals(-1L)) {
    							log.trace("Context " + context + " has new index "
    									+ newIndex);
    							RefreshEventData data = new RefreshEventData(context,
    									currentIndex, newIndex);
    							this.publisher.publishEvent(
    									new RefreshEvent(this, data, data.toString()));
    						}
    						else if (log.isTraceEnabled()) {
    							log.trace("Event for index already published for context "
    									+ context);
    						}
    						this.consulIndexes.put(context, newIndex);
    					}
    					else if (log.isTraceEnabled()) {
    						log.trace("Same index for context " + context);
    					}
    				}
    				else if (log.isTraceEnabled()) {
    					log.trace("No value for context " + context);
    				}
    
    			}
    			catch (Exception e) {
    				// only fail fast on the initial query, otherwise just log the error
    				if (this.firstTime &amp;&amp; this.properties.isFailFast()) {
    					log.error(
    							"Fail fast is set and there was an error reading configuration from consul.");
    					ReflectionUtils.rethrowRuntimeException(e);
    				}
    				else if (log.isTraceEnabled()) {
    					log.trace("Error querying consul Key/Values for context '"
    							+ context + "'", e);
    				}
    				else if (log.isWarnEnabled()) {
    					// simplified one line log message in the event of an agent
    					// failure
    					log.warn("Error querying consul Key/Values for context '"
    							+ context + "'. Message: " + e.getMessage());
    				}
    			}
    		}
    	}
    	this.firstTime = false;
    }
    
    原文地址:https://blog.csdn.net/qq_36918149/article/details/99709397
                                    </div>
  • 相关阅读:
    @ResponseBody和@RequestBoby的作用
    项目Alpha冲刺Day9
    项目Alpha冲刺Day8
    项目Alpha冲刺Day7
    设计模式第三次作业
    项目Alpha冲刺Day6
    树莓派历程笔记
    项目Alpha冲刺Day5
    django获取ip与数据重复性判定
    在django模板中添加jquery
  • 原文地址:https://www.cnblogs.com/jpfss/p/11903932.html
Copyright © 2011-2022 走看看