zoukankan      html  css  js  c++  java
  • spring-cloud 实现更新配置不用重启服务 @FreshScope

    继续前面搭建的spring cloud。

    这里是基于rabbitMQ搭建的,首先需要在电脑上安装rabbitMQ。

    在client端和server端分别加上如下依赖

      compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-bus-amqp', version: '1.0.1.RELEASE'

    AMQP (Advanced Message Queuing Protocol)是一种通讯协议,而rabbitMQ就是使用的这种通讯协议。

    client端的application.properties加上如下

    spring.rabbitmq.host=localhost
    spring.rabbitmq.port=5672
    spring.rabbitmq.username=root
    spring.rabbitmq.password=root

    client端的configuration包下的配置类文件上应该有@ConfigurationProperties()注解

    @Data
    @Component
    @ConfigurationProperties(prefix = "fzk")
    public class BaseConfiguration {
      private String nick;
    }

    在每个注入了这个configuration的类加上@FreshScope注解

    @RestController
    @RefreshScopepublic class ClientApplication {
      @Autowired
      Base base;
    
      @RequestMapping(value = "/foo", method = RequestMethod.GET)
      public String foo() {
    
        return base.getNick();
      }
    }

    完成之后执行下面命令下载jar包。

    gradle build
    gradle eclipse

    server端同样编译后,先启动server端,在启动client端。

    浏览器上输入localhost:8889/foo会看到获取到的数据。去gitlab修改下fzk-beta.properties,重新在浏览器上输入,发现现在获取的还是原来的数据,并没有修改。从服务端(http://localhost:8888/fzk/beta)可以获取到最新的数据。这里想让client端不重启服务就能获取到更新后的数据需要手动发送一个post请求到client端(http://localhost:8889/fresh)

    $ curl -X POST http://localhost:8889/refresh
    ["config.client.version","fzk.nick"]

    所以想数的时,这里并不是完全的自动。还需要调用一个接口,这个接口一般是通过存放config的push事件来触发的,如果一个服务可以直接写在webhook中。但是如果需要触发多个服务自动更新,可以在jenkins配置一个job,webhook出去这个job,这个job来触发多个服务的post请求操作。

  • 相关阅读:
    Js 作用域链
    JS 上下文模式
    javascript
    HTTP概念进阶
    JavaScript运行机制详解
    浅谈循环中setTimeout执行顺序问题
    Js 运行机制 (重点!!)
    javascript
    jQuery 知识点总结
    Educational Codeforces Round 87 (Rated for Div. 2)
  • 原文地址:https://www.cnblogs.com/badboyf/p/6522543.html
Copyright © 2011-2022 走看看