zoukankan      html  css  js  c++  java
  • config-server-bus动态更新配置

    config-server用来搭建配置中心,而配置信息一般使用gitlab仓库来存储,这样在你的配置发生改变时,不需要从新打包,而如果使用native的试,则需要从新打一个config-server的jar包。

    配置的热更新

    当你的服务的配置信息发生改变时,一般来说需要从新重启你的服务,配置信息才能生效,这对于我们来说是不友好的,所以springcloud有一种消息总线的方式来实现配置信息的热更新,更你的服务不需要从新启动。

    项目搭建

    eureka-server

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    

    程序添加注解

    @SpringBootApplication
    @EnableEurekaServer
    public class EurekaserverApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(EurekaserverApplication.class, args);
      }
    
    }
    

    yml文件

    server:
     port: 8761
    
    eureka:
     instance:
       hostname: localhost
     client:
      registerWithEureka: false
      fetchRegistry: false
      serviceUrl:
        defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      server:
       enable-self-preservation: false #自我保护机制
       eviction-interval-timer-in-ms: 30000  #及时踢出已关停的节点
    

    config-server

    它是配置中心,其它服务如果通过config-server在eureka里的服务名去连接它,这种是以eureka为核心;也可以单独指定,并把eureka的信息写到config-server仓库里,这是以config-server为核心,这两种方式都可以,咱们这个例子是以eureka为核心的,所以config-server也是一个eureka-client.

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    

    程序添加注解

    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableConfigServer
    public class ConfigserverApplication {
    
      public static void main(String[] args) {
        SpringApplication.run(ConfigserverApplication.class, args);
      }
    
    }
    
    

    添加yml配置

    spring:
      application:
        name: config-server
      cloud:
        config:
          server:
            git:
              uri: git@github.com:***/config_repo.git
              username: ***
              password: ***
              searchPaths: '{profile}'
    server:
      port: 8888
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    

    config-client-service1

    这是我们具体的业务服务,它是一个eureka-client也是一个config-client,它需要把自己注册到eureka里,也需要从config-server拉自己的信息,它需要有配置信息的热更新,所以这需要引用amqp包和actuator健康检测包。

    <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
    

    程序添加注解

    @SpringBootApplication
    @EnableDiscoveryClient
    @RestController
    @RefreshScope
    public class Service1Application {
    
      @Value("${auth.name:empty}")
      String author;
    
      public static void main(String[] args) {
        SpringApplication.run(Service1Application.class, args);
      }
    
      @GetMapping("/hello")
      public String hello() {
        return author;
      }
    }
    

    添加yml文件

    spring:
      cloud:
        bus.trace.enabled: true #配置动态更新
        rabbitmq:
          host: 127.0.0.1
          port: 5672
          username: guest
          password: guest
        config:
          discovery:
            enabled: true #这块表示启用service-id不用uri
            service-id: config-server  #这块是服务的id
          label: master
          profile: svt
      application:
        name: service1
    server:
      port: 8081
    eureka:
      instance:
        prefer-ip-address: true #基于IP地址的注册而不是主机名
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka
    logging:
      level:
        org:
          springframework:
            security: DEBUG
    management:
      endpoints:
        web:
          exposure:
            include: bus-refresh
    
    

    配置更新

    当你的config_repo仓库有文件更新时,你可以调用任意一个服务去触发它,测试的代码如

     curl -v -X POST "http://localhost:8081/actuator/bus-refresh"
    

    如果成功后,一般会返回httpstatuscode:204的状态码,当然这种也是手动更新,如果希望动态更新,可以在gitlab或者github上对config_repo项目添加webhook的事件,当有分支合并到dev或者master时,去自动触发bus-refresh。

  • 相关阅读:
    JS加密库
    异常处理
    uva 10673 Play with Floor and Ceil
    执⾏ Python 程序的三种⽅式----pycharm安装
    第⼀个 Python 程序
    认识 Python
    svg的使用
    elementUI中el-image显示不出来图片?img与el-image的区别
    类型转化与变量
    liunx
  • 原文地址:https://www.cnblogs.com/lori/p/11988692.html
Copyright © 2011-2022 走看看